Skip to content

Commit

Permalink
Add webhooks for template types
Browse files Browse the repository at this point in the history
  • Loading branch information
willie-yao committed Oct 19, 2023
1 parent 78152f8 commit 0251af4
Show file tree
Hide file tree
Showing 12 changed files with 1,832 additions and 200 deletions.
84 changes: 84 additions & 0 deletions api/v1beta1/azuremanagedclustertemplate_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2023 The Kubernetes Authors.
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 (
"fmt"
"reflect"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/cluster-api-provider-azure/feature"
"sigs.k8s.io/cluster-api-provider-azure/util/maps"
capifeature "sigs.k8s.io/cluster-api/feature"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// SetupWebhookWithManager sets up and registers the webhook with the manager.
func (r *AzureManagedClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// +kubebuilder:webhook:verbs=update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedclustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedclustertemplates,versions=v1beta1,name=validation.azuremanagedclustertemplates.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.Validator = &AzureManagedClusterTemplate{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *AzureManagedClusterTemplate) ValidateCreate() (admission.Warnings, error) {
// NOTE: AzureManagedClusterTemplate relies upon MachinePools, which is behind a feature gate flag.
// The webhook must prevent creating new objects in case the feature flag is disabled.
if !feature.Gates.Enabled(capifeature.MachinePool) {
return nil, field.Forbidden(
field.NewPath("spec"),
"cannot be set if the Cluster API 'MachinePool' feature flag is not enabled",
)
}
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *AzureManagedClusterTemplate) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
old := oldRaw.(*AzureManagedClusterTemplate)
var allErrs field.ErrorList

// custom headers are immutable
oldCustomHeaders := maps.FilterByKeyPrefix(old.ObjectMeta.Annotations, CustomHeaderPrefix)
newCustomHeaders := maps.FilterByKeyPrefix(r.ObjectMeta.Annotations, CustomHeaderPrefix)
if !reflect.DeepEqual(oldCustomHeaders, newCustomHeaders) {
allErrs = append(allErrs,
field.Invalid(
field.NewPath("metadata", "annotations"),
r.ObjectMeta.Annotations,
fmt.Sprintf("annotations with '%s' prefix are immutable", CustomHeaderPrefix)))
}

if len(allErrs) != 0 {
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureManagedClusterTemplate").GroupKind(), r.Name, allErrs)
}

return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *AzureManagedClusterTemplate) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}
10 changes: 10 additions & 0 deletions api/v1beta1/azuremanagedcontrolplane_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"golang.org/x/crypto/ssh"
"k8s.io/utils/ptr"
utilSSH "sigs.k8s.io/cluster-api-provider-azure/util/ssh"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

const (
Expand Down Expand Up @@ -50,6 +51,15 @@ func (m *AzureManagedControlPlane) setDefaultSSHPublicKey() error {
return nil
}

// setDefaultResourceGroupName sets the default ResourceGroupName for an AzureManagedControlPlane.
func (m *AzureManagedControlPlane) setDefaultResourceGroupName() {
if m.Spec.ResourceGroupName == "" {
if clusterName, ok := m.Labels[clusterv1.ClusterNameLabel]; ok {
m.Spec.ResourceGroupName = clusterName
}
}
}

// setDefaultNodeResourceGroupName sets the default NodeResourceGroup for an AzureManagedControlPlane.
func (m *AzureManagedControlPlane) setDefaultNodeResourceGroupName() {
if m.Spec.NodeResourceGroupName == "" {
Expand Down
Loading

0 comments on commit 0251af4

Please sign in to comment.