@@ -22,7 +22,7 @@ import (
2222
2323// nolint:unused
2424// log is for logging in this package.
25- var biosversionlog = logf .Log .WithName ("biosversion-resource" )
25+ var versionLog = logf .Log .WithName ("biosversion-resource" )
2626
2727// SetupBIOSVersionWebhookWithManager registers the webhook for BIOSVersion in the manager.
2828func SetupBIOSVersionWebhookWithManager (mgr ctrl.Manager ) error {
@@ -31,101 +31,99 @@ func SetupBIOSVersionWebhookWithManager(mgr ctrl.Manager) error {
3131 Complete ()
3232}
3333
34- // TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
35-
36- // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
3734// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
3835// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
3936// +kubebuilder:webhook:path=/validate-metal-ironcore-dev-v1alpha1-biosversion,mutating=false,failurePolicy=fail,sideEffects=None,groups=metal.ironcore.dev,resources=biosversions,verbs=create;update;delete,versions=v1alpha1,name=vbiosversion-v1alpha1.kb.io,admissionReviewVersions=v1
4037
4138// BIOSVersionCustomValidator struct is responsible for validating the BIOSVersion resource
4239// when it is created, updated, or deleted.
43- //
44- // NOTE: The +kubebuilder:object:generate=false marker prevents controller-gen from generating DeepCopy methods,
45- // as this struct is used only for temporary operations and does not need to be deeply copied.
4640type BIOSVersionCustomValidator struct {
47- Client client.Client
41+ client.Client
4842}
4943
5044var _ webhook.CustomValidator = & BIOSVersionCustomValidator {}
5145
5246// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type BIOSVersion.
5347func (v * BIOSVersionCustomValidator ) ValidateCreate (ctx context.Context , obj runtime.Object ) (admission.Warnings , error ) {
54- biosversion , ok := obj .(* metalv1alpha1.BIOSVersion )
48+ version , ok := obj .(* metalv1alpha1.BIOSVersion )
5549 if ! ok {
5650 return nil , fmt .Errorf ("expected a BIOSVersion object but got %T" , obj )
5751 }
58- biosversionlog .Info ("Validation for BIOSVersion upon creation" , "name" , biosversion .GetName ())
52+ versionLog .Info ("Validation for BIOSVersion upon creation" , "name" , version .GetName ())
5953
60- biosVersionList := & metalv1alpha1.BIOSVersionList {}
61- if err := v .Client .List (ctx , biosVersionList ); err != nil {
62- return nil , fmt .Errorf ("failed to list BIOSVersionList : %w" , err )
54+ versions := & metalv1alpha1.BIOSVersionList {}
55+ if err := v .Client .List (ctx , versions ); err != nil {
56+ return nil , fmt .Errorf ("failed to list BIOSVersion : %w" , err )
6357 }
64- return checkForDuplicateBIOSVersionRefToServer (biosVersionList , biosversion )
58+ return checkForDuplicateBIOSVersionRefToServer (versions , version )
6559}
6660
6761// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type BIOSVersion.
6862func (v * BIOSVersionCustomValidator ) ValidateUpdate (ctx context.Context , oldObj , newObj runtime.Object ) (admission.Warnings , error ) {
69- biosversion , ok := newObj .(* metalv1alpha1.BIOSVersion )
63+ newVersion , ok := newObj .(* metalv1alpha1.BIOSVersion )
7064 if ! ok {
7165 return nil , fmt .Errorf ("expected a BIOSVersion object for the newObj but got %T" , newObj )
7266 }
73- biosversionlog .Info ("Validation for BIOSVersion upon update" , "name" , biosversion .GetName ())
67+ versionLog .Info ("Validation for BIOSVersion upon update" , "name" , newVersion .GetName ())
7468
75- oldBIOSVersion , ok := oldObj .(* metalv1alpha1.BIOSVersion )
69+ oldVersion , ok := oldObj .(* metalv1alpha1.BIOSVersion )
7670 if ! ok {
7771 return nil , fmt .Errorf ("expected a BIOSVersion object for the oldObj but got %T" , oldObj )
7872 }
79- if oldBIOSVersion .Status .State == metalv1alpha1 .BIOSVersionStateInProgress &&
80- ! ShouldAllowForceUpdateInProgress (biosversion ) && oldBIOSVersion .Spec .ServerMaintenanceRef != nil {
73+ if oldVersion .Status .State == metalv1alpha1 .BIOSVersionStateInProgress &&
74+ ! ShouldAllowForceUpdateInProgress (newVersion ) && oldVersion .Spec .ServerMaintenanceRef != nil {
8175 err := fmt .Errorf ("BIOSVersion (%v) is in progress, unable to update %v" ,
82- oldBIOSVersion .Name ,
83- biosversion .Name )
76+ oldVersion .Name ,
77+ newVersion .Name )
8478 return nil , apierrors .NewInvalid (
85- schema.GroupKind {Group : biosversion .GroupVersionKind ().Group , Kind : biosversion .Kind },
86- biosversion .GetName (), field.ErrorList {field .Forbidden (field .NewPath ("spec" ), err .Error ())})
79+ schema.GroupKind {Group : newVersion .GroupVersionKind ().Group , Kind : newVersion .Kind },
80+ newVersion .GetName (), field.ErrorList {field .Forbidden (field .NewPath ("spec" ), err .Error ())})
8781 }
8882
89- biosVersionList := & metalv1alpha1.BIOSVersionList {}
90- if err := v .Client .List (ctx , biosVersionList ); err != nil {
91- return nil , fmt .Errorf ("failed to list BIOSVersionList : %w" , err )
83+ versions := & metalv1alpha1.BIOSVersionList {}
84+ if err := v .Client .List (ctx , versions ); err != nil {
85+ return nil , fmt .Errorf ("failed to list BIOSVersion : %w" , err )
9286 }
9387
94- return checkForDuplicateBIOSVersionRefToServer (biosVersionList , biosversion )
88+ return checkForDuplicateBIOSVersionRefToServer (versions , newVersion )
9589}
9690
9791// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type BIOSVersion.
9892func (v * BIOSVersionCustomValidator ) ValidateDelete (ctx context.Context , obj runtime.Object ) (admission.Warnings , error ) {
99- biosversion , ok := obj .(* metalv1alpha1.BIOSVersion )
93+ version , ok := obj .(* metalv1alpha1.BIOSVersion )
10094 if ! ok {
10195 return nil , fmt .Errorf ("expected a BIOSVersion object but got %T" , obj )
10296 }
103- biosversionlog .Info ("Validation for BIOSVersion upon deletion" , "name" , biosversion .GetName ())
97+ versionLog .Info ("Validation for BIOSVersion upon deletion" , "name" , version .GetName ())
10498
105- if biosversion .Status .State == metalv1alpha1 .BIOSVersionStateInProgress && ! ShouldAllowForceDeleteInProgress (biosversion ) {
99+ if version .Status .State == metalv1alpha1 .BIOSVersionStateInProgress && ! ShouldAllowForceDeleteInProgress (version ) {
106100 return nil , apierrors .NewBadRequest ("The bios version in progress, unable to delete" )
107101 }
108102 return nil , nil
109103}
110104
111- func checkForDuplicateBIOSVersionRefToServer (
112- biosVersionList * metalv1alpha1.BIOSVersionList ,
113- biosVersion * metalv1alpha1.BIOSVersion ,
114- ) (admission.Warnings , error ) {
115- for _ , bv := range biosVersionList .Items {
116- if biosVersion .Name == bv .Name {
105+ func checkForDuplicateBIOSVersionRefToServer (versions * metalv1alpha1.BIOSVersionList , version * metalv1alpha1.BIOSVersion ) (admission.Warnings , error ) {
106+ if version .Spec .ServerRef == nil {
107+ return nil , nil
108+ }
109+
110+ for _ , bv := range versions .Items {
111+ if version .Name == bv .Name {
112+ continue
113+ }
114+ if bv .Spec .ServerRef == nil {
117115 continue
118116 }
119- if biosVersion .Spec .ServerRef .Name == bv .Spec .ServerRef .Name {
120- err := fmt .Errorf ("server (%v ) referred in %v is duplicate of Server (%v ) referred in %v " ,
121- biosVersion .Spec .ServerRef ,
122- biosVersion .Name ,
123- bv .Spec .ServerRef ,
117+ if version .Spec .ServerRef .Name == bv .Spec .ServerRef .Name {
118+ err := fmt .Errorf ("server (%s ) referred in %s is duplicate of Server (%s ) referred in %s " ,
119+ version .Spec .ServerRef . Name ,
120+ version .Name ,
121+ bv .Spec .ServerRef . Name ,
124122 bv .Name ,
125123 )
126124 return nil , apierrors .NewInvalid (
127- schema.GroupKind {Group : biosVersion .GroupVersionKind ().Group , Kind : biosVersion .Kind },
128- biosVersion .GetName (), field.ErrorList {field .Duplicate (field .NewPath ("spec" ).Child ("ServerRef " ).Child ("Name " ), err )})
125+ schema.GroupKind {Group : version .GroupVersionKind ().Group , Kind : version .Kind },
126+ version .GetName (), field.ErrorList {field .Duplicate (field .NewPath ("spec" ).Child ("serverRef " ).Child ("name " ), err )})
129127 }
130128 }
131129 return nil , nil
0 commit comments