-
Notifications
You must be signed in to change notification settings - Fork 90
/
app.go
110 lines (94 loc) · 3.49 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package redact
import (
"bytes"
"context"
"fmt"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/render/helper"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
serializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
)
func GetAppRedactSpecConfigMapName(appSlug string) string {
return fmt.Sprintf("kotsadm-%s-redact-spec", appSlug)
}
func GetAppRedactSpecURI(appSlug string) string {
return fmt.Sprintf("configmap/%s/%s/%s", util.PodNamespace, GetAppRedactSpecConfigMapName(appSlug), redactSpecDataKey)
}
// CreateRenderedAppRedactSpec creates a configmap that contains the redaction yaml spec included in the application release
func CreateRenderedAppRedactSpec(appID string, sequence int64, kotsKinds *kotsutil.KotsKinds) error {
builtRedactor := kotsKinds.Redactor.DeepCopy()
if builtRedactor == nil {
builtRedactor = &troubleshootv1beta2.Redactor{
TypeMeta: metav1.TypeMeta{
Kind: "Redactor",
APIVersion: "troubleshoot.sh/v1beta2",
},
ObjectMeta: metav1.ObjectMeta{
Name: "default-redactor",
},
}
}
app, err := store.GetStore().GetApp(appID)
if err != nil {
return errors.Wrap(err, "failed to get app")
}
s := serializer.NewYAMLSerializer(serializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(builtRedactor, &b); err != nil {
return errors.Wrap(err, "failed to encode redactor")
}
templatedSpec := b.Bytes()
rs, err := helper.RenderAppFile(app, &sequence, templatedSpec, kotsKinds, util.PodNamespace)
if err != nil {
return errors.Wrap(err, "failed render redactor spec")
}
renderedSpec := string(rs)
clientset, err := k8sutil.GetClientset()
if err != nil {
return errors.Wrap(err, "failed to create clientset")
}
configMapName := GetAppRedactSpecConfigMapName(app.Slug)
existingConfigMap, err := clientset.CoreV1().ConfigMaps(util.PodNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{})
if err != nil && !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to read redactor configmap")
} else if kuberneteserrors.IsNotFound(err) {
configMap := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: util.PodNamespace,
Labels: kotsadmtypes.GetKotsadmLabels(),
},
Data: map[string]string{
redactSpecDataKey: renderedSpec,
},
}
_, err = clientset.CoreV1().ConfigMaps(util.PodNamespace).Create(context.TODO(), configMap, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create redactor configmap")
}
return nil
}
if existingConfigMap.Data == nil {
existingConfigMap.Data = map[string]string{}
}
existingConfigMap.Data[redactSpecDataKey] = renderedSpec
existingConfigMap.ObjectMeta.Labels = kotsadmtypes.GetKotsadmLabels()
_, err = clientset.CoreV1().ConfigMaps(util.PodNamespace).Update(context.TODO(), existingConfigMap, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update redactor configmap")
}
return nil
}