Skip to content

Commit a6a36cf

Browse files
Merge pull request #875 from andrewbelu/main
feat(overrides): add JSON patch for deployments
2 parents 6bc9167 + 26eb11f commit a6a36cf

File tree

10 files changed

+328
-15
lines changed

10 files changed

+328
-15
lines changed

api/v1beta1/temporalcluster_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ type DeploymentOverride struct {
174174
*ObjectMetaOverride `json:"metadata,omitempty"`
175175
// Specification of the desired behavior of the Deployment.
176176
// +optional
177-
Spec *DeploymentOverrideSpec `json:"spec,omitempty"`
177+
Spec *DeploymentOverrideSpec `json:"spec,omitempty"`
178+
JSONPatch *apiextensionsv1.JSON `json:"jsonPatch,omitempty"`
178179
}
179180

180181
// DeploymentOverrideSpec provides the ability to override a Deployment Spec.

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/temporal.io_temporalclusters.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ spec:
6464
deployment:
6565
description: Override configuration for the temporal service Deployment.
6666
properties:
67+
jsonPatch:
68+
x-kubernetes-preserve-unknown-fields: true
6769
metadata:
6870
description: |-
6971
ObjectMetaOverride provides the ability to override an object metadata.
@@ -2916,6 +2918,8 @@ spec:
29162918
deployment:
29172919
description: Override configuration for the temporal service Deployment.
29182920
properties:
2921+
jsonPatch:
2922+
x-kubernetes-preserve-unknown-fields: true
29192923
metadata:
29202924
description: |-
29212925
ObjectMetaOverride provides the ability to override an object metadata.
@@ -3081,6 +3085,8 @@ spec:
30813085
deployment:
30823086
description: Override configuration for the temporal service Deployment.
30833087
properties:
3088+
jsonPatch:
3089+
x-kubernetes-preserve-unknown-fields: true
30843090
metadata:
30853091
description: |-
30863092
ObjectMetaOverride provides the ability to override an object metadata.
@@ -3252,6 +3258,8 @@ spec:
32523258
deployment:
32533259
description: Override configuration for the temporal service Deployment.
32543260
properties:
3261+
jsonPatch:
3262+
x-kubernetes-preserve-unknown-fields: true
32553263
metadata:
32563264
description: |-
32573265
ObjectMetaOverride provides the ability to override an object metadata.
@@ -3417,6 +3425,8 @@ spec:
34173425
deployment:
34183426
description: Override configuration for the temporal service Deployment.
34193427
properties:
3428+
jsonPatch:
3429+
x-kubernetes-preserve-unknown-fields: true
34203430
metadata:
34213431
description: |-
34223432
ObjectMetaOverride provides the ability to override an object metadata.
@@ -3555,6 +3565,8 @@ spec:
35553565
deployment:
35563566
description: Override configuration for the temporal service Deployment.
35573567
properties:
3568+
jsonPatch:
3569+
x-kubernetes-preserve-unknown-fields: true
35583570
metadata:
35593571
description: |-
35603572
ObjectMetaOverride provides the ability to override an object metadata.
@@ -3643,6 +3655,8 @@ spec:
36433655
deployment:
36443656
description: Override configuration for the temporal service Deployment.
36453657
properties:
3658+
jsonPatch:
3659+
x-kubernetes-preserve-unknown-fields: true
36463660
metadata:
36473661
description: |-
36483662
ObjectMetaOverride provides the ability to override an object metadata.
@@ -3835,6 +3849,8 @@ spec:
38353849
deployment:
38363850
description: Override configuration for the temporal service Deployment.
38373851
properties:
3852+
jsonPatch:
3853+
x-kubernetes-preserve-unknown-fields: true
38383854
metadata:
38393855
description: |-
38403856
ObjectMetaOverride provides the ability to override an object metadata.

docs/api/v1beta1.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,18 @@ DeploymentOverrideSpec
14001400
</table>
14011401
</td>
14021402
</tr>
1403+
<tr>
1404+
<td>
1405+
<code>jsonPatch</code><br>
1406+
<em>
1407+
<a href="https://pkg.go.dev/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1#JSON">
1408+
k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1.JSON
1409+
</a>
1410+
</em>
1411+
</td>
1412+
<td>
1413+
</td>
1414+
</tr>
14031415
</tbody>
14041416
</table>
14051417
</div>

docs/features/overrides.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ The API provides you the ability to apply your overrides:
1515
- per temporal service (using `spec.services.[frontend|history|matching|worker].overrides`)
1616
- for all services (using `spec.services.overrides`)
1717

18+
There are two ways of performing overrides, one is via StrategicPatchMerge and one using RFC6902 JSON patches. You can find examples of both below. If working with certain fields that aren't handled by StrategicPatchMerge properly (i.e., arrays that don't have go struct tags for merging valid for your use case), you may want to consider using JSON patches.
19+
1820
## Overrides for all services
1921

2022
Here is a general example:
@@ -210,7 +212,7 @@ spec:
210212
value: example.com
211213
```
212214
213-
### Example: mount an extra volume to the frontend pod
215+
### Example: Mount an extra secret volume to the frontend pod
214216
215217
```yaml
216218
apiVersion: temporal.io/v1beta1
@@ -223,18 +225,18 @@ spec:
223225
frontend:
224226
overrides:
225227
deployment:
226-
spec:
227-
template:
228-
spec:
229-
containers:
230-
- name: service
231-
volumeMounts:
232-
- name: extra-volume
233-
mountPath: /etc/extra
234-
volumes:
235-
- name: extra-volume
236-
configMap:
237-
name: extra-config
228+
jsonPatch:
229+
- op: add
230+
path: /spec/template/spec/containers/0/volumeMounts/-
231+
value:
232+
name: extra-volume
233+
mountPath: /etc/extra
234+
- op: add
235+
path: /spec/template/spec/volumes/-
236+
value:
237+
name: extra-volume
238+
secret:
239+
secretName: test-secret
238240
```
239241
240242
### Example: Add an environment variable from secretRef to the frontend pod
@@ -286,6 +288,29 @@ spec:
286288
service: frontend.temporal.temporal.svc.cluster.local
287289
```
288290
291+
### Example: Add environment variable from a secret to frontend pod
292+
```yaml
293+
apiVersion: temporal.io/v1beta1
294+
kind: TemporalCluster
295+
metadata:
296+
name: prod
297+
spec:
298+
# [...]
299+
services:
300+
frontend:
301+
overrides:
302+
deployment:
303+
jsonPatch:
304+
- op: add
305+
path: /spec/template/spec/containers/0/env/-
306+
value:
307+
name: TEST
308+
valueFrom:
309+
secretKeyRef:
310+
name: test-secret
311+
key: test
312+
```
313+
289314
Read more in [Strategic Merge Patch](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md#strategic-merge-patch).
290315
291316
## Override UI deployment

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/alexandrevilain/controller-tools v0.3.0
1010
github.com/cert-manager/cert-manager v1.16.3
1111
github.com/elliotchance/orderedmap/v2 v2.4.0
12+
github.com/evanphx/json-patch/v5 v5.9.0
1213
github.com/go-logr/logr v1.4.2
1314
github.com/gocql/gocql v1.7.0
1415
github.com/google/uuid v1.6.0
@@ -53,7 +54,6 @@ require (
5354
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
5455
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da // indirect
5556
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
56-
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
5757
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
5858
github.com/felixge/httpsnoop v1.0.4 // indirect
5959
github.com/fxamacker/cbor/v2 v2.7.0 // indirect

pkg/kubernetes/overrides.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/alexandrevilain/temporal-operator/api/v1beta1"
2525
"github.com/alexandrevilain/temporal-operator/internal/metadata"
26+
jsonpatch "github.com/evanphx/json-patch/v5"
2627
appsv1 "k8s.io/api/apps/v1"
2728
corev1 "k8s.io/api/core/v1"
2829
"k8s.io/apimachinery/pkg/util/strategicpatch"
@@ -110,6 +111,24 @@ func ApplyDeploymentOverrides(deployment *appsv1.Deployment, override *v1beta1.D
110111
}
111112
}
112113

114+
if override.JSONPatch != nil {
115+
patch, err := jsonpatch.DecodePatch(override.JSONPatch.Raw)
116+
if err != nil {
117+
return fmt.Errorf("can't decode json patch: %w", err)
118+
}
119+
120+
original, err := json.Marshal(deployment)
121+
if err != nil {
122+
return fmt.Errorf("can't marshal deployment spec: %w", err)
123+
}
124+
125+
patched, err := patch.Apply(original)
126+
if err != nil {
127+
return fmt.Errorf("can't apply json patch: %w", err)
128+
}
129+
return json.Unmarshal(patched, &deployment)
130+
}
131+
113132
return nil
114133
}
115134

0 commit comments

Comments
 (0)