Path: blob/main/install/installer/pkg/common/customize.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1/// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package common56import (7corev1 "k8s.io/api/core/v1"8metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"9)1011type CustomizationType string1213const (14CustomizationTypeAnnotation CustomizationType = "annotation"15CustomizationTypeLabel CustomizationType = "label"16)1718func extractCustomizations(ctx *RenderContext, name string, typeMeta metav1.TypeMeta, customizationType CustomizationType) map[string]string {19customizations := make(map[string]string, 0)2021if ctx.Config.Customization != nil {22for _, customization := range *ctx.Config.Customization {23// Match the apiVersion, kind and name - nested to make more readable. Must match value or "*"24if customization.APIVersion == typeMeta.APIVersion || customization.APIVersion == "*" {25// Matches apiVersion26if customization.Kind == typeMeta.Kind || customization.Kind == "*" {27// Matches kind28if customization.Metadata.Name == name || customization.Metadata.Name == "*" {29// Matches the name30if customizationType == CustomizationTypeAnnotation {31// Annotations32customizations = mergeCustomizations(customizations, customization.Metadata.Annotations)33}34if customizationType == CustomizationTypeLabel {35// Labels36customizations = mergeCustomizations(customizations, customization.Metadata.Labels)37}38}39}40}41}42}4344return customizations45}4647func mergeCustomizations(customizations map[string]string, input map[string]string) map[string]string {48for k, v := range input {49customizations[k] = v50}5152return customizations53}5455func CustomizeAnnotation(ctx *RenderContext, component string, typeMeta metav1.TypeMeta, existingAnnotations ...func() map[string]string) map[string]string {56annotations := make(map[string]string, 0)5758// Apply the customizations59for k, v := range extractCustomizations(ctx, component, typeMeta, CustomizationTypeAnnotation) {60annotations[k] = v61}6263// Always apply existing annotations64for _, e := range existingAnnotations {65for k, v := range e() {66annotations[k] = v67}68}6970return annotations71}7273func CustomizeEnvvar(ctx *RenderContext, component string, existingEnvvars []corev1.EnvVar) []corev1.EnvVar {74// Order is important to support GitOps workflows75output := make([]corev1.EnvVar, 0)76customizeOrder := make([]string, 0)77existing := make(map[string]corev1.EnvVar, 0)7879// Use a map so we can remove duplicated keys80envvars := make(map[string]corev1.EnvVar, 0)8182// Get existing as a map so we don't override these83for _, e := range existingEnvvars {84// Ensure that existing envvars are first85customizeOrder = append(customizeOrder, e.Name)86existing[e.Name] = e87envvars[e.Name] = e88}8990// Apply the customizations - envvars only need to match name91if ctx.Config.Customization != nil {92for _, customization := range *ctx.Config.Customization {93if customization.Metadata.Name == component || customization.Metadata.Name == "*" {94for _, e := range customization.Spec.Env {95_, inExisting := existing[e.Name]96_, inEnvvars := envvars[e.Name]9798if !inExisting {99if !inEnvvars {100// Only interested in name101customizeOrder = append(customizeOrder, e.Name)102}103// Set the value if not in existing envvars104envvars[e.Name] = e105}106107}108}109}110}111112// Convert map back slice113for _, e := range customizeOrder {114output = append(output, envvars[e])115}116return output117}118119func CustomizeLabel(ctx *RenderContext, component string, typeMeta metav1.TypeMeta, existingLabels ...func() map[string]string) map[string]string {120labels := DefaultLabels(component)121122// Apply the customizations123for k, v := range extractCustomizations(ctx, component, typeMeta, CustomizationTypeLabel) {124labels[k] = v125}126127// Always apply existing labels128for _, e := range existingLabels {129for k, v := range e() {130labels[k] = v131}132}133134return labels135}136137138