Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion charts/karmada/templates/post-delete-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ spec:
set -ex
kubectl delete -f /opt/mount/ --ignore-not-found=true
kubectl delete -f /opt/crds/ --ignore-not-found=true -R
kubectl delete -f /opt/static-resources/ --ignore-not-found=true -R
for file in /opt/static-resources/*; do
if [ "$(basename "$file")" != "system-namespace.yaml" ]; then
kubectl delete -f "$file" --ignore-not-found=true -R || true
fi
done
Comment on lines +50 to +54

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using $(basename "$file") inside a loop spawns a subshell and executes an external binary (basename) for every file in the directory. Since the script is executed using bash, we can use bash's built-in parameter expansion ${file##*/} instead. This is more efficient, avoids spawning external processes, and is more robust as it doesn't rely on the availability of the basename command in the container image.

Additionally, we can add a check [[ -e "$file" ]] to ensure the file exists before attempting to delete it, preventing errors if the glob pattern does not match any files.

              for file in /opt/static-resources/*; do
                if [[ -e "$file" && "${file##*/}" != "system-namespace.yaml" ]]; then
                  kubectl delete -f "$file" --ignore-not-found=true -R || true
                fi
              done

kubectl delete cm/{{ $name }}-config -n {{ $namespace }} --ignore-not-found=true
kubectl delete deployment/{{ $name }}-controller-manager -n {{ $namespace }} --ignore-not-found=true
EOF
Expand Down