Skip to content

Commit 3a6326f

Browse files
committed
feat(webhook): validating api server cert sans
Signed-off-by: Dario Tranchitella <[email protected]>
1 parent 2f5ba48 commit 3a6326f

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

cmd/manager/cmd.go

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ func NewCmd(scheme *runtime.Scheme) *cobra.Command {
219219
},
220220
},
221221
routes.TenantControlPlaneValidate{}: {
222+
handlers.TenantControlPlaneCertSANs{},
222223
handlers.TenantControlPlaneName{},
223224
handlers.TenantControlPlaneVersion{},
224225
handlers.TenantControlPlaneKubeletAddresses{},
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)