From 88e7b83c0a3f0f4f45345589d2a3e5ab5a1bed26 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 6 Sep 2023 01:01:45 -0500 Subject: [PATCH] OCM-3247 | feat: validate disk size Signed-off-by: marcolan018 --- go.mod | 1 + go.sum | 2 + pkg/machinepool/validations/disk_size.go | 53 +++++++++++++++++++ pkg/machinepool/validations/disk_size_test.go | 43 +++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 pkg/machinepool/validations/disk_size.go create mode 100644 pkg/machinepool/validations/disk_size_test.go diff --git a/go.mod b/go.mod index 7eed205..c309fb5 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/openshift-online/ocm-common go 1.19 require ( + github.com/hashicorp/go-version v1.6.0 github.com/onsi/ginkgo/v2 v2.11.0 github.com/onsi/gomega v1.27.8 ) diff --git a/go.sum b/go.sum index 9f1c206..7b9f3cd 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= +github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU= github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM= diff --git a/pkg/machinepool/validations/disk_size.go b/pkg/machinepool/validations/disk_size.go new file mode 100644 index 0000000..aeb3c6d --- /dev/null +++ b/pkg/machinepool/validations/disk_size.go @@ -0,0 +1,53 @@ +package validations + +import ( + "fmt" + "strings" + + semver "github.com/hashicorp/go-version" +) + +const ( + machinePoolRootAWSVolumeSizeMin = 128 + // The following constants are in the helper file because putting them in the models creates + // a circular dependency between the models and the helpers + // machinePoolRootVolumeSizeMaxBefore414 is the maximum size of the root volume before 4.14 + // 1 TiB - limit before 4.14 due to some filesystem growing issues + machinePoolRootVolumeSizeMaxBefore414 = 1024 + // machinePoolRootVolumeSizeMaxAsOf414 is the maximum size of the root volume as of 4.14 + // 16 TiB - limit as of 4.14 + machinePoolRootVolumeSizeMaxAsOf414 = 16384 +) + +// ValidateMachinePoolRootDiskSize validates the root volume size for a machine pool in AWS. +func ValidateMachinePoolRootDiskSize(version string, machinePoolRootVolumeSize int) error { + machinePoolRootVolumeSizeMax, err := getAWSVolumeMaxSize(version) + if err != nil { + return err + } + + if machinePoolRootVolumeSize < machinePoolRootAWSVolumeSizeMin || + machinePoolRootVolumeSize > machinePoolRootVolumeSizeMax { + return fmt.Errorf("Invalid root disk size: %d GiB. Must be between %d GiB and %d GiB.", + machinePoolRootVolumeSize, + machinePoolRootAWSVolumeSizeMin, + machinePoolRootVolumeSizeMax) + } + + return nil +} + +// getAWSVolumeMaxSize returns the maximum size of the root volume for a machine pool in AWS. +func getAWSVolumeMaxSize(version string) (int, error) { + version414, _ := semver.NewVersion("4.14.0") + currentVersion, err := semver.NewVersion(strings.Replace(version, "openshift-v", "", 1)) + if err != nil { + return 0, err + } + + if currentVersion.GreaterThanOrEqual(version414) { + return machinePoolRootVolumeSizeMaxAsOf414, nil + } + + return machinePoolRootVolumeSizeMaxBefore414, nil +} diff --git a/pkg/machinepool/validations/disk_size_test.go b/pkg/machinepool/validations/disk_size_test.go new file mode 100644 index 0000000..172f000 --- /dev/null +++ b/pkg/machinepool/validations/disk_size_test.go @@ -0,0 +1,43 @@ +package validations + +import ( + . "github.com/onsi/ginkgo/v2/dsl/table" + . "github.com/onsi/gomega" +) + +var _ = DescribeTable("getAWSVolumeMaxSize", + func(testSpec struct { + name string + args string + want int + err error + }) { + got, err := getAWSVolumeMaxSize(testSpec.args) + Expect(err).ToNot(HaveOccurred()) + Expect(got).To(Equal(testSpec.want), "got %v, want %v", got, testSpec.want) + }, + Entry("valid version for 4.11", struct { + name string + args string + want int + err error + }{"valid version for 4.11", "4.11", 1024, nil}), + Entry("valid version for 4.13", struct { + name string + args string + want int + err error + }{"valid version for 4.13", "4.13", 1024, nil}), + Entry("invalid version for 4.14", struct { + name string + args string + want int + err error + }{"invalid version for 4.14", "4.14", 16384, nil}), + Entry("invalid version for 4.15", struct { + name string + args string + want int + err error + }{"invalid version for 4.15", "4.15", 16384, nil}), +)