Skip to content

Commit

Permalink
feat: add labels and annotations from metrics.provider.job.metadata t…
Browse files Browse the repository at this point in the history
…o created Jobs metadata
  • Loading branch information
hidalgopl committed May 7, 2023
1 parent 04b1e30 commit 74d2812
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/analysis/job.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ successful if the Job completes and had an exit code of zero, otherwise it is fa
- name: test
provider:
job:
metadata:
annotations:
foo: bar # annotations defined here will be copied to the Job object
labels:
foo: bar # labels defined here will be copied to the Job object
spec:
backoffLimit: 1
template:
Expand Down
20 changes: 13 additions & 7 deletions metricproviders/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,24 @@ func getJobIDSuffix(run *v1alpha1.AnalysisRun, metricName string) int {
}

func newMetricJob(run *v1alpha1.AnalysisRun, metric v1alpha1.Metric) (*batchv1.Job, error) {
jobAnnotations := metric.Provider.Job.Metadata.GetAnnotations()
jobLabels := metric.Provider.Job.Metadata.GetLabels()
if jobAnnotations == nil {
jobAnnotations = make(map[string]string)
}
if jobLabels == nil {
jobLabels = make(map[string]string)
}
jobLabels[AnalysisRunUIDLabelKey] = string(run.UID)
jobAnnotations[AnalysisRunNameAnnotationKey] = run.Name
jobAnnotations[AnalysisRunMetricAnnotationKey] = metric.Name
job := batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: newJobName(run, metric),
Namespace: run.Namespace,
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(run, analysisRunGVK)},
Annotations: map[string]string{
AnalysisRunNameAnnotationKey: run.Name,
AnalysisRunMetricAnnotationKey: metric.Name,
},
Labels: map[string]string{
AnalysisRunUIDLabelKey: string(run.UID),
},
Annotations: jobAnnotations,
Labels: jobLabels,
},
Spec: metric.Provider.Job.Spec,
}
Expand Down
17 changes: 17 additions & 0 deletions metricproviders/job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ func TestRun(t *testing.T) {
metric := run.Spec.Metrics[0]
metricsMetadata := p.GetMetadata(metric)
assert.Nil(t, metricsMetadata)
providerJobMetadataLabels := map[string]string{
"foo-label": "bar",
}
providerJobMetadataAnnotations := map[string]string{
"foo-annotation": "bar",
}
metric.Provider.Job.Metadata = metav1.ObjectMeta{
Labels: providerJobMetadataLabels,
Annotations: providerJobMetadataAnnotations,
}

measurement := p.Run(run, metric)

Expand All @@ -138,6 +148,13 @@ func TestRun(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedName, jobs.Items[0].Name)
assert.Equal(t, string(run.UID), jobs.Items[0].ObjectMeta.Labels[AnalysisRunUIDLabelKey])
for labelKey, labelVal := range providerJobMetadataLabels {
assert.Equal(t, labelVal, jobs.Items[0].ObjectMeta.Labels[labelKey])
}
for annotationKey, annotationVal := range providerJobMetadataAnnotations {
assert.Equal(t, annotationVal, jobs.Items[0].ObjectMeta.Annotations[annotationKey])
}

expectedOwnerRef := []metav1.OwnerReference{*metav1.NewControllerRef(run, analysisRunGVK)}
assert.Equal(t, expectedOwnerRef, jobs.Items[0].ObjectMeta.OwnerReferences)

Expand Down
5 changes: 5 additions & 0 deletions test/e2e/experiment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (s *ExperimentSuite) TestRolloutWithExperimentAndAnalysis() {
assert.Equal(s.T(), rs1Hash, envVars[1].Value)
assert.Equal(s.T(), rs2Hash, envVars[0].Value)

// verify if labels and annotations from AnalysisTemplate's .spec.metrics[0].provider.job.metadata
// are added to the Job.metadata
assert.Equal(t, "bar", job.GetLabels()["foo"])
assert.Equal(t, "bar2", job.GetAnnotations()["foo2"])

// verify the `templates.XXXXX.podTemplateHash` variables are working
expRSCanaryHash := expRSCanary.Spec.Template.Labels[rov1.DefaultRolloutUniqueLabelKey]
expRSBaselineHash := expRSBaseline.Spec.Template.Labels[rov1.DefaultRolloutUniqueLabelKey]
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/functional/rollout-experiment-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ spec:
- name: echo-pod-hash
provider:
job:
metadata:
labels:
foo: bar
annotations:
foo2: bar2
spec:
backoffLimit: 0
template:
Expand Down

0 comments on commit 74d2812

Please sign in to comment.