github.com/metaprov/modela-operator@v0.0.0-20240118193048-f378be8b74d2/api/v1alpha1/modela_webhook.go (about)

     1  /*
     2  Copyright 2022.
     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 v1alpha1
    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  )
    25  
    26  // log is for logging in this package.
    27  var modelalog = logf.Log.WithName("modela-resource")
    28  
    29  func (r *Modela) SetupWebhookWithManager(mgr ctrl.Manager) error {
    30  	return ctrl.NewWebhookManagedBy(mgr).
    31  		For(r).
    32  		Complete()
    33  }
    34  
    35  //+kubebuilder:webhook:path=/mutate-management-modela-ai-v1alpha1-modela,mutating=true,failurePolicy=fail,sideEffects=None,groups=management.modela.ai,resources=modelas,verbs=create;update,versions=v1alpha1,name=mmodela.kb.io,admissionReviewVersions=v1
    36  
    37  var _ webhook.Defaulter = &Modela{}
    38  
    39  // Default implements webhook.Defaulter so a webhook will be registered for the type
    40  func (r *Modela) Default() {
    41  	modelalog.Info("default", "name", r.Name)
    42  }
    43  
    44  // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
    45  //+kubebuilder:webhook:path=/validate-management-modela-ai-v1alpha1-modela,mutating=false,failurePolicy=fail,sideEffects=None,groups=management.modela.ai,resources=modelas,verbs=create;update,versions=v1alpha1,name=vmodela.kb.io,admissionReviewVersions=v1
    46  
    47  var _ webhook.Validator = &Modela{}
    48  
    49  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    50  func (r *Modela) ValidateCreate() error {
    51  	modelalog.Info("validate create", "name", r.Name)
    52  
    53  	return nil
    54  }
    55  
    56  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    57  func (r *Modela) ValidateUpdate(_ runtime.Object) error {
    58  	modelalog.Info("validate update", "name", r.Name)
    59  	return nil
    60  }
    61  
    62  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    63  func (r *Modela) ValidateDelete() error {
    64  	modelalog.Info("validate delete", "name", r.Name)
    65  	return nil
    66  }