Skip to content

Commit

Permalink
Add custom metadata support for AnalysisRun
Browse files Browse the repository at this point in the history
Signed-off-by: lukasz <lukasz.luszczynski@getyourguide.com>
  • Loading branch information
lukaszgyg committed Apr 24, 2023
1 parent 3c9c291 commit 0daf4cd
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/apis/rollouts/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,16 @@ type PodTemplateMetadata struct {
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,2,rep,name=annotations"`
}

// AnalysisRunMetadata extra labels to add to the AnalysisRun
type AnalysisRunMetadata struct {
// Labels Additional labels to add to the AnalysisRun
// +optional
Labels map[string]string `json:"labels,omitempty" protobuf:"bytes,1,rep,name=labels"`
// Annotations additional annotations to add to the AnalysisRun
// +optional
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,2,rep,name=annotations"`
}

// ReplicaSetSpecRef defines which RS that the experiment's template will use.
type ReplicaSetSpecRef string

Expand Down Expand Up @@ -701,6 +711,9 @@ type RolloutAnalysis struct {
// +patchStrategy=merge
// +optional
MeasurementRetention []MeasurementRetention `json:"measurementRetention,omitempty" patchStrategy:"merge" patchMergeKey:"metricName" protobuf:"bytes,4,rep,name=measurementRetention"`
// AnalysisRunMetadata sets labels and annotations to use for the AnalysisRun
// +optional
AnalysisRunMetadata AnalysisRunMetadata `json:"metadata,omitempty" protobuf:"bytes,4,opt,name=metadata"`
}

type RolloutAnalysisTemplate struct {
Expand Down
6 changes: 6 additions & 0 deletions rollout/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,15 @@ func (c *rolloutContext) newAnalysisRunFromRollout(rolloutAnalysis *v1alpha1.Rol
return nil, err
}
run.Labels = labels
for k, v := range rolloutAnalysis.AnalysisRunMetadata.Labels {
run.Labels[k] = v
}
run.Annotations = map[string]string{
annotations.RevisionAnnotation: revision,
}
for k, v := range rolloutAnalysis.AnalysisRunMetadata.Annotations {
run.Annotations[k] = v
}
run.OwnerReferences = []metav1.OwnerReference{*metav1.NewControllerRef(c.rollout, controllerKind)}
return run, nil
}
Expand Down
56 changes: 56 additions & 0 deletions rollout/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2336,3 +2336,59 @@ func TestAbortRolloutOnErrorPostPromotionAnalysis(t *testing.T) {
newConditions := updateConditionsPatch(*r2, progressingFalseAborted)
assert.Equal(t, calculatePatch(r2, fmt.Sprintf(expectedPatch, now, newConditions, conditions.RolloutAbortedReason, progressingFalseAborted.Message)), patch)
}

func TestCreateAnalysisRunWithCustomAnalysisRunMetadata(t *testing.T) {
f := newFixture(t)
defer f.Close()

steps := []v1alpha1.CanaryStep{{
SetWeight: int32Ptr(10),
}}
at := analysisTemplate("bar")
r1 := newCanaryRollout("foo", 10, nil, steps, pointer.Int32Ptr(0), intstr.FromInt(0), intstr.FromInt(1))
r2 := bumpVersion(r1)
ar := analysisRun(at, v1alpha1.RolloutTypeBackgroundRunLabel, r2)
r2.Spec.Strategy.Canary.Analysis = &v1alpha1.RolloutAnalysisBackground{
RolloutAnalysis: v1alpha1.RolloutAnalysis{
Templates: []v1alpha1.RolloutAnalysisTemplate{
{
TemplateName: at.Name,
},
},
AnalysisRunMetadata: v1alpha1.AnalysisRunMetadata{
Annotations: map[string]string{"testAnnotationKey": "testAnnotationValue"},
Labels: map[string]string{"testLabelKey": "testLabelValue"},
},
},
}

rs1 := newReplicaSetWithStatus(r1, 10, 10)
rs2 := newReplicaSetWithStatus(r2, 0, 0)
f.kubeobjects = append(f.kubeobjects, rs1, rs2)
f.replicaSetLister = append(f.replicaSetLister, rs1, rs2)
rs1PodHash := rs1.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]
rs2PodHash := rs2.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]

r2 = updateCanaryRolloutStatus(r2, rs1PodHash, 10, 0, 10, false)
progressingCondition, _ := newProgressingCondition(conditions.ReplicaSetUpdatedReason, rs2, "")
conditions.SetRolloutCondition(&r2.Status, progressingCondition)
availableCondition, _ := newAvailableCondition(true)
conditions.SetRolloutCondition(&r2.Status, availableCondition)
completeCond, _ := newCompletedCondition(false)
conditions.SetRolloutCondition(&r2.Status, completeCond)

f.rolloutLister = append(f.rolloutLister, r2)
f.analysisTemplateLister = append(f.analysisTemplateLister, at)
f.objects = append(f.objects, r2, at)

createdIndex := f.expectCreateAnalysisRunAction(ar)
f.expectUpdateReplicaSetAction(rs2)
_ = f.expectPatchRolloutAction(r1)

f.run(getKey(r2, t))
createdAr := f.getCreatedAnalysisRun(createdIndex)
expectedArName := fmt.Sprintf("%s-%s-%s", r2.Name, rs2PodHash, "2")
assert.Equal(t, expectedArName, createdAr.Name)
assert.Equal(t, "testAnnotationValue", createdAr.Annotations["testAnnotationKey"])
assert.Equal(t, "testLabelValue", createdAr.Labels["testLabelKey"])
}

0 comments on commit 0daf4cd

Please sign in to comment.