Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/pkg/common/customize.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
/// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package common
6
7
import (
8
corev1 "k8s.io/api/core/v1"
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
)
11
12
type CustomizationType string
13
14
const (
15
CustomizationTypeAnnotation CustomizationType = "annotation"
16
CustomizationTypeLabel CustomizationType = "label"
17
)
18
19
func extractCustomizations(ctx *RenderContext, name string, typeMeta metav1.TypeMeta, customizationType CustomizationType) map[string]string {
20
customizations := make(map[string]string, 0)
21
22
if ctx.Config.Customization != nil {
23
for _, customization := range *ctx.Config.Customization {
24
// Match the apiVersion, kind and name - nested to make more readable. Must match value or "*"
25
if customization.APIVersion == typeMeta.APIVersion || customization.APIVersion == "*" {
26
// Matches apiVersion
27
if customization.Kind == typeMeta.Kind || customization.Kind == "*" {
28
// Matches kind
29
if customization.Metadata.Name == name || customization.Metadata.Name == "*" {
30
// Matches the name
31
if customizationType == CustomizationTypeAnnotation {
32
// Annotations
33
customizations = mergeCustomizations(customizations, customization.Metadata.Annotations)
34
}
35
if customizationType == CustomizationTypeLabel {
36
// Labels
37
customizations = mergeCustomizations(customizations, customization.Metadata.Labels)
38
}
39
}
40
}
41
}
42
}
43
}
44
45
return customizations
46
}
47
48
func mergeCustomizations(customizations map[string]string, input map[string]string) map[string]string {
49
for k, v := range input {
50
customizations[k] = v
51
}
52
53
return customizations
54
}
55
56
func CustomizeAnnotation(ctx *RenderContext, component string, typeMeta metav1.TypeMeta, existingAnnotations ...func() map[string]string) map[string]string {
57
annotations := make(map[string]string, 0)
58
59
// Apply the customizations
60
for k, v := range extractCustomizations(ctx, component, typeMeta, CustomizationTypeAnnotation) {
61
annotations[k] = v
62
}
63
64
// Always apply existing annotations
65
for _, e := range existingAnnotations {
66
for k, v := range e() {
67
annotations[k] = v
68
}
69
}
70
71
return annotations
72
}
73
74
func CustomizeEnvvar(ctx *RenderContext, component string, existingEnvvars []corev1.EnvVar) []corev1.EnvVar {
75
// Order is important to support GitOps workflows
76
output := make([]corev1.EnvVar, 0)
77
customizeOrder := make([]string, 0)
78
existing := make(map[string]corev1.EnvVar, 0)
79
80
// Use a map so we can remove duplicated keys
81
envvars := make(map[string]corev1.EnvVar, 0)
82
83
// Get existing as a map so we don't override these
84
for _, e := range existingEnvvars {
85
// Ensure that existing envvars are first
86
customizeOrder = append(customizeOrder, e.Name)
87
existing[e.Name] = e
88
envvars[e.Name] = e
89
}
90
91
// Apply the customizations - envvars only need to match name
92
if ctx.Config.Customization != nil {
93
for _, customization := range *ctx.Config.Customization {
94
if customization.Metadata.Name == component || customization.Metadata.Name == "*" {
95
for _, e := range customization.Spec.Env {
96
_, inExisting := existing[e.Name]
97
_, inEnvvars := envvars[e.Name]
98
99
if !inExisting {
100
if !inEnvvars {
101
// Only interested in name
102
customizeOrder = append(customizeOrder, e.Name)
103
}
104
// Set the value if not in existing envvars
105
envvars[e.Name] = e
106
}
107
108
}
109
}
110
}
111
}
112
113
// Convert map back slice
114
for _, e := range customizeOrder {
115
output = append(output, envvars[e])
116
}
117
return output
118
}
119
120
func CustomizeLabel(ctx *RenderContext, component string, typeMeta metav1.TypeMeta, existingLabels ...func() map[string]string) map[string]string {
121
labels := DefaultLabels(component)
122
123
// Apply the customizations
124
for k, v := range extractCustomizations(ctx, component, typeMeta, CustomizationTypeLabel) {
125
labels[k] = v
126
}
127
128
// Always apply existing labels
129
for _, e := range existingLabels {
130
for k, v := range e() {
131
labels[k] = v
132
}
133
}
134
135
return labels
136
}
137
138