Skip to content

Commit

Permalink
Merge pull request #786 from silenteh/local-ssd-bugfix
Browse files Browse the repository at this point in the history
Allow to use local-ssd disk type
  • Loading branch information
k8s-ci-robot authored Dec 15, 2022
2 parents 63387e4 + 28f0044 commit 55bcb25
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.so
*.dylib
bin
cluster-api-provider-gcp

# Test binary, build with `go test -c`
*.test
Expand Down
2 changes: 1 addition & 1 deletion cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 74 additions & 0 deletions cloud/scope/machine_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 55bcb25

Please sign in to comment.