@@ -17,68 +17,114 @@ Namespaces are often used in the following scenarios:
17
17
18
18
## Usage
19
19
20
- This function can be used with any KRM function orchestrators (e.g. kpt).
21
-
22
- - If the resource is ` Namespace ` , ` set-namespace ` updates the ` metadata.name ` field.
23
- - If the resource is ` RoleBinding ` or ` ClusterRoleBinding ` resource, the function updates
24
- the namespace field in the ` subjects ` element whose name is ` default ` .
25
- - If the resource is ` CustomResourceDefinition ` (CRD), ` set-namespace ` updates the
26
- ` spec/conversion/webhook/clientConfig/service/namespace ` field.
27
- - If the resource is ` APIService ` , ` set-namespace ` updates the
28
- ` spec/service/namespace ` field.
29
- - If there is a [ ` depends-on ` ] annotation for a namespaced resource, the namespace
30
- section of the annotation will be updated if the referenced resource is also
31
- declared in the package.
20
+ This function replaces the KRM resources existing namespace to a new value.
32
21
22
+ ### Target KRM resources
23
+
24
+ - This function updates all namespace-scoped KRM resources ` metadata.namespace ` fields.
25
+ We determine whether a KRM resource is namespace scoped by checking if it has ` metadata.namespace ` set and matches the "oldNamespace"
26
+ If not, this function won't add new namespace.
27
+ - This function updates ` RoleBinding ` and ` ClusterRoleBinding ` resources ` subjects ` element whose kind is ` ServiceAccount `
28
+ and the subject's ` namespace ` is set and matches the "oldNamespace".
29
+ - This function updates ` CustomResourceDefinition ` (CRD) ` spec/conversion/webhook/clientConfig/service/namespace ` field
30
+ if the field is set and matches the "oldNamespace"
31
+ - This function updates ` APIService ` ` spec/service/namespace ` field if the field is set and matches the "oldNamespace"
32
+ - This function updates the KRM resources annotation ` config.kubernetes.io/depends-on ` if this annotation contains the
33
+ matching namespace.
34
+
35
+ ### FunctionConfig
36
+
37
+ This function supports the default ` ConfigMap ` as function config and a custom ` SetNamespace ` . See below examples
38
+
39
+ ` ConfigMap ` as functionConfig
33
40
``` yaml
34
41
apiVersion : v1
35
- kind : ServiceAccount
36
- metadata :
37
- name : sa
38
- namespace : example
39
- annotations :
40
- config.kubernetes.io/depends-on : /namespaces/example/ServiceAccount/foo # <= this will NOT be updated (resource not declared)
41
- ---
42
- kind : RoleBinding
43
- apiVersion : rbac.authorization.k8s.io/v1
44
- metadata :
45
- ...
46
- annotations :
47
- config.kubernetes.io/depends-on : /namespaces/example/ServiceAccount/sa # <== this will be updated (resource declared)
48
- subjects :
49
- - kind : ServiceAccount
50
- name : default # <================== name default is used
51
- namespace : example # <================== this will be updated
52
- roleRef :
53
- kind : Role
54
- name : confluent-operator
55
- apiGroup : rbac.authorization.k8s.io
42
+ kind : ConfigMap
43
+ data :
44
+ namespace : newNamespace # required
45
+ namespaceMatcher : example # update namespace whose value is "example" to "newNamespace"
56
46
` ` `
57
47
58
- This function can be used both declaratively and imperatively.
48
+ ` SetNamespace` as functionConfig
49
+ ` ` ` yaml
50
+ apiVersion: fn.kpt.dev/v1alpha1
51
+ kind: SetNamespace
52
+ namespace: newNamespace # required
53
+ namespaceMatcher: example # update namespace whose value is "example" to "newNamespace"
54
+ ` ` `
59
55
60
- ### FunctionConfig
61
56
62
- There are 2 kinds of ` functionConfig` supported by this function:
57
+ # ## Three updating modes
63
58
64
- - ` ConfigMap`
65
- - A custom resource of kind `SetNamespace`
59
+ This function supports three modes to flexibly choose and update the target namespaces.
66
60
67
- To use a `ConfigMap` as the `functionConfig`, the desired namespace must be
68
- specified in the `data. namespace` field.
61
+ # #### Restrict Mode
62
+ All target KRM resources namespace has to have the same value. All namespace will be updated to the new value.
69
63
70
- To add a namespace `staging` to all resources, we use the
71
- following `functionConfig` :
64
+ ` ConfigMap` as functionConfig
65
+ ` ` ` yaml
66
+ apiVersion: v1
67
+ kind: ConfigMap
68
+ data:
69
+ namespace: newNamespace # update all namespace fields to "newNamespace"
70
+ ` ` `
71
+
72
+ # #### DefaultNamespace Mode
73
+
74
+ The input `resourcelist.items` contains one and only one `Namespace` object. The function matches the namespace `metadata.name`
75
+ with all other KRM resources, and only update the namespace if it matches the `Namespace` object.
76
+ If more than one `Namespace` objects are found, raise errors;
72
77
78
+ ` ` ` yaml
79
+ kind: ResourceList
80
+ functionConfig:
81
+ apiVersion: v1
82
+ kind: ConfigMap
83
+ data:
84
+ namespace: newNs
85
+ items:
86
+ - apiVersion: v1
87
+ kind: Namespace
88
+ metadata:
89
+ name: example # updated to "newNs"
90
+ - apiVersion: v1
91
+ kind: Service
92
+ metadata:
93
+ name: the-service1
94
+ namespace: example # updated to "newNs"
95
+ - apiVersion: v1
96
+ kind: Service
97
+ metadata:
98
+ name: the-service2
99
+ namespace: irrelevant # skip since namespace does not match "example".
100
+ ` ` `
101
+
102
+ # #### Matcher Mode
103
+
104
+ Only updates the namespace which matches a given value. The "oldNamespace" refers to the argument in FunctionConfig
105
+
106
+ ` ConfigMap` as functionConfig
73
107
` ` ` yaml
74
108
apiVersion: v1
75
109
kind: ConfigMap
76
- metadata:
77
- name: my-config
78
110
data:
79
- namespace: staging
111
+ namespace: newNamespace
112
+ namespaceMatcher: example # update namespace whose value is "example" to "newNamespace"
113
+ ` ` `
114
+
115
+ ` SetNamespace` as functionConfig
116
+ ` ` ` yaml
117
+ apiVersion: fn.kpt.dev/v1alpha1
118
+ kind: SetNamespace
119
+ namespace: newNamespace
120
+ namespaceMatcher: example # update namespace whose value is "example" to "newNamespace"
80
121
` ` `
81
122
123
+ # ## DependsOn annotation
124
+
125
+ DependsOn annotation is a [kpt feature](https://kpt.dev/reference/annotations/depends-on/). This function updates the
126
+ namespace segment in a depends-on annotation if the namespace matches the `Namespace` object or `namespaceMatcher` field.
127
+
82
128
<!--mdtogo-->
83
129
84
130
[namespace] : https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
0 commit comments