Skip to content

Commit

Permalink
refactor: Simplify code by using slices.Clone (#712)
Browse files Browse the repository at this point in the history
**What problem does this PR solve?**:

**Which issue(s) this PR fixes**:
Fixes #

**How Has This Been Tested?**:
<!--
Please describe the tests that you ran to verify your changes.
Provide output from the tests and any manual steps needed to replicate
the tests.
-->

**Special notes for your reviewer**:
<!--
Use this to provide any additional information to the reviewers.
This may include:
- Best way to review the PR.
- Where the author wants the most review attention on.
- etc.
-->
  • Loading branch information
jimmidyson authored Jun 11, 2024
1 parent 511785f commit 7ee8f09
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
9 changes: 7 additions & 2 deletions pkg/handlers/generic/lifecycle/utils/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ func Test_EnsureOwnerRefForSecret(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

err := EnsureOwnerRefForSecret(context.Background(), tt.client, tt.secretName, tt.cluster)
err := EnsureOwnerRefForSecret(
context.Background(),
tt.client,
tt.secretName,
tt.cluster,
)
require.Equal(t, tt.wantErr, err)
if tt.wantErr != nil {
return
Expand All @@ -116,7 +121,7 @@ func Test_EnsureOwnerRefForSecret(t *testing.T) {
client.ObjectKey{Namespace: tt.cluster.Namespace, Name: tt.secretName},
secret,
)
assert.NoError(t, err, "failed to get updated secret")
require.NoError(t, err, "failed to get updated secret")
assert.Len(t, secret.OwnerReferences, tt.wantOwnerRefs)
})
}
Expand Down
32 changes: 7 additions & 25 deletions pkg/handlers/nutanix/mutation/machinedetails/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package machinedetails

import (
"context"
"slices"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -101,30 +101,12 @@ func (h *nutanixMachineDetailsPatchHandler) Mutate(
spec.MemorySize = nutanixMachineDetailsVar.MemorySize
spec.SystemDiskSize = nutanixMachineDetailsVar.SystemDiskSize

spec.Subnets = make(
[]capxv1.NutanixResourceIdentifier,
len(nutanixMachineDetailsVar.Subnets),
)

copy(spec.Subnets, nutanixMachineDetailsVar.Subnets)

spec.AdditionalCategories = make(
[]capxv1.NutanixCategoryIdentifier,
len(nutanixMachineDetailsVar.AdditionalCategories),
)

copy(spec.AdditionalCategories, nutanixMachineDetailsVar.AdditionalCategories)

if nutanixMachineDetailsVar.Project != nil {
spec.Project = ptr.To(
*nutanixMachineDetailsVar.Project,
)
}
spec.GPUs = make(
[]capxv1.NutanixGPU,
len(nutanixMachineDetailsVar.GPUs),
)
copy(spec.GPUs, nutanixMachineDetailsVar.GPUs)
spec.Subnets = slices.Clone(nutanixMachineDetailsVar.Subnets)
spec.AdditionalCategories = slices.Clone(nutanixMachineDetailsVar.AdditionalCategories)
spec.GPUs = slices.Clone(nutanixMachineDetailsVar.GPUs)

spec.Project = nutanixMachineDetailsVar.Project.DeepCopy()

obj.Spec.Template.Spec = spec
return nil
},
Expand Down

0 comments on commit 7ee8f09

Please sign in to comment.