Skip to content

Commit

Permalink
Correct logic around synthesis lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Olshevski committed Dec 14, 2023
1 parent 4a327ad commit 5acb252
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion internal/controllers/synthesis/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestControllerSynthesizerRollout(t *testing.T) {
// The first synthesizer update should be applied to the composition
testutil.Eventually(t, func() bool {
require.NoError(t, client.IgnoreNotFound(cli.Get(ctx, client.ObjectKeyFromObject(comp), comp)))
return comp.Status.CurrentState != nil && comp.Status.CurrentState.ObservedSynthesizerGeneration >= syn.Generation
return comp.Status.CurrentState != nil && comp.Status.CurrentState.ObservedSynthesizerGeneration == syn.Generation
})

// Wait for the informer cache to know about the last update
Expand Down
6 changes: 3 additions & 3 deletions internal/controllers/synthesis/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (c *podLifecycleController) Reconcile(ctx context.Context, req ctrl.Request
}

// No need to create a pod if everything is in sync
if comp.Status.CurrentState != nil && comp.Status.CurrentState.ResourceSlices != nil {
if comp.Status.CurrentState != nil && comp.Status.CurrentState.Synthesized {
return ctrl.Result{}, nil
}

Expand All @@ -117,7 +117,7 @@ func (c *podLifecycleController) shouldDeletePod(logger logr.Logger, comp *apiv1
var activeLatest bool
for _, pod := range pods.Items {
pod := pod
if pod.DeletionTimestamp != nil || !podDerivedFrom(comp, syn, &pod) {
if pod.DeletionTimestamp != nil || !podDerivedFrom(comp, &pod) {
continue
}
if activeLatest {
Expand All @@ -131,7 +131,7 @@ func (c *podLifecycleController) shouldDeletePod(logger logr.Logger, comp *apiv1
for _, pod := range pods.Items {
pod := pod
reason, shouldDelete := c.podStatusTerminal(&pod)
isCurrent := podDerivedFrom(comp, syn, &pod) && len(comp.Status.CurrentState.ResourceSlices) != 0
isCurrent := podDerivedFrom(comp, &pod)

// If the current pod is being deleted it's safe to create a new one if needed
// Avoid getting stuck by pods that fail to delete
Expand Down
10 changes: 3 additions & 7 deletions internal/controllers/synthesis/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,11 @@ func newPod(cfg *Config, scheme *runtime.Scheme, comp *apiv1.Composition, syn *a
return pod
}

func podDerivedFrom(comp *apiv1.Composition, syn *apiv1.Synthesizer, pod *corev1.Pod) bool {
func podDerivedFrom(comp *apiv1.Composition, pod *corev1.Pod) bool {
if pod.Annotations == nil {
return false
}

var (
compGen, _ = strconv.ParseInt(pod.Annotations["eno.azure.io/composition-generation"], 10, 0)
synGen, _ = strconv.ParseInt(pod.Annotations["eno.azure.io/synthesizer-generation"], 10, 0)
)

return compGen == comp.Generation && synGen == syn.Generation
compGen, _ := strconv.ParseInt(pod.Annotations["eno.azure.io/composition-generation"], 10, 0)
return compGen == comp.Generation
}
6 changes: 2 additions & 4 deletions internal/controllers/synthesis/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *statusController) Reconcile(ctx context.Context, req ctrl.Request) (ctr
if err != nil {
return ctrl.Result{}, fmt.Errorf("getting synthesizer: %w", err)
}
logger = logger.WithValues("synthesizerName", syn.Name)
logger = logger.WithValues("synthesizerName", syn.Name, "podName", pod.Name)

// Update composition status
var (
Expand Down Expand Up @@ -97,7 +97,5 @@ func (c *statusController) Reconcile(ctx context.Context, req ctrl.Request) (ctr

func shouldWriteStatus(comp *apiv1.Composition, podCompGen, podSynGen int64) bool {
current := comp.Status.CurrentState
isOldPod := current == nil || current.ObservedCompositionGeneration != podCompGen
isInSync := current != nil && current.PodCreation != nil
return !isOldPod && !isInSync
return current == nil || (current.ObservedCompositionGeneration == podCompGen && (current.PodCreation == nil || current.ObservedSynthesizerGeneration == 0))
}
2 changes: 1 addition & 1 deletion internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func NewPodController(t testing.TB, mgr ctrl.Manager, fn func(*apiv1.Composition
// Write all of the resource slices, update the resource slice count accordingly
// TODO: We need a controller to remove failed/outdated resource slice writes
sliceRefs := []*apiv1.ResourceSliceRef{}
if comp.Status.CurrentState.ResourceSlices == nil {
if !comp.Status.CurrentState.Synthesized {
for _, slice := range slices {
cp := slice.DeepCopy()
cp.Spec.CompositionGeneration = comp.Generation
Expand Down

0 comments on commit 5acb252

Please sign in to comment.