Skip to content

Commit b5350e0

Browse files
committed
added webhooks to make clusterclass work
1 parent db5dbac commit b5350e0

20 files changed

+400
-256
lines changed

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,25 @@ test-kubectl-workload: ## Run kubectl queries to get all capx workload related o
389389
kubectl -n ${TEST_NAMESPACE} get secret ${TEST_CLUSTER_NAME}-kubeconfig -o json | jq -r .data.value | base64 --decode > ${TEST_CLUSTER_NAME}.workload.kubeconfig
390390
kubectl --kubeconfig ./${TEST_CLUSTER_NAME}.workload.kubeconfig get nodes,ns
391391

392+
.PHONY: test-clusterclass-create
393+
test-clusterclass-create: cluster-templates
394+
clusterctl generate cluster ccls-test1 --from ./templates/cluster-template-clusterclass.yaml -n workloads > ccls-test1.yaml
395+
kubectl apply -f ./ccls-test1.yaml
396+
397+
.PHONY: test-clusterclass-delete
398+
test-clusterclass-delete:
399+
kubectl -n workloads delete cluster ccls-test1 || true
400+
kubectl -n workloads delete nutanixcluster ccls-test1 || true
401+
kubectl -n workloads delete clusterclass my-test-cluster-template || true
402+
kubectl -n workloads delete KubeadmConfigTemplate my-test-cluster-template-kcfgt || true
403+
rm ccls-test1.yaml || true
404+
405+
406+
.PHONY: test-kubectl-clusterclass
407+
test-kubectl-clusterclass:
408+
kubectl get cluster,NutanixCluster,Machine,NutanixMachine,MachineDeployment -A
409+
kubectl get NutanixClusterTemplate,clusterclass,KubeadmConfigTemplate,KubeadmControlPlaneTemplate,NutanixMachineTemplate -A
410+
392411
.PHONY: ginkgo-help
393412
ginkgo-help:
394413
$(GINKGO) help run

PROJECT

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ resources:
2727
kind: NutanixCluster
2828
path: github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/infrastructure/v1beta1
2929
version: v1beta1
30+
webhooks:
31+
defaulting: true
32+
validation: true
33+
webhookVersion: v1
3034
- api:
3135
crdVersion: v1
3236
namespaced: true
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright 2022 Nutanix
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/runtime"
21+
ctrl "sigs.k8s.io/controller-runtime"
22+
logf "sigs.k8s.io/controller-runtime/pkg/log"
23+
"sigs.k8s.io/controller-runtime/pkg/webhook"
24+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
25+
)
26+
27+
// log is for logging in this package.
28+
var nutanixclusterlog = logf.Log.WithName("nutanixcluster-resource")
29+
30+
// SetupWebhookWithManager will setup the manager to manage the webhooks
31+
func (r *NutanixCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
32+
return ctrl.NewWebhookManagedBy(mgr).
33+
For(r).
34+
Complete()
35+
}
36+
37+
// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
38+
39+
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-nutanixcluster,mutating=true,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=nutanixclusters,verbs=create;update,versions=v1beta1,name=mnutanixcluster.kb.io,admissionReviewVersions=v1
40+
41+
var _ webhook.Defaulter = &NutanixCluster{}
42+
43+
// Default implements webhook.Defaulter so a webhook will be registered for the type
44+
func (r *NutanixCluster) Default() {
45+
nutanixclusterlog.Info("default", "name", r.Name)
46+
47+
// TODO(user): fill in your defaulting logic.
48+
}
49+
50+
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
51+
//+kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-nutanixcluster,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=nutanixclusters,verbs=create;update,versions=v1beta1,name=vnutanixcluster.kb.io,admissionReviewVersions=v1
52+
53+
var _ webhook.Validator = &NutanixCluster{}
54+
55+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
56+
func (r *NutanixCluster) ValidateCreate() (admission.Warnings, error) {
57+
nutanixclusterlog.Info("validate create", "name", r.Name)
58+
59+
// TODO(user): fill in your validation logic upon object creation.
60+
return nil, nil
61+
}
62+
63+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
64+
func (r *NutanixCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
65+
nutanixclusterlog.Info("validate update", "name", r.Name)
66+
67+
// TODO(user): fill in your validation logic upon object update.
68+
return nil, nil
69+
}
70+
71+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
72+
func (r *NutanixCluster) ValidateDelete() (admission.Warnings, error) {
73+
nutanixclusterlog.Info("validate delete", "name", r.Name)
74+
75+
// TODO(user): fill in your validation logic upon object deletion.
76+
return nil, nil
77+
}

api/infrastructure/v1beta1/nutanixmachinetemplate_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type NutanixMachineTemplateSpec struct {
3030

3131
//+kubebuilder:object:root=true
3232
//+kubebuilder:resource:path=nutanixmachinetemplates,shortName=nmtmpl,scope=Namespaced,categories=cluster-api
33+
//+kubebuilder:subresource:status
3334
//+kubebuilder:storageversion
3435

3536
// NutanixMachineTemplate is the Schema for the nutanixmachinetemplates API

api/infrastructure/v1beta1/webhook_suite_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
"sigs.k8s.io/controller-runtime/pkg/envtest"
3939
logf "sigs.k8s.io/controller-runtime/pkg/log"
4040
"sigs.k8s.io/controller-runtime/pkg/log/zap"
41-
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
41+
"sigs.k8s.io/controller-runtime/pkg/webhook"
4242
)
4343

4444
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
@@ -107,14 +107,17 @@ var _ = BeforeSuite(func() {
107107
Port: webhookInstallOptions.LocalServingPort,
108108
CertDir: webhookInstallOptions.LocalServingCertDir,
109109
}),
110-
LeaderElection: false,
111-
Metrics: metricsserver.Options{BindAddress: "0"},
110+
LeaderElection: false,
111+
MetricsBindAddress: "0",
112112
})
113113
Expect(err).NotTo(HaveOccurred())
114114

115115
err = (&NutanixClusterTemplate{}).SetupWebhookWithManager(mgr)
116116
Expect(err).NotTo(HaveOccurred())
117117

118+
err = (&NutanixCluster{}).SetupWebhookWithManager(mgr)
119+
Expect(err).NotTo(HaveOccurred())
120+
118121
//+kubebuilder:scaffold:webhook
119122

120123
go func() {

cmd/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,17 @@ func main() {
171171
os.Exit(1)
172172
}
173173
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
174-
if err = (&infrastructurev1beta1.NutanixClusterTemplate{}).SetupWebhookWithManager(mgr); err != nil {
174+
if err = (&infrav1beta1.NutanixClusterTemplate{}).SetupWebhookWithManager(mgr); err != nil {
175175
setupLog.Error(err, "unable to create webhook", "webhook", "NutanixClusterTemplate")
176176
os.Exit(1)
177177
}
178178
}
179+
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
180+
if err = (&infrav1beta1.NutanixCluster{}).SetupWebhookWithManager(mgr); err != nil {
181+
setupLog.Error(err, "unable to create webhook", "webhook", "NutanixCluster")
182+
os.Exit(1)
183+
}
184+
}
179185
//+kubebuilder:scaffold:builder
180186

181187
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {

config/crd/bases/infrastructure.cluster.x-k8s.io_nutanixmachines.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,8 @@ spec:
615615
description: The machine address.
616616
type: string
617617
type:
618-
description: Machine address type, one of Hostname, ExternalIP
619-
or InternalIP.
618+
description: Machine address type, one of Hostname, ExternalIP,
619+
InternalIP, ExternalDNS or InternalDNS.
620620
type: string
621621
required:
622622
- address

config/crd/bases/infrastructure.cluster.x-k8s.io_nutanixmachinetemplates.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ spec:
546546
type: object
547547
served: true
548548
storage: true
549+
subresources:
550+
status: {}
549551
status:
550552
acceptedNames:
551553
kind: ""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The following patch adds a directive for certmanager to inject CA into the CRD
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
cert-manager.io/inject-ca-from: CERTIFICATE_NAMESPACE/CERTIFICATE_NAME
7+
name: nutanixclusters.infrastructure.cluster.x-k8s.io
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# The following patch enables a conversion webhook for the CRD
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
name: nutanixclusters.infrastructure.cluster.x-k8s.io
6+
spec:
7+
conversion:
8+
strategy: Webhook
9+
webhook:
10+
clientConfig:
11+
service:
12+
namespace: system
13+
name: webhook-service
14+
path: /convert
15+
conversionReviewVersions:
16+
- v1

0 commit comments

Comments
 (0)