Skip to content

Commit

Permalink
Merge pull request #12 from marcolan018/ocm-3247
Browse files Browse the repository at this point in the history
OCM-3247 | feat: validate disk size
  • Loading branch information
ciaranRoche authored Sep 6, 2023
2 parents 560af39 + 88e7b83 commit f1f61c8
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
53 changes: 53 additions & 0 deletions pkg/machinepool/validations/disk_size.go
Original file line number Diff line number Diff line change
@@ -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
}
43 changes: 43 additions & 0 deletions pkg/machinepool/validations/disk_size_test.go
Original file line number Diff line number Diff line change
@@ -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}),
)

0 comments on commit f1f61c8

Please sign in to comment.