-
Notifications
You must be signed in to change notification settings - Fork 216
/
validatingwebhook.go
131 lines (117 loc) · 4 KB
/
validatingwebhook.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
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"
)
type ValidatingWebhookNormalizer struct {
Live objectset.ObjectByGVK
}
func (v *ValidatingWebhookNormalizer) Normalize(un *unstructured.Unstructured) error {
if un == nil {
return nil
}
gvk := un.GroupVersionKind()
if gvk.Group != adregv1.GroupName || gvk.Kind != "ValidatingWebhookConfiguration" {
return nil
}
if gvk.Version == "v1beta1" {
return v.convertValidatingWebhookV1beta1(un)
}
return v.convertValidatingWebhookV1(un)
}
func (v *ValidatingWebhookNormalizer) convertValidatingWebhookV1beta1(un *unstructured.Unstructured) error {
var webhook adregv1beta1.ValidatingWebhookConfiguration
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(), v.Live)
liveWebhook, ok := live.(*unstructured.Unstructured)
if !ok {
continue
}
if err := setValidatingWebhookV1beta1CacertNil(liveWebhook, i); err != nil {
logrus.Errorf("Failed to normalize webhook cacert, err: %v", err)
return nil
}
}
}
return nil
}
func (v *ValidatingWebhookNormalizer) convertValidatingWebhookV1(un *unstructured.Unstructured) error {
var webhook adregv1.ValidatingWebhookConfiguration
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(), v.Live)
liveWebhook, ok := live.(*unstructured.Unstructured)
if !ok {
continue
}
if err := setValidatingWebhookV1CacertNil(liveWebhook, i); err != nil {
logrus.Errorf("Failed to normalize webhook cacert, err: %v", err)
return nil
}
}
}
return nil
}
func setValidatingWebhookV1CacertNil(un *unstructured.Unstructured, index int) error {
var webhook adregv1.ValidatingWebhookConfiguration
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("ValidatingWebhook normalization error: %v", err)
return err
}
}
return nil
}
func setValidatingWebhookV1beta1CacertNil(un *unstructured.Unstructured, index int) error {
var webhook adregv1beta1.ValidatingWebhookConfiguration
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("ValidatingWebhook normalization error: %v", err)
return err
}
}
return nil
}