-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `Access` field to `BMC` type Allow the definition of a `BMC` resource without depending on an `Endpoint` resource. * Generate API reference docs * Fix test * Rename `.spec.access` to `.spec.endpoint`
- Loading branch information
Showing
24 changed files
with
587 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
// log is for logging in this package. | ||
var bmclog = logf.Log.WithName("bmc-resource") | ||
|
||
// SetupWebhookWithManager will setup the manager to manage the webhooks | ||
func (r *BMC) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(r). | ||
Complete() | ||
} | ||
|
||
//+kubebuilder:webhook:path=/validate-metal-ironcore-dev-v1alpha1-bmc,mutating=false,failurePolicy=fail,sideEffects=None,groups=metal.ironcore.dev,resources=bmcs,verbs=create;update,versions=v1alpha1,name=vbmc.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Validator = &BMC{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *BMC) ValidateCreate() (admission.Warnings, error) { | ||
bmclog.Info("validate create", "name", r.Name) | ||
|
||
allErrs := field.ErrorList{} | ||
allErrs = append(allErrs, ValidateCreateBMCSpec(r.Spec, field.NewPath("spec"))...) | ||
|
||
if len(allErrs) != 0 { | ||
return nil, apierrors.NewInvalid( | ||
schema.GroupKind{Group: "metal.ironcore.dev", Kind: "BMC"}, | ||
r.Name, allErrs) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func ValidateCreateBMCSpec(spec BMCSpec, fldPath *field.Path) field.ErrorList { | ||
allErrs := field.ErrorList{} | ||
|
||
if spec.EndpointRef != nil && spec.Endpoint != nil { | ||
allErrs = append(allErrs, field.Invalid(fldPath.Child("endpointRef"), spec.EndpointRef, "only one of 'endpointRef' or 'endpoint' should be specified")) | ||
allErrs = append(allErrs, field.Invalid(fldPath.Child("endpoint"), spec.Endpoint, "only one of 'endpointRef' or 'endpoint' should be specified")) | ||
} | ||
if spec.EndpointRef == nil && spec.Endpoint == nil { | ||
allErrs = append(allErrs, field.Invalid(fldPath.Child("endpointRef"), spec.EndpointRef, "either 'endpointRef' or 'endpoint' must be specified")) | ||
allErrs = append(allErrs, field.Invalid(fldPath.Child("endpoint"), spec.Endpoint, "either 'endpointRef' or 'endpoint' must be specified")) | ||
} | ||
|
||
return allErrs | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *BMC) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { | ||
bmclog.Info("validate update", "name", r.Name) | ||
allErrs := field.ErrorList{} | ||
|
||
allErrs = append(allErrs, ValidateCreateBMCSpec(r.Spec, field.NewPath("spec"))...) | ||
|
||
if len(allErrs) != 0 { | ||
return nil, apierrors.NewInvalid( | ||
schema.GroupKind{Group: "metal.ironcore.dev", Kind: "BMC"}, | ||
r.Name, allErrs) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type | ||
func (r *BMC) ValidateDelete() (admission.Warnings, error) { | ||
bmclog.Info("validate delete", "name", r.Name) | ||
|
||
// TODO(user): fill in your validation logic upon object deletion. | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega" | ||
) | ||
|
||
var _ = Describe("BMC Webhook", func() { | ||
_ = SetupTest() | ||
|
||
It("Should deny if the BMC has EndpointRef and InlineEndpoint spec fields", func() { | ||
bmc := &BMC{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-invalid", | ||
}, | ||
Spec: BMCSpec{ | ||
EndpointRef: &v1.LocalObjectReference{Name: "foo"}, | ||
Endpoint: &InlineEndpoint{ | ||
IP: MustParseIP("127.0.0.1"), | ||
MACAddress: "aa:bb:cc:dd:ee:ff", | ||
}, | ||
}, | ||
} | ||
Expect(k8sClient.Create(ctx, bmc)).To(HaveOccurred()) | ||
Eventually(Get(bmc)).Should(Satisfy(errors.IsNotFound)) | ||
}) | ||
|
||
It("Should deny if the BMC has no EndpointRef and InlineEndpoint spec fields", func() { | ||
bmc := &BMC{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-empty", | ||
}, | ||
Spec: BMCSpec{}, | ||
} | ||
Expect(k8sClient.Create(ctx, bmc)).To(HaveOccurred()) | ||
Eventually(Get(bmc)).Should(Satisfy(errors.IsNotFound)) | ||
}) | ||
|
||
It("Should admit if the BMC has an EndpointRef but no InlineEndpoint spec field", func() { | ||
bmc := &BMC{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "test-", | ||
}, | ||
Spec: BMCSpec{ | ||
EndpointRef: &v1.LocalObjectReference{Name: "foo"}, | ||
}, | ||
} | ||
Expect(k8sClient.Create(ctx, bmc)).To(Succeed()) | ||
DeferCleanup(k8sClient.Delete, bmc) | ||
}) | ||
|
||
It("Should deny if the BMC EndpointRef spec field has been removed", func() { | ||
bmc := &BMC{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "test-", | ||
}, | ||
Spec: BMCSpec{ | ||
EndpointRef: &v1.LocalObjectReference{Name: "foo"}, | ||
}, | ||
} | ||
Expect(k8sClient.Create(ctx, bmc)).To(Succeed()) | ||
DeferCleanup(k8sClient.Delete, bmc) | ||
|
||
Eventually(Update(bmc, func() { | ||
bmc.Spec.EndpointRef = nil | ||
})).Should(Not(Succeed())) | ||
|
||
Eventually(Object(bmc)).Should(SatisfyAll(HaveField( | ||
"Spec.EndpointRef", &v1.LocalObjectReference{Name: "foo"}))) | ||
}) | ||
|
||
It("Should admit if the BMC is changing EndpointRef to InlineEndpoint spec field", func() { | ||
bmc := &BMC{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "test-", | ||
}, | ||
Spec: BMCSpec{ | ||
EndpointRef: &v1.LocalObjectReference{Name: "foo"}, | ||
}, | ||
} | ||
Expect(k8sClient.Create(ctx, bmc)).To(Succeed()) | ||
DeferCleanup(k8sClient.Delete, bmc) | ||
|
||
Eventually(Update(bmc, func() { | ||
bmc.Spec.EndpointRef = nil | ||
bmc.Spec.Endpoint = &InlineEndpoint{ | ||
IP: MustParseIP("127.0.0.1"), | ||
MACAddress: "aa:bb:cc:dd:ee:ff", | ||
} | ||
})).Should(Succeed()) | ||
}) | ||
|
||
It("Should admit if the BMC has no EndpointRef but an InlineEndpoint spec field", func() { | ||
bmc := &BMC{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "test-", | ||
}, | ||
Spec: BMCSpec{ | ||
Endpoint: &InlineEndpoint{ | ||
IP: MustParseIP("127.0.0.1"), | ||
MACAddress: "aa:bb:cc:dd:ee:ff", | ||
}, | ||
}, | ||
} | ||
Expect(k8sClient.Create(ctx, bmc)).To(Succeed()) | ||
DeferCleanup(k8sClient.Delete, bmc) | ||
}) | ||
|
||
It("Should deny if the BMC InlineEndpoint spec field has been removed", func() { | ||
bmc := &BMC{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "test-", | ||
}, | ||
Spec: BMCSpec{ | ||
Endpoint: &InlineEndpoint{ | ||
IP: MustParseIP("127.0.0.1"), | ||
MACAddress: "aa:bb:cc:dd:ee:ff", | ||
}, | ||
}, | ||
} | ||
Expect(k8sClient.Create(ctx, bmc)).To(Succeed()) | ||
DeferCleanup(k8sClient.Delete, bmc) | ||
|
||
Eventually(Update(bmc, func() { | ||
bmc.Spec.Endpoint = nil | ||
})).Should(Not(Succeed())) | ||
|
||
Eventually(Object(bmc)).Should(SatisfyAll( | ||
HaveField("Spec.Endpoint.IP", MustParseIP("127.0.0.1")), | ||
HaveField("Spec.Endpoint.MACAddress", "aa:bb:cc:dd:ee:ff"), | ||
)) | ||
}) | ||
|
||
It("Should admit if the BMC has is changing to an EndpointRef from an InlineEndpoint spec field", func() { | ||
bmc := &BMC{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "test-", | ||
}, | ||
Spec: BMCSpec{ | ||
Endpoint: &InlineEndpoint{ | ||
IP: MustParseIP("127.0.0.1"), | ||
MACAddress: "aa:bb:cc:dd:ee:ff", | ||
}, | ||
}, | ||
} | ||
Expect(k8sClient.Create(ctx, bmc)).To(Succeed()) | ||
DeferCleanup(k8sClient.Delete, bmc) | ||
|
||
Eventually(Update(bmc, func() { | ||
bmc.Spec.EndpointRef = &v1.LocalObjectReference{Name: "foo"} | ||
bmc.Spec.Endpoint = nil | ||
})).Should(Succeed()) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.