/
patch.go
81 lines (71 loc) · 2.69 KB
/
patch.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
package certgenerator
import (
"fmt"
"github.com/joelspeed/webhook-certificate-generator/pkg/utils"
arv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"k8s.io/client-go/kubernetes"
)
func patchMutating(client *kubernetes.Clientset, name string, namespace string, service string) error {
caBundle, err := fetchCABundle(client)
if err != nil {
return fmt.Errorf("error retrieving ca bundle: %v", err)
}
mwc, err := utils.GetMutatingWebhookConfiguration(client, name)
if err != nil {
return fmt.Errorf("failed to fetch mutating webhook configuration: %v", err)
}
mwc.Webhooks = patchMutatingWebhooks(mwc.Webhooks, caBundle, namespace, service)
_, err = utils.UpdateMutatingWebhookConfiguration(client, mwc)
if err != nil {
return fmt.Errorf("failed updating mutating webhook configuration: %v", err)
}
return nil
}
func patchValidating(client *kubernetes.Clientset, name string, namespace string, service string) error {
caBundle, err := fetchCABundle(client)
if err != nil {
return fmt.Errorf("error retrieving ca bundle: %v", err)
}
vwc, err := utils.GetValidatingWebhookConfiguration(client, name)
if err != nil {
return fmt.Errorf("failed to fetch validating webhook configuration: %v", err)
}
vwc.Webhooks = patchValidatingWebhooks(vwc.Webhooks, caBundle, namespace, service)
_, err = utils.UpdateValidatingWebhookConfiguration(client, vwc)
if err != nil {
return fmt.Errorf("failed updating validating webhook configuration: %v", err)
}
return nil
}
func fetchCABundle(client *kubernetes.Clientset) ([]byte, error) {
cm, err := utils.GetConfigMap(client, "kube-system", "extension-apiserver-authentication")
if err != nil {
return nil, fmt.Errorf("couldn't retrieve auth configmap: %v", err)
}
if bundle, ok := cm.Data["client-ca-file"]; ok {
return []byte(bundle), nil
}
return nil, fmt.Errorf("no client-ca-file in configmap")
}
func patchMutatingWebhooks(webhooks []arv1beta1.MutatingWebhook, caBundle []byte, namespace string, name string) []arv1beta1.MutatingWebhook {
outWebhooks := []arv1beta1.MutatingWebhook{}
for _, wh := range webhooks {
if wh.ClientConfig.Service.Namespace == namespace &&
wh.ClientConfig.Service.Name == name {
wh.ClientConfig.CABundle = caBundle
}
outWebhooks = append(outWebhooks, wh)
}
return outWebhooks
}
func patchValidatingWebhooks(webhooks []arv1beta1.ValidatingWebhook, caBundle []byte, namespace string, name string) []arv1beta1.ValidatingWebhook {
outWebhooks := []arv1beta1.ValidatingWebhook{}
for _, wh := range webhooks {
if wh.ClientConfig.Service.Namespace == namespace &&
wh.ClientConfig.Service.Name == name {
wh.ClientConfig.CABundle = caBundle
}
outWebhooks = append(outWebhooks, wh)
}
return outWebhooks
}