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

Commit 0912b8b

Browse files
committed
adding hrq documentation
Signed-off-by: unknown <[email protected]>
1 parent 97817c0 commit 0912b8b

File tree

4 files changed

+240
-5
lines changed

4 files changed

+240
-5
lines changed

docs/user-guide/concepts.md

+63-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespaces are, and _why_ they behave the way they do.
1313
* [Basic concepts](#basic)
1414
* [Parents, children, trees and forests](#basic-trees)
1515
* [Full namespaces and subnamespaces](#basic-subns)
16+
* [Hierarchical resource quotas (HRQs)](#hrq)
1617
* [Policy inheritance and object propagation](#basic-propagation)
1718
* [Tree labels and non-propagated policies](#basic-labels)
1819
* [Exceptions and propagation control](#basic-exceptions)
@@ -87,6 +88,8 @@ namespaces:
8788
as isolation units for their own services. However, namespace creation is a
8889
privileged cluster-level operation, and you typically want to control this
8990
privilege very closely.
91+
* You might want to give some amount of resources to a team (similar to `ResourceQuota`), and they can
92+
distribute those resources between their subnamespaces.
9093
* Finally, you might want to avoid having to find unique names for every
9194
namespace in the cluster.
9295

@@ -218,6 +221,64 @@ probably not a great idea.
218221
You can create a subnamespace from the command line via `kubectl hns create child
219222
-n parent`.
220223

224+
<a name="hrq">
225+
226+
### Hierarchical resource quotas (HRQs)
227+
228+
When you want to give some amount of resources to `team-a`, and want them to be able to
229+
flexibly use resources in any of their subnamespaces, you create a `HierarchicalResourceQuota`
230+
in namespace `team-a`. The sum of all resources from all the subnamespaces of the
231+
members wont be over the amount of resources that is configured in
232+
`HierarchicalResourceQuota` of namespace `team-a`. All of the reasources of `team-a` are
233+
equally shared between the applications in their subnamespaces, which is very efficient.
234+
235+
In addition, you can let an org or team's admin create their own hierarchical
236+
quotas without violating the overall HRQ for their org or team. For example,
237+
if you start with the following structure:
238+
```
239+
company-a
240+
├── organization-a
241+
│ ├── org-a-team-1
242+
│ ├── org-a-team-2
243+
│ ...
244+
├── organization-b
245+
│ ├── org-b-team-1
246+
│ ├── org-b-team-2
247+
│ ...
248+
...
249+
```
250+
Instead of each team asking from the `cluster-admin` to modify their `ResourceQuota`,
251+
you can insert an additional "policy" namespace above each level to hold
252+
the policy objects (like hierarchical quota) that the sub-admin _cannot_
253+
change, while giving them permission to create their own quotas in the
254+
lower level namespaces, like this:
255+
```
256+
company-a-policy
257+
└── company-a
258+
```
259+
And put the resources HRQ in the `company-a-policy` namespace. This will restrict
260+
whole `company-a` to the amount of resources that they are paying for. Then `company-a`
261+
can do similar HRQ with their organizations:
262+
```
263+
company-a-policy (has HRQ)
264+
└── company-a
265+
├── org-a-policy (has HRQ)
266+
│ └── organization-a
267+
├── org-b-policy (has HRQ)
268+
│ └── organization-b
269+
...
270+
```
271+
Lower-level quotas cannot override more restrictive quotas from ancestor namespaces;
272+
the most restrictive quota always wins.
273+
This way each individual can fairly and securely distribute their resources across
274+
their members.
275+
276+
To implement hierarchical quotas, HNC automatically creates `ResourceQuota` objects in each
277+
affected namespace. This is a part of the internal implementation and shouldn't be modified or
278+
inspected. Use the `kubectl hns hrq` command to inspect hierarchical quotas,
279+
or look at the `HierarchicalResourceQuota` object in the ancestor
280+
namespaces.
281+
221282
<a name="basic-propagation">
222283

223284
### Policy inheritance and object propagation
@@ -327,7 +388,7 @@ HNC typically propagates _all_ objects of a [specified type](how-to.md#admin-res
327388
from ancestor namespaces to descendant namespaces. However, sometimes this is
328389
too restrictive, and you need to create ***exceptions*** to certain policies. For example:
329390

330-
* A ResourceQuota was propagated to many children, but one child namespace now
391+
* A `ResourceQuota` was propagated to many children, but one child namespace now
331392
has higher requirements than the rest. Rather than getting rid of the quota in
332393
the parent namespace, or raising the limit for everyone, you can stop the
333394
quota in the parent from being propagated to that _one_ child namespace,
@@ -345,7 +406,7 @@ an object can also control how it is propagated to descendant namespaces.
345406
If you modify an exception - for example, by removing it - this could cause
346407
the object to be propagated to descendants from which it had previously been
347408
excluded. This could cause you to accidentally overwrite objects that were
348-
intended to be exceptions from higher-level policies, like the ResourceQuota
409+
intended to be exceptions from higher-level policies, like the `ResourceQuota`
349410
in the example above. To prevent this, if modifying an exception would cause
350411
HNC to overwrite another object, HNC’s admission controllers will prevent you
351412
from modifying the object, and will identify the objects that would have been

docs/user-guide/how-to.md

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This document describes common tasks you might want to accomplish using HNC.
1010
* [Create a subnamespace](#use-subns-create)
1111
* [Inspect namespace hierarchies](#use-inspect)
1212
* [Propagating policies across namespaces](#use-propagate)
13+
* [Apply hierarchical resource quotas (HRQs)](#use-hrq)
1314
* [Select namespaces based on their hierarchies](#use-select)
1415
* [Delete a subnamespace](#use-subns-delete)
1516
* [Organize full namespaces into a hierarchy](#use-full)
@@ -186,6 +187,17 @@ permissions. To understand why an object is not being propagated to a namespace,
186187
use `kubectl hns describe <ns>`, where `<ns>` is either the source (ancestor) or
187188
destination (descendant) namespace.
188189

190+
<a name="use-hrq"/>
191+
192+
### Limit Resources over parent namespaces
193+
194+
HNC has an object called `HierarchicalResourceQuota` which is a drop-in replacement for `ResourceQuota`
195+
but across all the namespaces in a hierarchy. It allows you to distribute your resources between
196+
teams, and those teams can distribute their resources between their subteams.
197+
[Learn how it works](concepts.md#hierarchical-resource-quota) or see an [quickstart example](quickstart.md#hrq)
198+
199+
Note: Decimal point values cannot be specified in HRQ (you can't do `cpu: 1.5` but you can do `cpu: 1500m`). See [#292](https://github.com/kubernetes-sigs/hierarchical-namespaces/issues/292)
200+
189201
<a name="use-select"/>
190202

191203
### Select namespaces based on their hierarchies

docs/user-guide/quickstart.md

+98-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ also to all contributors since then.
2121
* [Basic functionality](#basic)
2222
* [Propagating different resources](#resources)
2323
* [Hierarchical network policy](#netpol)
24+
* [Hierarchical resource quotas](#hrq)
2425
* [Subnamespaces deep-dive](#subns)
2526
* [Keeping objects out of certain namespaces](#exceptions)
2627

@@ -341,7 +342,7 @@ Now we'll create a default network policy that blocks any ingress from other
341342
namespaces:
342343

343344
```bash
344-
cat << EOF | kubectl apply -f -
345+
kubectl apply -f - << EOF
345346
kind: NetworkPolicy
346347
apiVersion: networking.k8s.io/v1
347348
metadata:
@@ -394,7 +395,7 @@ other. We do this by creating a policy that selects namespaces based on the
394395
that is automatically added by the HNC.
395396

396397
```bash
397-
cat << EOF | kubectl apply -f -
398+
kubectl apply -f - << EOF
398399
kind: NetworkPolicy
399400
apiVersion: networking.k8s.io/v1
400401
metadata:
@@ -458,6 +459,100 @@ kubectl delete svc s2 -n service-2
458459
kubectl delete pods s2 -n service-2
459460
```
460461

462+
<a name="hrq"/>
463+
464+
### Hierarchical resource quotas
465+
466+
_Will demonstrate: Create and delete [HierarchicalResourceQuota](concepts.md#hierarchical-resource-quota)._
467+
468+
Let's assume you own _acme-org_ from the previous example:
469+
```
470+
acme-org
471+
├── [s] team-a
472+
└── [s] team-b
473+
```
474+
475+
You can create a `HierarchicalResourceQuota` in namespace `acme-org`, and the sum of
476+
all subnamespaces resource usage cant go over what is configured in the hrq.
477+
478+
Creating the hrq:
479+
```bash
480+
kubectl apply -f - << EOF
481+
kind: HierarchicalResourceQuota
482+
apiVersion: hnc.x-k8s.io/v1alpha2
483+
metadata:
484+
name: acme-org-hrq
485+
namespace: acme-org
486+
spec:
487+
hard:
488+
requests.cpu: 1
489+
requests.memory: 2Gi
490+
limits.cpu: 2
491+
limits.memory: 4Gi
492+
services: 1
493+
EOF
494+
```
495+
496+
Lets create Service in namespace `team-a`:
497+
```bash
498+
kubectl apply -f - << EOF
499+
kind: Service
500+
apiVersion: v1
501+
metadata:
502+
name: team-a-svc
503+
namespace: team-a
504+
spec:
505+
selector:
506+
place: holder
507+
ports:
508+
- protocol: TCP
509+
port: 80
510+
targetport: 80
511+
EOF
512+
```
513+
514+
And when we try to create Service in namespace `team-b`:
515+
```bash
516+
kubectl apply -f - << EOF
517+
kind: Service
518+
apiVersion: v1
519+
metadata:
520+
name: team-b-svc
521+
namespace: team-b
522+
spec:
523+
selector:
524+
place: holder
525+
ports:
526+
- protocol: TCP
527+
port: 80
528+
targetport: 80
529+
EOF
530+
```
531+
We get an error:
532+
```
533+
Error from server (Forbidden):
534+
error when creating "STDIN":
535+
admission webhood "resourcesquotasstatus.hnc.x-k8s.io" denied the request:
536+
exceeded hierarchical quota in namespace "acme-org":
537+
"acme-org-hrq", requested: services=1, used: services=1, limited: services=1
538+
```
539+
540+
To view the HRQ usage, simply run:
541+
```bash
542+
kubectl hns hrq -n acme-org
543+
```
544+
you can also view in all namespace:
545+
```bash
546+
kubectl hns hrq --all-namespaces
547+
```
548+
549+
and you can delete the hrq via simply deleting the CR:
550+
```bash
551+
kubectl delete hrq acme-org-hrq -n acme-org
552+
```
553+
554+
Note: Decimal point values cannot be specified (you can't do `cpu: 1.5` but you can do `cpu: 1500m`). See [#292](https://github.com/kubernetes-sigs/hierarchical-namespaces/issues/292)
555+
461556
<a name="subns"/>
462557

463558
### Subnamespaces deep dive
@@ -672,7 +767,7 @@ Of course, the annotation can also be part of the object when you create it:
672767

673768
```bash
674769
kubectl delete secret my-secret -n acme-org
675-
cat << EOF | k create -f -
770+
kubectl create -f - << EOF
676771
apiVersion: v1
677772
kind: Secret
678773
metadata:

test/e2e/quickstart_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,73 @@ spec:
191191
RunErrorShouldContain("wget: download timed out", defTimeout, clientCmd, nsTeamB, alpineArgs, wgetArgs)
192192
})
193193

194+
It("Create and delete HierarchicalResourceQuota", func() {
195+
// set up initial structure
196+
CreateNamespace(nsOrg)
197+
CreateSubnamespace(nsTeamA, nsOrg)
198+
CreateSubnamespace(nsTeamB, nsOrg)
199+
200+
expected := "" + // empty string make go fmt happy
201+
nsOrg + "\n" +
202+
"├── [s] " + nsTeamA + "\n" +
203+
"└── [s] " + nsTeamB
204+
// The subnamespaces takes a bit of time to show up
205+
RunShouldContain(expected, propogationTimeout, "kubectl hns tree", nsOrg)
206+
207+
// create hrq in parent acme-org
208+
hrq:=`# quickstart_test.go: hrq in acme-org
209+
kind: HierarchicalResourceQuota
210+
apiVersion: hnc.x-k8s.io/v1alpha2
211+
metadata:
212+
name: acme-org-hrq
213+
namespace: acme-org
214+
spec:
215+
hard:
216+
requests.cpu: 2
217+
requests.memory: 4Gi
218+
limits.cpu: 2
219+
limits.memory: 4Gi
220+
services: 1`
221+
MustApplyYAML(hrq)
222+
223+
// create service in team-a
224+
serviceA:=`
225+
kind: Service
226+
apiVersion: v1
227+
metadata:
228+
name: `+nsTeamA+`-svc
229+
namespace: `+nsTeamA+`
230+
spec:
231+
selector:
232+
place: holder
233+
ports:
234+
- protocol: TCP
235+
port: 80
236+
targetport: 80`
237+
MustApplyYAML(serviceA)
238+
239+
// show that you can't use resources more than the hrq
240+
serviceB:=`
241+
kind: Service
242+
apiVersion: v1
243+
metadata:
244+
name: `+nsTeamB+`-svc
245+
namespace: `+nsTeamB+`
246+
spec:
247+
selector:
248+
place: holder
249+
ports:
250+
- protocol: TCP
251+
port: 80
252+
targetport: 80`
253+
MustNotApplyYAML(serviceB)
254+
255+
// show hrq usage
256+
RunShouldContain("requests.cpu: 0/1, requests.memory: 0/2Gi limits.cpu: 0/2, limits.memory: 0/4Gi, services 1/1", defTimeout, "kubectl hns hrq", "-n", nsOrg)
257+
258+
MustRun("kubectl delete hrq", nsOrg + "-hrq", "-n", nsOrg)
259+
})
260+
194261
It("Should create and delete subnamespaces", func() {
195262
// set up initial structure
196263
CreateNamespace(nsOrg)

0 commit comments

Comments
 (0)