-
Notifications
You must be signed in to change notification settings - Fork 88
/
config.go
95 lines (79 loc) · 2.63 KB
/
config.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
package ingress
import (
"context"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
"github.com/replicatedhq/kots/pkg/kotsutil"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
ConfigMapName = "kotsadm-ingress-config"
)
func GetConfig(ctx context.Context, namespace string) (*kotsv1beta1.IngressConfig, error) {
clientset, err := k8sutil.GetClientset()
if err != nil {
return nil, errors.Wrap(err, "failed to get k8s client set")
}
configMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(ctx, ConfigMapName, metav1.GetOptions{})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
return &kotsv1beta1.IngressConfig{}, nil
}
return nil, errors.Wrap(err, "failed to get config map")
}
ingressConfig, err := kotsutil.LoadIngressConfigFromContents([]byte(configMap.Data["ingress.yaml"]))
if err != nil {
return nil, errors.Wrap(err, "failed to decode ingress config")
}
return ingressConfig, err
}
func SetConfig(ctx context.Context, namespace string, ingressConfig kotsv1beta1.IngressConfig) error {
clientset, err := k8sutil.GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get k8s client set")
}
configMap, err := ingressConfigResource(ingressConfig)
if err != nil {
return err
}
existingConfigMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(ctx, ConfigMapName, metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get config map")
}
_, err = clientset.CoreV1().ConfigMaps(namespace).Create(ctx, configMap, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create config map")
}
return nil
}
existingConfigMap.Data = configMap.Data
_, err = clientset.CoreV1().ConfigMaps(namespace).Update(ctx, existingConfigMap, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update config map")
}
return nil
}
func ingressConfigResource(ingressConfig kotsv1beta1.IngressConfig) (*corev1.ConfigMap, error) {
data, err := kotsutil.EncodeIngressConfig(ingressConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to encode ingress config")
}
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: ConfigMapName,
Labels: kotsadmtypes.GetKotsadmLabels(),
},
Data: map[string]string{
"ingress.yaml": string(data),
},
}, nil
}