Skip to content

Commit

Permalink
added webhook for nutanixmachinetemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakm-ntnx committed Dec 1, 2023
1 parent 8260d8f commit f046f86
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 16 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ test-kubectl-workload: ## Run kubectl queries to get all capx workload related o

.PHONY: test-clusterclass-create
test-clusterclass-create: cluster-templates
clusterctl generate cluster ccls-test1 --from ./templates/cluster-template-clusterclass.yaml -n workloads > ccls-test1.yaml
clusterctl generate cluster ccls-test1 --from ./templates/cluster-template-clusterclass.yaml -n $(TEST_NAMESPACE) > ccls-test1.yaml
kubectl create ns $(TEST_NAMESPACE) || true
kubectl apply -f ./ccls-test1.yaml

Expand All @@ -407,14 +407,18 @@ test-clusterclass-delete:
kubectl -n $(TEST_NAMESPACE) delete KubeadmConfigTemplate my-test-cluster-template-md-kcfgt || true
kubectl -n $(TEST_NAMESPACE) delete kubeadmcontrolplanetemplate my-test-cluster-template-kcpt || true
kubectl -n $(TEST_NAMESPACE) delete NutanixClustertemplate my-test-cluster-template-nct || true
## kubectl -n $(TEST_NAMESPACE) delete secret ccls-test1 || true
kubectl -n $(TEST_NAMESPACE) delete secret ccls-test1
kubectl -n $(TEST_NAMESPACE) delete cm user-ca-bundle
rm ccls-test1.yaml || true


.PHONY: test-kubectl-clusterclass
test-kubectl-clusterclass:
kubectl get cluster,NutanixCluster,Machine,NutanixMachine,MachineDeployment -n $(TEST_NAMESPACE)
kubectl get NutanixClusterTemplate,clusterclass,KubeadmConfigTemplate,KubeadmControlPlaneTemplate,NutanixMachineTemplate,secret,configmap -n $(TEST_NAMESPACE)
kubectl -n capx-system get endpoints
kubectl -n $(TEST_NAMESPACE) get cluster,machine,MachineDeployment
kubectl -n $(TEST_NAMESPACE) get NutanixCluster,NutanixMachine -n $(TEST_NAMESPACE)
kubectl -n $(TEST_NAMESPACE) get NutanixClusterTemplate,clusterclass,KubeadmConfigTemplate,KubeadmControlPlaneTemplate,NutanixMachineTemplate,secret,configmap -n $(TEST_NAMESPACE)
kubectl -n $(TEST_NAMESPACE) get ValidatingWebhookConfiguration,MutatingWebhookConfiguration

.PHONY: ginkgo-help
ginkgo-help:
Expand Down
4 changes: 4 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ resources:
kind: NutanixMachineTemplate
path: github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/infrastructure/v1beta1
version: v1beta1
webhooks:
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand Down
77 changes: 77 additions & 0 deletions api/infrastructure/v1beta1/nutanixmachinetemplate_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2022 Nutanix
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
var nutanixmachinetemplatelog = logf.Log.WithName("nutanixmachinetemplate-resource")

// SetupWebhookWithManager will setup the manager to manage the webhooks
func (r *NutanixMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-nutanixmachinetemplate,mutating=true,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=nutanixmachinetemplates,verbs=create;update,versions=v1beta1,name=mnutanixmachinetemplate.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &NutanixMachineTemplate{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *NutanixMachineTemplate) Default() {
nutanixmachinetemplatelog.Info("default", "name", r.Name)

// TODO(user): fill in your defaulting logic.
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-nutanixmachinetemplate,mutating=false,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=nutanixmachinetemplates,verbs=create;update,versions=v1beta1,name=vnutanixmachinetemplate.kb.io,admissionReviewVersions=v1

var _ webhook.Validator = &NutanixMachineTemplate{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *NutanixMachineTemplate) ValidateCreate() (admission.Warnings, error) {
nutanixmachinetemplatelog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *NutanixMachineTemplate) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
nutanixmachinetemplatelog.Info("validate update", "name", r.Name)

// TODO(user): fill in your validation logic upon object update.
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *NutanixMachineTemplate) ValidateDelete() (admission.Warnings, error) {
nutanixmachinetemplatelog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil, nil
}
3 changes: 3 additions & 0 deletions api/infrastructure/v1beta1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ var _ = BeforeSuite(func() {
err = (&NutanixCluster{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

err = (&NutanixMachineTemplate{}).SetupWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:webhook

go func() {
Expand Down
16 changes: 11 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"

infrav1alpha4 "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/infrastructure/v1alpha4"
infrav1beta1 "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/infrastructure/v1beta1"
infrastructurev1beta1 "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/infrastructure/v1beta1"
infrastructurecontroller "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/internal/controller/infrastructure"
//+kubebuilder:scaffold:imports
)
Expand All @@ -66,9 +66,9 @@ func init() {
utilruntime.Must(bootstrapv1.AddToScheme(scheme))

utilruntime.Must(infrav1alpha4.AddToScheme(scheme))
utilruntime.Must(infrav1beta1.AddToScheme(scheme))
utilruntime.Must(infrastructurev1beta1.AddToScheme(scheme))

utilruntime.Must(infrav1beta1.AddToScheme(scheme))
utilruntime.Must(infrastructurev1beta1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

Expand Down Expand Up @@ -177,17 +177,23 @@ func main() {
os.Exit(1)
}
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&infrav1beta1.NutanixClusterTemplate{}).SetupWebhookWithManager(mgr); err != nil {
if err = (&infrastructurev1beta1.NutanixClusterTemplate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "NutanixClusterTemplate")
os.Exit(1)
}
}
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&infrav1beta1.NutanixCluster{}).SetupWebhookWithManager(mgr); err != nil {
if err = (&infrastructurev1beta1.NutanixCluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "NutanixCluster")
os.Exit(1)
}
}
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&infrastructurev1beta1.NutanixMachineTemplate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "NutanixMachineTemplate")
os.Exit(1)
}
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The following patch adds a directive for certmanager to inject CA into the CRD
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
cert-manager.io/inject-ca-from: CERTIFICATE_NAMESPACE/CERTIFICATE_NAME
name: nutanixmachinetemplates.infrastructure.cluster.x-k8s.io
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# The following patch enables a conversion webhook for the CRD
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: nutanixmachinetemplates.infrastructure.cluster.x-k8s.io
spec:
conversion:
strategy: Webhook
webhook:
clientConfig:
service:
namespace: system
name: webhook-service
path: /convert
conversionReviewVersions:
- v1
40 changes: 40 additions & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ webhooks:
resources:
- nutanixclustertemplates
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-infrastructure-cluster-x-k8s-io-v1beta1-nutanixmachinetemplate
failurePolicy: Fail
name: mnutanixmachinetemplate.kb.io
rules:
- apiGroups:
- infrastructure.cluster.x-k8s.io
apiVersions:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- nutanixmachinetemplates
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
Expand Down Expand Up @@ -90,3 +110,23 @@ webhooks:
resources:
- nutanixclustertemplates
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-infrastructure-cluster-x-k8s-io-v1beta1-nutanixmachinetemplate
failurePolicy: Fail
name: vnutanixmachinetemplate.kb.io
rules:
- apiGroups:
- infrastructure.cluster.x-k8s.io
apiVersions:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- nutanixmachinetemplates
sideEffects: None
2 changes: 0 additions & 2 deletions test/e2e/capx_regression.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ package e2e
import (
"context"

infrav1 "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/v1beta1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"strings"
"testing"

infrav1 "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/v1beta1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
storagev1 "k8s.io/api/storage/v1"
Expand Down
3 changes: 0 additions & 3 deletions test/e2e/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ import (
"strings"
"time"

infrav1 "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/v1beta1"
"github.com/nutanix-cloud-native/cluster-api-provider-nutanix/controllers"

credentialTypes "github.com/nutanix-cloud-native/prism-go-client/environment/credentials"
prismGoClientV3 "github.com/nutanix-cloud-native/prism-go-client/v3"
. "github.com/onsi/gomega"
Expand Down

0 comments on commit f046f86

Please sign in to comment.