Skip to content
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
34 changes: 18 additions & 16 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,16 @@ func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk {
return disk
}

// InstanceAdditionalDiskSpec returns compute instance additional attched-disk spec.
func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
additionalDisks := make([]*compute.AttachedDisk, 0, len(m.GCPMachine.Spec.AdditionalDisks))
for _, disk := range m.GCPMachine.Spec.AdditionalDisks {
// instanceAdditionalDiskSpec returns compute instance additional attched-disk spec.
func instanceAdditionalDiskSpec(ctx context.Context, spec []infrav1.AttachedDiskSpec, rootDiskEncryptionKey *infrav1.CustomerEncryptionKey, zone string, resourceManagerTags infrav1.ResourceManagerTags) []*compute.AttachedDisk {
additionalDisks := make([]*compute.AttachedDisk, 0, len(spec))
for _, disk := range spec {
additionalDisk := &compute.AttachedDisk{
AutoDelete: true,
InitializeParams: &compute.AttachedDiskInitializeParams{
DiskSizeGb: ptr.Deref(disk.Size, 30),
DiskType: path.Join("zones", m.Zone(), "diskTypes", string(*disk.DeviceType)),
ResourceManagerTags: shared.ResourceTagConvert(context.TODO(), m.GCPMachine.Spec.ResourceManagerTags),
DiskType: path.Join("zones", zone, "diskTypes", string(*disk.DeviceType)),
ResourceManagerTags: shared.ResourceTagConvert(ctx, resourceManagerTags),
},
}
if strings.HasSuffix(additionalDisk.InitializeParams.DiskType, string(infrav1.LocalSsdDiskType)) {
Expand All @@ -297,20 +297,20 @@ func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
additionalDisk.Interface = "NVME"
}
if disk.EncryptionKey != nil {
if m.GCPMachine.Spec.RootDiskEncryptionKey.KeyType == infrav1.CustomerManagedKey && m.GCPMachine.Spec.RootDiskEncryptionKey.ManagedKey != nil {
if rootDiskEncryptionKey.KeyType == infrav1.CustomerManagedKey && rootDiskEncryptionKey.ManagedKey != nil {
additionalDisk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
KmsKeyName: m.GCPMachine.Spec.RootDiskEncryptionKey.ManagedKey.KMSKeyName,
KmsKeyName: rootDiskEncryptionKey.ManagedKey.KMSKeyName,
}
if m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount != nil {
additionalDisk.DiskEncryptionKey.KmsKeyServiceAccount = *m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount
if rootDiskEncryptionKey.KMSKeyServiceAccount != nil {
additionalDisk.DiskEncryptionKey.KmsKeyServiceAccount = *rootDiskEncryptionKey.KMSKeyServiceAccount
}
} else if m.GCPMachine.Spec.RootDiskEncryptionKey.KeyType == infrav1.CustomerSuppliedKey && m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey != nil {
} else if rootDiskEncryptionKey.KeyType == infrav1.CustomerSuppliedKey && rootDiskEncryptionKey.SuppliedKey != nil {
additionalDisk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
RawKey: string(m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey.RawKey),
RsaEncryptedKey: string(m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey.RSAEncryptedKey),
RawKey: string(rootDiskEncryptionKey.SuppliedKey.RawKey),
RsaEncryptedKey: string(rootDiskEncryptionKey.SuppliedKey.RSAEncryptedKey),
}
if m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount != nil {
additionalDisk.DiskEncryptionKey.KmsKeyServiceAccount = *m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount
if rootDiskEncryptionKey.KMSKeyServiceAccount != nil {
additionalDisk.DiskEncryptionKey.KmsKeyServiceAccount = *rootDiskEncryptionKey.KMSKeyServiceAccount
}
}
}
Expand Down Expand Up @@ -375,6 +375,8 @@ func (m *MachineScope) InstanceAdditionalMetadataSpec() *compute.Metadata {

// InstanceSpec returns instance spec.
func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
ctx := context.TODO()

instance := &compute.Instance{
Name: m.Name(),
Zone: m.Zone(),
Expand Down Expand Up @@ -461,7 +463,7 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
}

instance.Disks = append(instance.Disks, m.InstanceImageSpec())
instance.Disks = append(instance.Disks, m.InstanceAdditionalDiskSpec()...)
instance.Disks = append(instance.Disks, instanceAdditionalDiskSpec(ctx, m.GCPMachine.Spec.AdditionalDisks, m.GCPMachine.Spec.RootDiskEncryptionKey, m.Zone(), m.ResourceManagerTags())...)
instance.Metadata = m.InstanceAdditionalMetadataSpec()
instance.ServiceAccounts = append(instance.ServiceAccounts, m.InstanceServiceAccountsSpec())
instance.NetworkInterfaces = append(instance.NetworkInterfaces, m.InstanceNetworkInterfaceSpec())
Expand Down
5 changes: 4 additions & 1 deletion cloud/scope/machine_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scope

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -12,6 +13,8 @@ import (
// This test verifies that if a user selects "local-ssd"
// as a disk type then the MachineScope correctly detects it as such.
func TestMachineLocalSSDDiskType(t *testing.T) {
ctx := context.Background()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use the T.Context() here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow! I have been wanting this for a long time. I had missed that it was introduced - but it was introduced in 1.24.

I think we're pinned to go 1.23 in go.mod, but it gets auto-bumped if we move to CAPI 1.11 (#1509)

We can also bump the go toolchain in go.mod - do we want to do that?

(I totally agree that when we bump to go 1.24 we should use T.Context here and probably in many other places!)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree let's defer that after we get CAPI 1.11 and go 1.24 in


// Register the GCPMachine and GCPMachineList in a schema.
schema, err := infrav1.SchemeBuilder.Register(&infrav1.GCPMachine{}, &infrav1.GCPMachineList{}).Build()

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

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

// Get the local-ssd disk now.
Expand Down
2 changes: 0 additions & 2 deletions cloud/services/compute/instances/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ type instancegroupsInterface interface {
type Scope interface {
cloud.Machine
InstanceSpec(log logr.Logger) *compute.Instance
InstanceImageSpec() *compute.AttachedDisk
InstanceAdditionalDiskSpec() []*compute.AttachedDisk
}

// Service implements instances reconciler.
Expand Down