Skip to content
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

[installer]: implementation customization functions #10906

Merged
merged 4 commits into from
Jun 27, 2022
Merged
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
82 changes: 81 additions & 1 deletion install/installer/pkg/common/customize.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,59 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type CustomizationType string

const (
CustomizationTypeAnnotation CustomizationType = "annotation"
CustomizationTypeLabel CustomizationType = "label"
)

func extractCustomizations(ctx *RenderContext, name string, typeMeta metav1.TypeMeta, customizationType CustomizationType) map[string]string {
customizations := make(map[string]string, 0)

if ctx.Config.Customization != nil {
for _, customization := range *ctx.Config.Customization {
// Match the apiVersion, kind and name - nested to make more readable. Must match value or "*"
if customization.APIVersion == typeMeta.APIVersion || customization.APIVersion == "*" {
// Matches apiVersion
if customization.Kind == typeMeta.Kind || customization.Kind == "*" {
// Matches kind
if customization.Metadata.Name == name || customization.Metadata.Name == "*" {
// Matches the name
if customizationType == CustomizationTypeAnnotation {
// Annotations
customizations = mergeCustomizations(customizations, customization.Metadata.Annotations)
}
if customizationType == CustomizationTypeLabel {
// Labels
customizations = mergeCustomizations(customizations, customization.Metadata.Labels)
}
}
}
}
}
}

return customizations
}

func mergeCustomizations(customizations map[string]string, input map[string]string) map[string]string {
for k, v := range input {
customizations[k] = v
}

return customizations
}

func CustomizeAnnotation(ctx *RenderContext, component string, typeMeta metav1.TypeMeta, existingAnnotations ...func() map[string]string) map[string]string {
annotations := make(map[string]string, 0)

// Apply the customizations
for k, v := range extractCustomizations(ctx, component, typeMeta, CustomizationTypeAnnotation) {
annotations[k] = v
}

// Always apply existing annotations
for _, e := range existingAnnotations {
for k, v := range e() {
annotations[k] = v
Expand All @@ -21,12 +71,42 @@ func CustomizeAnnotation(ctx *RenderContext, component string, typeMeta metav1.T
}

func CustomizeEnvvar(ctx *RenderContext, component string, existingEnvvars []corev1.EnvVar) []corev1.EnvVar {
return existingEnvvars
// Use a map so we can remove duplicated keys
envvars := make(map[string]corev1.EnvVar, 0)

// Apply the customizations - envvars only need to match name
if ctx.Config.Customization != nil {
for _, customization := range *ctx.Config.Customization {
if customization.Metadata.Name == component || customization.Metadata.Name == "*" {
for _, e := range customization.Spec.Env {
envvars[e.Name] = e
}
}
}
}

// Always apply existing envvars
for _, e := range existingEnvvars {
envvars[e.Name] = e
}

// Convert map back slice
output := make([]corev1.EnvVar, 0, len(envvars))
for _, e := range envvars {
output = append(output, e)
}
return output
}

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

// Apply the customizations
for k, v := range extractCustomizations(ctx, component, typeMeta, CustomizationTypeLabel) {
labels[k] = v
}

// Always apply existing labels
for _, e := range existingLabels {
for k, v := range e() {
labels[k] = v
Expand Down
Loading