sidero/app/caps-controller-manager/api/v1alpha3/metalmachinetemplate_webhook.go
Andrey Smirnov c29d4645d9
feat: make MetalMachineTemplate immutable
This allows proper upgrades by replacing the `MetalMachineTemplate`.

If the `MetalMachineTemployment` is mutated, the changes are not rolled
out automatically.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
2022-04-18 22:31:22 +03:00

45 lines
1.6 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package v1alpha3
import (
"github.com/google/go-cmp/cmp"
apierrors "k8s.io/apimachinery/pkg/api/errors"
runtime "k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
func (r *MetalMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
//+kubebuilder:webhook:verbs=create;update;delete,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-metalmachinetemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=metalmachinetemplates,versions=v1alpha3,name=vmetalmachinetemplates.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1
var _ webhook.Validator = &MetalMachineTemplate{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *MetalMachineTemplate) ValidateCreate() error {
return nil
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *MetalMachineTemplate) ValidateUpdate(oldRaw runtime.Object) error {
old := oldRaw.(*MetalMachineTemplate)
if !cmp.Equal(r.Spec, old.Spec) {
return apierrors.NewBadRequest("MetalMachineTemplate.Spec is immutable")
}
return nil
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *MetalMachineTemplate) ValidateDelete() error {
return nil
}