From ce57fbb752d64686d2ebca8a16efaf9a0de5b57a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 12 Jan 2023 17:19:17 +0800 Subject: [PATCH] Feat: need one Trait to set Rollout strategy of Workload (#5327) Signed-off-by: StevenLeiZhang (cherry picked from commit 5d00b2ac73ed448069b1c65dc2c1f94c3a44fa5a) Co-authored-by: StevenLeiZhang --- .../defwithtemplate/k8s-update-strategy.yaml | 77 ++++++++++++++++ .../def-doc/trait/k8s-update-strategy.eg.md | 91 +++++++++++++++++++ .../internal/trait/k8s-update-strategy.cue | 74 +++++++++++++++ ...ation-daemon-with-k8s-update-strategy.yaml | 54 +++++++++++ .../application-with-k8s-update-strategy.yaml | 33 +++++++ 5 files changed, 329 insertions(+) create mode 100644 charts/vela-core/templates/defwithtemplate/k8s-update-strategy.yaml create mode 100644 references/docgen/def-doc/trait/k8s-update-strategy.eg.md create mode 100644 vela-templates/definitions/internal/trait/k8s-update-strategy.cue create mode 100644 vela-templates/definitions/usage-examples/application-daemon-with-k8s-update-strategy.yaml create mode 100644 vela-templates/definitions/usage-examples/application-with-k8s-update-strategy.yaml diff --git a/charts/vela-core/templates/defwithtemplate/k8s-update-strategy.yaml b/charts/vela-core/templates/defwithtemplate/k8s-update-strategy.yaml new file mode 100644 index 000000000..3558e9b01 --- /dev/null +++ b/charts/vela-core/templates/defwithtemplate/k8s-update-strategy.yaml @@ -0,0 +1,77 @@ +# Code generated by KubeVela templates. DO NOT EDIT. Please edit the original cue file. +# Definition source cue file: vela-templates/definitions/internal/k8s-update-strategy.cue +apiVersion: core.oam.dev/v1beta1 +kind: TraitDefinition +metadata: + annotations: + definition.oam.dev/alias: "" + definition.oam.dev/description: Set k8s update strategy for Deployment/DaemonSet/StatefulSet + name: k8s-update-strategy + namespace: {{ include "systemDefinitionNamespace" . }} +spec: + appliesToWorkloads: + - deployments.apps + - statefulsets.apps + - daemonsets.apps + conflictsWith: [] + podDisruptive: false + schematic: + cue: + template: | + patch: spec: { + if parameter.targetKind == "Deployment" && parameter.strategy.type != "OnDelete" { + // +patchStrategy=retainKeys + strategy: { + type: parameter.strategy.type + if parameter.strategy.type == "RollingUpdate" { + rollingUpdate: { + maxSurge: parameter.strategy.rollingStrategy.maxSurge + maxUnavailable: parameter.strategy.rollingStrategy.maxUnavailable + } + } + } + } + + if parameter.targetKind == "StatefulSet" && parameter.strategy.type != "Recreate" { + // +patchStrategy=retainKeys + updateStrategy: { + type: parameter.strategy.type + if parameter.strategy.type == "RollingUpdate" { + rollingUpdate: partition: parameter.strategy.rollingStrategy.partition + } + } + } + + if parameter.targetKind == "DaemonSet" && parameter.strategy.type != "Recreate" { + // +patchStrategy=retainKeys + updateStrategy: { + type: parameter.strategy.type + if parameter.strategy.type == "RollingUpdate" { + rollingUpdate: { + maxSurge: parameter.strategy.rollingStrategy.maxSurge + maxUnavailable: parameter.strategy.rollingStrategy.maxUnavailable + } + } + } + } + + } + parameter: { + // +usage=Specify the apiVersion of target + targetAPIVersion: *"apps/v1" | string + // +usage=Specify the kind of target + targetKind: *"Deployment" | "StatefulSet" | "DaemonSet" + // +usage=Specify the strategy of update + strategy: { + // +usage=Specify the strategy type + type: *"RollingUpdate" | "Recreate" | "OnDelete" + // +usage=Specify the parameters of rollong update strategy + rollingStrategy?: { + maxSurge: *"25%" | string + maxUnavailable: *"25%" | string + partition: *0 | int + } + } + } + workloadRefPath: "" + diff --git a/references/docgen/def-doc/trait/k8s-update-strategy.eg.md b/references/docgen/def-doc/trait/k8s-update-strategy.eg.md new file mode 100644 index 000000000..d1c05bf74 --- /dev/null +++ b/references/docgen/def-doc/trait/k8s-update-strategy.eg.md @@ -0,0 +1,91 @@ +```yaml +apiVersion: core.oam.dev/v1beta1 +kind: Application +metadata: + name: application-with-update-strategy +spec: + components: + - name: helloworld + type: webservice + properties: + cpu: "0.5" + exposeType: ClusterIP + image: oamdev/hello-world:latest + memory: 1024Mi + ports: + - expose: true + port: 80 + protocol: TCP + traits: + - type: scaler + properties: + replicas: 5 + - type: k8s-update-strategy + properties: + targetAPIVersion: apps/v1 + targetKind: Deployment + strategy: + type: RollingUpdate + rollingStrategy: + maxSurge: 20% + maxUnavailable: 30% +--- +apiVersion: core.oam.dev/v1beta1 +kind: Application +metadata: + name: application-node-exporter +spec: + components: + - name: node-exporter + type: daemon + properties: + image: prom/node-exporter + imagePullPolicy: IfNotPresent + volumeMounts: + hostPath: + - mountPath: /host/sys + mountPropagation: HostToContainer + name: sys + path: /sys + readOnly: true + - mountPath: /host/root + mountPropagation: HostToContainer + name: root + path: / + readOnly: true + traits: + - properties: + args: + - --path.sysfs=/host/sys + - --path.rootfs=/host/root + - --no-collector.wifi + - --no-collector.hwmon + - --collector.filesystem.ignored-mount-points=^/(dev|proc|sys|var/lib/docker/.+|var/lib/kubelet/pods/.+)($|/) + - --collector.netclass.ignored-devices=^(veth.*)$ + type: command + - properties: + annotations: + prometheus.io/path: /metrics + prometheus.io/port: "8080" + prometheus.io/scrape: "true" + port: + - 9100 + type: expose + - properties: + cpu: 0.1 + memory: 250Mi + type: resource + - type: k8s-update-strategy + properties: + targetAPIVersion: apps/v1 + targetKind: DaemonSet + strategy: + type: RollingUpdate + rollingStrategy: + maxSurge: 20% + maxUnavailable: 30% + + + + +``` \ No newline at end of file diff --git a/vela-templates/definitions/internal/trait/k8s-update-strategy.cue b/vela-templates/definitions/internal/trait/k8s-update-strategy.cue new file mode 100644 index 000000000..34eb2fe10 --- /dev/null +++ b/vela-templates/definitions/internal/trait/k8s-update-strategy.cue @@ -0,0 +1,74 @@ +"k8s-update-strategy": { + alias: "" + annotations: {} + attributes: { + appliesToWorkloads: ["deployments.apps", "statefulsets.apps", "daemonsets.apps"] + conflictsWith: [] + podDisruptive: false + workloadRefPath: "" + } + description: "Set k8s update strategy for Deployment/DaemonSet/StatefulSet" + labels: {} + type: "trait" +} + +template: { + patch: { + spec: { + if parameter.targetKind == "Deployment" && parameter.strategy.type != "OnDelete" { + // +patchStrategy=retainKeys + strategy: { + type: parameter.strategy.type + if parameter.strategy.type == "RollingUpdate" { + rollingUpdate: { + maxSurge: parameter.strategy.rollingStrategy.maxSurge + maxUnavailable: parameter.strategy.rollingStrategy.maxUnavailable + } + } + } + } + + if parameter.targetKind == "StatefulSet" && parameter.strategy.type != "Recreate" { + // +patchStrategy=retainKeys + updateStrategy: { + type: parameter.strategy.type + if parameter.strategy.type == "RollingUpdate" { + rollingUpdate: { + partition: parameter.strategy.rollingStrategy.partition + } + } + } + } + + if parameter.targetKind == "DaemonSet" && parameter.strategy.type != "Recreate" { + // +patchStrategy=retainKeys + updateStrategy: { + type: parameter.strategy.type + if parameter.strategy.type == "RollingUpdate" { + rollingUpdate: { + maxSurge: parameter.strategy.rollingStrategy.maxSurge + maxUnavailable: parameter.strategy.rollingStrategy.maxUnavailable + } + } + } + } + + }} + parameter: { + // +usage=Specify the apiVersion of target + targetAPIVersion: *"apps/v1" | string + // +usage=Specify the kind of target + targetKind: *"Deployment" | "StatefulSet" | "DaemonSet" + // +usage=Specify the strategy of update + strategy: { + // +usage=Specify the strategy type + type: *"RollingUpdate" | "Recreate" | "OnDelete" + // +usage=Specify the parameters of rollong update strategy + rollingStrategy?: { + maxSurge: *"25%" | string + maxUnavailable: *"25%" | string + partition: *0 | int + } + } + } +} diff --git a/vela-templates/definitions/usage-examples/application-daemon-with-k8s-update-strategy.yaml b/vela-templates/definitions/usage-examples/application-daemon-with-k8s-update-strategy.yaml new file mode 100644 index 000000000..32c4664b0 --- /dev/null +++ b/vela-templates/definitions/usage-examples/application-daemon-with-k8s-update-strategy.yaml @@ -0,0 +1,54 @@ +apiVersion: core.oam.dev/v1beta1 +kind: Application +metadata: + name: application-node-exporter +spec: + components: + - name: node-exporter + type: daemon + properties: + image: prom/node-exporter + imagePullPolicy: IfNotPresent + volumeMounts: + hostPath: + - mountPath: /host/sys + mountPropagation: HostToContainer + name: sys + path: /sys + readOnly: true + - mountPath: /host/root + mountPropagation: HostToContainer + name: root + path: / + readOnly: true + traits: + - properties: + args: + - --path.sysfs=/host/sys + - --path.rootfs=/host/root + - --no-collector.wifi + - --no-collector.hwmon + - --collector.filesystem.ignored-mount-points=^/(dev|proc|sys|var/lib/docker/.+|var/lib/kubelet/pods/.+)($|/) + - --collector.netclass.ignored-devices=^(veth.*)$ + type: command + - properties: + annotations: + prometheus.io/path: /metrics + prometheus.io/port: "8080" + prometheus.io/scrape: "true" + port: + - 9100 + type: expose + - properties: + cpu: 0.1 + memory: 250Mi + type: resource + - type: k8s-update-strategy + properties: + targetAPIVersion: apps/v1 + targetKind: DaemonSet + strategy: + type: RollingUpdate + rollingStrategy: + maxSurge: 20% + maxUnavailable: 30% diff --git a/vela-templates/definitions/usage-examples/application-with-k8s-update-strategy.yaml b/vela-templates/definitions/usage-examples/application-with-k8s-update-strategy.yaml new file mode 100644 index 000000000..9e3be25b5 --- /dev/null +++ b/vela-templates/definitions/usage-examples/application-with-k8s-update-strategy.yaml @@ -0,0 +1,33 @@ +apiVersion: core.oam.dev/v1beta1 +kind: Application +metadata: + name: application-with-update-strategy +spec: + components: + - name: helloworld + type: webservice + properties: + cpu: "0.5" + exposeType: ClusterIP + image: oamdev/hello-world:latest + memory: 1024Mi + ports: + - expose: true + port: 80 + protocol: TCP + traits: + - type: scaler + properties: + replicas: 5 + - type: k8s-update-strategy + properties: + targetAPIVersion: apps/v1 + targetKind: Deployment + strategy: + type: RollingUpdate + rollingStrategy: + maxSurge: 20% + maxUnavailable: 30% + + +