diff --git a/.gitignore b/.gitignore index 41d804ecf..98b1337af 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.so *.dylib bin +cluster-api-provider-gcp # Test binary, build with `go test -c` *.test diff --git a/cloud/scope/machine.go b/cloud/scope/machine.go index 0705421ba..2cdfa1fa1 100644 --- a/cloud/scope/machine.go +++ b/cloud/scope/machine.go @@ -254,7 +254,7 @@ func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk { DiskType: path.Join("zones", m.Zone(), "diskTypes", string(*disk.DeviceType)), }, } - if additionalDisk.InitializeParams.DiskType == string(infrav1.LocalSsdDiskType) { + if strings.HasSuffix(additionalDisk.InitializeParams.DiskType, string(infrav1.LocalSsdDiskType)) { additionalDisk.Type = "SCRATCH" // Default is PERSISTENT. // Override the Disk size additionalDisk.InitializeParams.DiskSizeGb = 375 diff --git a/cloud/scope/machine_test.go b/cloud/scope/machine_test.go new file mode 100644 index 000000000..333a6605d --- /dev/null +++ b/cloud/scope/machine_test.go @@ -0,0 +1,74 @@ +package scope + +import ( + "testing" + + "github.com/stretchr/testify/assert" + infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1" + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +// 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) { + // Register the GCPMachine and GCPMachineList in a schema. + schema, err := infrav1.SchemeBuilder.Register(&infrav1.GCPMachine{}, &infrav1.GCPMachineList{}).Build() + + // Make sure no errors were triggered. + assert.Nil(t, err) + + // Create a controller fake client. + // It's best to use envtest but in this case we are not really using the client + // just passing it as parameter to the NewMachineScope to test the dik functionality. + testClient := fake.NewClientBuilder().WithScheme(schema).Build() + + // New test machine, needed as parameter. + failureDomain := "example.com" + testMachine := clusterv1.Machine{ + Spec: clusterv1.MachineSpec{ + FailureDomain: &failureDomain, + }, + } + + // GCPMachine for mocking the disks. + // Set the disk to local-ssd. + diskType := infrav1.LocalSsdDiskType + diskSize := int64(100) + + testGCPMachine := infrav1.GCPMachine{ + Spec: infrav1.GCPMachineSpec{ + AdditionalDisks: []infrav1.AttachedDiskSpec{ + { + DeviceType: &diskType, + Size: &diskSize, + }, + }, + }, + } + + // Finally put together the parameters. + testScopeParams := MachineScopeParams{ + Client: testClient, + Machine: &testMachine, + GCPMachine: &testGCPMachine, + } + + // Create the scope + testMachineScope, err := NewMachineScope(testScopeParams) + + // Make sure the machine scope is create correctly. + assert.Nil(t, err) + assert.NotNil(t, testMachineScope) + + // Now make sure the local-ssd disk type is detected as SCRATCH. + diskSpec := testMachineScope.InstanceAdditionalDiskSpec() + assert.NotEmpty(t, diskSpec) + + // Get the local-ssd disk now. + localSSDTest := diskSpec[0] + assert.True(t, localSSDTest.AutoDelete) + assert.Equal(t, "SCRATCH", localSSDTest.Type) + assert.Equal(t, "NVME", localSSDTest.Interface) + assert.Equal(t, int64(375), localSSDTest.InitializeParams.DiskSizeGb) +} diff --git a/go.mod b/go.mod index 039cf17be..4b94ae180 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,8 @@ require ( github.com/onsi/gomega v1.24.1 github.com/pkg/errors v0.9.1 github.com/spf13/pflag v1.0.5 - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 + github.com/stretchr/testify v1.8.1 + golang.org/x/mod v0.7.0 golang.org/x/net v0.4.0 google.golang.org/api v0.104.0 k8s.io/api v0.25.0 @@ -88,6 +89,7 @@ require ( github.com/opencontainers/image-spec v1.0.2 // indirect github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.13.0 // indirect github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/common v0.37.0 // indirect diff --git a/go.sum b/go.sum index 39d520665..e52e4997f 100644 --- a/go.sum +++ b/go.sum @@ -623,8 +623,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA= +golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=