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

View File

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