Skip to content

Commit 1f5ebf8

Browse files
authored
Update create job name validation to Kuberentes DNS to allow '.' in (#23)
names Signed-off-by: joshvanl <[email protected]>
1 parent 5eb0c3e commit 1f5ebf8

File tree

5 files changed

+51
-17
lines changed

5 files changed

+51
-17
lines changed

api.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"google.golang.org/protobuf/proto"
15+
"k8s.io/apimachinery/pkg/util/validation"
1516

1617
"github.com/diagridio/go-etcd-cron/api"
1718
)
@@ -107,8 +108,10 @@ func validateName(name string) error {
107108
return errors.New("job name cannot be empty")
108109
}
109110

110-
if strings.Contains(name, "/") || strings.Contains(name, ".") {
111-
return fmt.Errorf("job name cannot contain '/' or '.': %s", name)
111+
for _, segment := range strings.Split(strings.ToLower(name), "||") {
112+
if errs := validation.IsDNS1123Subdomain(segment); len(errs) > 0 {
113+
return fmt.Errorf("job name is invalid %q: %s", name, strings.Join(errs, ", "))
114+
}
112115
}
113116

114117
return nil

api_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func Test_validateName(t *testing.T) {
446446
},
447447
{
448448
name: "fo.o",
449-
expErr: true,
449+
expErr: false,
450450
},
451451
{
452452
name: "fo...o",
@@ -456,6 +456,30 @@ func Test_validateName(t *testing.T) {
456456
name: "valid",
457457
expErr: false,
458458
},
459+
{
460+
name: "||",
461+
expErr: true,
462+
},
463+
{
464+
name: "foo||",
465+
expErr: true,
466+
},
467+
{
468+
name: "||foo",
469+
expErr: true,
470+
},
471+
{
472+
name: "foo||foo",
473+
expErr: false,
474+
},
475+
{
476+
name: "foo.bar||foo",
477+
expErr: false,
478+
},
479+
{
480+
name: "foo.BAR||foo",
481+
expErr: false,
482+
},
459483
}
460484

461485
for _, test := range tests {

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
go.etcd.io/etcd/server/v3 v3.5.13
1414
go.uber.org/zap v1.26.0
1515
google.golang.org/protobuf v1.33.0
16+
k8s.io/apimachinery v0.26.9
1617
k8s.io/utils v0.0.0-20240310230437-4693a0247e57
1718
)
1819

@@ -35,10 +36,10 @@ require (
3536
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
3637
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
3738
github.com/jonboulle/clockwork v0.2.2 // indirect
38-
github.com/json-iterator/go v1.1.11 // indirect
39+
github.com/json-iterator/go v1.1.12 // indirect
3940
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
4041
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
41-
github.com/modern-go/reflect2 v1.0.1 // indirect
42+
github.com/modern-go/reflect2 v1.0.2 // indirect
4243
github.com/pmezard/go-difflib v1.0.0 // indirect
4344
github.com/prometheus/client_golang v1.11.1 // indirect
4445
github.com/prometheus/client_model v0.2.0 // indirect
@@ -74,5 +75,5 @@ require (
7475
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
7576
gopkg.in/yaml.v2 v2.4.0 // indirect
7677
gopkg.in/yaml.v3 v3.0.1 // indirect
77-
sigs.k8s.io/yaml v1.2.0 // indirect
78+
sigs.k8s.io/yaml v1.3.0 // indirect
7879
)

go.sum

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUB
111111
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
112112
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
113113
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
114-
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
115114
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
115+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
116+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
116117
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
117118
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
118119
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
@@ -133,8 +134,9 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
133134
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
134135
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
135136
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
136-
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
137137
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
138+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
139+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
138140
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
139141
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
140142
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
@@ -351,7 +353,6 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
351353
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
352354
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
353355
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
354-
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
355356
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
356357
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
357358
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
@@ -360,7 +361,9 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
360361
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
361362
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
362363
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
364+
k8s.io/apimachinery v0.26.9 h1:5yAV9cFR7Z4gIorKcAjWnx4uxtxiFsERwq4Pvmx0CCg=
365+
k8s.io/apimachinery v0.26.9/go.mod h1:qYzLkrQ9lhrZRh0jNKo2cfvf/R1/kQONnSiyB7NUJU0=
363366
k8s.io/utils v0.0.0-20240310230437-4693a0247e57 h1:gbqbevonBh57eILzModw6mrkbwM0gQBEuevE/AaBsHY=
364367
k8s.io/utils v0.0.0-20240310230437-4693a0247e57/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
365-
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
366-
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
368+
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
369+
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=

gomod2nix.toml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ schema = 3
6565
version = "v0.2.2"
6666
hash = "sha256-ryvMsRMd1CeuYyyLTfGRWb36AbZyxtomPBEB7hzrrqE="
6767
[mod."github.com/json-iterator/go"]
68-
version = "v1.1.11"
69-
hash = "sha256-/rcZZy458AsEqrSgtneGghVUpHNXJfT66QQHI6hyERU="
68+
version = "v1.1.12"
69+
hash = "sha256-To8A0h+lbfZ/6zM+2PpRpY3+L6725OPC66lffq6fUoM="
7070
[mod."github.com/matttproud/golang_protobuf_extensions"]
7171
version = "v1.0.1"
7272
hash = "sha256-ystDNStxR90j4CK+AMcEQ5oyYFRgWoGdvWlS0XQMDLQ="
7373
[mod."github.com/modern-go/concurrent"]
7474
version = "v0.0.0-20180306012644-bacd9c7ef1dd"
7575
hash = "sha256-OTySieAgPWR4oJnlohaFTeK1tRaVp/b0d1rYY8xKMzo="
7676
[mod."github.com/modern-go/reflect2"]
77-
version = "v1.0.1"
78-
hash = "sha256-5D1HGVBc/REwPVdlPYcXsbZM80OIh7V5uiyKAbMA5qo="
77+
version = "v1.0.2"
78+
hash = "sha256-+W9EIW7okXIXjWEgOaMh58eLvBZ7OshW2EhaIpNLSBU="
7979
[mod."github.com/pmezard/go-difflib"]
8080
version = "v1.0.0"
8181
hash = "sha256-/FtmHnaGjdvEIKAJtrUfEhV7EVo5A/eYrtdnUkuxLDA="
@@ -202,9 +202,12 @@ schema = 3
202202
[mod."gopkg.in/yaml.v3"]
203203
version = "v3.0.1"
204204
hash = "sha256-FqL9TKYJ0XkNwJFnq9j0VvJ5ZUU1RvH/52h/f5bkYAU="
205+
[mod."k8s.io/apimachinery"]
206+
version = "v0.26.9"
207+
hash = "sha256-nEKn5vbOfwT8+1E8MAopC+jDwoh6k3sBmeSMi4JoLbw="
205208
[mod."k8s.io/utils"]
206209
version = "v0.0.0-20240310230437-4693a0247e57"
207210
hash = "sha256-mzQaOH7Nqbf/paRg9u4J57+lvg4nvZ1kw1M59/BSYVU="
208211
[mod."sigs.k8s.io/yaml"]
209-
version = "v1.2.0"
210-
hash = "sha256-HHqIfaFuKGalif3bP2CcHm8MT8Ily1kJQL9Q8Kz2WHA="
212+
version = "v1.3.0"
213+
hash = "sha256-RVp8vca2wxg8pcBDYospG7Z1dujoH7zXNu2rgZ1kky0="

0 commit comments

Comments
 (0)