From 77eaad31f7c0cc1833b443134ef2bc6879d1eb0a Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Fri, 6 Sep 2024 13:39:11 -0400 Subject: [PATCH] supply additional labels and annotations to fs-minio pods --- pkg/kotsutil/kots.go | 34 ++++++++++++++++++++ pkg/snapshot/filesystem_minio.go | 53 ++++++++++++++++++++++++-------- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/pkg/kotsutil/kots.go b/pkg/kotsutil/kots.go index 73ee3b5c49..99b8639b80 100644 --- a/pkg/kotsutil/kots.go +++ b/pkg/kotsutil/kots.go @@ -8,6 +8,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "k8s.io/client-go/kubernetes" "os" "path" "path/filepath" @@ -1140,6 +1141,8 @@ type InstallationParams struct { WithMinio bool AppVersionLabel string RequestedChannelSlug string + AdditionalAnnotations map[string]string + AdditionalLabels map[string]string } func GetInstallationParams(configMapName string) (InstallationParams, error) { @@ -1150,6 +1153,12 @@ func GetInstallationParams(configMapName string) (InstallationParams, error) { return autoConfig, errors.Wrap(err, "failed to get k8s clientset") } + return GetInstallationParamsWithClientset(clientset, configMapName) +} + +func GetInstallationParamsWithClientset(clientset kubernetes.Interface, configMapName string) (InstallationParams, error) { + autoConfig := InstallationParams{} + isKurl, err := kurl.IsKurl(clientset) if err != nil { return autoConfig, errors.Wrap(err, "failed to check if cluster is kurl") @@ -1183,6 +1192,31 @@ func GetInstallationParams(configMapName string) (InstallationParams, error) { autoConfig.EnableImageDeletion = isKurl } + autoConfig.AdditionalAnnotations = make(map[string]string) + allAnnotations := strings.Split(kotsadmConfigMap.Data["additional-annotations"], ",") + for _, annotation := range allAnnotations { + if annotation == "" { + continue + } + parts := strings.Split(annotation, "=") + if len(parts) != 2 { + return autoConfig, errors.Errorf("invalid additional annotation %q", annotation) + } + autoConfig.AdditionalAnnotations[parts[0]] = parts[1] + } + autoConfig.AdditionalLabels = make(map[string]string) + allLabels := strings.Split(kotsadmConfigMap.Data["additional-labels"], ",") + for _, label := range allLabels { + if label == "" { + continue + } + parts := strings.Split(label, "=") + if len(parts) != 2 { + return autoConfig, errors.Errorf("invalid additional label %q", label) + } + autoConfig.AdditionalLabels[parts[0]] = parts[1] + } + return autoConfig, nil } diff --git a/pkg/snapshot/filesystem_minio.go b/pkg/snapshot/filesystem_minio.go index afe5970f86..4464610df6 100644 --- a/pkg/snapshot/filesystem_minio.go +++ b/pkg/snapshot/filesystem_minio.go @@ -19,6 +19,7 @@ import ( kotsadmresources "github.com/replicatedhq/kots/pkg/kotsadm/resources" kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types" kotsadmversion "github.com/replicatedhq/kots/pkg/kotsadm/version" + "github.com/replicatedhq/kots/pkg/kotsutil" "github.com/replicatedhq/kots/pkg/kurl" "github.com/replicatedhq/kots/pkg/logger" kotss3 "github.com/replicatedhq/kots/pkg/s3" @@ -300,6 +301,11 @@ func fileSystemMinioDeploymentResource(clientset kubernetes.Interface, secretChe } } + globalOptions, err := kotsutil.GetInstallationParamsWithClientset(clientset, kotsadmtypes.KotsadmConfigMap) + if err != nil { + return nil, errors.Wrap(err, "failed to get global options") + } + env := []corev1.EnvVar{ { Name: "MINIO_UPDATE", @@ -329,13 +335,28 @@ func fileSystemMinioDeploymentResource(clientset kubernetes.Interface, secretChe }, } + podAnnotations := map[string]string{ + "kots.io/fs-minio-creds-secret-checksum": secretChecksum, + } + for k, v := range globalOptions.AdditionalAnnotations { + podAnnotations[k] = v + } + podLabels := map[string]string{ + "app": "kotsadm-fs-minio", + } + for k, v := range globalOptions.AdditionalLabels { + podLabels[k] = v + } + return &appsv1.Deployment{ TypeMeta: metav1.TypeMeta{ APIVersion: "apps/v1", Kind: "Deployment", }, ObjectMeta: metav1.ObjectMeta{ - Name: FileSystemMinioDeploymentName, + Name: FileSystemMinioDeploymentName, + Labels: globalOptions.AdditionalLabels, + Annotations: globalOptions.AdditionalAnnotations, }, Spec: appsv1.DeploymentSpec{ Replicas: pointer.Int32Ptr(1), @@ -349,12 +370,8 @@ func fileSystemMinioDeploymentResource(clientset kubernetes.Interface, secretChe }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{ - "app": "kotsadm-fs-minio", - }, - Annotations: map[string]string{ - "kots.io/fs-minio-creds-secret-checksum": secretChecksum, - }, + Labels: podLabels, + Annotations: podAnnotations, }, Spec: corev1.PodSpec{ SecurityContext: &securityContext, @@ -837,18 +854,28 @@ func fileSystemMinioConfigPod(clientset kubernetes.Interface, deployOptions File } } + globalOptions, err := kotsutil.GetInstallationParamsWithClientset(clientset, kotsadmtypes.KotsadmConfigMap) + if err != nil { + return nil, errors.Wrap(err, "failed to get global options") + } + podLabels := map[string]string{ + "app": "kotsadm-fs-minio", + "check": podCheckTag, + } + for k, v := range globalOptions.AdditionalLabels { + podLabels[k] = v + } + pod := &corev1.Pod{ TypeMeta: metav1.TypeMeta{ APIVersion: "v1", Kind: "Pod", }, ObjectMeta: metav1.ObjectMeta{ - Name: podName, - Namespace: deployOptions.Namespace, - Labels: map[string]string{ - "app": "kotsadm-fs-minio", - "check": podCheckTag, - }, + Name: podName, + Namespace: deployOptions.Namespace, + Labels: podLabels, + Annotations: globalOptions.AdditionalAnnotations, }, Spec: corev1.PodSpec{ SecurityContext: &securityContext,