-
Notifications
You must be signed in to change notification settings - Fork 117
/
wrappers.go
63 lines (54 loc) · 1.99 KB
/
wrappers.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
package webhookca
import (
"context"
"fmt"
"github.com/pkg/errors"
v1 "k8s.io/api/admissionregistration/v1"
apixv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/maistra/istio-operator/pkg/controller/common"
)
type webhookGetter interface {
Get(ctx context.Context, cl client.Client, name types.NamespacedName) (webhookWrapper, error)
}
type webhookWrapper interface {
MetaObject() metav1.Object
Object() runtime.Object
ClientConfigs() []*v1.WebhookClientConfig
Copy() webhookWrapper
NamespacedName() types.NamespacedName
UpdateCABundle(ctx context.Context, cl client.Client, caBundle []byte) error
}
func toWebhookWrapper(obj runtime.Object) (webhookWrapper, error) {
switch wh := obj.(type) {
case *v1.ValidatingWebhookConfiguration:
return &validatingWebhookWrapper{ValidatingWebhookConfiguration: wh}, nil
case *v1.MutatingWebhookConfiguration:
return &mutatingWebhookWrapper{MutatingWebhookConfiguration: wh}, nil
case *apixv1.CustomResourceDefinition:
return &conversionWebhookWrapper{CustomResourceDefinition: wh}, nil
}
return nil, fmt.Errorf("object is not a [MutatingWebhookConfiguration, ValidatingWebhookConfiguration, CustomResourceDefinition]: %T", obj)
}
func updateAdmissionWebHookCABundles(ctx context.Context, cl client.Client, currentConfig webhookWrapper, caBundle []byte) error {
logger := common.LogFromContext(ctx)
updated := false
newConfig := currentConfig.Copy()
for _, clientConfig := range newConfig.ClientConfigs() {
updated = common.InjectCABundle(clientConfig, caBundle) || updated
}
if updated {
logger.Info("Updating CABundle")
err := cl.Update(ctx, newConfig.Object())
if err != nil {
return errors.Wrap(err, "failed to update CABundle")
}
logger.Info("CABundle updated")
return nil
}
logger.Info("Correct CABundle already present. Ignoring")
return nil
}