Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SKS-3181: Should hook elfMachine UPDATE verbs in mutation webhook #187

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ webhooks:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- elfmachines
sideEffects: None
Expand Down
20 changes: 18 additions & 2 deletions webhooks/elfmachine_webhook_mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

infrav1 "github.com/smartxworks/cluster-api-provider-elf/api/v1beta1"
annotationsutil "github.com/smartxworks/cluster-api-provider-elf/pkg/util/annotations"
"github.com/smartxworks/cluster-api-provider-elf/pkg/version"
)

Expand All @@ -45,7 +46,7 @@ func (m *ElfMachineMutation) SetupWebhookWithManager(mgr ctrl.Manager) error {
}

//+kubebuilder:object:generate=false
//+kubebuilder:webhook:verbs=create,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-elfmachine,mutating=true,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=elfmachines,versions=v1beta1,name=mutation.elfmachine.infrastructure.x-k8s.io,admissionReviewVersions=v1
//+kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-elfmachine,mutating=true,failurePolicy=fail,sideEffects=None,groups=infrastructure.cluster.x-k8s.io,resources=elfmachines,versions=v1beta1,name=mutation.elfmachine.infrastructure.x-k8s.io,admissionReviewVersions=v1

type ElfMachineMutation struct {
client.Client
Expand All @@ -64,7 +65,22 @@ func (m *ElfMachineMutation) Handle(ctx goctx.Context, request admission.Request
}

if elfMachine.Spec.NumCoresPerSocket <= 0 {
elfMachine.Spec.NumCoresPerSocket = elfMachine.Spec.NumCPUs
// Prefer to set the value to be the same as elfMachineTemplate
elfMachineTemplateName := annotationsutil.GetTemplateClonedFromName(&elfMachine)
if elfMachineTemplateName != "" {
var elfMachineTemplate infrav1.ElfMachineTemplate
if err := m.Get(ctx, client.ObjectKey{
Namespace: elfMachine.Namespace,
Name: annotationsutil.GetTemplateClonedFromName(&elfMachine),
}, &elfMachineTemplate); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
elfMachine.Spec.NumCoresPerSocket = elfMachineTemplate.Spec.Template.Spec.NumCoresPerSocket
}
// If elfMachineTemplate also has no value, set it to be the same as NumCPUs
if elfMachine.Spec.NumCoresPerSocket <= 0 {
elfMachine.Spec.NumCoresPerSocket = elfMachine.Spec.NumCPUs
}
}

if marshaledElfMachine, err := json.Marshal(elfMachine); err != nil {
Expand Down
30 changes: 28 additions & 2 deletions webhooks/elfmachine_webhook_mutation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ import (
admissionv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

infrav1 "github.com/smartxworks/cluster-api-provider-elf/api/v1beta1"
"github.com/smartxworks/cluster-api-provider-elf/pkg/util/annotations"
"github.com/smartxworks/cluster-api-provider-elf/pkg/version"
"github.com/smartxworks/cluster-api-provider-elf/test/fake"
)
Expand All @@ -46,6 +49,7 @@ var (

func TestElfMachineMutation(t *testing.T) {
g := NewWithT(t)
scheme := newScheme(g)
tests := []testCase{}

elfMachine := fake.NewElfMachine(nil)
Expand All @@ -54,7 +58,7 @@ func TestElfMachineMutation(t *testing.T) {
raw, err := marshal(elfMachine)
g.Expect(err).NotTo(HaveOccurred())
tests = append(tests, testCase{
name: "should set CAPE version",
name: "should set CAPE version and numCoresPerSocket",
admissionRequest: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{
Kind: metav1.GroupVersionKind{Group: infrav1.GroupVersion.Group, Version: infrav1.GroupVersion.Version, Kind: "ElfMachine"},
Operation: admissionv1.Create,
Expand All @@ -67,9 +71,30 @@ func TestElfMachineMutation(t *testing.T) {
},
})

elfMachineTemplate := fake.NewElfMachineTemplate()
elfMachineTemplate.Spec.Template.Spec.NumCoresPerSocket = 2
elfMachine = fake.NewElfMachine(nil)
annotations.AddAnnotations(elfMachine, map[string]string{clusterv1.TemplateClonedFromNameAnnotation: elfMachineTemplate.Name})
elfMachine.Spec.NumCoresPerSocket = 0
raw, err = marshal(elfMachine)
g.Expect(err).NotTo(HaveOccurred())
tests = append(tests, testCase{
name: "should set NumCoresPerSocket from elfMachineTemplate",
admissionRequest: admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{
Kind: metav1.GroupVersionKind{Group: infrav1.GroupVersion.Group, Version: infrav1.GroupVersion.Version, Kind: "ElfMachine"},
Operation: admissionv1.Update,
Object: runtime.RawExtension{Raw: raw},
}},
expectRespAllowed: true,
expectPatchs: []jsonpatch.Operation{
{Operation: "add", Path: "/spec/numCoresPerSocket", Value: float64(elfMachineTemplate.Spec.Template.Spec.NumCoresPerSocket)},
},
client: fakeclient.NewClientBuilder().WithScheme(scheme).WithObjects(elfMachineTemplate).Build(),
})

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mutation := ElfMachineMutation{}
mutation := ElfMachineMutation{Client: tc.client}
mutation.InjectDecoder(admission.NewDecoder(scheme))

resp := mutation.Handle(context.Background(), tc.admissionRequest)
Expand All @@ -92,4 +117,5 @@ type testCase struct {
admissionRequest admission.Request
expectRespAllowed bool
expectPatchs []jsonpatch.Operation
client client.Client
}
3 changes: 2 additions & 1 deletion webhooks/elfmachine_webhook_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func (v *ElfMachineValidator) ValidateUpdate(ctx goctx.Context, oldObj, newObj r
if elfMachine.Spec.NumCPUs != elfMachineTemplate.Spec.Template.Spec.NumCPUs {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "numCPUs"), elfMachine.Spec.NumCPUs, fmt.Sprintf(canOnlyModifiedThroughElfMachineTemplate, elfMachineTemplateName)))
}
if elfMachine.Spec.NumCoresPerSocket != elfMachineTemplate.Spec.Template.Spec.NumCoresPerSocket {
if elfMachine.Spec.NumCoresPerSocket != 0 && elfMachineTemplate.Spec.Template.Spec.NumCoresPerSocket != 0 &&
elfMachine.Spec.NumCoresPerSocket != elfMachineTemplate.Spec.Template.Spec.NumCoresPerSocket {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "numCoresPerSocket"), elfMachine.Spec.NumCoresPerSocket, fmt.Sprintf(canOnlyModifiedThroughElfMachineTemplate, elfMachineTemplateName)))
}
if elfMachine.Spec.MemoryMiB != elfMachineTemplate.Spec.Template.Spec.MemoryMiB {
Expand Down