github.com/kotalco/kotal@v0.3.0/apis/chainlink/v1alpha1/node_validation_webhook.go (about)

     1  package v1alpha1
     2  
     3  import (
     4  	"fmt"
     5  
     6  	apierrors "k8s.io/apimachinery/pkg/api/errors"
     7  	"k8s.io/apimachinery/pkg/runtime"
     8  	"k8s.io/apimachinery/pkg/runtime/schema"
     9  	"k8s.io/apimachinery/pkg/util/validation/field"
    10  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    11  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    12  )
    13  
    14  // +kubebuilder:webhook:verbs=create;update,path=/validate-chainlink-kotal-io-v1alpha1-node,mutating=false,failurePolicy=fail,groups=chainlink.kotal.io,resources=nodes,versions=v1alpha1,name=validate-chainlink-v1alpha1-node.kb.io,sideEffects=None,admissionReviewVersions=v1
    15  
    16  var _ webhook.Validator = &Node{}
    17  
    18  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    19  func (r *Node) ValidateCreate() (admission.Warnings, error) {
    20  	var allErrors field.ErrorList
    21  
    22  	nodelog.Info("validate create", "name", r.Name)
    23  
    24  	allErrors = append(allErrors, r.Spec.Resources.ValidateCreate()...)
    25  
    26  	if len(allErrors) == 0 {
    27  		return nil, nil
    28  	}
    29  
    30  	return nil, apierrors.NewInvalid(schema.GroupKind{}, r.Name, allErrors)
    31  
    32  }
    33  
    34  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    35  func (r *Node) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
    36  	var allErrors field.ErrorList
    37  	oldNode := old.(*Node)
    38  
    39  	nodelog.Info("validate update", "name", r.Name)
    40  
    41  	allErrors = append(allErrors, r.Spec.Resources.ValidateUpdate(&oldNode.Spec.Resources)...)
    42  
    43  	if oldNode.Spec.EthereumChainId != r.Spec.EthereumChainId {
    44  		err := field.Invalid(field.NewPath("spec").Child("ethereumChainId"), fmt.Sprintf("%d", r.Spec.EthereumChainId), "field is immutable")
    45  		allErrors = append(allErrors, err)
    46  	}
    47  
    48  	if oldNode.Spec.LinkContractAddress != r.Spec.LinkContractAddress {
    49  		err := field.Invalid(field.NewPath("spec").Child("linkContractAddress"), r.Spec.LinkContractAddress, "field is immutable")
    50  		allErrors = append(allErrors, err)
    51  	}
    52  
    53  	if len(allErrors) == 0 {
    54  		return nil, nil
    55  	}
    56  
    57  	return nil, apierrors.NewInvalid(schema.GroupKind{}, r.Name, allErrors)
    58  }
    59  
    60  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    61  func (r *Node) ValidateDelete() (admission.Warnings, error) {
    62  	nodelog.Info("validate delete", "name", r.Name)
    63  	return nil, nil
    64  }