From 3310bb189bcc5a79040a9e2b73c3890385a678dc Mon Sep 17 00:00:00 2001 From: Nathaniel Graham Date: Fri, 29 Mar 2024 16:44:02 -0600 Subject: [PATCH] Marshal hubSize from MCE spec (#654) Signed-off-by: Nathaniel Graham --- api/v1/multiclusterengine_methods.go | 25 +++++++- api/v1/multiclusterengine_methods_test.go | 63 +++++++++++++++++++ api/v1/multiclusterengine_types.go | 30 +++++++++ ...ster.openshift.io_multiclusterengines.yaml | 11 ++++ 4 files changed, 128 insertions(+), 1 deletion(-) diff --git a/api/v1/multiclusterengine_methods.go b/api/v1/multiclusterengine_methods.go index 1637e385..70268733 100644 --- a/api/v1/multiclusterengine_methods.go +++ b/api/v1/multiclusterengine_methods.go @@ -18,7 +18,10 @@ limitations under the License. package v1 -import "fmt" +import ( + "encoding/json" + "fmt" +) const ( AssistedService = "assisted-service" @@ -212,6 +215,26 @@ func IsInHostedMode(mce *MultiClusterEngine) bool { return false } +func (h HubSize) String() string { + return HubSizeStrings[h] +} + +func (h *HubSize) UnmarshalJSON(b []byte) error { + fmt.Println("Unmarshaling JSON is occuring") + var hubsize string + if err := json.Unmarshal(b, &hubsize); err != nil { + return err + } + + var exists bool + *h, exists = HubSizeFromString[hubsize] + + if !exists { + return fmt.Errorf("key %v does not exist in map", hubsize) + } + return nil +} + /* GetLegacyPrometheusKind returns a list of legacy kind resources that are required to be removed before updating to ACM 2.9 and later. diff --git a/api/v1/multiclusterengine_methods_test.go b/api/v1/multiclusterengine_methods_test.go index 42cfd98d..b1b48a06 100644 --- a/api/v1/multiclusterengine_methods_test.go +++ b/api/v1/multiclusterengine_methods_test.go @@ -1,6 +1,7 @@ package v1_test import ( + "encoding/json" "testing" . "github.com/onsi/ginkgo/v2" @@ -172,3 +173,65 @@ func TestGetLegacyServiceMonitorName(t *testing.T) { }) } } + +func TestHubSizeDefault(t *testing.T) { + tests := []struct { + name string + spec api.MultiClusterEngineSpec + want api.HubSize + }{ + { + name: "Default is Medium", + spec: api.MultiClusterEngineSpec{}, + want: api.Medium, + }, + { + name: "Override Default with Large", + spec: api.MultiClusterEngineSpec{ + HubSize: api.Large, + }, + want: api.Large, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + hsize := tt.spec.HubSize + if hsize != tt.want { + t.Errorf("HubSize: %v, want: %v", hsize, tt.want) + } + }) + } +} + +func TestHubSizeMarshal(t *testing.T) { + tests := []struct { + name string + yamlstring string + want api.HubSize + }{ + { + name: "Marshal defaults to M", + yamlstring: `{}`, + want: api.Medium, + }, + { + name: "Marshals when overriding default with large", + yamlstring: `{"hubSize": "L"}`, + want: api.Large, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var out api.MultiClusterEngineSpec + t.Logf("spec before marshal: %v\n", out) + err := json.Unmarshal([]byte([]byte(tt.yamlstring)), &out) + t.Logf("spec after marshal: %v\n", out) + if err != nil { + t.Errorf("Unable to unmarshal yaml string: %v. %v", tt.yamlstring, err) + } + if out.HubSize != tt.want { + t.Errorf("Hubsize not desired. HubSize: %v, want: %v", out.HubSize, tt.want) + } + }) + } +} diff --git a/api/v1/multiclusterengine_types.go b/api/v1/multiclusterengine_types.go index d6bfdbc4..50d7648f 100644 --- a/api/v1/multiclusterengine_types.go +++ b/api/v1/multiclusterengine_types.go @@ -26,6 +26,31 @@ import ( // AvailabilityType ... type AvailabilityType string +type HubSize uint8 + +// Putting medium first here defaults it to Medium +const ( + Medium = iota + Small + Large + ExtraLarge +) + +var ( + HubSizeStrings = map[HubSize]string{ + Small: "S", + Medium: "M", + Large: "L", + ExtraLarge: "XL", + } + HubSizeFromString = map[string]HubSize{ + "S": Small, + "M": Medium, + "L": Large, + "XL": ExtraLarge, + } +) + // DeploymentMode type DeploymentMode string @@ -50,6 +75,11 @@ type MultiClusterEngineSpec struct { // Set the nodeselectors NodeSelector map[string]string `json:"nodeSelector,omitempty"` + // The resource allocation bucket for this hub to use. + // [S (Small), M (Medium), L (Large), XL (Extra Large)]. Defaults to (M)edium if not specified. + //+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Hub Size",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:hidden"} + HubSize HubSize `json:"hubSize,omitempty"` + // Override pull secret for accessing MultiClusterEngine operand and endpoint images //+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Image Pull Secret",xDescriptors={"urn:alm:descriptor:io.kubernetes:Secret","urn:alm:descriptor:com.tectonic.ui:advanced"} ImagePullSecret string `json:"imagePullSecret,omitempty"` diff --git a/config/crd/bases/multicluster.openshift.io_multiclusterengines.yaml b/config/crd/bases/multicluster.openshift.io_multiclusterengines.yaml index 2157bc98..2ba68870 100644 --- a/config/crd/bases/multicluster.openshift.io_multiclusterengines.yaml +++ b/config/crd/bases/multicluster.openshift.io_multiclusterengines.yaml @@ -53,6 +53,17 @@ spec: description: 'Specifies deployment replication for improved availability. Options are: Basic and High (default)' type: string + hubSize: + description: The resource allocation bucket for this hub to use. + [S (Small), M (Medium), L (Large), XL (Extra Large)]. Defaults to + (M)edium if not specified. + type: string + enum: + - S + - M + - L + - XL + default: M imagePullSecret: description: Override pull secret for accessing MultiClusterEngine operand and endpoint images