This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
adapt.go
83 lines (75 loc) · 2.38 KB
/
adapt.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
package host
import (
"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/kubernetes"
"github.com/caos/orbos/internal/operator/orbiter/kinds/clusters/kubernetes/resources"
macherrs "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
const (
group = "getambassador.io"
version = "v2"
kind = "Host"
)
func AdaptFuncToEnsure(namespace, name string, labels map[string]string, hostname string, authority string, privateKeySecret string, selector map[string]string, tlsSecret string) (resources.QueryFunc, error) {
acme := map[string]interface{}{
"authority": authority,
}
if privateKeySecret != "" {
acme["privateKeySecret"] = map[string]interface{}{
"name": privateKeySecret,
}
}
selectorInternal := make(map[string]interface{}, 0)
for k, v := range selector {
selectorInternal[k] = v
}
crd := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": kind,
"apiVersion": group + "/" + version,
"metadata": map[string]interface{}{
"name": name,
"namespace": namespace,
"labels": labels,
"annotations": map[string]interface{}{
"aes_res_changed": "true",
},
},
"spec": map[string]interface{}{
"hostname": hostname,
"acmeProvider": acme,
"ambassadorId": []string{
"default",
},
"selector": map[string]interface{}{
"matchLabels": selectorInternal,
},
"tlsSecret": map[string]interface{}{
"name": tlsSecret,
},
},
}}
return func(k8sClient *kubernetes.Client) (resources.EnsureFunc, error) {
res, err := k8sClient.GetNamespacedCRDResource(group, version, kind, namespace, name)
if err != nil && !macherrs.IsNotFound(err) {
return nil, err
}
resourceVersion := ""
if res != nil {
meta := res.Object["metadata"].(map[string]interface{})
resourceVersion = meta["resourceVersion"].(string)
}
if resourceVersion != "" {
meta := crd.Object["metadata"].(map[string]interface{})
meta["resourceVersion"] = resourceVersion
}
return func(k8sClient *kubernetes.Client) error {
return k8sClient.ApplyNamespacedCRDResource(group, version, kind, namespace, name, crd)
}, nil
}, nil
}
func AdaptFuncToDestroy(namespace, name string) (resources.DestroyFunc, error) {
return func(client *kubernetes.Client) error {
return client.DeleteNamespacedCRDResource(group, version, kind, namespace, name)
}, nil
}