Skip to content

Commit

Permalink
Define annotation structure and parse directly
Browse files Browse the repository at this point in the history
  • Loading branch information
jennchenn committed Nov 6, 2024
1 parent f1305ff commit 3e72df5
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
26 changes: 26 additions & 0 deletions pkg/clusteragent/autoscaling/workload/model/metadata.go
Original file line number Diff line number Diff line change
@@ -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],
}
}
64 changes: 64 additions & 0 deletions pkg/clusteragent/autoscaling/workload/model/metadata_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
8 changes: 4 additions & 4 deletions pkg/clusteragent/autoscaling/workload/model/pod_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 3e72df5

Please sign in to comment.