From 950411dbe8f1416f021135ef1ca90d52108b30c6 Mon Sep 17 00:00:00 2001 From: Reeta Singh Date: Fri, 27 Aug 2021 03:45:22 -0700 Subject: [PATCH] Trait: Add TraitDefinition for PVC (#2158) * Feat(trait): add trait definition for persistent volume claim * Feat(trait): add trait definition for persistent volume claim --- .../templates/defwithtemplate/pvc.yaml | 120 ++++++++++++++++++ vela-templates/definitions/internal/pvc.cue | 117 +++++++++++++++++ .../usage-examples/application-with-pvc.yaml | 26 ++++ 3 files changed, 263 insertions(+) create mode 100644 charts/vela-core/templates/defwithtemplate/pvc.yaml create mode 100644 vela-templates/definitions/internal/pvc.cue create mode 100644 vela-templates/definitions/usage-examples/application-with-pvc.yaml diff --git a/charts/vela-core/templates/defwithtemplate/pvc.yaml b/charts/vela-core/templates/defwithtemplate/pvc.yaml new file mode 100644 index 000000000..ab1ca48c1 --- /dev/null +++ b/charts/vela-core/templates/defwithtemplate/pvc.yaml @@ -0,0 +1,120 @@ +# Code generated by KubeVela templates. DO NOT EDIT. Please edit the original cue file. +# Definition source cue file: vela-templates/definitions/internal/pvc.cue +apiVersion: core.oam.dev/v1beta1 +kind: TraitDefinition +metadata: + annotations: + definition.oam.dev/description: Create a Persistent Volume Claim and mount the PVC as volume to the first container in the pod + name: pvc + namespace: {{.Values.systemDefinitionNamespace}} +spec: + appliesToWorkloads: + - deployments.apps + podDisruptive: true + schematic: + cue: + template: | + patch: spec: template: spec: { + containers: [{ + if parameter.volumeMode == "Block" { + // +patchKey=name + volumeDevices: [ + for v in parameter.volumesToMount { + { + name: v.name + devicePath: v.devicePath + } + }, + ] + } + if parameter.volumeMode == "Filesystem" { + // +patchKey=name + volumeMounts: [ + for v in parameter.volumesToMount { + { + name: v.name + mountPath: v.mountPath + } + }, + ] + } + }] + + // +patchKey=name + volumes: [ + for v in parameter.volumesToMount { + { + name: v.name + persistentVolumeClaim: claimName: parameter.claimName + } + }, + ] + } + outputs: pvc: { + apiVersion: "v1" + kind: "PersistentVolumeClaim" + metadata: name: parameter.claimName + spec: { + accessModes: parameter.accessModes + volumeMode: parameter.volumeMode + if parameter.volumeName != _|_ { + volumeName: parameter.volumeName + } + + if parameter.storageClassName != _|_ { + storageClassName: parameter.storageClassName + } + resources: requests: storage: parameter.resources.requests.storage + if parameter.resources.limits.storage != _|_ { + resources: limits: storage: parameter.resources.limits.storage + } + if parameter.dataSourceRef != _|_ { + dataSourceRef: parameter.dataSourceRef + } + if parameter.dataSource != _|_ { + dataSource: parameter.dataSource + } + if parameter.selector != _|_ { + dataSource: parameter.selector + } + } + } + parameter: { + claimName: string + volumeMode: *"Filesystem" | string + volumeName?: string + accessModes: [...string] + storageClassName?: string + resources: { + requests: storage: =~"^([1-9][0-9]{0,63})(E|P|T|G|M|K|Ei|Pi|Ti|Gi|Mi|Ki)$" + limits?: storage: =~"^([1-9][0-9]{0,63})(E|P|T|G|M|K|Ei|Pi|Ti|Gi|Mi|Ki)$" + } + dataSourceRef?: { + name: string + kind: string + apiGroup: string + } + dataSource?: { + name: string + kind: string + apiGroup: string + } + selector?: { + matchLabels?: [string]: string + matchExpressions?: { + key: string + values: [...string] + operator: string + } + } + volumesToMount: [...{ + name: string + if volumeMode == "Block" { + devicePath: string + } + if volumeMode == "Filesystem" { + mountPath: string + } + }] + } + diff --git a/vela-templates/definitions/internal/pvc.cue b/vela-templates/definitions/internal/pvc.cue new file mode 100644 index 000000000..46ecefbaf --- /dev/null +++ b/vela-templates/definitions/internal/pvc.cue @@ -0,0 +1,117 @@ +pvc: { + type: "trait" + annotations: {} + labels: {} + description: "Create a Persistent Volume Claim and mount the PVC as volume to the first container in the pod" + attributes: { + appliesToWorkloads: ["deployments.apps"] + podDisruptive: true + } +} +template: { + patch: spec: template: spec: { + containers: [{ + if parameter.volumeMode == "Block" { + // +patchKey=name + volumeDevices: [ + for v in parameter.volumesToMount { + { + name: v.name + devicePath: v.devicePath + } + }, + ] + } + if parameter.volumeMode == "Filesystem" { + // +patchKey=name + volumeMounts: [ + for v in parameter.volumesToMount { + { + name: v.name + mountPath: v.mountPath + } + }, + ] + } + }] + + // +patchKey=name + volumes: [ + for v in parameter.volumesToMount { + { + name: v.name + persistentVolumeClaim: claimName: parameter.claimName + } + }, + ] + } + outputs: pvc: { + apiVersion: "v1" + kind: "PersistentVolumeClaim" + metadata: { + name: parameter.claimName + } + spec: { + accessModes: parameter.accessModes + volumeMode: parameter.volumeMode + if parameter.volumeName != _|_ { + volumeName: parameter.volumeName + } + + if parameter.storageClassName != _|_ { + storageClassName: parameter.storageClassName + } + resources: requests: storage: parameter.resources.requests.storage + if parameter.resources.limits.storage != _|_ { + resources: limits: storage: parameter.resources.limits.storage + } + if parameter.dataSourceRef != _|_ { + dataSourceRef: parameter.dataSourceRef + } + if parameter.dataSource != _|_ { + dataSource: parameter.dataSource + } + if parameter.selector != _|_ { + dataSource: parameter.selector + } + } + } + parameter: { + claimName: string + volumeMode: *"Filesystem" | string + volumeName?: string + accessModes: [...string] + storageClassName?: string + resources: { + requests: storage: =~"^([1-9][0-9]{0,63})(E|P|T|G|M|K|Ei|Pi|Ti|Gi|Mi|Ki)$" + limits?: storage: =~"^([1-9][0-9]{0,63})(E|P|T|G|M|K|Ei|Pi|Ti|Gi|Mi|Ki)$" + } + dataSourceRef?: { + name: string + kind: string + apiGroup: string + } + dataSource?: { + name: string + kind: string + apiGroup: string + } + selector?: { + matchLabels?: [string]: string + matchExpressions?: { + key: string + values: [...string] + operator: string + } + } + volumesToMount: [...{ + name: string + if volumeMode == "Block" { + devicePath: string + } + if volumeMode == "Filesystem" { + mountPath: string + } + }] + } +} diff --git a/vela-templates/definitions/usage-examples/application-with-pvc.yaml b/vela-templates/definitions/usage-examples/application-with-pvc.yaml new file mode 100644 index 000000000..0ba780514 --- /dev/null +++ b/vela-templates/definitions/usage-examples/application-with-pvc.yaml @@ -0,0 +1,26 @@ +apiVersion: core.oam.dev/v1beta1 +kind: Application +metadata: + name: application-with-pvc +spec: + components: + - name: busybox-runner + type: worker + properties: + image: busybox + cmd: + - sleep + - '1000' + traits: + - type: pvc + properties: + claimName: "busybox-pvc" + accessModes: + - "ReadWriteOnce" + volumeMode: "Block" + resources: + requests: + storage: "2Gi" + volumesToMount: + - name: "volume-with-pvc" + devicePath: "/tmp"