Path: blob/main/install/installer/pkg/common/display.go
2500 views
// Copyright (c) 2021 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 (7"regexp"8"sort"9"strings"1011corev1 "k8s.io/api/core/v1"1213metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"14"sigs.k8s.io/yaml"15)1617// Those occurring earlier in the list get installed before those occurring later in the list.18// Based on Helm's list, with our CRDs added in1920var sortOrder = []string{21"Namespace",22"NetworkPolicy",23"ResourceQuota",24"Issuer",25"Certificate",26"LimitRange",27"PodDisruptionBudget",28"ServiceAccount",29"Secret",30"SecretList",31"ConfigMap",32"StorageClass",33"PersistentVolume",34"PersistentVolumeClaim",35"CustomResourceDefinition",36"ClusterRole",37"ClusterRoleList",38"ClusterRoleBinding",39"ClusterRoleBindingList",40"Role",41"RoleList",42"RoleBinding",43"RoleBindingList",44"Service",45"DaemonSet",46"Pod",47"ReplicationController",48"ReplicaSet",49"StatefulSet",50"Deployment",51"HorizontalPodAutoscaler",52"Job",53"CronJob",54"Ingress",55"APIService",56}5758type RuntimeObject struct {59metav1.TypeMeta `json:",inline"`60Metadata metav1.ObjectMeta `json:"metadata"`61Content string `json:"-"`62}6364func DependencySortingRenderFunc(objects []RuntimeObject) ([]RuntimeObject, error) {65sortMap := map[string]int{}66for k, v := range sortOrder {67sortMap[v] = k68}6970sort.SliceStable(objects, func(i, j int) bool {71scoreI := sortMap[objects[i].Kind]72scoreJ := sortMap[objects[j].Kind]7374if scoreI == scoreJ {75return objects[i].Metadata.Name < objects[j].Metadata.Name76}7778return scoreI < scoreJ79})8081return objects, nil82}8384func GenerateInstallationConfigMap(ctx *RenderContext, objects []RuntimeObject) ([]RuntimeObject, error) {85cfgMapData := make([]string, 0)86component := "gitpod-app"8788// Convert to a simplified object that allows us to access the objects89for _, c := range objects {90// Jobs are excluded as they break uninstallation if they've been cleaned up91if c.Kind != "" && !(c.APIVersion == TypeMetaBatchJob.APIVersion && c.Kind == TypeMetaBatchJob.Kind) {92marshal, err := yaml.Marshal(c)93if err != nil {94return nil, err95}9697cfgMapData = append(cfgMapData, string(marshal))98}99}100101cfgMap := corev1.ConfigMap{102TypeMeta: TypeMetaConfigmap,103ObjectMeta: metav1.ObjectMeta{104Name: component,105Namespace: ctx.Namespace,106Labels: CustomizeLabel(ctx, component, TypeMetaConfigmap),107Annotations: CustomizeAnnotation(ctx, component, TypeMetaConfigmap),108},109}110111// generate the config map data so it can be injected to the object112marshal, err := yaml.Marshal(cfgMap)113if err != nil {114return nil, err115}116117cfgMapData = append(cfgMapData, string(marshal))118119// Generate the data, including this config map120cfgMap.Data = map[string]string{121"app.yaml": strings.Join(cfgMapData, "---\n"),122}123124// regenerate the config map so it can be injected into the charts with this config map in125marshal, err = yaml.Marshal(cfgMap)126if err != nil {127return nil, err128}129130// Add in the ConfigMap131objects = append(objects, RuntimeObject{132TypeMeta: cfgMap.TypeMeta,133Metadata: cfgMap.ObjectMeta,134Content: string(marshal),135})136137return objects, nil138}139140func YamlToRuntimeObject(objects []string) ([]RuntimeObject, error) {141sortedObjects := make([]RuntimeObject, 0, len(objects))142for _, o := range objects {143// Assume multi-document YAML144re := regexp.MustCompile("(^|\n)---")145items := re.Split(o, -1)146147for _, p := range items {148var v RuntimeObject149err := yaml.Unmarshal([]byte(p), &v)150if err != nil {151return nil, err152}153154// remove any empty charts155ctnt := strings.Trim(p, "\n")156if len(strings.TrimSpace(ctnt)) == 0 {157continue158}159160v.Content = ctnt161sortedObjects = append(sortedObjects, v)162}163}164165return sortedObjects, nil166}167168169