Skip to content

Commit c7399d5

Browse files
committed
add more lofic for rollout wait
1 parent b68173f commit c7399d5

File tree

1 file changed

+88
-3
lines changed
  • test/extended/machine_config

1 file changed

+88
-3
lines changed

test/extended/machine_config/ocl.go

+88-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
e2e "k8s.io/kubernetes/test/e2e/framework"
1414

1515
mcfgv1 "github.com/openshift/api/machineconfiguration/v1"
16-
//mcfgv1alpha1 "github.com/openshift/api/machineconfiguration/v1alpha1"
16+
mcfgv1alpha1 "github.com/openshift/api/machineconfiguration/v1alpha1"
1717
commonstate "github.com/openshift/machine-config-operator/pkg/controller/common/state"
18+
ctrlnode "github.com/openshift/machine-config-operator/pkg/controller/node"
1819
corev1 "k8s.io/api/core/v1"
1920
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2021

@@ -105,7 +106,10 @@ func AllNodePoolOptInTest(oc *exutil.CLI, moscFixture string, mcpFixture string)
105106

106107
mcp, err := getMCPFromFixture(mcpFixture)
107108
o.Expect(err).NotTo(o.HaveOccurred())
108-
109+
110+
mosc, err := getMOSCFromFixture(moscFixture)
111+
o.Expect(err).NotTo(o.HaveOccurred())
112+
109113
waitTime := time.Minute * 20
110114
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
111115
defer cancel()
@@ -114,7 +118,9 @@ func AllNodePoolOptInTest(oc *exutil.CLI, moscFixture string, mcpFixture string)
114118
err = waitForBuild(ctx, machineConfigClient, mcp)
115119
o.Expect(err).NotTo(o.HaveOccurred(), "Waiting for MOSB to be created and builder pod to Succeed")
116120

117-
// Wait for the build Image to be applied to all nodes
121+
// Wait for the build Image to be applied to mcp successfully
122+
err = waitForRollout(ctx, machineConfigClient, oc, mcp, mosc)
123+
o.Expect(err).NotTo(o.HaveOccurred(), "Waiting for MCP to have desired config as the new built image")
118124
}
119125

120126
func getMCPFromFixture(path string) (*mcfgv1.MachineConfigPool, error) {
@@ -132,6 +138,84 @@ func getMCPFromFixture(path string) (*mcfgv1.MachineConfigPool, error) {
132138
return mcp, err
133139
}
134140

141+
func getMOSCFromFixture(path string) (*mcfgv1alpha1.MachineOSConfig, error) {
142+
data, err := ioutil.ReadFile(path)
143+
if err != nil {
144+
return nil, err
145+
}
146+
147+
mosc := new(mcfgv1alpha1.MachineOSConfig)
148+
err = yaml.Unmarshal(data, mosc)
149+
if err != nil {
150+
return nil, err
151+
}
152+
153+
return mosc, err
154+
}
155+
156+
func getNodesForPool(ctx context.Context, oc *exutil.CLI, pool *mcfgv1.MachineConfigPool) (*corev1.NodeList, error) {
157+
selector, err := metav1.LabelSelectorAsSelector(pool.Spec.NodeSelector)
158+
if err != nil {
159+
return nil, fmt.Errorf("invalid label selector: %w", err)
160+
}
161+
nodes, err := oc.KubeClient().CoreV1().Nodes().List(ctx, metav1.ListOptions{LabelSelector: selector.String(),})
162+
if err != nil {
163+
return nil, fmt.Errorf("couldnt get nodes for mcp: %w", err)
164+
}
165+
return nodes, nil
166+
}
167+
168+
func waitForRollout(ctx context.Context, clientset *mcClient.Clientset, oc *exutil.CLI, mcpG *mcfgv1.MachineConfigPool, moscG *mcfgv1alpha1.MachineOSConfig) error {
169+
return wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (done bool, err error) {
170+
171+
//Get mosb
172+
var mosb mcfgv1alpha1.MachineOSBuild
173+
var mosbFound bool
174+
mosbList, err := clientset.MachineconfigurationV1alpha1().MachineOSBuilds().List(ctx, metav1.ListOptions{})
175+
if err != nil {
176+
return false, err
177+
}
178+
for _, m := range mosbList.Items {
179+
if m.Spec.DesiredConfig.Name == mcpG.Spec.Configuration.Name {
180+
mosb = m
181+
mosbFound = true
182+
break
183+
}
184+
}
185+
if !mosbFound {
186+
return false, fmt.Errorf("could not find mosb")
187+
}
188+
189+
// Get mosc
190+
mosc, err := clientset.MachineconfigurationV1alpha1().MachineOSConfigs().Get(ctx, moscG.ObjectMeta.Name, metav1.GetOptions{})
191+
if err != nil {
192+
return false, fmt.Errorf("couldnt get mosc: %w", err)
193+
}
194+
195+
// Get Pool
196+
pool, err := clientset.MachineconfigurationV1().MachineConfigPools().Get(ctx, mcpG.ObjectMeta.Name, metav1.GetOptions{})
197+
if err != nil {
198+
return false, fmt.Errorf("couldnt get pool: %w", err)
199+
}
200+
201+
// Get nodes
202+
nodes, err := getNodesForPool(ctx, oc, pool)
203+
204+
// Ensure rollout
205+
doneNodes := 0
206+
for _, node := range nodes.Items {
207+
lns := commonstate.NewLayeredNodeState(&node)
208+
if mosb.Spec.DesiredConfig.Name == pool.Spec.Configuration.Name && ctrlnode.IsNodeDoneAt(&node, pool, true) && lns.IsCurrentImageEqualToBuild(mosc){
209+
doneNodes += 1
210+
}
211+
}
212+
if doneNodes == len(nodes.Items){
213+
return true, nil
214+
}
215+
return false, nil
216+
})
217+
}
218+
135219
func waitForBuild(ctx context.Context, clientset *mcClient.Clientset, mcp *mcfgv1.MachineConfigPool) error {
136220
return wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (done bool, err error) {
137221

@@ -164,6 +248,7 @@ func waitForBuild(ctx context.Context, clientset *mcClient.Clientset, mcp *mcfgv
164248
if state.IsBuildFailure() {
165249
return false, fmt.Errorf("build %s failed after %s", mosb.Name, time.Since(start))
166250
}
251+
break
167252
}
168253
}
169254
return false, nil

0 commit comments

Comments
 (0)