diff --git a/components/serverless/internal/controllers/serverless/service.go b/components/serverless/internal/controllers/serverless/service.go index 36e87f3b9..668cd51f5 100644 --- a/components/serverless/internal/controllers/serverless/service.go +++ b/components/serverless/internal/controllers/serverless/service.go @@ -49,6 +49,7 @@ func buildStateFnUpdateService(newService corev1.Service) stateFn { svc.Spec.Type = newService.Spec.Type svc.ObjectMeta.Labels = newService.GetLabels() + mergeMapWithNewValues(svc.ObjectMeta.Annotations, newService.GetAnnotations()) r.log.Info(fmt.Sprintf("Updating Service %s", svc.GetName())) diff --git a/components/serverless/internal/controllers/serverless/service_test.go b/components/serverless/internal/controllers/serverless/service_test.go index d66ca085b..516c34edc 100644 --- a/components/serverless/internal/controllers/serverless/service_test.go +++ b/components/serverless/internal/controllers/serverless/service_test.go @@ -26,12 +26,13 @@ func TestFunctionReconciler_equalServices(t *testing.T) { want bool }{ { - name: "simple case", + name: "simple equal case", args: args{ existing: corev1.Service{ ObjectMeta: metav1.ObjectMeta{ - Name: "svc-name", - Namespace: "svc-ns", + Name: "svc-name", + Namespace: "svc-ns", + Annotations: prometheusSvcAnnotations(), Labels: map[string]string{ "label1": "label1", }, @@ -54,6 +55,7 @@ func TestFunctionReconciler_equalServices(t *testing.T) { Labels: map[string]string{ "label1": "label1", }, + Annotations: prometheusSvcAnnotations(), }, Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{{ diff --git a/components/serverless/internal/controllers/serverless/system_state.go b/components/serverless/internal/controllers/serverless/system_state.go index d0e35f911..adb3fb1ff 100644 --- a/components/serverless/internal/controllers/serverless/system_state.go +++ b/components/serverless/internal/controllers/serverless/system_state.go @@ -56,13 +56,16 @@ func (s *systemState) functionLabels() map[string]string { } func (s *systemState) functionAnnotations() map[string]string { + return prometheusSvcAnnotations() +} + +func prometheusSvcAnnotations() map[string]string { return map[string]string{ - "prometheus.io/port": "80", + "prometheus.io/port": "8080", "prometheus.io/path": "/metrics", "prometheus.io/scrape": "true", } } - func (s *systemState) buildImageAddress(registryAddress string) string { var imageTag string isGitType := s.instance.TypeOf(serverlessv1alpha2.FunctionTypeGit) diff --git a/components/serverless/internal/controllers/serverless/utils.go b/components/serverless/internal/controllers/serverless/utils.go index 79aab43e2..7df884bb8 100644 --- a/components/serverless/internal/controllers/serverless/utils.go +++ b/components/serverless/internal/controllers/serverless/utils.go @@ -132,11 +132,11 @@ func didNotFail(j batchv1.Job) bool { func countJobs(l batchv1.JobList, predicates ...func(batchv1.Job) bool) int { var out int -processing_next_item: +processingNextItem: for _, j := range l.Items { for _, p := range predicates { if !p(j) { - continue processing_next_item + continue processingNextItem } } out++ @@ -186,22 +186,48 @@ func mapsEqual(existing, expected map[string]string) bool { return true } +func mapsContains(mapSet, mapSubset map[string]string) bool { + if len(mapSet) < len(mapSubset) { + return false + } + + for key, value := range mapSubset { + if v, ok := mapSet[key]; !ok || v != value { + return false + } + } + + return true +} + +func mergeMapWithNewValues(existing, newValues map[string]string) { + for key, value := range newValues { + existing[key] = value + } +} + // TODO refactor to make this code more readable func equalDeployments(existing appsv1.Deployment, expected appsv1.Deployment) bool { - return len(existing.Spec.Template.Spec.Containers) == 1 && - len(existing.Spec.Template.Spec.Containers) == len(expected.Spec.Template.Spec.Containers) && - existing.Spec.Template.Spec.Containers[0].Image == expected.Spec.Template.Spec.Containers[0].Image && - envsEqual(existing.Spec.Template.Spec.Containers[0].Env, expected.Spec.Template.Spec.Containers[0].Env) && - mapsEqual(existing.GetLabels(), expected.GetLabels()) && - mapsEqual(existing.Spec.Template.GetLabels(), expected.Spec.Template.GetLabels()) && - equalResources(existing.Spec.Template.Spec.Containers[0].Resources, expected.Spec.Template.Spec.Containers[0].Resources) && - equalInt32Pointer(existing.Spec.Replicas, expected.Spec.Replicas) && - equalSecretMounts(existing.Spec.Template.Spec, expected.Spec.Template.Spec) && - mapsEqual(existing.Spec.Template.GetAnnotations(), expected.Spec.Template.GetAnnotations()) + result := true + result = result && len(existing.Spec.Template.Spec.Containers) == 1 + result = result && len(existing.Spec.Template.Spec.Containers) == len(expected.Spec.Template.Spec.Containers) + + result = result && existing.Spec.Template.Spec.Containers[0].Image == expected.Spec.Template.Spec.Containers[0].Image + result = result && envsEqual(existing.Spec.Template.Spec.Containers[0].Env, expected.Spec.Template.Spec.Containers[0].Env) + result = result && equalResources(existing.Spec.Template.Spec.Containers[0].Resources, expected.Spec.Template.Spec.Containers[0].Resources) + + result = result && mapsEqual(existing.GetLabels(), expected.GetLabels()) + result = result && mapsEqual(existing.Spec.Template.GetLabels(), expected.Spec.Template.GetLabels()) + result = result && equalInt32Pointer(existing.Spec.Replicas, expected.Spec.Replicas) + + result = result && mapsEqual(existing.Spec.Template.GetAnnotations(), expected.Spec.Template.GetAnnotations()) + result = result && equalSecretMounts(existing.Spec.Template.Spec, expected.Spec.Template.Spec) + return result } func equalServices(existing corev1.Service, expected corev1.Service) bool { return mapsEqual(existing.Spec.Selector, expected.Spec.Selector) && + mapsContains(existing.Annotations, prometheusSvcAnnotations()) && mapsEqual(existing.Labels, expected.Labels) && len(existing.Spec.Ports) == len(expected.Spec.Ports) && len(expected.Spec.Ports) > 0 &&