Skip to content

Commit eb5ec1a

Browse files
authored
Merge pull request #562 from stuggi/OSPRH-8063-18.0.2
[18.0.0-proposed] [webhook] add ValidateDNS1123Label webhook func
2 parents cf21bcf + afd2de2 commit eb5ec1a

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

modules/common/webhook/rfc.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2024 Red Hat
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package webhook
18+
19+
import (
20+
"k8s.io/apimachinery/pkg/util/validation"
21+
"k8s.io/apimachinery/pkg/util/validation/field"
22+
)
23+
24+
// ValidateDNS1123Label - validates a list of strings are RFC 1123 label conform. Using the
25+
// correction parameter the validation.DNS1123LabelMaxLength (63) get reduced by the correction
26+
// value
27+
//
28+
// example usage:
29+
//
30+
// ValidateDNS1123Label(<path>, {"foo", "bar"}, 5)
31+
func ValidateDNS1123Label(basePath *field.Path, keys []string, correction int) field.ErrorList {
32+
allErrs := field.ErrorList{}
33+
34+
for _, key := range keys {
35+
msgs := validation.IsDNS1123Label(key)
36+
37+
maxLength := validation.DNS1123LabelMaxLength - correction
38+
39+
if correction > 0 && len(key) > maxLength {
40+
msgs = append(msgs, validation.MaxLenError(maxLength))
41+
}
42+
43+
for _, msg := range msgs {
44+
allErrs = append(allErrs, field.Invalid(basePath.Key(key), key, msg))
45+
}
46+
}
47+
48+
return allErrs
49+
}

modules/common/webhook/rfc_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2024 Red Hat
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package webhook
18+
19+
import (
20+
"testing"
21+
22+
"k8s.io/apimachinery/pkg/util/validation/field"
23+
24+
. "github.com/onsi/gomega"
25+
)
26+
27+
func TestValidateDNS1123Label(t *testing.T) {
28+
tests := []struct {
29+
name string
30+
keys []string
31+
corr int
32+
want bool
33+
}{
34+
{
35+
name: "valid name",
36+
keys: []string{"foo123"},
37+
corr: 0,
38+
want: false,
39+
},
40+
{
41+
name: "valid max lenth",
42+
keys: []string{"foo-1234567890-1234567890-1234567890-1234567890-1234567890-1234"},
43+
corr: 0,
44+
want: false,
45+
},
46+
{
47+
name: "invalid max lenth",
48+
keys: []string{"foo-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890"},
49+
corr: 0,
50+
want: true,
51+
},
52+
{
53+
name: "invalid max lenth with correction",
54+
keys: []string{"foo-1234567890-1234567890-1234567890-1234567890-1234567890-1234"},
55+
corr: 5,
56+
want: true,
57+
},
58+
{
59+
name: "invalid char",
60+
keys: []string{"foo_bar"},
61+
corr: 0,
62+
want: true,
63+
},
64+
{
65+
name: "invalid multiple reasons",
66+
keys: []string{"foo123", "foo-1234567890-1234567890-1234567890-1234567890-1234567890-1234567890", "foo_bar"},
67+
corr: 0,
68+
want: true,
69+
},
70+
}
71+
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
g := NewWithT(t)
75+
76+
p := field.NewPath("foo")
77+
78+
errs := ValidateDNS1123Label(p, tt.keys, tt.corr)
79+
if tt.want {
80+
g.Expect(errs).ToNot(BeEmpty())
81+
} else {
82+
g.Expect(errs).To(BeEmpty())
83+
}
84+
})
85+
}
86+
}

0 commit comments

Comments
 (0)