Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metricprovider): allow user to define metrics.provider.job.metadata #2762

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/analysis/job.md
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
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
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
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(s.T(), "bar", job.GetLabels()["foo"])
assert.Equal(s.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
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