Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal - delete cluster secret during undeploy #455

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions api/common/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

const (
ManagedByBTPOperatorLabel = "services.cloud.sap.com/managed-by-sap-btp-operator"
ClusterSecretLabel = "services.cloud.sap.com/cluster-secret"

NamespaceLabel = "_namespace"
K8sNameLabel = "_k8sname"
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func createClusterSecret(client client.Client) {
clusterSecret := &v1.Secret{}
clusterSecret.Name = "sap-btp-operator-clusterid"
clusterSecret.Namespace = config.Get().ReleaseNamespace
clusterSecret.Labels = map[string]string{common.ManagedByBTPOperatorLabel: "true", common.ClusterSecretLabel: "true"}
clusterSecret.StringData = map[string]string{"INITIAL_CLUSTER_ID": config.Get().ClusterID}
clusterSecret.Labels = map[string]string{common.ManagedByBTPOperatorLabel: "true"}
if err := client.Create(context.Background(), clusterSecret); err != nil {
Expand Down
28 changes: 18 additions & 10 deletions sapbtp-operator-charts/templates/pre-delete-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,23 @@ spec:
args:
- "-c"
- |
servicebindingsCRD="servicebindings.services.cloud.sap.com"
kubectl get $servicebindingsCRD -A
kubectl get $servicebindingsCRD -A --output=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace | tail -n +2 | while read -r line; do
kubectl get servicebindings.services.cloud.sap.com -A --output=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace | tail -n +2 | while read -r line; do
name=$(echo $line | awk '{print $1}')
namespace=$(echo $line | awk '{print $2}')
echo "Deleting binding with Name: $name, and Namespace: $namespace"
timeout 30 kubectl delete $servicebindingsCRD $name -n $namespace
timeout 30 kubectl delete servicebindings.services.cloud.sap.com $name -n $namespace
if [ $? -ne 0 ]; then
echo "Failed to delete service binding $name"
exit 1
fi
done
echo "No more bindings found"
echo "Deleting bindings finished successfully"

serviceinstanceCRD="serviceinstances.services.cloud.sap.com"
kubectl get $serviceinstanceCRD -A
kubectl get $serviceinstanceCRD -A --output=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace | tail -n +2 | while read -r line; do
kubectl get serviceinstances.services.cloud.sap.com -A --output=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace | tail -n +2 | while read -r line; do
name=$(echo $line | awk '{print $1}')
namespace=$(echo $line | awk '{print $2}')
echo "Deleting instance with Name: $name, and Namespace: $namespace"
timeout 30 kubectl delete $serviceinstanceCRD $name -n $namespace
timeout 30 kubectl delete serviceinstances.services.cloud.sap.com $name -n $namespace
if [ $? -ne 0 ]; then
echo "Failed to delete service instance $name"
exit 1
Expand All @@ -62,8 +58,20 @@ spec:
if [ $? -ne 0 ]; then
exit 1
fi
echo "Deleting instances finished successfully"

kubectl get secrets -l "services.cloud.sap.com/cluster-secret" -A --output=custom-columns=NAME:.metadata.name,NAMESPACE:.metadata.namespace | tail -n +2 | while read -r line; do
name=$(echo $line | awk '{print $1}')
namespace=$(echo $line | awk '{print $2}')
echo "Deleting secret with Name: $name, and Namespace: $namespace"
timeout 30 kubectl delete secret $name -n $namespace
if [ $? -ne 0 ]; then
echo "Failed to delete secret $name"
exit 1
fi
done
echo "Cluster secret deletion finished successfully"

echo "No more instances found"
exit 0

restartPolicy: Never
Loading