...

Source file src/edge-infra.dev/pkg/sds/display/k8s/apis/v2/nodedisplayconfig_webhook.go

Documentation: edge-infra.dev/pkg/sds/display/k8s/apis/v2

     1  //nolint:revive
     2  package v2
     3  
     4  import (
     5  	runtime "k8s.io/apimachinery/pkg/runtime"
     6  	ctrl "sigs.k8s.io/controller-runtime"
     7  	"sigs.k8s.io/controller-runtime/pkg/conversion"
     8  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
     9  
    10  	v1 "edge-infra.dev/pkg/sds/display/k8s/apis/v1"
    11  )
    12  
    13  // +kubebuilder:webhook:verbs=create;update,path=/validate-display-edge-ncr-com-v2-nodedisplayconfig,mutating=false,failurePolicy=fail,groups=display.edge.ncr.com,resources=nodedisplayconfigs,versions=v2,name=v2-nodedisplayconfig-validation-webhook.display.edge.ncr.com,sideEffects=None,admissionReviewVersions=v1
    14  
    15  func (nodeDisplayConfig *NodeDisplayConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
    16  	return ctrl.NewWebhookManagedBy(mgr).For(nodeDisplayConfig).Complete()
    17  }
    18  
    19  // NodeDisplayConfig must satisfy the Validator interface for the validation webhook to start.
    20  var _ admission.Validator = &NodeDisplayConfig{}
    21  
    22  func (nodeDisplayConfig *NodeDisplayConfig) ValidateCreate() (admission.Warnings, error) {
    23  	return nil, nodeDisplayConfig.Spec.Validate()
    24  }
    25  
    26  func (nodeDisplayConfig *NodeDisplayConfig) ValidateUpdate(runtime.Object) (admission.Warnings, error) {
    27  	return nil, nodeDisplayConfig.Spec.Validate()
    28  }
    29  
    30  func (*NodeDisplayConfig) ValidateDelete() (admission.Warnings, error) {
    31  	// delete requests are always valid
    32  	return nil, nil
    33  }
    34  
    35  // v2 NodeDisplayConfig must be convertible to and from the hub
    36  // version (v1) for the conversion webhook to start.
    37  var _ conversion.Convertible = &NodeDisplayConfig{}
    38  
    39  // Converts V1 to V2.
    40  //
    41  // This simply copies the V1 spec into the V2 NodeDisplayConfig
    42  // to be upgraded by displayctl later on.
    43  func (dst *NodeDisplayConfig) ConvertFrom(src conversion.Hub) error {
    44  	nodeDisplayConfig := src.(*v1.NodeDisplayConfig)
    45  	dst.ObjectMeta = nodeDisplayConfig.ObjectMeta
    46  	dst.Spec = &DisplayConfig{
    47  		V1: nodeDisplayConfig.Spec,
    48  	}
    49  	return nil
    50  }
    51  
    52  // Converts V2 to V1.
    53  //
    54  // V1 is deprecated and not in use. Conversion from V2 to V1
    55  // is unsupported and simply removes the spec from the object.
    56  // As V2 is the stored version there is no data loss and the
    57  // spec can always be retrieved via the V2 API.
    58  func (src *NodeDisplayConfig) ConvertTo(dst conversion.Hub) error {
    59  	nodeDisplayConfig := dst.(*v1.NodeDisplayConfig)
    60  	nodeDisplayConfig.ObjectMeta = src.ObjectMeta
    61  	nodeDisplayConfig.Spec = nil
    62  	return nil
    63  }
    64  

View as plain text