diff --git a/Makefile b/Makefile index 91a52c5b06..dd10b28e7e 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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: diff --git a/PROJECT b/PROJECT index 3b76fd6ad6..e245fd853a 100644 --- a/PROJECT +++ b/PROJECT @@ -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 diff --git a/api/infrastructure/v1beta1/nutanixmachinetemplate_webhook.go b/api/infrastructure/v1beta1/nutanixmachinetemplate_webhook.go new file mode 100644 index 0000000000..581032a87f --- /dev/null +++ b/api/infrastructure/v1beta1/nutanixmachinetemplate_webhook.go @@ -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 +} diff --git a/api/infrastructure/v1beta1/webhook_suite_test.go b/api/infrastructure/v1beta1/webhook_suite_test.go index ecde7c9c42..de517ea2f8 100644 --- a/api/infrastructure/v1beta1/webhook_suite_test.go +++ b/api/infrastructure/v1beta1/webhook_suite_test.go @@ -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() { diff --git a/cmd/main.go b/cmd/main.go index c43ee42f46..1fdfa1207c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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 ) @@ -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 } @@ -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 { diff --git a/config/crd/patches/cainjection_in_infrastructure_nutanixmachinetemplates.yaml b/config/crd/patches/cainjection_in_infrastructure_nutanixmachinetemplates.yaml new file mode 100644 index 0000000000..7c905f90bd --- /dev/null +++ b/config/crd/patches/cainjection_in_infrastructure_nutanixmachinetemplates.yaml @@ -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 diff --git a/config/crd/patches/webhook_in_infrastructure_nutanixmachinetemplates.yaml b/config/crd/patches/webhook_in_infrastructure_nutanixmachinetemplates.yaml new file mode 100644 index 0000000000..d6074457df --- /dev/null +++ b/config/crd/patches/webhook_in_infrastructure_nutanixmachinetemplates.yaml @@ -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 diff --git a/config/webhook/manifests.yaml b/config/webhook/manifests.yaml index 6d9ca50dda..099a18f7ef 100644 --- a/config/webhook/manifests.yaml +++ b/config/webhook/manifests.yaml @@ -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 @@ -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 diff --git a/test/e2e/capx_regression.go b/test/e2e/capx_regression.go index a184c19a82..7b57540ba0 100644 --- a/test/e2e/capx_regression.go +++ b/test/e2e/capx_regression.go @@ -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" diff --git a/test/e2e/e2e_suite_test.go b/test/e2e/e2e_suite_test.go index c59e9869c0..0d201d3107 100644 --- a/test/e2e/e2e_suite_test.go +++ b/test/e2e/e2e_suite_test.go @@ -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" diff --git a/test/e2e/test_helpers.go b/test/e2e/test_helpers.go index 0a3bdcff52..d91eb74fa8 100644 --- a/test/e2e/test_helpers.go +++ b/test/e2e/test_helpers.go @@ -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"