Skip to content

Commit

Permalink
Fix race
Browse files Browse the repository at this point in the history
  • Loading branch information
jveski committed Dec 7, 2023
1 parent b8162bb commit ff1d957
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ func NewPodController(t testing.TB, mgr ctrl.Manager, fn func(*apiv1.Composition
if err != nil {
return reconcile.Result{}, err
}

// The state is populated async with pod creation so it may not exist at this point
if comp.Status.CurrentState == nil {
return reconcile.Result{}, nil // wait for controller to write initial status
return reconcile.Result{}, nil
}

syn := &apiv1.Synthesizer{}
Expand All @@ -228,23 +230,26 @@ func NewPodController(t testing.TB, mgr ctrl.Manager, fn func(*apiv1.Composition
if err != nil {
return reconcile.Result{}, err
}
if len(pods.Items) == 0 {
return reconcile.Result{}, nil // no pods yet
}

for _, pod := range pods.Items {
pod := pod

// The real pod controller will ignore outdated (probably deleting) pods
compGen, _ := strconv.ParseInt(pod.Annotations["eno.azure.io/composition-generation"], 10, 0)
synGen, _ := strconv.ParseInt(pod.Annotations["eno.azure.io/synthesizer-generation"], 10, 0)
if synGen < syn.Generation || compGen < comp.Generation {
t.Logf("skipping pod %s because it's out of date (%d < %d || %d < %d)", pod.Name, synGen, syn.Generation, compGen, comp.Generation)
continue
}

// nil func == 0 slices
var slices []*apiv1.ResourceSlice
if fn != nil {
slices = fn(comp, syn)
}

// Write all of the resource slices, update the resource slice count accordingly
// TODO: We need a controller to remove failed/outdated resource slice writes
if comp.Status.CurrentState.ResourceSliceCount == nil {
for _, slice := range slices {
cp := slice.DeepCopy()
Expand All @@ -258,7 +263,6 @@ func NewPodController(t testing.TB, mgr ctrl.Manager, fn func(*apiv1.Composition
t.Logf("created resource slice: %s", cp.Name)
}

// Add resource slice count - the wrapper will do this in the real world
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
err := cli.Get(ctx, r.NamespacedName, comp)
if err != nil {
Expand Down

0 comments on commit ff1d957

Please sign in to comment.