diff --git a/rollout/analysis.go b/rollout/analysis.go index 5c5c5f8aec..26205235a1 100644 --- a/rollout/analysis.go +++ b/rollout/analysis.go @@ -313,7 +313,7 @@ func (c *rolloutContext) reconcilePostPromotionAnalysisRun() (*v1alpha1.Analysis func (c *rolloutContext) reconcileBackgroundAnalysisRun() (*v1alpha1.AnalysisRun, error) { currentAr := c.currentArs.CanaryBackground - if c.rollout.Spec.Strategy.Canary.Analysis == nil { + if c.rollout.Spec.Strategy.Canary.Analysis == nil || len(c.rollout.Spec.Strategy.Canary.Analysis.Templates) == 0 { err := c.cancelAnalysisRuns([]*v1alpha1.AnalysisRun{currentAr}) return nil, err } diff --git a/rollout/analysis_test.go b/rollout/analysis_test.go index 624134fbb6..4b5d5d72d7 100644 --- a/rollout/analysis_test.go +++ b/rollout/analysis_test.go @@ -2468,3 +2468,45 @@ func TestCreateAnalysisRunWithCustomAnalysisRunMetadataAndROCopyLabels(t *testin assert.Equal(t, "testLabelValue", createdAr.Labels["testLabelKey"]) assert.Equal(t, "1234", createdAr.Labels["my-label"]) } + +func TestCancelBackgroundAnalysisRunWhenRolloutAnalysisHasNoTemplate(t *testing.T) { + f := newFixture(t) + defer f.Close() + + at := analysisTemplate("bar") + steps := []v1alpha1.CanaryStep{ + {SetWeight: pointer.Int32Ptr(10)}, + } + + r1 := newCanaryRollout("foo", 1, nil, steps, pointer.Int32Ptr(1), intstr.FromInt(0), intstr.FromInt(1)) + rs1 := newReplicaSetWithStatus(r1, 1, 1) + rs1PodHash := rs1.Labels[v1alpha1.DefaultRolloutUniqueLabelKey] + r1 = updateCanaryRolloutStatus(r1, rs1PodHash, 1, 1, 1, false) + ar := analysisRun(at, v1alpha1.RolloutTypeStepLabel, r1) + r1.Status.Canary.CurrentBackgroundAnalysisRunStatus = &v1alpha1.RolloutAnalysisRunStatus{ + Name: ar.Name, + Status: v1alpha1.AnalysisPhaseRunning, + } + + r2 := bumpVersion(r1) + r2.Spec.Strategy.Canary.Analysis = &v1alpha1.RolloutAnalysisBackground{ + RolloutAnalysis: v1alpha1.RolloutAnalysis{}, // No templates provided. + } + rs2 := newReplicaSetWithStatus(r2, 0, 0) + + f.kubeobjects = append(f.kubeobjects, rs1, rs2) + f.replicaSetLister = append(f.replicaSetLister, rs1, rs2) + f.rolloutLister = append(f.rolloutLister, r2) + f.analysisTemplateLister = append(f.analysisTemplateLister, at) + f.analysisRunLister = append(f.analysisRunLister, ar) + f.objects = append(f.objects, r2, at, ar) + + _ = f.expectPatchAnalysisRunAction(ar) + patchIndex := f.expectPatchRolloutAction(r2) + _ = f.expectUpdateReplicaSetAction(rs1) + f.run(getKey(r2, t)) + + patch := f.getPatchedRollout(patchIndex) + + assert.Contains(t, patch, `"currentBackgroundAnalysisRunStatus":null`) +}