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

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