diff --git a/handlers/deploy.go b/handlers/deploy.go index 487ae20d2f..ecbe34f44c 100644 --- a/handlers/deploy.go +++ b/handlers/deploy.go @@ -268,7 +268,9 @@ func buildAnnotations(request types.FunctionDeployment) map[string]string { annotations = map[string]string{} } - annotations["prometheus.io.scrape"] = "false" + if _, ok := annotations["prometheus.io.scrape"]; !ok { + annotations["prometheus.io.scrape"] = "false" + } return annotations } diff --git a/handlers/deploy_test.go b/handlers/deploy_test.go index fc98d3454b..e051359958 100644 --- a/handlers/deploy_test.go +++ b/handlers/deploy_test.go @@ -32,6 +32,25 @@ func Test_buildAnnotations_Empty_In_CreateRequest(t *testing.T) { } } +func Test_buildAnnotations_Premetheus_NotOverridden(t *testing.T) { + request := types.FunctionDeployment{Annotations: &map[string]string{"prometheus.io.scrape": "true"}} + + annotations := buildAnnotations(request) + + if len(annotations) != 1 { + t.Errorf("want: %d annotations got: %d", 1, len(annotations)) + } + + v, ok := annotations["prometheus.io.scrape"] + if !ok { + t.Errorf("missing prometheus.io.scrape key") + } + + if v != "true" { + t.Errorf("want: %s for annotation prometheus.io.scrape got: %s", "true", v) + } +} + func Test_buildAnnotations_From_CreateRequest(t *testing.T) { request := types.FunctionDeployment{ Annotations: &map[string]string{ diff --git a/pkg/controller/deployment_test.go b/pkg/controller/deployment_test.go index 6c0cae6217..cc4ca78fac 100644 --- a/pkg/controller/deployment_test.go +++ b/pkg/controller/deployment_test.go @@ -72,3 +72,42 @@ func Test_newDeployment(t *testing.T) { t.Fail() } } + +func Test_newDeployment_PrometheusScrape_NotOverridden(t *testing.T) { + function := &faasv1.Function{ + ObjectMeta: metav1.ObjectMeta{ + Name: "kubesec", + }, + Spec: faasv1.FunctionSpec{ + Name: "kubesec", + Image: "docker.io/kubesec/kubesec", + Annotations: &map[string]string{ + "prometheus.io.scrape": "true", + }, + }, + } + + factory := NewFunctionFactory(fake.NewSimpleClientset(), + k8s.DeploymentConfig{ + HTTPProbe: false, + SetNonRootUser: true, + LivenessProbe: &k8s.ProbeConfig{ + PeriodSeconds: 1, + TimeoutSeconds: 3, + InitialDelaySeconds: 0, + }, + ReadinessProbe: &k8s.ProbeConfig{ + PeriodSeconds: 1, + TimeoutSeconds: 3, + InitialDelaySeconds: 0, + }, + }) + + secrets := map[string]*corev1.Secret{} + + deployment := newDeployment(function, nil, secrets, factory) + + if deployment.Spec.Template.Annotations["prometheus.io.scrape"] != "true" { + t.Errorf("Annotation prometheus.io.scrape should be %s, was: %s", "true", deployment.Spec.Template.Annotations["prometheus.io.scrape"]) + } +}