Skip to content

Fix overriding of prometheus scrape #624

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion handlers/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
19 changes: 19 additions & 0 deletions handlers/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably add a return there, or use t.Fatalf that @LucasRoesler introduced me to, which returns out of the test.

Also extract "true" to want := "true" please.

I'll merge, but send in the alteration after.

}

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{
Expand Down
39 changes: 39 additions & 0 deletions pkg/controller/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
}