Skip to content

Commit

Permalink
configurable metrics-server image through private annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
neoaggelos committed Jun 2, 2024
1 parent 7df07c0 commit 9df847b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/k8s/pkg/k8sd/features/metrics-server/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package metrics_server

import "github.com/canonical/k8s/pkg/k8sd/types"

const (
annotationImageRepo = "k8sd/v1alpha1/metrics-server/image-repo"
annotationImageTag = "k8sd/v1alpha1/metrics-server/image-tag"
)

type config struct {
imageRepo string
imageTag string
}

func internalConfig(annotations types.Annotations) config {
config := config{
imageRepo: imageRepo,
imageTag: imageTag,
}

if v, ok := annotations.Get(annotationImageRepo); ok {
config.imageRepo = v
}
if v, ok := annotations.Get(annotationImageTag); ok {
config.imageTag = v
}

return config
}
6 changes: 4 additions & 2 deletions src/k8s/pkg/k8sd/features/metrics-server/metrics_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import (
func ApplyMetricsServer(ctx context.Context, snap snap.Snap, cfg types.MetricsServer, annotations types.Annotations) error {
m := snap.HelmClient()

config := internalConfig(annotations)

values := map[string]any{
"image": map[string]any{
"repository": imageRepo,
"tag": imageTag,
"repository": config.imageRepo,
"tag": config.imageTag,
},
"securityContext": map[string]any{
// ROCKs with Pebble as the entrypoint do not work with this option.
Expand Down
27 changes: 26 additions & 1 deletion src/k8s/pkg/k8sd/features/metrics-server/metrics_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestApplyMetricsServer(t *testing.T) {
},
}

err := metrics_server.ApplyMetricsServer(context.Background(), s, tc.config)
err := metrics_server.ApplyMetricsServer(context.Background(), s, tc.config, nil)
g.Expect(err).ToNot(HaveOccurred())

g.Expect(h.ApplyCalledWith).To(ConsistOf(SatisfyAll(
Expand All @@ -53,4 +53,29 @@ func TestApplyMetricsServer(t *testing.T) {
)))
})
}

t.Run("Annotations", func(t *testing.T) {
g := NewWithT(t)
h := &helmmock.Mock{}
s := &snapmock.Snap{
Mock: snapmock.Mock{
HelmClient: h,
},
}

cfg := types.MetricsServer{
Enabled: utils.Pointer(true),
}
annotations := types.Annotations{
"k8sd/v1alpha1/metrics-server/image-repo": "custom-image",
"k8sd/v1alpha1/metrics-server/image-tag": "custom-tag",
}

err := metrics_server.ApplyMetricsServer(context.Background(), s, cfg, annotations)
g.Expect(err).To(BeNil())
g.Expect(h.ApplyCalledWith).To(ConsistOf(HaveField("Values", HaveKeyWithValue("image", SatisfyAll(
HaveKeyWithValue("repository", "custom-image"),
HaveKeyWithValue("tag", "custom-tag"),
)))))
})
}

0 comments on commit 9df847b

Please sign in to comment.