Skip to content

Commit 7ee8f09

Browse files
authored
refactor: Simplify code by using slices.Clone (#712)
**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. -->
1 parent 511785f commit 7ee8f09

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

pkg/handlers/generic/lifecycle/utils/secrets_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ func Test_EnsureOwnerRefForSecret(t *testing.T) {
104104
t.Run(tt.name, func(t *testing.T) {
105105
t.Parallel()
106106

107-
err := EnsureOwnerRefForSecret(context.Background(), tt.client, tt.secretName, tt.cluster)
107+
err := EnsureOwnerRefForSecret(
108+
context.Background(),
109+
tt.client,
110+
tt.secretName,
111+
tt.cluster,
112+
)
108113
require.Equal(t, tt.wantErr, err)
109114
if tt.wantErr != nil {
110115
return
@@ -116,7 +121,7 @@ func Test_EnsureOwnerRefForSecret(t *testing.T) {
116121
client.ObjectKey{Namespace: tt.cluster.Namespace, Name: tt.secretName},
117122
secret,
118123
)
119-
assert.NoError(t, err, "failed to get updated secret")
124+
require.NoError(t, err, "failed to get updated secret")
120125
assert.Len(t, secret.OwnerReferences, tt.wantOwnerRefs)
121126
})
122127
}

pkg/handlers/nutanix/mutation/machinedetails/inject.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ package machinedetails
55

66
import (
77
"context"
8+
"slices"
89

910
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1011
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11-
"k8s.io/utils/ptr"
1212
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1313
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1414
ctrl "sigs.k8s.io/controller-runtime"
@@ -101,30 +101,12 @@ func (h *nutanixMachineDetailsPatchHandler) Mutate(
101101
spec.MemorySize = nutanixMachineDetailsVar.MemorySize
102102
spec.SystemDiskSize = nutanixMachineDetailsVar.SystemDiskSize
103103

104-
spec.Subnets = make(
105-
[]capxv1.NutanixResourceIdentifier,
106-
len(nutanixMachineDetailsVar.Subnets),
107-
)
108-
109-
copy(spec.Subnets, nutanixMachineDetailsVar.Subnets)
110-
111-
spec.AdditionalCategories = make(
112-
[]capxv1.NutanixCategoryIdentifier,
113-
len(nutanixMachineDetailsVar.AdditionalCategories),
114-
)
115-
116-
copy(spec.AdditionalCategories, nutanixMachineDetailsVar.AdditionalCategories)
117-
118-
if nutanixMachineDetailsVar.Project != nil {
119-
spec.Project = ptr.To(
120-
*nutanixMachineDetailsVar.Project,
121-
)
122-
}
123-
spec.GPUs = make(
124-
[]capxv1.NutanixGPU,
125-
len(nutanixMachineDetailsVar.GPUs),
126-
)
127-
copy(spec.GPUs, nutanixMachineDetailsVar.GPUs)
104+
spec.Subnets = slices.Clone(nutanixMachineDetailsVar.Subnets)
105+
spec.AdditionalCategories = slices.Clone(nutanixMachineDetailsVar.AdditionalCategories)
106+
spec.GPUs = slices.Clone(nutanixMachineDetailsVar.GPUs)
107+
108+
spec.Project = nutanixMachineDetailsVar.Project.DeepCopy()
109+
128110
obj.Spec.Template.Spec = spec
129111
return nil
130112
},

0 commit comments

Comments
 (0)