-
Notifications
You must be signed in to change notification settings - Fork 88
/
theme.go
110 lines (92 loc) · 3.33 KB
/
theme.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 deploy
import (
"context"
"encoding/base64"
"github.com/pkg/errors"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func ensureDexThemeConfigMap(ctx context.Context, clientset kubernetes.Interface, options Options) error {
configMap, err := dexThemeConfigMapResource(options)
if err != nil {
return err
}
existingConfigMap, err := clientset.CoreV1().ConfigMaps(options.Namespace).Get(ctx, configMap.Name, metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing config map")
}
_, err = clientset.CoreV1().ConfigMaps(options.Namespace).Create(ctx, configMap, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create config map")
}
return nil
}
existingConfigMap = updateDexThemeConfigMap(existingConfigMap, configMap)
_, err = clientset.CoreV1().ConfigMaps(options.Namespace).Update(ctx, existingConfigMap, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update config map")
}
return nil
}
func dexThemeConfigMapResource(options Options) (*corev1.ConfigMap, error) {
var styleCSS, logo, favicon []byte
if options.IdentitySpec.WebConfig != nil && options.IdentitySpec.WebConfig.Theme != nil {
theme := options.IdentitySpec.WebConfig.Theme
var err error
styleCSS, err = base64.StdEncoding.DecodeString(theme.StyleCSSBase64)
if err != nil {
return nil, errors.Wrap(err, "failed to decode style.css")
}
logo, err = base64.StdEncoding.DecodeString(theme.LogoBase64)
if err != nil {
return nil, errors.Wrap(err, "failed to decode style.css")
}
favicon, err = base64.StdEncoding.DecodeString(theme.FaviconBase64)
if err != nil {
return nil, errors.Wrap(err, "failed to decode style.css")
}
}
data := map[string]string{
"styles.css": string(styleCSS),
}
binaryData := map[string][]byte{
"logo.png": logo,
"favicon.png": favicon,
}
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: prefixName(options.NamePrefix, "dex-theme"),
Namespace: options.Namespace,
Labels: kotsadmtypes.GetKotsadmLabels(AdditionalLabels(options.NamePrefix, options.AdditionalLabels)),
},
Data: data,
BinaryData: binaryData,
}, nil
}
func updateDexThemeConfigMap(existingConfigMap, desiredConfigMap *corev1.ConfigMap) *corev1.ConfigMap {
if existingConfigMap.Data == nil {
existingConfigMap.Data = map[string]string{}
}
existingConfigMap.Data["styles.css"] = desiredConfigMap.Data["styles.css"]
if existingConfigMap.BinaryData == nil {
existingConfigMap.BinaryData = map[string][]byte{}
}
existingConfigMap.BinaryData["logo.png"] = desiredConfigMap.BinaryData["logo.png"]
existingConfigMap.BinaryData["favicon.png"] = desiredConfigMap.BinaryData["favicon.png"]
return existingConfigMap
}
func deleteDexThemeConfigMap(ctx context.Context, clientset kubernetes.Interface, namespace, namePrefix string) error {
err := clientset.CoreV1().ConfigMaps(namespace).Delete(ctx, prefixName(namePrefix, "dex-theme"), metav1.DeleteOptions{})
if kuberneteserrors.IsNotFound(err) {
return nil
}
return err
}