Skip to content

Commit 1229c0a

Browse files
authored
Merge pull request kubernetes-retired#234 from mochizuki875/feature-230
Fix validation of Subnamespaceanchor deletion
2 parents e9d8bb8 + 8bfe0ea commit 1229c0a

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

internal/anchor/validator.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,16 @@ func (v *Validator) handle(req *anchorRequest) admission.Response {
7575
cnm := req.anchor.Name
7676
cns := v.Forest.Get(cnm)
7777

78-
errStrs := validation.IsDNS1123Label(cnm)
79-
if len(errStrs) != 0 {
80-
fldPath := field.NewPath("metadata", "name")
81-
msg := fmt.Sprintf("not a valid namespace name: %s", strings.Join(errStrs, "; "))
82-
allErrs := field.ErrorList{
83-
field.Invalid(fldPath, cnm, msg),
78+
if req.op != k8sadm.Delete {
79+
errStrs := validation.IsDNS1123Label(cnm)
80+
if len(errStrs) != 0 {
81+
fldPath := field.NewPath("metadata", "name")
82+
msg := fmt.Sprintf("not a valid namespace name: %s", strings.Join(errStrs, "; "))
83+
allErrs := field.ErrorList{
84+
field.Invalid(fldPath, cnm, msg),
85+
}
86+
return webhooks.DenyInvalid(api.SubnamespaceAnchorGK, cnm, allErrs)
8487
}
85-
return webhooks.DenyInvalid(api.SubnamespaceAnchorGK, cnm, allErrs)
8688
}
8789

8890
labelErrs := config.ValidateManagedLabels(req.anchor.Spec.Labels)

internal/anchor/validator_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestCreateSubnamespaces(t *testing.T) {
3232
{name: "for existing non-subns child", pnm: "a", cnm: "c", fail: true},
3333
{name: "for existing subns", pnm: "a", cnm: "b"},
3434
{name: "for non DNS label compliant child", pnm: "a", cnm: "child.01", fail: true},
35+
{name: "with name more than 63 characters", pnm: "a", cnm: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", fail: true},
3536
}
3637
for _, tc := range tests {
3738
t.Run(tc.name, func(t *testing.T) {
@@ -55,6 +56,41 @@ func TestCreateSubnamespaces(t *testing.T) {
5556
}
5657
}
5758

59+
func TestDeleteSubnamespaces(t *testing.T) {
60+
f := foresttest.Create("-Aa")
61+
v := &Validator{Forest: f}
62+
63+
tests := []struct {
64+
name string
65+
pnm string
66+
cnm string
67+
fail bool
68+
}{
69+
{name: "for existing subns", pnm: "a", cnm: "b"},
70+
{name: "with name more than 63 characters", pnm: "a", cnm: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
71+
}
72+
for _, tc := range tests {
73+
t.Run(tc.name, func(t *testing.T) {
74+
// Setup
75+
g := NewWithT(t)
76+
anchor := &api.SubnamespaceAnchor{}
77+
anchor.ObjectMeta.Namespace = tc.pnm
78+
anchor.ObjectMeta.Name = tc.cnm
79+
req := &anchorRequest{
80+
anchor: anchor,
81+
op: k8sadm.Delete,
82+
}
83+
84+
// Test
85+
got := v.handle(req)
86+
87+
// Report
88+
logResult(t, got.AdmissionResponse.Result)
89+
g.Expect(got.AdmissionResponse.Allowed).ShouldNot(Equal(tc.fail))
90+
})
91+
}
92+
}
93+
5894
func TestManagedMeta(t *testing.T) {
5995
f := foresttest.Create("-") // a
6096
v := &Validator{Forest: f}

0 commit comments

Comments
 (0)