-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add name util functions for capi apibuilder
- Loading branch information
1 parent
71fae6a
commit fc932d6
Showing
3 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} | ||
} |