From 3e72df57a60ac245bcab836e0d23b994b9eedf1b Mon Sep 17 00:00:00 2001 From: Jennifer Chen Date: Wed, 6 Nov 2024 13:12:03 -0500 Subject: [PATCH] Define annotation structure and parse directly --- .../autoscaling/workload/model/metadata.go | 26 ++++++++ .../workload/model/metadata_test.go | 64 +++++++++++++++++++ .../workload/model/pod_autoscaler.go | 8 +-- 3 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 pkg/clusteragent/autoscaling/workload/model/metadata.go create mode 100644 pkg/clusteragent/autoscaling/workload/model/metadata_test.go diff --git a/pkg/clusteragent/autoscaling/workload/model/metadata.go b/pkg/clusteragent/autoscaling/workload/model/metadata.go new file mode 100644 index 0000000000000..a2aa0f7814791 --- /dev/null +++ b/pkg/clusteragent/autoscaling/workload/model/metadata.go @@ -0,0 +1,26 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2024-present Datadog, Inc. + +//go:build kubeapiserver + +package model + +const ( + AnnotationsURLKey = "autoscaling.datadoghq.com/url" + AnnotationsFallbackURLKey = "autoscaling.datadoghq.com/fallback-url" +) + +// Annotations represents the relevant annotations on a DatadogPodAutoscaler object +type Annotations struct { + Endpoint string + FallbackEndpoint string +} + +func ParseAnnotations(annotations map[string]string) Annotations { + return Annotations{ + Endpoint: annotations[AnnotationsURLKey], + FallbackEndpoint: annotations[AnnotationsFallbackURLKey], + } +} diff --git a/pkg/clusteragent/autoscaling/workload/model/metadata_test.go b/pkg/clusteragent/autoscaling/workload/model/metadata_test.go new file mode 100644 index 0000000000000..f2839c9d22132 --- /dev/null +++ b/pkg/clusteragent/autoscaling/workload/model/metadata_test.go @@ -0,0 +1,64 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2024-present Datadog, Inc. + +//go:build kubeapiserver && test + +package model + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseAnnotation(t *testing.T) { + tests := []struct { + name string + annotations map[string]string + expected Annotations + }{ + { + name: "Empty annotations", + annotations: map[string]string{}, + expected: Annotations{}, + }, + { + name: "URL annotation", + annotations: map[string]string{ + AnnotationsURLKey: "localhost:8080/test", + }, + expected: Annotations{ + Endpoint: "localhost:8080/test", + }, + }, + { + name: "Fallback annotation", + annotations: map[string]string{ + AnnotationsFallbackURLKey: "localhost:8080/fallback", + }, + expected: Annotations{ + FallbackEndpoint: "localhost:8080/fallback", + }, + }, + { + name: "URL and Fallback annotation", + annotations: map[string]string{ + AnnotationsURLKey: "localhost:8080/test", + AnnotationsFallbackURLKey: "localhost:8080/fallback", + }, + expected: Annotations{ + Endpoint: "localhost:8080/test", + FallbackEndpoint: "localhost:8080/fallback", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + parsedAnnotation := ParseAnnotations(tt.annotations) + assert.Equal(t, tt.expected, parsedAnnotation) + }) + } +} diff --git a/pkg/clusteragent/autoscaling/workload/model/pod_autoscaler.go b/pkg/clusteragent/autoscaling/workload/model/pod_autoscaler.go index 47c9dceab60fd..875713e8343e7 100644 --- a/pkg/clusteragent/autoscaling/workload/model/pod_autoscaler.go +++ b/pkg/clusteragent/autoscaling/workload/model/pod_autoscaler.go @@ -39,7 +39,7 @@ type PodAutoscalerInternal struct { name string // annotations are the annotations of the PodAutoscaler - annotations map[string]string + annotations Annotations // creationTimestamp is the time when the kubernetes object was created // creationTimestamp is stored in .DatadogPodAutoscaler.CreationTimestamp @@ -111,7 +111,7 @@ func NewPodAutoscalerInternal(podAutoscaler *datadoghq.DatadogPodAutoscaler) Pod pai := PodAutoscalerInternal{ namespace: podAutoscaler.Namespace, name: podAutoscaler.Name, - annotations: podAutoscaler.Annotations, + annotations: ParseAnnotations(podAutoscaler.Annotations), } pai.UpdateFromPodAutoscaler(podAutoscaler) pai.UpdateFromStatus(&podAutoscaler.Status) @@ -138,7 +138,7 @@ func NewPodAutoscalerFromSettings(ns, name string, podAutoscalerSpec *datadoghq. func (p *PodAutoscalerInternal) UpdateFromPodAutoscaler(podAutoscaler *datadoghq.DatadogPodAutoscaler) { p.creationTimestamp = podAutoscaler.CreationTimestamp.Time p.generation = podAutoscaler.Generation - p.annotations = podAutoscaler.Annotations + p.annotations = ParseAnnotations(podAutoscaler.Annotations) p.spec = podAutoscaler.Spec.DeepCopy() // Reset the target GVK as it might have changed // Resolving the target GVK is done in the controller sync to ensure proper sync and error handling @@ -326,7 +326,7 @@ func (p *PodAutoscalerInternal) Name() string { } // Annotations returns the annotations on the PodAutoscaler -func (p *PodAutoscalerInternal) Annotations() map[string]string { +func (p *PodAutoscalerInternal) Annotations() Annotations { return p.annotations }