Skip to content

Commit 43876e5

Browse files
committed
chore: refactor InstanceAdditionalDiskSpec for reuse in MachinePool
1 parent bf2b788 commit 43876e5

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

cloud/scope/machine.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,16 @@ func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk {
274274
return disk
275275
}
276276

277-
// InstanceAdditionalDiskSpec returns compute instance additional attched-disk spec.
278-
func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
279-
additionalDisks := make([]*compute.AttachedDisk, 0, len(m.GCPMachine.Spec.AdditionalDisks))
280-
for _, disk := range m.GCPMachine.Spec.AdditionalDisks {
277+
// instanceAdditionalDiskSpec returns compute instance additional attched-disk spec.
278+
func instanceAdditionalDiskSpec(ctx context.Context, spec []infrav1.AttachedDiskSpec, rootDiskEncryptionKey *infrav1.CustomerEncryptionKey, zone string, resourceManagerTags infrav1.ResourceManagerTags) []*compute.AttachedDisk {
279+
additionalDisks := make([]*compute.AttachedDisk, 0, len(spec))
280+
for _, disk := range spec {
281281
additionalDisk := &compute.AttachedDisk{
282282
AutoDelete: true,
283283
InitializeParams: &compute.AttachedDiskInitializeParams{
284284
DiskSizeGb: ptr.Deref(disk.Size, 30),
285-
DiskType: path.Join("zones", m.Zone(), "diskTypes", string(*disk.DeviceType)),
286-
ResourceManagerTags: shared.ResourceTagConvert(context.TODO(), m.GCPMachine.Spec.ResourceManagerTags),
285+
DiskType: path.Join("zones", zone, "diskTypes", string(*disk.DeviceType)),
286+
ResourceManagerTags: shared.ResourceTagConvert(ctx, resourceManagerTags),
287287
},
288288
}
289289
if strings.HasSuffix(additionalDisk.InitializeParams.DiskType, string(infrav1.LocalSsdDiskType)) {
@@ -297,20 +297,20 @@ func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
297297
additionalDisk.Interface = "NVME"
298298
}
299299
if disk.EncryptionKey != nil {
300-
if m.GCPMachine.Spec.RootDiskEncryptionKey.KeyType == infrav1.CustomerManagedKey && m.GCPMachine.Spec.RootDiskEncryptionKey.ManagedKey != nil {
300+
if rootDiskEncryptionKey.KeyType == infrav1.CustomerManagedKey && rootDiskEncryptionKey.ManagedKey != nil {
301301
additionalDisk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
302-
KmsKeyName: m.GCPMachine.Spec.RootDiskEncryptionKey.ManagedKey.KMSKeyName,
302+
KmsKeyName: rootDiskEncryptionKey.ManagedKey.KMSKeyName,
303303
}
304-
if m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount != nil {
305-
additionalDisk.DiskEncryptionKey.KmsKeyServiceAccount = *m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount
304+
if rootDiskEncryptionKey.KMSKeyServiceAccount != nil {
305+
additionalDisk.DiskEncryptionKey.KmsKeyServiceAccount = *rootDiskEncryptionKey.KMSKeyServiceAccount
306306
}
307-
} else if m.GCPMachine.Spec.RootDiskEncryptionKey.KeyType == infrav1.CustomerSuppliedKey && m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey != nil {
307+
} else if rootDiskEncryptionKey.KeyType == infrav1.CustomerSuppliedKey && rootDiskEncryptionKey.SuppliedKey != nil {
308308
additionalDisk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
309-
RawKey: string(m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey.RawKey),
310-
RsaEncryptedKey: string(m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey.RSAEncryptedKey),
309+
RawKey: string(rootDiskEncryptionKey.SuppliedKey.RawKey),
310+
RsaEncryptedKey: string(rootDiskEncryptionKey.SuppliedKey.RSAEncryptedKey),
311311
}
312-
if m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount != nil {
313-
additionalDisk.DiskEncryptionKey.KmsKeyServiceAccount = *m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount
312+
if rootDiskEncryptionKey.KMSKeyServiceAccount != nil {
313+
additionalDisk.DiskEncryptionKey.KmsKeyServiceAccount = *rootDiskEncryptionKey.KMSKeyServiceAccount
314314
}
315315
}
316316
}
@@ -375,6 +375,8 @@ func (m *MachineScope) InstanceAdditionalMetadataSpec() *compute.Metadata {
375375

376376
// InstanceSpec returns instance spec.
377377
func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
378+
ctx := context.TODO()
379+
378380
instance := &compute.Instance{
379381
Name: m.Name(),
380382
Zone: m.Zone(),
@@ -461,7 +463,7 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
461463
}
462464

463465
instance.Disks = append(instance.Disks, m.InstanceImageSpec())
464-
instance.Disks = append(instance.Disks, m.InstanceAdditionalDiskSpec()...)
466+
instance.Disks = append(instance.Disks, instanceAdditionalDiskSpec(ctx, m.GCPMachine.Spec.AdditionalDisks, m.GCPMachine.Spec.RootDiskEncryptionKey, m.Zone(), m.ResourceManagerTags())...)
465467
instance.Metadata = m.InstanceAdditionalMetadataSpec()
466468
instance.ServiceAccounts = append(instance.ServiceAccounts, m.InstanceServiceAccountsSpec())
467469
instance.NetworkInterfaces = append(instance.NetworkInterfaces, m.InstanceNetworkInterfaceSpec())

cloud/scope/machine_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scope
22

33
import (
4+
"context"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -12,6 +13,8 @@ import (
1213
// This test verifies that if a user selects "local-ssd"
1314
// as a disk type then the MachineScope correctly detects it as such.
1415
func TestMachineLocalSSDDiskType(t *testing.T) {
16+
ctx := context.Background()
17+
1518
// Register the GCPMachine and GCPMachineList in a schema.
1619
schema, err := infrav1.SchemeBuilder.Register(&infrav1.GCPMachine{}, &infrav1.GCPMachineList{}).Build()
1720

@@ -62,7 +65,7 @@ func TestMachineLocalSSDDiskType(t *testing.T) {
6265
assert.NotNil(t, testMachineScope)
6366

6467
// Now make sure the local-ssd disk type is detected as SCRATCH.
65-
diskSpec := testMachineScope.InstanceAdditionalDiskSpec()
68+
diskSpec := instanceAdditionalDiskSpec(ctx, testGCPMachine.Spec.AdditionalDisks, testGCPMachine.Spec.RootDiskEncryptionKey, testMachineScope.Zone(), testGCPMachine.Spec.ResourceManagerTags)
6669
assert.NotEmpty(t, diskSpec)
6770

6871
// Get the local-ssd disk now.

cloud/services/compute/instances/service.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ type instancegroupsInterface interface {
4545
type Scope interface {
4646
cloud.Machine
4747
InstanceSpec(log logr.Logger) *compute.Instance
48-
InstanceImageSpec() *compute.AttachedDisk
49-
InstanceAdditionalDiskSpec() []*compute.AttachedDisk
5048
}
5149

5250
// Service implements instances reconciler.

0 commit comments

Comments
 (0)