-
Notifications
You must be signed in to change notification settings - Fork 216
/
mutatingwebhook.go
140 lines (125 loc) · 4.22 KB
/
mutatingwebhook.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package normalizers
import (
"github.com/sirupsen/logrus"
"github.com/rancher/wrangler/v2/pkg/objectset"
adregv1 "k8s.io/api/admissionregistration/v1"
adregv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
type MutatingWebhookNormalizer struct {
Live objectset.ObjectByGVK
}
func (m *MutatingWebhookNormalizer) Normalize(un *unstructured.Unstructured) error {
if un == nil {
return nil
}
gvk := un.GroupVersionKind()
if gvk.Group != adregv1.GroupName || gvk.Kind != "MutatingWebhookConfiguration" {
return nil
}
if gvk.Version == "v1beta1" {
return m.convertMutatingWebhookV1beta1(un)
}
return m.convertMutatingWebhookV1(un)
}
func (m *MutatingWebhookNormalizer) convertMutatingWebhookV1beta1(un *unstructured.Unstructured) error {
var webhook adregv1beta1.MutatingWebhookConfiguration
err := runtime.DefaultUnstructuredConverter.FromUnstructured(un.Object, &webhook)
if err != nil {
logrus.Error("Failed to convert unstructured to webhook")
return nil
}
for i, config := range webhook.Webhooks {
if webhook.UID == "" && string(config.ClientConfig.CABundle) == "\n" {
live := lookupLive(un.GroupVersionKind(), un.GetName(), un.GetNamespace(), m.Live)
liveWebhook, ok := live.(*unstructured.Unstructured)
if !ok {
continue
}
if err := setMutatingWebhookV1beta1CacertNil(liveWebhook, i); err != nil {
logrus.Errorf("Failed to normalize webhook cacert, err: %v", err)
return nil
}
}
}
return nil
}
func (m *MutatingWebhookNormalizer) convertMutatingWebhookV1(un *unstructured.Unstructured) error {
var webhook adregv1.MutatingWebhookConfiguration
err := runtime.DefaultUnstructuredConverter.FromUnstructured(un.Object, &webhook)
if err != nil {
logrus.Errorf("Failed to convert unstructured to webhook, err: %v", err)
return nil
}
for i, config := range webhook.Webhooks {
if webhook.UID == "" && string(config.ClientConfig.CABundle) == "\n" {
live := lookupLive(un.GroupVersionKind(), un.GetName(), un.GetNamespace(), m.Live)
liveWebhook, ok := live.(*unstructured.Unstructured)
if !ok {
continue
}
if err := setMutatingWebhookV1CacertNil(liveWebhook, i); err != nil {
logrus.Errorf("Failed to normalize webhook cacert, err: %v", err)
return nil
}
}
}
return nil
}
func setMutatingWebhookV1CacertNil(un *unstructured.Unstructured, index int) error {
var webhook adregv1.MutatingWebhookConfiguration
err := runtime.DefaultUnstructuredConverter.FromUnstructured(un.Object, &webhook)
if err != nil {
logrus.Errorf("Failed to convert unstructured to webhook, err: %v", err)
return err
}
if index >= len(webhook.Webhooks) {
return nil
}
webhook.Webhooks[index].ClientConfig.CABundle = nil
newObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&webhook)
if err != nil {
logrus.Errorf("Failed to convert unstructured to webhook, err: %v", err)
return err
}
if webhook.Webhooks != nil {
if err = unstructured.SetNestedField(un.Object, newObj["webhooks"], "webhooks"); err != nil {
logrus.Errorf("MutatingWebhook normalization error: %v", err)
return err
}
}
return nil
}
func setMutatingWebhookV1beta1CacertNil(un *unstructured.Unstructured, index int) error {
var webhook adregv1beta1.MutatingWebhookConfiguration
err := runtime.DefaultUnstructuredConverter.FromUnstructured(un.Object, &webhook)
if err != nil {
logrus.Error("Failed to convert unstructured to webhook")
return err
}
if index >= len(webhook.Webhooks) {
return nil
}
webhook.Webhooks[index].ClientConfig.CABundle = nil
newObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&webhook)
if err != nil {
logrus.Errorf("Failed to convert unstructured to webhook, err: %v", err)
return err
}
if webhook.Webhooks != nil {
if err = unstructured.SetNestedField(un.Object, newObj["webhooks"], "webhooks"); err != nil {
logrus.Errorf("MutatingWebhook normalization error: %v", err)
return err
}
}
return nil
}
func lookupLive(gvk schema.GroupVersionKind, name, namespace string, live objectset.ObjectByGVK) runtime.Object {
key := objectset.ObjectKey{
Namespace: namespace,
Name: name,
}
return live[gvk][key]
}