Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/pkg/common/display.go
2500 views
1
// Copyright (c) 2021 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
"regexp"
9
"sort"
10
"strings"
11
12
corev1 "k8s.io/api/core/v1"
13
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
"sigs.k8s.io/yaml"
16
)
17
18
// Those occurring earlier in the list get installed before those occurring later in the list.
19
// Based on Helm's list, with our CRDs added in
20
21
var sortOrder = []string{
22
"Namespace",
23
"NetworkPolicy",
24
"ResourceQuota",
25
"Issuer",
26
"Certificate",
27
"LimitRange",
28
"PodDisruptionBudget",
29
"ServiceAccount",
30
"Secret",
31
"SecretList",
32
"ConfigMap",
33
"StorageClass",
34
"PersistentVolume",
35
"PersistentVolumeClaim",
36
"CustomResourceDefinition",
37
"ClusterRole",
38
"ClusterRoleList",
39
"ClusterRoleBinding",
40
"ClusterRoleBindingList",
41
"Role",
42
"RoleList",
43
"RoleBinding",
44
"RoleBindingList",
45
"Service",
46
"DaemonSet",
47
"Pod",
48
"ReplicationController",
49
"ReplicaSet",
50
"StatefulSet",
51
"Deployment",
52
"HorizontalPodAutoscaler",
53
"Job",
54
"CronJob",
55
"Ingress",
56
"APIService",
57
}
58
59
type RuntimeObject struct {
60
metav1.TypeMeta `json:",inline"`
61
Metadata metav1.ObjectMeta `json:"metadata"`
62
Content string `json:"-"`
63
}
64
65
func DependencySortingRenderFunc(objects []RuntimeObject) ([]RuntimeObject, error) {
66
sortMap := map[string]int{}
67
for k, v := range sortOrder {
68
sortMap[v] = k
69
}
70
71
sort.SliceStable(objects, func(i, j int) bool {
72
scoreI := sortMap[objects[i].Kind]
73
scoreJ := sortMap[objects[j].Kind]
74
75
if scoreI == scoreJ {
76
return objects[i].Metadata.Name < objects[j].Metadata.Name
77
}
78
79
return scoreI < scoreJ
80
})
81
82
return objects, nil
83
}
84
85
func GenerateInstallationConfigMap(ctx *RenderContext, objects []RuntimeObject) ([]RuntimeObject, error) {
86
cfgMapData := make([]string, 0)
87
component := "gitpod-app"
88
89
// Convert to a simplified object that allows us to access the objects
90
for _, c := range objects {
91
// Jobs are excluded as they break uninstallation if they've been cleaned up
92
if c.Kind != "" && !(c.APIVersion == TypeMetaBatchJob.APIVersion && c.Kind == TypeMetaBatchJob.Kind) {
93
marshal, err := yaml.Marshal(c)
94
if err != nil {
95
return nil, err
96
}
97
98
cfgMapData = append(cfgMapData, string(marshal))
99
}
100
}
101
102
cfgMap := corev1.ConfigMap{
103
TypeMeta: TypeMetaConfigmap,
104
ObjectMeta: metav1.ObjectMeta{
105
Name: component,
106
Namespace: ctx.Namespace,
107
Labels: CustomizeLabel(ctx, component, TypeMetaConfigmap),
108
Annotations: CustomizeAnnotation(ctx, component, TypeMetaConfigmap),
109
},
110
}
111
112
// generate the config map data so it can be injected to the object
113
marshal, err := yaml.Marshal(cfgMap)
114
if err != nil {
115
return nil, err
116
}
117
118
cfgMapData = append(cfgMapData, string(marshal))
119
120
// Generate the data, including this config map
121
cfgMap.Data = map[string]string{
122
"app.yaml": strings.Join(cfgMapData, "---\n"),
123
}
124
125
// regenerate the config map so it can be injected into the charts with this config map in
126
marshal, err = yaml.Marshal(cfgMap)
127
if err != nil {
128
return nil, err
129
}
130
131
// Add in the ConfigMap
132
objects = append(objects, RuntimeObject{
133
TypeMeta: cfgMap.TypeMeta,
134
Metadata: cfgMap.ObjectMeta,
135
Content: string(marshal),
136
})
137
138
return objects, nil
139
}
140
141
func YamlToRuntimeObject(objects []string) ([]RuntimeObject, error) {
142
sortedObjects := make([]RuntimeObject, 0, len(objects))
143
for _, o := range objects {
144
// Assume multi-document YAML
145
re := regexp.MustCompile("(^|\n)---")
146
items := re.Split(o, -1)
147
148
for _, p := range items {
149
var v RuntimeObject
150
err := yaml.Unmarshal([]byte(p), &v)
151
if err != nil {
152
return nil, err
153
}
154
155
// remove any empty charts
156
ctnt := strings.Trim(p, "\n")
157
if len(strings.TrimSpace(ctnt)) == 0 {
158
continue
159
}
160
161
v.Content = ctnt
162
sortedObjects = append(sortedObjects, v)
163
}
164
}
165
166
return sortedObjects, nil
167
}
168
169