Skip to content

Commit

Permalink
Add name util functions for capi apibuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayiwang7 committed May 3, 2022
1 parent 71fae6a commit fc932d6
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/clusterapi/apibuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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),
},
Expand Down
42 changes: 42 additions & 0 deletions pkg/clusterapi/name.go
Original file line number Diff line number Diff line change
@@ -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
}
121 changes: 121 additions & 0 deletions pkg/clusterapi/name_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}
}

0 comments on commit fc932d6

Please sign in to comment.