|
| 1 | +// Copyright 2022 Clastix Labs |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package handlers |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "gomodules.xyz/jsonpatch/v2" |
| 10 | + "k8s.io/apimachinery/pkg/runtime" |
| 11 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 12 | + "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 14 | + |
| 15 | + kamajiv1alpha1 "github.com/clastix/kamaji/api/v1alpha1" |
| 16 | + "github.com/clastix/kamaji/internal/webhook/utils" |
| 17 | +) |
| 18 | + |
| 19 | +type TenantControlPlaneCertSANs struct{} |
| 20 | + |
| 21 | +func (t TenantControlPlaneCertSANs) ValidateCertSANs(tcp *kamajiv1alpha1.TenantControlPlane) error { |
| 22 | + if len(tcp.Spec.NetworkProfile.CertSANs) == 0 { |
| 23 | + return nil |
| 24 | + } |
| 25 | + |
| 26 | + if err := validation.ValidateCertSANs(tcp.Spec.NetworkProfile.CertSANs, field.NewPath("spec.networkProfile.certSANs")); err != nil { |
| 27 | + return err.ToAggregate() |
| 28 | + } |
| 29 | + |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +func (t TenantControlPlaneCertSANs) OnCreate(obj runtime.Object) AdmissionResponse { |
| 34 | + return func(context.Context, admission.Request) ([]jsonpatch.JsonPatchOperation, error) { |
| 35 | + tcp := obj.(*kamajiv1alpha1.TenantControlPlane) //nolint:forcetypeassert |
| 36 | + |
| 37 | + return nil, t.ValidateCertSANs(tcp) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (t TenantControlPlaneCertSANs) OnDelete(runtime.Object) AdmissionResponse { |
| 42 | + return utils.NilOp() |
| 43 | +} |
| 44 | + |
| 45 | +func (t TenantControlPlaneCertSANs) OnUpdate(newObject runtime.Object, prevObject runtime.Object) AdmissionResponse { |
| 46 | + return func(context.Context, admission.Request) ([]jsonpatch.JsonPatchOperation, error) { |
| 47 | + tcp := newObject.(*kamajiv1alpha1.TenantControlPlane) //nolint:forcetypeassert |
| 48 | + |
| 49 | + return nil, t.ValidateCertSANs(tcp) |
| 50 | + } |
| 51 | +} |
0 commit comments