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% + + +