Skip to content

Commit 99e18f5

Browse files
authored
Merge pull request kubernetes#84900 from MikeSpreitzer/add-namespace-to-rule
Enable Priority and Fairness to discriminate on target namespace
2 parents f1e912c + 793b5a7 commit 99e18f5

File tree

11 files changed

+542
-160
lines changed

11 files changed

+542
-160
lines changed

api/openapi-spec/swagger.json

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/flowcontrol/types.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ const (
2626
ResourceAll = "*"
2727
VerbAll = "*"
2828
NonResourceAll = "*"
29+
NameAll = "*"
2930

30-
NameAll = "*"
31+
NamespaceEvery = "*" // matches every particular namespace
3132
)
3233

3334
// System preset priority level names
@@ -210,28 +211,53 @@ type ServiceAccountSubject struct {
210211
Name string
211212
}
212213

213-
// ResourcePolicyRule is a predicate that matches some resource requests, testing the request's verb and the target
214-
// resource. A ResourcePolicyRule matches a request if and only if: (a) at least one member
215-
// of verbs matches the request, (b) at least one member of apiGroups matches the request, and (c) at least one member
216-
// of resources matches the request.
214+
// ResourcePolicyRule is a predicate that matches some resource
215+
// requests, testing the request's verb and the target resource. A
216+
// ResourcePolicyRule matches a resource request if and only if: (a)
217+
// at least one member of verbs matches the request, (b) at least one
218+
// member of apiGroups matches the request, (c) at least one member of
219+
// resources matches the request, and (d) least one member of
220+
// namespaces matches the request.
217221
type ResourcePolicyRule struct {
218222
// `verbs` is a list of matching verbs and may not be empty.
219-
// "*" matches all verbs. if it is present, it must be the only entry.
223+
// "*" matches all verbs and, if present, must be the only entry.
220224
// +listType=set
221225
// Required.
222226
Verbs []string
227+
223228
// `apiGroups` is a list of matching API groups and may not be empty.
224-
// "*" matches all api-groups. if it is present, it must be the only entry.
229+
// "*" matches all API groups and, if present, must be the only entry.
225230
// +listType=set
226231
// Required.
227232
APIGroups []string
228-
// `resources` is a list of matching resources (i.e., lowercase and plural) with, if desired, subresource.
229-
// For example, [ "services", "nodes/status" ].
230-
// This list may not be empty.
231-
// "*" matches all resources. if it is present, it must be the only entry.
232-
// +listType=set
233+
234+
// `resources` is a list of matching resources (i.e., lowercase
235+
// and plural) with, if desired, subresource. For example, [
236+
// "services", "nodes/status" ]. This list may not be empty.
237+
// "*" matches all resources and, if present, must be the only entry.
233238
// Required.
239+
// +listType=set
234240
Resources []string
241+
242+
// `clusterScope` indicates whether to match requests that do not
243+
// specify a namespace (which happens either because the resource
244+
// is not namespaced or the request targets all namespaces).
245+
// If this field is omitted or false then the `namespaces` field
246+
// must contain a non-empty list.
247+
// +optional
248+
ClusterScope bool
249+
250+
// `namespaces` is a list of target namespaces that restricts
251+
// matches. A request that specifies a target namespace matches
252+
// only if either (a) this list contains that target namespace or
253+
// (b) this list contains "*". Note that "*" matches any
254+
// specified namespace but does not match a request that _does
255+
// not specify_ a namespace (see the `clusterScope` field for
256+
// that).
257+
// This list may be empty, but only if `clusterScope` is true.
258+
// +optional
259+
// +listType=set
260+
Namespaces []string
235261
}
236262

237263
// NonResourcePolicyRule is a predicate that matches non-resource requests according to their verb and the

pkg/apis/flowcontrol/v1alpha1/zz_generated.conversion.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/flowcontrol/validation/validation.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,25 @@ func ValidateFlowSchemaResourcePolicyRule(rule *flowcontrol.ResourcePolicyRule,
245245
allErrs = append(allErrs, field.Invalid(fldPath.Child("resources"), rule.Resources, "if '*' is present, must not specify other resources"))
246246
}
247247

248+
if len(rule.Namespaces) == 0 && !rule.ClusterScope {
249+
allErrs = append(allErrs, field.Required(fldPath.Child("namespaces"), "resource rules that are not cluster scoped must supply at least one namespace"))
250+
} else if hasWildcard(rule.Namespaces) {
251+
if len(rule.Namespaces) > 1 {
252+
allErrs = append(allErrs, field.Invalid(fldPath.Child("namespaces"), rule.Namespaces, "if '*' is present, must not specify other namespaces"))
253+
}
254+
} else {
255+
for idx, tgtNS := range rule.Namespaces {
256+
for _, msg := range apimachineryvalidation.ValidateNamespaceName(tgtNS, false) {
257+
allErrs = append(allErrs, field.Invalid(fldPath.Child("namespaces").Index(idx), tgtNS, nsErrIntro+msg))
258+
}
259+
}
260+
}
261+
248262
return allErrs
249263
}
250264

265+
const nsErrIntro = "each member of this list must be '*' or a DNS-1123 label; "
266+
251267
// ValidateFlowSchemaStatus validates status for the flow-schema.
252268
func ValidateFlowSchemaStatus(status *flowcontrol.FlowSchemaStatus, fldPath *field.Path) field.ErrorList {
253269
var allErrs field.ErrorList
@@ -424,8 +440,12 @@ func ValidateNonResourceURLPath(path string, fldPath *field.Path) *field.Error {
424440
}
425441

426442
func hasWildcard(operations []string) bool {
427-
for _, o := range operations {
428-
if o == "*" {
443+
return memberInList("*", operations...)
444+
}
445+
446+
func memberInList(seek string, a ...string) bool {
447+
for _, ai := range a {
448+
if ai == seek {
429449
return true
430450
}
431451
}

0 commit comments

Comments
 (0)