-
Notifications
You must be signed in to change notification settings - Fork 444
/
validating_webhook_configuration.go
56 lines (41 loc) · 1.79 KB
/
validating_webhook_configuration.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
package kube
import (
"context"
errors "github.com/rotisserie/eris"
"github.com/solo-io/go-utils/contextutils"
"go.uber.org/zap"
"k8s.io/api/admissionregistration/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type WebhookTlsConfig struct {
ServiceName, ServiceNamespace string
CaBundle []byte
}
func UpdateValidatingWebhookConfigurationCaBundle(ctx context.Context, kube kubernetes.Interface, vwcName string, cfg WebhookTlsConfig) error {
contextutils.LoggerFrom(ctx).Infow("attempting to patch caBundle for ValidatingWebhookConfiguration", zap.String("svc", cfg.ServiceName), zap.String("vwc", vwcName))
vwc, err := kube.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations().Get(ctx, vwcName, metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "failed to retrieve vwc")
}
setCaBundle(ctx, vwc, cfg)
if _, err := kube.AdmissionregistrationV1beta1().ValidatingWebhookConfigurations().Update(ctx, vwc, metav1.UpdateOptions{}); err != nil {
return errors.Wrapf(err, "failed to update vwc")
}
return nil
}
func setCaBundle(ctx context.Context, vwc *v1beta1.ValidatingWebhookConfiguration, cfg WebhookTlsConfig) {
encodedCaBundle := cfg.CaBundle
for i, wh := range vwc.Webhooks {
if wh.ClientConfig.Service == nil {
continue
}
svcName, svcNamespace := wh.ClientConfig.Service.Name, wh.ClientConfig.Service.Namespace
// if we find a webhook cfg that targets our service, update it
if svcName == cfg.ServiceName && svcNamespace == cfg.ServiceNamespace {
wh.ClientConfig.CABundle = encodedCaBundle
vwc.Webhooks[i] = wh
contextutils.LoggerFrom(ctx).Infow("set CA bundle on ValidatingWebhookConfiguration", zap.String("svc", svcName), zap.String("vwc", vwc.Name), zap.Int("webhook", i))
}
}
}