From c4afd54b7c5693d508350cecbf219d08bb3f4ddc Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Wed, 18 Jun 2025 17:37:59 +0200 Subject: [PATCH] Fix cluster-deregistration: update workflowtemplate and functions Change-Id: Ibd4c2195d6b56a002daaf81f3ce9f93e69af446b Signed-off-by: garciadeblas --- .../scripts/library/helper-functions.rc | 2 +- .../scripts/library/krm-functions.rc | 20 ++- .../cluster-management-wft.yaml | 143 +++++++++--------- ...aml => full-purge-delete-cluster-wft.yaml} | 19 ++- 4 files changed, 100 insertions(+), 84 deletions(-) rename installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/canned-operations/{full-uninstall-flux-remote-cluster-wft.yaml => full-purge-delete-cluster-wft.yaml} (89%) diff --git a/docker/osm-krm-functions/scripts/library/helper-functions.rc b/docker/osm-krm-functions/scripts/library/helper-functions.rc index 29e00ffd..be3601a2 100644 --- a/docker/osm-krm-functions/scripts/library/helper-functions.rc +++ b/docker/osm-krm-functions/scripts/library/helper-functions.rc @@ -403,7 +403,7 @@ function full_json_patch() { local TARGET_NAME="$2" local OPERATION="$3" local PATCH_PATH="$4" - # Gathers all optional parameters for transformer funcion (if any) and puts them into an array for further use + # Gathers all optional parameters for transformer function (if any) and puts them into an array for further use local ALL_PARAMS=( "${@}" ) local VALUES=( "${ALL_PARAMS[@]:4}" ) diff --git a/docker/osm-krm-functions/scripts/library/krm-functions.rc b/docker/osm-krm-functions/scripts/library/krm-functions.rc index 07a47f86..61f6b696 100644 --- a/docker/osm-krm-functions/scripts/library/krm-functions.rc +++ b/docker/osm-krm-functions/scripts/library/krm-functions.rc @@ -577,12 +577,12 @@ function disconnect_flux_remote_cluster() { # Calculates key folders ## Base folder with Kustomizations for the new cluster - local CLUSTER_FOLDER="${FLEET_REPO_DIR}/clusters/${CLUSTER_KUSTOMIZATION_NAME}" + # local CLUSTER_FOLDER="${FLEET_REPO_DIR}/clusters/${CLUSTER_KUSTOMIZATION_NAME}" ## Folder where managed resources are defined in the management cluster local MGMT_RESOURCES_CLUSTER_DIR="${FLEET_REPO_DIR}/${MGMT_PROJECT_NAME}/managed-resources/_management/${CLUSTER_KUSTOMIZATION_NAME}" - # Delete Flux resources syncronized directly from remote cluster - rm -rf "${CLUSTER_FOLDER}/flux-system" + # Delete Flux resources synchronized directly from remote cluster + # rm -rf "${CLUSTER_FOLDER}/flux-system" # Delete Flux resources bootstraped remotely rm -rf "${MGMT_RESOURCES_CLUSTER_DIR}/cluster-bootstrap-${CLUSTER_KUSTOMIZATION_NAME}.yaml" @@ -719,6 +719,18 @@ function delete_remote_cluster() { local APPS_DIR="${8:-"${FLEET_REPO_DIR}/${PROJECT_NAME}/app-profiles/${CLUSTER_KUSTOMIZATION_NAME}"}" local CLUSTER_DIR="${9:-"${FLEET_REPO_DIR}/clusters/${CLUSTER_KUSTOMIZATION_NAME}"}" + # Optional input: Do I need a purge operation first? + local PURGE="${10:-"false"}" + + + # Perform the purge if needed + if [[ "${PURGE,,}" == "true" ]]; then + echo "Purging the remote Flux instalation..." + flux uninstall -s --namespace=flux-system + fi + + echo "Deleting cluster profiles and (when applicable) its cloud resources..." + # Delete profile folders rm -rf "${INFRA_CONTROLLERS_DIR}" rm -rf "${INFRA_CONFIGS_DIR}" @@ -728,7 +740,7 @@ function delete_remote_cluster() { # Delete base cluster Kustomizations rm -rf "${CLUSTER_DIR}" - # Delete cluster resources + # Delete cluster resources if managed by OSM (otherwise, this will be ignored) rm -rf "${MGMT_RESOURCES_DIR}/${CLUSTER_KUSTOMIZATION_NAME}" } diff --git a/installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/building-blocks/cluster-management-wft.yaml b/installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/building-blocks/cluster-management-wft.yaml index 78fbc46b..9140b0b1 100644 --- a/installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/building-blocks/cluster-management-wft.yaml +++ b/installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/building-blocks/cluster-management-wft.yaml @@ -235,80 +235,6 @@ spec: # fsGroup: 10000 - # TODO: Deprecated - To be removed - # Create a PaaS cluster in Azure - - name: create-cluster-aks - inputs: - parameters: - # Volumes with cloned repos - - name: fleet_volume_name - - name: fleet_mount_path - value: "/fleet" - - name: sw_catalogs_volume_name - - name: sw_catalogs_mount_path - value: "/sw-catalogs" - - # Specific parameters - - name: cluster_name - - name: vm_size - - name: node_count - - name: cluster_location - - name: rg_name - - name: k8s_version - - name: providerconfig_name - - name: cluster_kustomization_name - - name: mgmt_project_name - value: "osm_admin" - - # Other parameters - Recommended to keep defaults - - name: fleet_repo_dir - value: "/fleet/fleet-osm/" - - name: sw_catalogs_repo_dir - value: "/sw-catalogs/sw-catalogs-osm/" - - name: target_folder - value: "{{inputs.parameters.fleet_repo_dir}}/{{inputs.parameters.mgmt_project_name}}/managed-resources/_management" - - name: manifest_filename - value: "{{inputs.parameters.cluster_name}}.yaml" - - name: templates - value: "{{inputs.parameters.sw_catalogs_repo_dir}}/cloud-resources/aks/templates/" - - name: template_manifest_filename - value: "aks01.yaml" - volumes: - - name: fleet-repo-volume - persistentVolumeClaim: - claimName: '{{inputs.parameters.fleet_volume_name}}' - - name: sw-catalogs-repo-volume - persistentVolumeClaim: - claimName: '{{inputs.parameters.sw_catalogs_volume_name}}' - container: - image: opensourcemano/osm-krm-functions:testing-daily - # imagePullPolicy: Always - # securityContext: - # runAsUser: 10000 - # runAsGroup: 10000 - # fsGroup: 10000 - volumeMounts: - - name: fleet-repo-volume - mountPath: '{{inputs.parameters.fleet_mount_path}}' - - name: sw-catalogs-repo-volume - mountPath: '{{inputs.parameters.sw_catalogs_mount_path}}' - command: ["/app/scripts/entrypoint.sh"] - args: - - create_cluster_aks - - "{{inputs.parameters.cluster_name}}" - - "{{inputs.parameters.vm_size}}" - - "{{inputs.parameters.node_count}}" - - "{{inputs.parameters.cluster_location}}" - - "{{inputs.parameters.rg_name}}" - - "{{inputs.parameters.k8s_version}}" - - "{{inputs.parameters.providerconfig_name}}" - - "{{inputs.parameters.cluster_kustomization_name}}" - - "{{inputs.parameters.target_folder}}" - - "{{inputs.parameters.manifest_filename}}" - - "{{inputs.parameters.templates}}" - - "{{inputs.parameters.template_manifest_filename}}" - - # Bootstrap remote cluster running in **ANY cloud** - name: bootstrap-remote-cluster inputs: @@ -478,3 +404,72 @@ spec: runAsUser: 10000 # runAsGroup: 10000 # fsGroup: 10000 + + + # Purge and delete cluster from **ANY** cloud + - name: purge-delete-cluster + inputs: + parameters: + # Volumes with cloned repos + - name: fleet_volume_name + - name: fleet_mount_path + value: "/fleet" + + # Specific parameters + - name: cluster_kustomization_name + - name: project_name + value: "osm_admin" + + # Is a purge needed before deleting? + - name: purge + value: "true" + ## Secret where the kubeconfig is stored (may not exist) + - name: temp_kubeconfig_secret_name + valueFrom: + expression: "kubeconfig-{{inputs.parameters.cluster_kustomization_name}}" + + # Other parameters - Recommended to keep defaults + - name: temp_kubeconfig_secret_key + value: "kubeconfig" + - name: fleet_repo_dir + value: "/fleet/fleet-osm/" + - name: mgmt_resources_dir + value: "{{inputs.parameters.fleet_repo_dir}}/{{inputs.parameters.project_name}}/managed-resources/_management" + + container: + image: ttl.sh/osm-krm-functions:24h + # imagePullPolicy: Always + command: ["/app/scripts/entrypoint.sh"] + args: + - delete_remote_cluster + - "{{inputs.parameters.cluster_kustomization_name}}" + - "{{inputs.parameters.project_name}}" + - "{{inputs.parameters.fleet_repo_dir}}" + - "{{inputs.parameters.mgmt_resources_dir}}" + - '' + - '' + - '' + - '' + - '' + - "{{inputs.parameters.purge}}" + + volumeMounts: + - name: fleet-repo-volume + mountPath: '{{inputs.parameters.fleet_mount_path}}' + - name: kubeconfig-volume + mountPath: '/app/.kube' + volumes: + - name: fleet-repo-volume + persistentVolumeClaim: + claimName: '{{inputs.parameters.fleet_volume_name}}' + - name: kubeconfig-volume + secret: + secretName: '{{inputs.parameters.temp_kubeconfig_secret_name}}' + optional: true + items: + - key: '{{inputs.parameters.temp_kubeconfig_secret_key}}' + path: 'config' + securityContext: + fsGroup: 10000 + # runAsUser: 10000 + # runAsGroup: 10000 diff --git a/installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/canned-operations/full-uninstall-flux-remote-cluster-wft.yaml b/installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/canned-operations/full-purge-delete-cluster-wft.yaml similarity index 89% rename from installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/canned-operations/full-uninstall-flux-remote-cluster-wft.yaml rename to installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/canned-operations/full-purge-delete-cluster-wft.yaml index b3ef1d78..e2cb443b 100644 --- a/installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/canned-operations/full-uninstall-flux-remote-cluster-wft.yaml +++ b/installers/flux/templates/sw-catalogs/infra-configs/osm-workflows/templates/wf-templates/canned-operations/full-purge-delete-cluster-wft.yaml @@ -18,7 +18,7 @@ apiVersion: argoproj.io/v1alpha1 kind: WorkflowTemplate metadata: - name: full-uninstall-flux-remote-cluster-wft + name: full-purge-delete-cluster-wft namespace: osm-workflows spec: @@ -34,6 +34,9 @@ spec: - name: cluster_kustomization_name - name: project_name value: "osm_admin" + - name: temp_kubeconfig_secret_name + - name: purge + value: "true" # Debugging - name: dry_run @@ -46,12 +49,12 @@ spec: # secondsAfterSuccess: 50 # Time to live after workflow is successful # secondsAfterFailure: 50 # Time to live after workflow fails - entrypoint: uninstall-flux + entrypoint: delete-cluster templates: # Main template - - name: uninstall-flux + - name: delete-cluster inputs: parameters: # Git repos @@ -62,6 +65,8 @@ spec: # Specific parameters - name: cluster_kustomization_name - name: project_name + - name: purge + - name: temp_kubeconfig_secret_name # Debugging - name: dry_run @@ -96,10 +101,10 @@ spec: # ------ end of preparations for transaction # ------ Transformations - - - name: delete-cluster + - - name: purge-delete-cluster templateRef: name: cluster-management-wft - template: delete-cluster + template: purge-delete-cluster arguments: parameters: # Volumes with cloned repos @@ -112,6 +117,10 @@ spec: value: "{{inputs.parameters.cluster_kustomization_name}}" - name: project_name value: "{{inputs.parameters.project_name}}" + - name: purge + value: "{{inputs.parameters.purge}}" + - name: temp_kubeconfig_secret_name + value: "{{inputs.parameters.temp_kubeconfig_secret_name}}" # ------ end of transformations # ------ Commit transaction -- 2.25.1