Skip to content

Commit 9543b19

Browse files
CR-13046 cluster name bug (#485)
* fix cluster name validation + tests * small test change * fixes * small fix + bump
1 parent 82cfcd1 commit 9543b19

File tree

5 files changed

+67
-30
lines changed

5 files changed

+67
-30
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.430
1+
VERSION=v0.0.431
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/cluster.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,43 +193,57 @@ func setClusterName(ctx context.Context, opts *ClusterAddOptions) error {
193193
}
194194

195195
var err error
196-
sanitizedName := sanitizeClusterName(opts.kubeContext)
196+
sanitizedName, err := sanitizeClusterName(opts.kubeContext)
197+
if err != nil {
198+
return err
199+
}
200+
197201
opts.clusterName, err = ensureNoClusterNameDuplicates(ctx, sanitizedName, opts.runtimeName)
198202

199203
return err
200204
}
201205

202206
func validateClusterName(name string) error {
203-
maxDNSNameLength := 253
204-
if len(name) > maxDNSNameLength {
205-
return fmt.Errorf("cluster name can contain no more than 253 characters")
207+
maxNameLength := 63
208+
if len(name) > maxNameLength {
209+
return fmt.Errorf("cluster name can contain no more than 63 characters")
206210
}
207211

208-
match, err := regexp.MatchString("^[a-z\\d]([-a-z\\d\\.]{0,251}[a-z\\d])?$", name)
212+
match, err := regexp.MatchString("^[a-z]([-a-z\\d]{0,61}[a-z\\d])?$", name)
209213
if err != nil {
210214
return err
211215
}
212216

213217
if !match {
214-
return fmt.Errorf("cluster name must be according to k8s resource naming rules")
218+
return fmt.Errorf("cluster name must be according to k8s RFC 1035 label names rules")
215219
}
216220

217221
return nil
218222
}
219223

220-
// copied from https://github.com/argoproj/argo-cd/blob/master/applicationset/generators/cluster.go#L214
221-
func sanitizeClusterName(name string) string {
222-
invalidDNSNameChars := regexp.MustCompile("[^-a-z0-9.]")
223-
maxDNSNameLength := 253
224+
// partially copied from https://github.com/argoproj/argo-cd/blob/master/applicationset/generators/cluster.go#L214
225+
func sanitizeClusterName(name string) (string, error) {
226+
nameKeeper := name
227+
invalidNameChars := regexp.MustCompile("[^-a-z0-9]")
228+
maxNameLength := 63
224229

225230
name = strings.ToLower(name)
226-
name = invalidDNSNameChars.ReplaceAllString(name, "-")
231+
name = invalidNameChars.ReplaceAllString(name, "-")
227232
// saving space for 2 chars in case a cluster with the sanitized name already exists
228-
if len(name) > (maxDNSNameLength - 2) {
229-
name = name[:(maxDNSNameLength - 2)]
233+
if len(name) > (maxNameLength - 2) {
234+
name = name[:(maxNameLength - 2)]
230235
}
231236

232-
return strings.Trim(name, "-.")
237+
name = strings.Trim(name, "-.")
238+
239+
beginsWithNum := regexp.MustCompile(`^\d+`)
240+
name = beginsWithNum.ReplaceAllString(name, "")
241+
242+
if name == "" {
243+
return "", fmt.Errorf("failed sanitizing cluster name \"%s\". please use --name flag manually", nameKeeper)
244+
}
245+
246+
return name, nil
233247
}
234248

235249
func ensureNoClusterNameDuplicates(ctx context.Context, name string, runtimeName string) (string, error) {

cmd/commands/cluster_test.go

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,41 @@ func Test_sanitizeClusterName(t *testing.T) {
7474
name string
7575
args args
7676
want string
77+
wantErr bool
7778
}{
7879
{
7980
name: "should return sanitized string",
8081
args: args{
81-
name: "^-.test!@-:cluster&*`;')test.cluster(-12_3=+::±§.",
82+
name: "^-.Test!@-:cluster&*`;')test.cluster(-12_3=+::±§.",
8283
},
83-
want: "test----cluster------test.cluster--12-3",
84+
want: "test----cluster------test-cluster--12-3",
85+
wantErr: false,
86+
},
87+
{
88+
name: "should return sanitized string",
89+
args: args{
90+
name: "^-.123test!@-:cluster&*`;')test.cluster(-12_3=+::±§.",
91+
},
92+
want: "test----cluster------test-cluster--12-3",
93+
wantErr: false,
94+
},
95+
{
96+
name: "should return error of sanitization failed",
97+
args: args{
98+
name: "12345",
99+
},
100+
want: "",
101+
wantErr: true,
84102
},
85103
}
86104
for _, tt := range tests {
87105
t.Run(tt.name, func(t *testing.T) {
88-
if got := sanitizeClusterName(tt.args.name); got != tt.want {
106+
got, err := sanitizeClusterName(tt.args.name)
107+
108+
if (err != nil) != tt.wantErr {
109+
t.Errorf("sanitizeClusterName() error = %v, wantErr %v", err, tt.wantErr)
110+
}
111+
if got != tt.want {
89112
t.Errorf("sanitizeClusterName() = %v, want %v", got, tt.want)
90113
}
91114
})
@@ -104,35 +127,35 @@ func Test_validateClusterName(t *testing.T) {
104127
{
105128
name: "name should be valid",
106129
args: args{
107-
name: "1test-cluster.test.cluster123z",
130+
name: "test-cluster-123",
108131
},
109132
wantErr: false,
110133
},
111134
{
112-
name: "name should not be valid",
135+
name: "name should not be valid (contains uppercase)",
113136
args: args{
114-
name: ".test-cluster",
137+
name: "Test-cluster",
115138
},
116139
wantErr: true,
117140
},
118141
{
119-
name: "name should not be valid",
142+
name: "name should not be valid (contains invalid chars)",
120143
args: args{
121-
name: "test-cluster.",
144+
name: "test-cluster:test/cluster.123#",
122145
},
123146
wantErr: true,
124147
},
125148
{
126-
name: "name should not be valid",
149+
name: "name should not be valid (begins with numeric char)",
127150
args: args{
128-
name: "Test-cluster",
151+
name: "2test-cluster",
129152
},
130153
wantErr: true,
131154
},
132155
{
133-
name: "name should not be valid",
156+
name: "name should not be valid (too long)",
134157
args: args{
135-
name: "test-cluster:test/cluster",
158+
name: "this-cluster-name-is-too-long-1-this-cluster-name-is-too-long-1-this-cluster-name-is-too-long-1-123",
136159
},
137160
wantErr: true,
138161
},

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.430/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.431/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.430/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.431/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.1
8-
version: 0.0.430
8+
version: 0.0.431
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

0 commit comments

Comments
 (0)