Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit bdaef15

Browse files
authored
Merge pull request #124 from gkarthiks/master
Validate anchor names are RFC 1123 labels
2 parents 441f27d + 4008fd5 commit bdaef15

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

internal/anchor/validator.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package anchor
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/go-logr/logr"
89
k8sadm "k8s.io/api/admission/v1"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/util/validation"
1012
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
1113

1214
api "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2"
@@ -77,6 +79,12 @@ func (v *Validator) handle(req *anchorRequest) admission.Response {
7779
cnm := req.anchor.Name
7880
cns := v.Forest.Get(cnm)
7981

82+
errStrs := validation.IsDNS1123Label(cnm)
83+
if len(errStrs) != 0 {
84+
msg := fmt.Sprintf("Subnamespace %s is not a valid namespace name: %s", cnm, strings.Join(errStrs, "; "))
85+
return webhooks.Deny(metav1.StatusReasonInvalid, msg)
86+
}
87+
8088
switch req.op {
8189
case k8sadm.Create:
8290
// Can't create subnamespaces in unmanaged namespaces

internal/anchor/validator_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestCreateSubnamespaces(t *testing.T) {
3131
{name: "with an existing ns name (the ns is not a subnamespace of it)", pnm: "c", cnm: "b", fail: true},
3232
{name: "for existing non-subns child", pnm: "a", cnm: "c", fail: true},
3333
{name: "for existing subns", pnm: "a", cnm: "b"},
34+
{name: "for non DNS label compliant child", pnm: "a", cnm: "child.01", fail: true},
3435
}
3536
for _, tc := range tests {
3637
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)