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

Commit 281b24f

Browse files
authored
Merge pull request #145 from adrianludwin/prop-doc
Fix managed label conditions and docs
2 parents 9c08a71 + 9b9ca10 commit 281b24f

File tree

3 files changed

+122
-28
lines changed

3 files changed

+122
-28
lines changed

api/v1alpha2/hierarchy_types.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ const (
5454
ConditionActivitiesHalted string = "ActivitiesHalted"
5555
ConditionBadConfiguration string = "BadConfiguration"
5656

57-
// Condition reasons.
57+
// Condition reasons. Please keep this list in alphabetical order. IF ADDING ANYTHING HERE, PLEASE
58+
// ALSO ADD THEM TO AllConditions, BELOW.
5859
ReasonAncestor string = "AncestorHaltActivities"
60+
ReasonAnchorMissing string = "SubnamespaceAnchorMissing"
5961
ReasonDeletingCRD string = "DeletingCRD"
62+
ReasonIllegalManagedAnnotation string = "IllegalManagedAnnotation"
63+
ReasonIllegalManagedLabel string = "IllegalManagedLabel"
64+
ReasonIllegalParent string = "IllegalParent"
6065
ReasonInCycle string = "InCycle"
6166
ReasonParentMissing string = "ParentMissing"
62-
ReasonIllegalParent string = "IllegalParent"
63-
ReasonAnchorMissing string = "SubnamespaceAnchorMissing"
64-
ReasonIllegalManagedLabel string = "IllegalManagedLabel"
65-
ReasonIllegalManagedAnnotation string = "IllegalManagedAnnotation"
6667
)
6768

6869
// AllConditions have all the conditions by type and reason. Please keep this
@@ -72,9 +73,11 @@ var AllConditions = map[string][]string{
7273
ConditionActivitiesHalted: {
7374
ReasonAncestor,
7475
ReasonDeletingCRD,
76+
ReasonIllegalManagedAnnotation,
77+
ReasonIllegalManagedLabel,
78+
ReasonIllegalParent,
7579
ReasonInCycle,
7680
ReasonParentMissing,
77-
ReasonIllegalParent,
7881
},
7982
ConditionBadConfiguration: {
8083
ReasonAnchorMissing,

docs/user-guide/concepts.md

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ namespaces are, and _why_ they behave the way they do.
1414
* [Parents, children, trees and forests](#basic-trees)
1515
* [Full namespaces and subnamespaces](#basic-subns)
1616
* [Policy inheritance and object propagation](#basic-propagation)
17-
* [Namespace labels and non-propagated policies](#basic-labels)
17+
* [Tree labels and non-propagated policies](#basic-labels)
1818
* [Exceptions and propagation control](#basic-exceptions)
1919
* [Administration](#admin)
2020
* [Hierarchical Configuration](#admin-hc)
2121
* [Namespaces administrators](#admin-admin)
2222
* [Conditions](#admin-conditions)
23+
* [Namespace label and annotation propagation](#admin-managed-labels)
2324
* [Labels and annotations read by HNC](#admin-labels-read)
2425
* [Labels and annotations set by HNC](#admin-labels-set)
2526

@@ -291,7 +292,7 @@ following three labels applied to it:
291292
* `team-a.tree.hnc.x-k8s.io/depth: 1`
292293
* `division-x.tree.hnc.x-k8s.io/depth: 2`
293294

294-
Due to their suffixes, these are known as **_tree labels_**.
295+
Due to their suffixes, these are known as ***tree labels***.
295296

296297
Tree labels can be used in two ways. Firstly, any policy that uses namespace
297298
label selectors may use them directly - even if those policies are not
@@ -314,6 +315,9 @@ whichever labels they like. However, HNC will overwrite any changes made to
314315
these labels, so other applications can trust these labels for policy
315316
application.
316317

318+
*Note: in HNC v1.0, [managed labels](#admin-managed-labels) may also be trusted
319+
for policy purposes.*
320+
317321
<a name="basic-exceptions"/>
318322

319323
### Exceptions and propagation control
@@ -353,21 +357,12 @@ be replaced.
353357
There are some built-in exceptions to prevent certain known (auto-generated)
354358
objects from being propagated by HNC.
355359

356-
If ConfigMaps propagation is enabled, any ConfigMaps named `istio-ca-root-cert`
357-
or `kube-root-ca.crt` will not be propagated. These are auto-created in new
358-
namespaces by Istio and Kubernetes respectively. As they are auto-generated,
359-
adding annotations is not possible and HNC will by default exclude them.
360-
361-
Similarly, Kubernetes ServiceAccount Secrets will also by default be excluded
362-
from propagation.
363-
364-
In addition, propagation exclusions are also used for Rancher-managed Kubernetes
365-
clusters. Rancher uses a "project" concept that bundles namespaces and thus sets
366-
roles, rolebindings, etc. for all namespaces of a project. This leads to
367-
conflicts with HNC, so all resources created by Rancher (which are automatically
368-
labeled with `"cattle.io/creator": "norman"` by Rancher, cf. [their
369-
docs](https://rancher.com/docs/rancher/v2.6/en/system-tools/#remove)) are
370-
excluded from propagation.
360+
* Kubernetes Service Account Secrets
361+
* ConfigMaps named `istio-ca-root-cert` or `kube-root-ca.crt`, which are
362+
auto-created in new namespaces by Istio and Kubernetes respectively
363+
* *Planned for HNC v1.0+:* Any objects with the label
364+
`cattle.io/creator:norman`, which are [inserted by Rancher to support
365+
Projects](https://rancher.com/docs/rancher/v2.6/en/system-tools/#remove))
371366

372367
<a name="admin"/>
373368

@@ -505,6 +500,40 @@ can either query such objects directly, or via `kubectl hns describe NAMESPACE`.
505500
The event will include machine-readable and human-readable information about the
506501
problem, and will generally require human intervention to resolve.
507502

503+
<a name="admin-managed-labels"/>
504+
505+
### Managed labels and annotations
506+
507+
***Managed labels and annotations are planned for HNC v1.0+***
508+
509+
Just as certain objects can be propagated from parent namespaces to their
510+
descendants, so can certain labels and annotations on namespaces. For example,
511+
an admin may define a `mycorp.com/environment:prod` label on a parent namespace,
512+
and ensure that it will be automatically propagated to all descendants of that
513+
namespace.
514+
515+
However, managed labels (and annotations - the remainder of this section applies
516+
to both) cannot be used simply by putting a label on a parent namespace, for
517+
several reasons:
518+
519+
* Users may not intend HNC to overwrite their existing labels simply because one
520+
of their ancestors has a conflicting label.
521+
* When a namespace's ancestors change, it's unclear which labels should be
522+
removed because they were propagated from an ancestor, and which were
523+
intended to be applied to the namespace itself.
524+
525+
Therefore, by default, HNC will _not_ propagate any labels on namespaces; the
526+
HNC admin must define which labels are _managed_ by modifying the command-line
527+
options of HNC and restarting HNC.
528+
529+
In addition, managed labels may _never_ be set simply by adding them to a
530+
namespace, as it would be impossible to distinguish between a "source" label and
531+
a "propagated" label (unlike propagated objects, which are annotated by
532+
`hnc.x-k8s.io/inherited-from`). Instead, they must be added in the
533+
`HierarchyConfiguration` object.
534+
535+
See [here](how-to.md#admin-managed-labels) for more details.
536+
508537
<a name="admin-labels-read">
509538

510539
### Labels and annotations read by HNC
@@ -514,8 +543,8 @@ objects, in addition to using the custom resources it defines.
514543

515544
#### propagate.hnc.x-k8s.io/TYPE (annotation on objects)
516545

517-
These annotations may be added to any namespaced object to define exceptions to
518-
propagation rules. More information to come.
546+
These annotations may be added to any namespaced object to define
547+
[exceptions](#basic-exceptions) to propagation rules.
519548

520549
#### hnc.x-k8s.io/managed-by (annotation on namespaces)
521550

@@ -567,6 +596,9 @@ HNC annotates and labels objects in several circumstances. Typically, most users
567596
(or admins) will never need to care about these, but occasionally they may cause
568597
some odd changes in behaviour that you need to be aware of.
569598

599+
See also [managed labels and annotations](#admin-managed-labels), which are
600+
defined by admins, not by HNC itself.
601+
570602
#### app.kubernetes.io/managed-by (label on objects)
571603

572604
HNC sets this label on any object that it propagates, taking the place of any

docs/user-guide/how-to.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ This document describes common tasks you might want to accomplish using HNC.
1515
* [Organize full namespaces into a hierarchy](#use-full)
1616
* [Resolve conditions on a namespace](#use-resolve-cond)
1717
* [Limit the propagation of an object to descendant namespaces](#use-limit-propagation)
18+
* [Add a label or annotation to all namespaces in a subtree](#use-managed-labels)
1819
* [Administer HNC](#admin)
1920
* [Install or upgrade HNC on a cluster](#admin-install)
2021
* [Uninstall HNC from a cluster](#admin-uninstall)
2122
* [Excluding namespaces from HNC](#admin-excluded-namespaces)
2223
* [Backing up and restoring HNC data](#admin-backup-restore)
2324
* [Administer who has access to HNC properties](#admin-access)
2425
* [Modify the resources propagated by HNC](#admin-resources)
26+
* [Ask HNC to manage certain labels and annotations](#admin-managed-labels)
2527
* [Gather metrics](#admin-metrics)
2628
* [Modify command-line arguments](#admin-cli-args)
2729

@@ -189,9 +191,9 @@ destination (descendant) namespace.
189191
### Select namespaces based on their hierarchies
190192

191193
HNC inserts labels onto your namespaces to allow trees (and subtrees) of
192-
namespaces to be selected by policies such as `NetworkPolicy`.
193-
194-
This section is under construction (as of Oct 2020). For now, please see the
194+
namespaces to be selected by policies such as Network Policies. For more
195+
information, read about the [concept](concepts.md#basic-labels) or see an
196+
example for how they can apply to Network Policies in the
195197
[quickstart](quickstart.md#netpol).
196198

197199
<a name="use-subns-delete"/>
@@ -401,6 +403,37 @@ metadata:
401403
EOF
402404
```
403405

406+
<a name="use-managed-labels"/>
407+
408+
### Add a label or annotation to all namespaces in a subtree
409+
410+
***Managed labels and annotations are planned for HNC v1.0+***
411+
412+
If your administrator has [created managed labels or
413+
annotations](#admin-managed-labels), you may set them on any namespace where you
414+
have permission to edit the `hierarchyconfigurations/hierarchy` object. For
415+
example, if your admin has set `env` as a managed label, you may set it on your
416+
namespace as follows:
417+
418+
```
419+
apiVersion: hnc.x-k8s.io/v1alpha2
420+
kind: HierarchyConfiguration
421+
metadata:
422+
name: hierarchy
423+
namespace: child
424+
… < other stuff > …
425+
spec:
426+
labels: # add
427+
- key: env # add
428+
value: prod # add
429+
```
430+
431+
You may similarly set managed annotations via the `.spec.annotations` list. Note
432+
that any label or annotation that conflicts with one set in an ancestor
433+
namespace will be silently ignored (this will eventually
434+
[be](https://github.com/kubernetes-sigs/hierarchical-namespaces/issues/143)
435+
[improved](https://github.com/kubernetes-sigs/hierarchical-namespaces/issues/144)).
436+
404437
<a name="admin"/>
405438

406439
## Administer HNC
@@ -724,6 +757,32 @@ immediately, you can use the `--force` flag with `kubectl hns config
724757
set-resource` to add a resource directly in the `Propagate` mode. You can also
725758
edit the `config` object directly, which will bypass this protection.
726759

760+
761+
762+
### Ask HNC to manage certain labels and annotations
763+
764+
***Managed labels and annotations are planned for HNC v1.0+***
765+
766+
See [here](concepts.md#admin-managed-labels) for the background on managed
767+
labels and annotations. In order to get HNC to manage a label or annotation, use
768+
the following [command-line arguments](#admin-cli-args):
769+
770+
* `--managed-namespace-label` for labels
771+
* `--managed-namespace-annotation` for annotations
772+
773+
Both of these options may be specified multiple times, and both of them are
774+
regular expressions, but automaticaly have `^...$` surrounding them. For
775+
example, to ask HNC to manage any label with the prefix `hier.mycorp.com`, set
776+
the parameter `--managed-namespace-label hier.mycorp.com/.*`.
777+
778+
When you ask HNC to start managing a label or annotation, all _existing_
779+
namespace labels/annotations that match the pattern will immediately be deleted
780+
(unless they are specified in the `HierarchyConfiguration`), so use this feature
781+
with care.
782+
783+
See [here](#use-managed-labels) for more information on how to use managed
784+
labels and annotations once they've been enabled.
785+
727786
728787

729788
### Gather metrics

0 commit comments

Comments
 (0)