@@ -3,6 +3,7 @@ package namespaces
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "strings"
6
7
7
8
"github.com/go-logr/logr"
8
9
k8sadm "k8s.io/api/admission/v1"
@@ -86,6 +87,11 @@ func (v *Validator) handle(req *nsRequest) admission.Response {
86
87
if rsp := v .nameExistsInExternalHierarchy (req ); ! rsp .Allowed {
87
88
return rsp
88
89
}
90
+
91
+ if rsp := v .illegalTreeLabel (req ); ! rsp .Allowed {
92
+ return rsp
93
+ }
94
+
89
95
case k8sadm .Update :
90
96
if rsp := v .illegalIncludedNamespaceLabel (req ); ! rsp .Allowed {
91
97
return rsp
@@ -96,6 +102,11 @@ func (v *Validator) handle(req *nsRequest) admission.Response {
96
102
if rsp := v .conflictBetweenParentAndExternalManager (req , ns ); ! rsp .Allowed {
97
103
return rsp
98
104
}
105
+
106
+ if rsp := v .illegalTreeLabel (req ); ! rsp .Allowed {
107
+ return rsp
108
+ }
109
+
99
110
case k8sadm .Delete :
100
111
if rsp := v .cannotDeleteSubnamespace (req ); ! rsp .Allowed {
101
112
return rsp
@@ -108,6 +119,40 @@ func (v *Validator) handle(req *nsRequest) admission.Response {
108
119
return webhooks .Allow ("" )
109
120
}
110
121
122
+ // illegalTreeLabel checks if tree labels are being created or modified
123
+ // by any user or service account since only HNC service account is
124
+ // allowed to do so
125
+ func (v * Validator ) illegalTreeLabel (req * nsRequest ) admission.Response {
126
+ msg := "Cannot set or modify tree label %q in namespace %q; these can only be modified by HNC."
127
+
128
+ oldLabels := map [string ]string {}
129
+ if req .oldns != nil {
130
+ oldLabels = req .oldns .Labels
131
+ }
132
+ // Ensure the users hasn't added or changed any tree labels
133
+ for key , val := range req .ns .Labels {
134
+ if ! strings .Contains (key , api .LabelTreeDepthSuffix ) {
135
+ continue
136
+ }
137
+
138
+ // Check if new HNC label tree key isn't being added
139
+ if oldLabels [key ] != val {
140
+ return webhooks .Deny (metav1 .StatusReasonForbidden , fmt .Sprintf (msg , key , req .ns .Name ))
141
+ }
142
+ }
143
+
144
+ for key := range oldLabels {
145
+ // Make sure nothing's been deleted
146
+ if strings .Contains (key , api .LabelTreeDepthSuffix ) {
147
+ if _ , ok := req .ns .Labels [key ]; ! ok {
148
+ return webhooks .Deny (metav1 .StatusReasonForbidden , fmt .Sprintf (msg , key , req .ns .Name ))
149
+ }
150
+ }
151
+ }
152
+
153
+ return webhooks .Allow ("" )
154
+ }
155
+
111
156
// illegalIncludedNamespaceLabel checks if there's any illegal use of the
112
157
// included-namespace label on namespaces. It only checks a Create or an Update
113
158
// request.
0 commit comments