Skip to content

Allow custom annotations for k8s resources #10827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions install/installer/pkg/common/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ func GenerateInstallationConfigMap(ctx *RenderContext, objects []RuntimeObject)
cfgMap := corev1.ConfigMap{
TypeMeta: TypeMetaConfigmap,
ObjectMeta: metav1.ObjectMeta{
Name: component,
Namespace: ctx.Namespace,
Labels: DefaultLabels(component),
Name: component,
Namespace: ctx.Namespace,
Labels: CustomOverrideLabel(ctx, component, TypeMetaConfigmap),
Annotations: CustomOverrideAnnotation(ctx, component, TypeMetaConfigmap),
},
}

Expand Down
19 changes: 16 additions & 3 deletions install/installer/pkg/common/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ func DefaultServiceAccount(component string) RenderFunc {
&corev1.ServiceAccount{
TypeMeta: TypeMetaServiceAccount,
ObjectMeta: metav1.ObjectMeta{
Name: component,
Namespace: cfg.Namespace,
Labels: DefaultLabels(component),
Name: component,
Namespace: cfg.Namespace,
Labels: CustomOverrideLabel(cfg, component, TypeMetaServiceAccount),
Annotations: CustomOverrideAnnotation(cfg, component, TypeMetaServiceAccount),
},
AutomountServiceAccountToken: pointer.Bool(true),
ImagePullSecrets: pullSecrets,
Expand Down Expand Up @@ -81,6 +82,18 @@ func GenerateService(component string, ports []ServicePort, mod ...func(spec *co
m(service)
}

// Apply any custom overrides - perform after the custom modifications so they can overridden

// Annotations
service.ObjectMeta.Annotations = CustomOverrideAnnotation(cfg, component, TypeMetaService, func() map[string]string {
return service.ObjectMeta.Annotations
})

// Labels
service.ObjectMeta.Labels = CustomOverrideLabel(cfg, component, TypeMetaService, func() map[string]string {
return service.ObjectMeta.Labels
})

return []runtime.Object{service}, nil
}
}
Expand Down
84 changes: 84 additions & 0 deletions install/installer/pkg/common/overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the MIT License. See License-MIT.txt in the project root for license information.

package common

import (
"sort"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// CustomOverrideAnnotation override the annotations based upon rules that the config defines
func CustomOverrideAnnotation(ctx *RenderContext, component string, typeMeta metav1.TypeMeta, existingAnnotations ...func() map[string]string) map[string]string {
// Get the metadata kind in lowercase
kind := strings.ToLower(typeMeta.Kind)

annotations := make(map[string]string)

// Start with any existing annotations
for _, e := range existingAnnotations {
for k, v := range e() {
annotations[k] = v
}
}

if ctx.Config.Components != nil && ctx.Config.Components.Annotations != nil {
annotationKeys := make([]string, 0, len(*ctx.Config.Components.Annotations))
for k := range *ctx.Config.Components.Annotations {
annotationKeys = append(annotationKeys, k)
}
// Ensure that "*" comes first
sort.Strings(annotationKeys)

for _, annotationKindKey := range annotationKeys {
annotationKindData := (*ctx.Config.Components.Annotations)[annotationKindKey]

if annotationKindKey == "*" || annotationKindKey == kind {
// The key here matches this resource's "kind"
componentKeys := make([]string, 0, len(annotationKindData))
for k := range annotationKindData {
componentKeys = append(componentKeys, k)
}
// Ensure that "*" comes first
sort.Strings(componentKeys)

for _, componentName := range componentKeys {
annotationMap := annotationKindData[componentName]
if componentName == "*" || componentName == component {
// The key here match this resource's "name" - we can now look to add these to the annotation map
for k, v := range annotationMap {
if v == "" {
// Delete the key/value pair
delete(annotations, k)
} else {
// Add the key/value
annotations[k] = v
}
}
}
}
}
}
}

return annotations
}

func CustomOverrideEnvvar(ctx *RenderContext, component string, existingEnvvars []corev1.EnvVar) []corev1.EnvVar {
return existingEnvvars
}

func CustomOverrideLabel(ctx *RenderContext, component string, typeMeta metav1.TypeMeta, existingLabels ...func() map[string]string) map[string]string {
labels := DefaultLabels(component)

for _, e := range existingLabels {
for k, v := range e() {
labels[k] = v
}
}

return labels
}
Loading