From 00e6c9df16c21afc5e0eaae38340c9662ec50e47 Mon Sep 17 00:00:00 2001 From: Haochuan Date: Thu, 16 Feb 2023 14:26:21 +0900 Subject: [PATCH 1/3] Support custom deployment strategy type --- jsonnetlib/config.jsonnet | 1 + jsonnetlib/k8s.jsonnet | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/jsonnetlib/config.jsonnet b/jsonnetlib/config.jsonnet index 0d05cf1..46cbf38 100644 --- a/jsonnetlib/config.jsonnet +++ b/jsonnetlib/config.jsonnet @@ -27,6 +27,7 @@ assert std.length(v) > 0 : 'version is empty'; terminationGracePeriodSeconds: 30, ingressClassName: 'nginx', pathType: 'ImplementationSpecific', + strategyType: 'RollingUpdate', // config helper func local root = self, diff --git a/jsonnetlib/k8s.jsonnet b/jsonnetlib/k8s.jsonnet index 5b305e4..eef9305 100644 --- a/jsonnetlib/k8s.jsonnet +++ b/jsonnetlib/k8s.jsonnet @@ -334,6 +334,7 @@ cfg { maxReplicas=3, targetCPUUtilizationPercentage=75, podSpec=root.podSpec, + strategyType=root.strategyType, ):: { apiVersion: 'v1', kind: 'List', @@ -359,6 +360,7 @@ cfg { volumes=volumes, terminationGracePeriodSeconds=terminationGracePeriodSeconds, podSpec=podSpec, + strategyType=strategyType, ), root.svc(namespace, name, port, targetPort), root.single_svc_ingress( @@ -476,6 +478,7 @@ cfg { volumes=[], terminationGracePeriodSeconds=root.terminationGracePeriodSeconds, podSpec=root.podSpec, + strategyType=root.strategyType ):: { local labels = { app: name }, local probe = if withoutProbe then {} @@ -548,7 +551,7 @@ cfg { maxSurge: maxSurge, maxUnavailable: 0, }, - type: 'RollingUpdate', + type: strategyType, }, selector: { matchLabels: labels, From 7312c8acd6170429c00b40aa6ae99741f372b160 Mon Sep 17 00:00:00 2001 From: Haochuan Date: Thu, 16 Feb 2023 16:21:38 +0900 Subject: [PATCH 2/3] Support custom deployment strategy type --- jsonnetlib/config.jsonnet | 1 - jsonnetlib/k8s.jsonnet | 28 +++++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/jsonnetlib/config.jsonnet b/jsonnetlib/config.jsonnet index 46cbf38..0d05cf1 100644 --- a/jsonnetlib/config.jsonnet +++ b/jsonnetlib/config.jsonnet @@ -27,7 +27,6 @@ assert std.length(v) > 0 : 'version is empty'; terminationGracePeriodSeconds: 30, ingressClassName: 'nginx', pathType: 'ImplementationSpecific', - strategyType: 'RollingUpdate', // config helper func local root = self, diff --git a/jsonnetlib/k8s.jsonnet b/jsonnetlib/k8s.jsonnet index eef9305..ab4288c 100644 --- a/jsonnetlib/k8s.jsonnet +++ b/jsonnetlib/k8s.jsonnet @@ -334,7 +334,7 @@ cfg { maxReplicas=3, targetCPUUtilizationPercentage=75, podSpec=root.podSpec, - strategyType=root.strategyType, + withoutRollingUpdate=false, ):: { apiVersion: 'v1', kind: 'List', @@ -360,7 +360,7 @@ cfg { volumes=volumes, terminationGracePeriodSeconds=terminationGracePeriodSeconds, podSpec=podSpec, - strategyType=strategyType, + withoutRollingUpdate=withoutRollingUpdate, ), root.svc(namespace, name, port, targetPort), root.single_svc_ingress( @@ -478,7 +478,7 @@ cfg { volumes=[], terminationGracePeriodSeconds=root.terminationGracePeriodSeconds, podSpec=root.podSpec, - strategyType=root.strategyType + withoutRollingUpdate=false, ):: { local labels = { app: name }, local probe = if withoutProbe then {} @@ -537,6 +537,19 @@ cfg { }, }, }, + local strategy = if withoutRollingUpdate then { + strategy: { + type: 'Recreate', + }, + } else { + strategy: { + rollingUpdate: { + maxSurge: maxSurge, + maxUnavailable: 0, + }, + type: 'RollingUpdate', + }, + }, apiVersion: 'apps/v1', kind: 'Deployment', metadata: { @@ -546,13 +559,6 @@ cfg { }, spec: { replicas: replicas, - strategy: { - rollingUpdate: { - maxSurge: maxSurge, - maxUnavailable: 0, - }, - type: strategyType, - }, selector: { matchLabels: labels, }, @@ -586,7 +592,7 @@ cfg { ], } + affinity + vols + imagePullSecretsRef(imagePullSecrets) + podSpec + graceShutdown, }, - }, + } + strategy, }, pdb(namespace=root.defaultNamespace, name, minAvailable=root.minAvailable):: { From 3d32c496853d3c1000fed02355d3949dea726441 Mon Sep 17 00:00:00 2001 From: Haochuan Date: Thu, 16 Feb 2023 16:23:34 +0900 Subject: [PATCH 3/3] Add test case for withoutRollingUpdate --- .../test_image2url_without_rolling_update.jsonnet | 11 +++++++++++ test_cases.json | 13 +++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 example/k8s/tests/test_image2url_without_rolling_update.jsonnet diff --git a/example/k8s/tests/test_image2url_without_rolling_update.jsonnet b/example/k8s/tests/test_image2url_without_rolling_update.jsonnet new file mode 100644 index 0000000..81a75b2 --- /dev/null +++ b/example/k8s/tests/test_image2url_without_rolling_update.jsonnet @@ -0,0 +1,11 @@ +local k8s = import 'k8s.jsonnet'; + +k8s.list([ + k8s.image_to_url( + namespace='example', + name='demo', + host='demo.example.theplant-dev.com', + path='/', + withoutRollingUpdate=true + ), +]) diff --git a/test_cases.json b/test_cases.json index 4451aa0..9f8e932 100644 --- a/test_cases.json +++ b/test_cases.json @@ -257,5 +257,18 @@ "expected": "500m" } ] + }, + { + "show_args": "./example/k8s/tests/test_image2url_without_rolling_update.jsonnet", + "asserts": [ + { + "jq_path": ".items[0].spec.strategy.type", + "expected": "Recreate" + }, + { + "jq_path": ".items[0].spec.strategy.rollingUpdate", + "expected": null + } + ] } ]