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

Implement proper port for prometheus #559

Merged
merged 5 commits into from
Jan 10, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -54,6 +55,7 @@ func TestFunctionReconciler_equalServices(t *testing.T) {
Labels: map[string]string{
"label1": "label1",
},
Annotations: prometheusSvcAnnotations(),
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 38 additions & 12 deletions components/serverless/internal/controllers/serverless/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -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
anoipm marked this conversation as resolved.
Show resolved Hide resolved
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 &&
Expand Down