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 type: string
properties: default: ""
volumeName: description: |
type: string The name of the PVC to delete. If empty, no action will be taken.
namespace:
type: string
default:
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 kubectl get pvc $(params.persistence4delete.volumeName) -n $(params.persistence4delete.namespace) >/dev/null 2>&1; then
echo "PVC $(params.persistence4delete.volumeName) exists, deleting..."
kubectl delete pvc $(params.persistence4delete.volumeName) -n $(params.persistence4delete.namespace)
echo "PVC $(params.persistence4delete.volumeName) deleted."
else
echo "PVC $(params.persistence4delete.volumeName) does not exist, skipping deletion."
fi
else
echo "No persistence4delete.volumeName provided, nothing to delete."
fi
if [ -n "$(params.pvcName)" ]; then
echo "🔧 Deleting PVC: $(params.pvcName)"
kubectl delete pvc $(params.pvcName) || echo "⚠️ PVC not found: $(params.pvcName)"
else
echo "✅ No PVC to delete (pvcName is empty)"
fi

View File

@ -4,60 +4,57 @@ metadata:
name: before-pipeline name: before-pipeline
spec: spec:
params: params:
- name: persistence4create - name: pvcEnable
type: object type: string
properties: default: "false"
enabled: description: |
type: string Whether to create a PersistentVolumeClaim (PVC).
namespace: Set to "true" to create a PVC. Set to "false" to skip creation.
type: string
storageClass: - name: pvcSize
type: string type: string
accessModes: default: "1Gi"
type: array description: |
size: The requested size of the PVC (e.g., "1Gi", "5Gi").
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: - ReadWriteOnce
$ACCESS_MODES resources:
resources: requests:
requests: storage: $(params.pvcSize)
storage: $(params.persistence4create.size) volumeMode: Filesystem
storageClassName: $(params.persistence4create.storageClass) 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