Skip to content

Commit 89d7af1

Browse files
committed
Initial impl of managed labels/annotations
See kubernetes-retired#47. This is the initial implementation of managed labels and annotations - that is, the ability to set a label (or annotation) in a HierarchyConfiguration object, and have that label (...) propagated to all descendants, similar to the way objects are propagated. As with objects, only allowlisted labels are propagated, as defined by the command line option '--managed-namespace-[labels|annotations]'. Still to come: validator support, better conditions for conflicts, better testing for external namespaces. Tested: see new integ tests.
1 parent ce8668f commit 89d7af1

File tree

9 files changed

+373
-39
lines changed

9 files changed

+373
-39
lines changed

api/v1alpha2/hierarchy_types.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ const (
5555
ConditionBadConfiguration string = "BadConfiguration"
5656

5757
// Condition reasons.
58-
ReasonAncestor string = "AncestorHaltActivities"
59-
ReasonDeletingCRD string = "DeletingCRD"
60-
ReasonInCycle string = "InCycle"
61-
ReasonParentMissing string = "ParentMissing"
62-
ReasonIllegalParent string = "IllegalParent"
63-
ReasonAnchorMissing string = "SubnamespaceAnchorMissing"
58+
ReasonAncestor string = "AncestorHaltActivities"
59+
ReasonDeletingCRD string = "DeletingCRD"
60+
ReasonInCycle string = "InCycle"
61+
ReasonParentMissing string = "ParentMissing"
62+
ReasonIllegalParent string = "IllegalParent"
63+
ReasonAnchorMissing string = "SubnamespaceAnchorMissing"
64+
ReasonIllegalManagedLabel string = "IllegalManagedLabel"
65+
ReasonIllegalManagedAnnotation string = "IllegalManagedAnnotation"
6466
)
6567

6668
// AllConditions have all the conditions by type and reason. Please keep this
@@ -124,6 +126,18 @@ type HierarchyConfigurationSpec struct {
124126
// AllowCascadingDeletion indicates if the subnamespaces of this namespace are
125127
// allowed to cascading delete.
126128
AllowCascadingDeletion bool `json:"allowCascadingDeletion,omitempty"`
129+
130+
// Lables is a list of labels and values to apply to the current namespace and all of its
131+
// descendants. All label keys must be specified on the command line by
132+
// --managed-namespace-labels. A namespace cannot have a KVP that conflicts with one of its
133+
// ancestors.
134+
Labels []MetaKVP `json:"labels,omitempty"`
135+
136+
// Annotations is a list of annotations and values to apply to the current namespace and all of
137+
// its descendants. All annotation keys must be specified on the command line by
138+
// --managed-namespace-annotations. A namespace cannot have a KVP that conflicts with one of its
139+
// ancestors.
140+
Annotations []MetaKVP `json:"annotations,omitempty"`
127141
}
128142

129143
// HierarchyStatus defines the observed state of Hierarchy
@@ -147,6 +161,15 @@ type HierarchyConfigurationList struct {
147161
Items []HierarchyConfiguration `json:"items"`
148162
}
149163

164+
// MetaKVP represents a label or annotation
165+
type MetaKVP struct {
166+
// Name is the name of the label or annotation.
167+
Name string `json:"name"`
168+
169+
// Value is the value of the label or annotation.
170+
Value string `json:"value"`
171+
}
172+
150173
// metav1.Condition is introduced in k8s.io/apimachinery v0.20.0-alpha.1 and we
151174
// don't want to take a dependency on it yet, thus we copied the below struct from
152175
// https://github.com/kubernetes/apimachinery/blob/master/pkg/apis/meta/v1/types.go:

api/v1alpha2/zz_generated.deepcopy.go

Lines changed: 26 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/manager/main.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ var (
6565
restartOnSecretRefresh bool
6666
unpropagatedAnnotations arrayArg
6767
excludedNamespaces arrayArg
68+
managedNamespaceLabels arrayArg
69+
managedNamespaceAnnots arrayArg
6870
includedNamespacesRegex string
6971
)
7072

@@ -99,11 +101,29 @@ func main() {
99101
flag.Var(&excludedNamespaces, "excluded-namespace", "A namespace that, if present, will be excluded from HNC management. May be specified multiple times, with each instance specifying one namespace. See the user guide for more information.")
100102
flag.StringVar(&includedNamespacesRegex, "included-namespace-regex", ".*", "Namespace regular expression. Namespaces that match this regexp will be included and handle by HNC. As it is a regex, this parameter cannot be specified multiple times. Implicit wrapping of the expression \"^...$\" is done here")
101103
flag.BoolVar(&restartOnSecretRefresh, "cert-restart-on-secret-refresh", false, "Kills the process when secrets are refreshed so that the pod can be restarted (secrets take up to 60s to be updated by running pods)")
104+
flag.Var(&managedNamespaceLabels, "managed-namespace-labels", "A list of labels on namespaces that are managed by HNC. These labels may only be set via the HierarchyConfiguration object. See the user guide for more information.")
105+
flag.Var(&managedNamespaceAnnots, "managed-namespace-annotations", "A list of annotations on namespaces that are managed by HNC. These labels may only be set via the HierarchyConfiguration object. See the user guide for more details.")
102106
flag.Parse()
107+
103108
// Assign the array args to the configuration variables after the args are parsed.
104109
config.UnpropagatedAnnotations = unpropagatedAnnotations
105-
106110
config.SetNamespaces(includedNamespacesRegex, excludedNamespaces...)
111+
config.ManagedNamespaceLabels = map[string]bool{}
112+
for _, l := range managedNamespaceLabels {
113+
if strings.Contains(l, v1a2.MetaGroup) {
114+
setupLog.Info("Error: cannot use --managed-namespace-labels to control labels in the " + v1a2.MetaGroup + " group")
115+
os.Exit(1)
116+
}
117+
config.ManagedNamespaceLabels[l] = true
118+
}
119+
config.ManagedNamespaceAnnotations = map[string]bool{}
120+
for _, a := range managedNamespaceAnnots {
121+
if strings.Contains(a, v1a2.MetaGroup) {
122+
setupLog.Info("Error: cannot use --managed-namespace-annotations to control annotations in the " + v1a2.MetaGroup + " group")
123+
os.Exit(1)
124+
}
125+
config.ManagedNamespaceAnnotations[a] = true
126+
}
107127

108128
// Enable OpenCensus exporters to export metrics
109129
// to Stackdriver Monitoring.

config/crd/bases/hnc.x-k8s.io_hierarchyconfigurations.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,44 @@ spec:
4646
description: AllowCascadingDeletion indicates if the subnamespaces
4747
of this namespace are allowed to cascading delete.
4848
type: boolean
49+
annotations:
50+
description: Annotations is a list of annotations and values to apply
51+
to the current namespace and all of its descendants. All annotation
52+
keys must be specified on the command line by --managed-namespace-annotations.
53+
A namespace cannot have a KVP that conflicts with one of its ancestors.
54+
items:
55+
description: MetaKVP represents a label or annotation
56+
properties:
57+
name:
58+
description: Name is the name of the label or annotation.
59+
type: string
60+
value:
61+
description: Value is the value of the label or annotation.
62+
type: string
63+
required:
64+
- name
65+
- value
66+
type: object
67+
type: array
68+
labels:
69+
description: Lables is a list of labels and values to apply to the
70+
current namespace and all of its descendants. All label keys must
71+
be specified on the command line by --managed-namespace-labels.
72+
A namespace cannot have a KVP that conflicts with one of its ancestors.
73+
items:
74+
description: MetaKVP represents a label or annotation
75+
properties:
76+
name:
77+
description: Name is the name of the label or annotation.
78+
type: string
79+
value:
80+
description: Value is the value of the label or annotation.
81+
type: string
82+
required:
83+
- name
84+
- value
85+
type: object
86+
type: array
4987
parent:
5088
description: Parent indicates the parent of this namespace, if any.
5189
type: string

internal/config/default_config.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
package config
22

3-
// UnpropgatedAnnotations is a list of annotations on objects that should _not_ be propagated by HNC.
4-
// Much like HNC itself, other systems (such as GKE Config Sync) use annotations to "claim" an
5-
// object - such as deleting objects it doesn't recognize. By removing these annotations on
6-
// propgated objects, HNC ensures that other systems won't attempt to claim the same object.
7-
//
8-
// This value is controlled by the --unpropagated-annotation command line, which may be set multiple
9-
// times.
10-
var UnpropagatedAnnotations []string
3+
var (
4+
// UnpropgatedAnnotations is a list of annotations on objects that should _not_ be propagated by HNC.
5+
// Much like HNC itself, other systems (such as GKE Config Sync) use annotations to "claim" an
6+
// object - such as deleting objects it doesn't recognize. By removing these annotations on
7+
// propgated objects, HNC ensures that other systems won't attempt to claim the same object.
8+
//
9+
// This value is controlled by the --unpropagated-annotation command line, which may be set multiple
10+
// times.
11+
UnpropagatedAnnotations []string
12+
13+
// ManagedNamespaceLabels is a set of labels whose values are controlled by the "labels" field in
14+
// the HierarchyController CR. Any label in this list is removed from all managed namespaces
15+
// unless specifically specified by the HC of the namespace or one of its ancestors.
16+
ManagedNamespaceLabels map[string]bool
17+
18+
// ManagedNamespaceAnnotations is a set of annotations whose values are controlled by the "labels" field in
19+
// the HierarchyController CR. Any label in this list is removed from all managed namespaces
20+
// unless specifically specified by the HC of the namespace or one of its ancestors.
21+
ManagedNamespaceAnnotations map[string]bool
22+
)

internal/forest/namespace.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ type Namespace struct {
3232
// and to store the tree labels of external namespaces.
3333
labels map[string]string
3434

35+
// ManagedLabels are all managed labels explicitly set on this namespace (i.e., excluding anything
36+
// set by ancestors).
37+
ManagedLabels map[string]string
38+
39+
// ManagedAnnotations are all managed annotations explicitly set on this namespace (i.e.,
40+
// excluding anything set by ancestors).
41+
ManagedAnnotations map[string]string
42+
3543
// sourceObjects store the objects created by users, identified by GVK and name.
3644
// It serves as the source of truth for object controllers to propagate objects.
3745
sourceObjects objects

0 commit comments

Comments
 (0)