From 60796cb386570e44ad98fe2dcc300d26c1252b28 Mon Sep 17 00:00:00 2001 From: Nathaniel Graham Date: Fri, 3 May 2024 10:33:43 -0600 Subject: [PATCH] Added hubsize annotation (#687) * Added hubsize annotation Signed-off-by: Nathaniel Graham * Added unit test for getHubSize Signed-off-by: Nathaniel Graham * removed duplicate test case Signed-off-by: Nathaniel Graham --------- Signed-off-by: Nathaniel Graham --- api/v1/multiclusterengine_methods_test.go | 56 +++++++++---------- api/v1/multiclusterengine_types.go | 15 +++--- pkg/rendering/renderer.go | 6 ++- pkg/utils/annotations.go | 17 ++++++ pkg/utils/utils_test.go | 65 +++++++++++++++++++++++ 5 files changed, 123 insertions(+), 36 deletions(-) diff --git a/api/v1/multiclusterengine_methods_test.go b/api/v1/multiclusterengine_methods_test.go index b27f98b3..6d5e5753 100644 --- a/api/v1/multiclusterengine_methods_test.go +++ b/api/v1/multiclusterengine_methods_test.go @@ -1,7 +1,6 @@ package v1_test import ( - "encoding/json" "testing" . "github.com/onsi/ginkgo/v2" @@ -174,30 +173,31 @@ func TestGetLegacyServiceMonitorName(t *testing.T) { } } -func TestHubSizeMarshal(t *testing.T) { - tests := []struct { - name string - yamlstring string - want api.HubSize - }{ - { - name: "Marshals when setting hubSize to Large", - yamlstring: `{"hubSize": "Large"}`, - 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) - } - }) - } -} +// TODO: put this back later +// func TestHubSizeMarshal(t *testing.T) { +// tests := []struct { +// name string +// yamlstring string +// want api.HubSize +// }{ +// { +// name: "Marshals when setting hubSize to Large", +// yamlstring: `{"hubSize": "Large"}`, +// 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 ce187cd9..6ec9b254 100644 --- a/api/v1/multiclusterengine_types.go +++ b/api/v1/multiclusterengine_types.go @@ -59,13 +59,14 @@ type MultiClusterEngineSpec struct { // Set the nodeselectors NodeSelector map[string]string `json:"nodeSelector,omitempty"` - // The resource allocation bucket for this hub to use. - // Small, Medium, Large, XLarge]. Defaults to Small if not specified. - //+kubebuilder:validation:Enum:=Small;Medium;Large;XLarge - //+kubebuilder:default:=Small - //+kubebuilder:validation:Type:=string - //+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Hub Size",xDescriptors={"urn:alm:descriptor:com.tectonic.ui:hidden"} - HubSize HubSize `json:"hubSize,omitempty"` + // TODO: put this back later + // // The resource allocation bucket for this hub to use. + // // Small, Medium, Large, XLarge]. Defaults to Small if not specified. + // //+kubebuilder:validation:Enum:=Small;Medium;Large;XLarge + // //+kubebuilder:default:=Small + // //+kubebuilder:validation:Type:=string + // //+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"} diff --git a/pkg/rendering/renderer.go b/pkg/rendering/renderer.go index d47abce6..60d15c65 100644 --- a/pkg/rendering/renderer.go +++ b/pkg/rendering/renderer.go @@ -314,7 +314,11 @@ func injectValuesOverrides(values *Values, backplaneConfig *v1.MultiClusterEngin values.Global.Namespace = backplaneConfig.Spec.TargetNamespace - values.Global.HubSize = backplaneConfig.Spec.HubSize + // TODO: remove this after backplaneConfig.Spec.HubSize is back + values.Global.HubSize = utils.GetHubSize(backplaneConfig) + + // TODO: put this back later + // values.Global.HubSize = backplaneConfig.Spec.HubSize values.Global.PullSecret = backplaneConfig.Spec.ImagePullSecret diff --git a/pkg/utils/annotations.go b/pkg/utils/annotations.go index d980ecf3..66401ab2 100644 --- a/pkg/utils/annotations.go +++ b/pkg/utils/annotations.go @@ -63,6 +63,12 @@ var ( containing resource template overrides. */ AnnotationTemplateOverridesCM = "installer.multicluster.openshift.io/template-override-configmap" + + /* + AnnotationHubSize is an annotation used in multiclusterhub to specify a hub size that can be + used by other components. MCE can use this standalone or inherit it from MCH + */ + AnnotationHubSize = "installer.multicluster.openshift.io/hub-size" ) /* @@ -73,6 +79,17 @@ func IsPaused(instance *backplanev1.MultiClusterEngine) bool { return IsAnnotationTrue(instance, AnnotationMCEPause) || IsAnnotationTrue(instance, DeprecatedAnnotationMCEPause) } +/* +GetHubSize gets the current hubsize, returning "Small" as default if the annotation is not found. +*/ +func GetHubSize(instance *backplanev1.MultiClusterEngine) backplanev1.HubSize { + hubsize := getAnnotation(instance, AnnotationHubSize) + if hubsize != "" { + return backplanev1.HubSize(hubsize) + } + return backplanev1.Small +} + /* IsAnnotationTrue checks if a specific annotation key in the given instance is set to "true". */ diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 193dd1cd..2259f864 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -97,6 +97,71 @@ func Test_deduplicate(t *testing.T) { } } +func TestGetHubSize(t *testing.T) { + tests := []struct { + name string + mce *backplanev1.MultiClusterEngine + want backplanev1.HubSize + }{ + { + name: "get default", + mce: &backplanev1.MultiClusterEngine{}, + want: backplanev1.Small, + }, + { + name: "set hubsize Small", + mce: &backplanev1.MultiClusterEngine{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + AnnotationHubSize: "Small", + }, + }, + }, + want: backplanev1.Small, + }, + { + name: "set hubsize Medium", + mce: &backplanev1.MultiClusterEngine{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + AnnotationHubSize: "Medium", + }, + }, + }, + want: backplanev1.Medium, + }, + { + name: "set hubsize Large", + mce: &backplanev1.MultiClusterEngine{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + AnnotationHubSize: "Large", + }, + }, + }, + want: backplanev1.Large, + }, + { + name: "set hubsize XLarge", + mce: &backplanev1.MultiClusterEngine{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + AnnotationHubSize: "XLarge", + }, + }, + }, + want: backplanev1.XLarge, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := GetHubSize(tt.mce); got != tt.want { + t.Errorf("GetHubSize() = %v, want %v", got, tt.want) + } + }) + } +} + func TestGetHubType(t *testing.T) { tests := []struct { name string