From fc932d6dd7c2d41ea71a1948edca20aadb431aae Mon Sep 17 00:00:00 2001 From: Jiayi Wang Date: Tue, 3 May 2022 12:05:25 -0400 Subject: [PATCH] Add name util functions for capi apibuilder --- pkg/clusterapi/apibuilder.go | 4 +- pkg/clusterapi/name.go | 42 ++++++++++++ pkg/clusterapi/name_test.go | 121 +++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 pkg/clusterapi/name.go create mode 100644 pkg/clusterapi/name_test.go diff --git a/pkg/clusterapi/apibuilder.go b/pkg/clusterapi/apibuilder.go index fe240f83ead5..fabb7ef8fd4d 100644 --- a/pkg/clusterapi/apibuilder.go +++ b/pkg/clusterapi/apibuilder.go @@ -112,7 +112,7 @@ func KubeadmControlPlane(clusterSpec *cluster.Spec, infrastructureObject APIObje Kind: kubeadmControlPlaneKind, }, ObjectMeta: metav1.ObjectMeta{ - Name: clusterSpec.Cluster.GetName(), + Name: KubeadmControlPlaneName(clusterSpec), Namespace: constants.EksaSystemNamespace, }, Spec: controlplanev1.KubeadmControlPlaneSpec{ @@ -225,7 +225,7 @@ func MachineDeployment(clusterSpec *cluster.Spec, workerNodeGroupConfig v1alpha1 Kind: machineDeploymentKind, }, ObjectMeta: metav1.ObjectMeta{ - Name: workerNodeGroupConfig.Name, + Name: MachineDeploymentName(workerNodeGroupConfig), Namespace: constants.EksaSystemNamespace, Labels: clusterLabels(clusterName), }, diff --git a/pkg/clusterapi/name.go b/pkg/clusterapi/name.go new file mode 100644 index 000000000000..b807b3aa1816 --- /dev/null +++ b/pkg/clusterapi/name.go @@ -0,0 +1,42 @@ +package clusterapi + +import ( + "fmt" + "regexp" + "strconv" + + "github.com/aws/eks-anywhere/pkg/api/v1alpha1" + "github.com/aws/eks-anywhere/pkg/cluster" +) + +var nameRegex = regexp.MustCompile(`(.*?)(-)(\d+)$`) + +func IncrementName(name string) (string, error) { + match := nameRegex.FindStringSubmatch(name) + if match == nil { + return "", fmt.Errorf(`invalid format of name [name=%s]. Name has to follow regex pattern "(-)(\d+)$", e.g. machinetemplate-cp-1`, name) + } + + n, err := strconv.Atoi(match[3]) + if err != nil { + return "", fmt.Errorf("converting object suffix to int: %v", err) + } + + return ObjectName(match[1], n+1), nil +} + +func ObjectName(baseName string, version int) string { + return fmt.Sprintf("%s-%d", baseName, version) +} + +func DefaultObjectName(baseName string) string { + return ObjectName(baseName, 1) +} + +func KubeadmControlPlaneName(clusterSpec *cluster.Spec) string { + return clusterSpec.Cluster.GetName() +} + +func MachineDeploymentName(workerNodeGroupConfig v1alpha1.WorkerNodeGroupConfiguration) string { + return workerNodeGroupConfig.Name +} diff --git a/pkg/clusterapi/name_test.go b/pkg/clusterapi/name_test.go new file mode 100644 index 000000000000..c4904b65cbad --- /dev/null +++ b/pkg/clusterapi/name_test.go @@ -0,0 +1,121 @@ +package clusterapi_test + +import ( + "testing" + + . "github.com/onsi/gomega" + + "github.com/aws/eks-anywhere/pkg/clusterapi" +) + +func TestIncrementName(t *testing.T) { + tests := []struct { + name string + oldName string + want string + wantErr string + }{ + { + name: "valid", + oldName: "cluster-1", + want: "cluster-2", + wantErr: "", + }, + { + name: "invalid format", + oldName: "cluster-1a", + want: "", + wantErr: "invalid format of name", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + got, err := clusterapi.IncrementName(tt.oldName) + if tt.wantErr == "" { + g.Expect(err).To(Succeed()) + g.Expect(got).To(Equal(tt.want)) + } else { + g.Expect(err).To(MatchError(ContainSubstring(tt.wantErr))) + } + }) + } +} + +func TestObjectName(t *testing.T) { + tests := []struct { + name string + base string + version int + want string + }{ + { + name: "cluster-1", + base: "cluster", + version: 1, + want: "cluster-1", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + g.Expect(clusterapi.ObjectName(tt.base, tt.version)).To(Equal(tt.want)) + }) + } +} + +func TestDefaultObjectName(t *testing.T) { + tests := []struct { + name string + base string + want string + }{ + { + name: "cluster-1", + base: "cluster", + want: "cluster-1", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + g.Expect(clusterapi.DefaultObjectName(tt.base)).To(Equal(tt.want)) + }) + } +} + +func TestKubeadmControlPlaneName(t *testing.T) { + tests := []struct { + name string + want string + }{ + { + name: "test cluster", + want: "test-cluster", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := newApiBuilerTest(t) + g.Expect(clusterapi.KubeadmControlPlaneName(g.clusterSpec)).To(Equal(tt.want)) + }) + } +} + +func TestMachineDeploymentName(t *testing.T) { + tests := []struct { + name string + want string + }{ + { + name: "wng 1", + want: "wng-1", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := newApiBuilerTest(t) + g.Expect(clusterapi.MachineDeploymentName(*g.workerNodeGroupConfig)).To(Equal(tt.want)) + }) + } +}