|
| 1 | +# Modifying Kubernetes objects |
| 2 | + |
| 3 | +You can delegate Kubernetes object manipulation to the module-sdk. |
| 4 | + |
| 5 | +To do this, you have to write one or more JSON or YAML documents describing operation type and its parameters to a file. |
| 6 | +List of possible operations and corresponding JSON specifications can be found below. |
| 7 | + |
| 8 | +The path to the file is found in the `$KUBERNETES_PATCH_PATH` environment variable. |
| 9 | + |
| 10 | +## Operations |
| 11 | + |
| 12 | +### Create |
| 13 | + |
| 14 | +* `operation` — specifies an operation's type. |
| 15 | + * `CreateOrUpdate` — accept a Kubernetes object. |
| 16 | + It retrieves an object, and if it already exists, computes a JSON Merge Patch and applies it (will not update .status field). |
| 17 | + If it does not exist, we create the object. |
| 18 | + * `Create` — will fail if an object already exists |
| 19 | + * `CreateIfNotExists` — create an object if such an object does not already |
| 20 | + exist by namespace/name. |
| 21 | +* `object` — full object specification including "apiVersion", "kind" and all necessary metadata. Can be a normal JSON or YAML object or a stringified JSON or YAML object. |
| 22 | + |
| 23 | +#### Example |
| 24 | + |
| 25 | +```json |
| 26 | +{ |
| 27 | + "operation": "CreateOrUpdate", |
| 28 | + "object": { |
| 29 | + "apiVersion": "apps/v1", |
| 30 | + "kind": "DaemonSet", |
| 31 | + "metadata": { |
| 32 | + "name": "flannel", |
| 33 | + "namespace": "d8-flannel" |
| 34 | + }, |
| 35 | + "spec": { |
| 36 | + "selector": { |
| 37 | + "matchLabels": { |
| 38 | + "app": "flannel" |
| 39 | + } |
| 40 | + }, |
| 41 | + "template": { |
| 42 | + "metadata": { |
| 43 | + "labels": { |
| 44 | + "app": "flannel", |
| 45 | + "tier": "node" |
| 46 | + } |
| 47 | + }, |
| 48 | + "spec": { |
| 49 | + "containers": [ |
| 50 | + { |
| 51 | + "args": [ |
| 52 | + "--ip-masq", |
| 53 | + "--kube-subnet-mgr" |
| 54 | + ], |
| 55 | + "image": "flannel:v0.11", |
| 56 | + "name": "kube-flannel", |
| 57 | + "securityContext": { |
| 58 | + "privileged": true |
| 59 | + } |
| 60 | + } |
| 61 | + ], |
| 62 | + "hostNetwork": true, |
| 63 | + "imagePullSecrets": [ |
| 64 | + { |
| 65 | + "name": "registry" |
| 66 | + } |
| 67 | + ], |
| 68 | + "terminationGracePeriodSeconds": 5 |
| 69 | + } |
| 70 | + }, |
| 71 | + "updateStrategy": { |
| 72 | + "type": "RollingUpdate" |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +```yaml |
| 80 | +operation: Create |
| 81 | +object: |
| 82 | + apiVersion: v1 |
| 83 | + kind: ConfigMap |
| 84 | + metadata: |
| 85 | + namespace: default |
| 86 | + name: testcm |
| 87 | + data: ... |
| 88 | +``` |
| 89 | +
|
| 90 | +```yaml |
| 91 | +operation: Create |
| 92 | +object: | |
| 93 | + {"apiVersion":"v1", "kind":"ConfigMap", |
| 94 | + "metadata":{"namespace":"default","name":"testcm"}, |
| 95 | + "data":{"foo": "bar"}} |
| 96 | +``` |
| 97 | +
|
| 98 | +### Delete |
| 99 | +
|
| 100 | +* `operation` — specifies an operation's type. Deletion types map directly to Kubernetes |
| 101 | + DELETE's [`propagationPolicy`][controller-gc]. |
| 102 | + * `Delete` — foreground deletion. Module-sdk will block the queue until the referenced object and all its descendants are deleted. |
| 103 | + * `DeleteInBackground` — will delete the referenced object immediately. All its descendants will be removed by Kubernetes' |
| 104 | + garbage collector. |
| 105 | + * `DeleteNonCascading` — will delete the referenced object immediately, and orphan all its descendants. |
| 106 | +* `apiVersion` — optional field that specifies object's apiVersion. If not present, we'll use preferred apiVersion |
| 107 | + for the given kind. |
| 108 | +* `kind` — object's Kind. |
| 109 | +* `namespace` — object's namespace. If empty, implies operation on a cluster-level resource. |
| 110 | +* `name` — object's name. |
| 111 | +* `subresource` — a subresource name if subresource is to be transformed. For example, `status`. |
| 112 | + |
| 113 | +#### Example |
| 114 | + |
| 115 | +```json |
| 116 | +{ |
| 117 | + "operation": "Delete", |
| 118 | + "kind": "Pod", |
| 119 | + "namespace": "default", |
| 120 | + "name": "nginx" |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### Patch |
| 125 | + |
| 126 | +Use `JQPatch` for almost everything. Consider using `MergePatch` or `JSONPatch` if you are attempting to modify |
| 127 | +rapidly changing object, for example `status` field with many concurrent changes (and incrementing `resourceVersion`). |
| 128 | + |
| 129 | +Be careful, when updating a `.status` field. If a `/status` subresource is enabled on a resource, |
| 130 | +it'll ignore updates to the `.status` field if you haven't specified `subresource: status` in the operation spec. |
| 131 | +More info [here][spec-and-status]. |
| 132 | + |
| 133 | +#### JQPatch |
| 134 | + |
| 135 | +* `operation` — specifies an operation's type. |
| 136 | +* `apiVersion` — optional field that specifies object's apiVersion. If not present, we'll use preferred apiVersion |
| 137 | + for the given kind. |
| 138 | +* `kind` — object's Kind. |
| 139 | +* `namespace` — object's Namespace. If empty, implies operation on a Cluster-level resource. |
| 140 | +* `name` — object's name. |
| 141 | +* `jqFilter` — describes transformations to perform on an object. |
| 142 | +* `subresource` — a subresource name if subresource is to be transformed. For example, `status`. |
| 143 | +##### Example |
| 144 | + |
| 145 | +```json |
| 146 | +{ |
| 147 | + "operation": "JQPatch", |
| 148 | + "kind": "Deployment", |
| 149 | + "namespace": "default", |
| 150 | + "name": "nginx", |
| 151 | + "jqFilter": ".spec.replicas = 1" |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +#### MergePatch |
| 156 | + |
| 157 | +* `operation` — specifies an operation's type. |
| 158 | +* `apiVersion` — optional field that specifies object's apiVersion. If not present, we'll use preferred apiVersion |
| 159 | + for the given kind. |
| 160 | +* `kind` — object's Kind. |
| 161 | +* `namespace` — object's Namespace. If empty, implies operation on a Cluster-level resource. |
| 162 | +* `name` — object's name. |
| 163 | +* `mergePatch` — describes transformations to perform on an object. Can be a normal JSON or YAML object or a stringified JSON or YAML object. |
| 164 | +* `subresource` — e.g., `status`. |
| 165 | +* `ignoreMissingObject` — set to true to ignore error when patching non existent object. |
| 166 | + |
| 167 | +##### Example |
| 168 | + |
| 169 | +```json |
| 170 | +{ |
| 171 | + "operation": "MergePatch", |
| 172 | + "kind": "Deployment", |
| 173 | + "namespace": "default", |
| 174 | + "name": "nginx", |
| 175 | + "mergePatch": { |
| 176 | + "spec": { |
| 177 | + "replicas": 1 |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +```yaml |
| 184 | +operation: MergePatch |
| 185 | +kind: Deployment |
| 186 | +namespace: default |
| 187 | +name: nginx |
| 188 | +ignoreMissingObject: true |
| 189 | +mergePatch: | |
| 190 | + spec: |
| 191 | + replicas: 1 |
| 192 | +``` |
| 193 | + |
| 194 | +#### JSONPatch |
| 195 | + |
| 196 | +* `operation` — specifies an operation's type. |
| 197 | +* `apiVersion` — optional field that specifies object's apiVersion. If not present, we'll use preferred apiVersion |
| 198 | + for the given kind. |
| 199 | +* `kind` — object's Kind. |
| 200 | +* `namespace` — object's Namespace. If empty, implies operation on a Cluster-level resource. |
| 201 | +* `name` — object's name. |
| 202 | +* `jsonPatch` — describes transformations to perform on an object. Can be a normal JSON or YAML array or a stringified JSON or YAML array. |
| 203 | +* `subresource` — a subresource name if subresource is to be transformed. For example, `status`. |
| 204 | +* `ignoreMissingObject` — set to true to ignore error when patching non existent object. |
| 205 | + |
| 206 | +##### Example |
| 207 | + |
| 208 | +```json |
| 209 | +{ |
| 210 | + "operation": "JSONPatch", |
| 211 | + "kind": "Deployment", |
| 212 | + "namespace": "default", |
| 213 | + "name": "nginx", |
| 214 | + "jsonPatch": [ |
| 215 | + {"op": "replace", "path": "/spec/replicas", "value": 1} |
| 216 | + ] |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +```json |
| 221 | +{ |
| 222 | + "operation": "JSONPatch", |
| 223 | + "kind": "Deployment", |
| 224 | + "namespace": "default", |
| 225 | + "name": "nginx", |
| 226 | + "jsonPatch": "[ |
| 227 | + {\"op\": \"replace\", \"path\": \"/spec/replicas\", \"value\": 1} |
| 228 | + ]" |
| 229 | +} |
| 230 | +``` |
| 231 | + |
| 232 | +[controller-gc]: https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/ |
| 233 | +[spec-and-status]: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#spec-and-status |
0 commit comments