Skip to content

Commit

Permalink
Marshal hubSize from MCE spec (#654)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathaniel Graham <[email protected]>
  • Loading branch information
ngraham20 authored Mar 29, 2024
1 parent 27e1364 commit 3310bb1
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
25 changes: 24 additions & 1 deletion api/v1/multiclusterengine_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ limitations under the License.

package v1

import "fmt"
import (
"encoding/json"
"fmt"
)

const (
AssistedService = "assisted-service"
Expand Down Expand Up @@ -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.
Expand Down
63 changes: 63 additions & 0 deletions api/v1/multiclusterengine_methods_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1_test

import (
"encoding/json"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -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)
}
})
}
}
30 changes: 30 additions & 0 deletions api/v1/multiclusterengine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3310bb1

Please sign in to comment.