This commit is contained in:
병준 박 2025-04-10 09:14:16 +00:00
parent 0da248f013
commit 7e5cab8a97
2 changed files with 60 additions and 73 deletions

View File

@ -4,32 +4,22 @@ metadata:
name: after-pipeline name: after-pipeline
spec: spec:
params: params:
- name: persistence4delete - name: pvcName
type: object
properties:
volumeName:
type: string type: string
namespace: default: ""
type: string description: |
default: The name of the PVC to delete. If empty, no action will be taken.
volumeName: ""
namespace: ""
steps: steps:
- name: delete-pvc-if-exists - name: maybe-delete-pvc
image: bitnami/kubectl:latest image: bitnami/kubectl:latest
script: | script: |
#!/usr/bin/env bash #!/bin/sh
set -e
if [ -n "$(params.persistence4delete.volumeName)" ]; then if [ -n "$(params.pvcName)" ]; then
if kubectl get pvc $(params.persistence4delete.volumeName) -n $(params.persistence4delete.namespace) >/dev/null 2>&1; then echo "🔧 Deleting PVC: $(params.pvcName)"
echo "PVC $(params.persistence4delete.volumeName) exists, deleting..." kubectl delete pvc $(params.pvcName) || echo "⚠️ PVC not found: $(params.pvcName)"
kubectl delete pvc $(params.persistence4delete.volumeName) -n $(params.persistence4delete.namespace)
echo "PVC $(params.persistence4delete.volumeName) deleted."
else else
echo "PVC $(params.persistence4delete.volumeName) does not exist, skipping deletion." echo "✅ No PVC to delete (pvcName is empty)"
fi fi
else
echo "No persistence4delete.volumeName provided, nothing to delete."
fi

View File

@ -4,60 +4,57 @@ metadata:
name: before-pipeline name: before-pipeline
spec: spec:
params: params:
- name: persistence4create - name: pvcEnable
type: object
properties:
enabled:
type: string type: string
namespace: default: "false"
description: |
Whether to create a PersistentVolumeClaim (PVC).
Set to "true" to create a PVC. Set to "false" to skip creation.
- name: pvcSize
type: string type: string
storageClass: default: "1Gi"
type: string description: |
accessModes: The requested size of the PVC (e.g., "1Gi", "5Gi").
type: array
size:
type: string
default:
enabled: "false"
namespace: "default"
storageClass: ""
accessModes:
- "ReadWriteOnce"
size: "1Gi"
results: results:
- name: pvcName - name: pvcName
description: Name of the created PVC (if enabled) description: |
The name of the created PVC. If pvcEnable is false, this will be an empty string.
steps: steps:
- name: create-pvc-if-enabled - name: maybe-create-pvc
image: bitnami/kubectl:latest image: bitnami/kubectl:latest
script: | script: |
#!/usr/bin/env bash #!/bin/sh
set -e
if [ "$(params.persistence4create.enabled)" == "true" ]; then echo "pvcEnable: $(params.pvcEnable)"
pvcName="before-pipeline-pvc-$(context.taskRun.uid)" if [ "$(params.pvcEnable)" = "true" ]; then
# 배열을 그대로 YAML에 반영 # Generate a random PVC name
ACCESS_MODES="" RAND=$(head /dev/urandom | tr -dc a-z0-9 | head -c 6)
for mode in $(params.persistence4create.accessModes); do PVC_NAME="before-pvc-${RAND}"
ACCESS_MODES="$ACCESS_MODES - $mode\n" echo "Creating PVC: ${PVC_NAME}"
done
# Apply PVC manifest
cat <<EOF | kubectl apply -f - cat <<EOF | kubectl apply -f -
apiVersion: v1 apiVersion: v1
kind: PersistentVolumeClaim kind: PersistentVolumeClaim
metadata: metadata:
name: $pvcName name: ${PVC_NAME}
namespace: $(params.persistence4create.namespace) spec:
spec:
accessModes: accessModes:
$ACCESS_MODES - ReadWriteOnce
resources: resources:
requests: requests:
storage: $(params.persistence4create.size) storage: $(params.pvcSize)
storageClassName: $(params.persistence4create.storageClass) volumeMode: Filesystem
EOF EOF
echo "PVC $pvcName created."
echo -n "$pvcName" > /tekton/results/pvcName
else
echo "Persistence disabled, no PVC created."
echo -n "" > /tekton/results/pvcName
fi
# Output the PVC name as a Task result
echo -n "${PVC_NAME}" > $(results.pvcName.path)
echo "PVC creation complete: ${PVC_NAME}"
else
echo "PVC creation skipped (pvcEnable=false)"
echo -n "" > $(results.pvcName.path)
fi