forked from Azure/azure-cli-extensions
-
Notifications
You must be signed in to change notification settings - Fork 2
Skip CRDs with AKS addon manager label #31
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
Open
jhmadhav
wants to merge
1
commit into
main
Choose a base branch
from
hejakkam/skip-aks-crd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4589,8 +4589,32 @@ def crd_cleanup_force_delete( | |
| ) | ||
|
|
||
| timeout_for_crd_deletion = "20s" | ||
| deleted_crds = [] | ||
| for crds in consts.CRD_FOR_FORCE_DELETE: | ||
| full_crds = f"{crds}.{cloud_based_domain}" | ||
|
|
||
| # Check if CRD has the addonmanager.kubernetes.io/mode: Reconcile label | ||
| # If present, skip deletion to avoid disrupting AKS managed resources | ||
| get_crd_cmd = [kubectl_client_location, "get", "crd", full_crds, "-ojson"] | ||
| if kube_config: | ||
| get_crd_cmd.extend(["--kubeconfig", kube_config]) | ||
| if kube_context: | ||
| get_crd_cmd.extend(["--context", kube_context]) | ||
| get_crd_output = Popen(get_crd_cmd, stdout=PIPE, stderr=PIPE) | ||
| # Note: communicate() can only be called once on a Popen object, | ||
| # so we must capture stdout here and reuse it below | ||
| stdout, _ = get_crd_output.communicate() | ||
|
|
||
| if get_crd_output.returncode == 0: | ||
| try: | ||
| crd_json = json.loads(stdout.strip()) | ||
| labels = crd_json.get("metadata", {}).get("labels", {}) | ||
| if labels.get("addonmanager.kubernetes.io/mode") == "Reconcile": | ||
| # Skip deletion for CRDs managed by AKS addon manager | ||
| continue | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to completely skip the delete? i assumed its selective skip for those with the label |
||
| except json.JSONDecodeError: | ||
| pass | ||
|
|
||
| cmd_helm_delete = [ | ||
| kubectl_client_location, | ||
| "delete", | ||
|
|
@@ -4607,6 +4631,7 @@ def crd_cleanup_force_delete( | |
| cmd_helm_delete.extend(["--context", kube_context]) | ||
| response_helm_delete = Popen(cmd_helm_delete, stdout=PIPE, stderr=PIPE) | ||
| _, _ = response_helm_delete.communicate() | ||
| deleted_crds.append(full_crds) | ||
|
|
||
| # Timer added to have sufficient time after CRD deletion | ||
| # to check the status of the CRD ( deleted or terminating ) | ||
|
|
@@ -4616,37 +4641,41 @@ def crd_cleanup_force_delete( | |
| current_path = os.path.abspath(os.path.dirname(__file__)) | ||
| yaml_file_path = os.path.join(current_path, "remove_crd_finalizer.yaml") | ||
|
|
||
| # Patch if CRD is in Terminating state | ||
| for crds in consts.CRD_FOR_FORCE_DELETE: | ||
| full_crds = f"{crds}.{cloud_based_domain}" | ||
| cmd = [kubectl_client_location, "get", "crd", full_crds, "-ojson"] | ||
| # Patch if CRD is in Terminating state (only for CRDs we attempted to delete) | ||
| for full_crds in deleted_crds: | ||
| get_cmd = [kubectl_client_location, "get", "crd", full_crds, "-ojson"] | ||
| if kube_config: | ||
| cmd.extend(["--kubeconfig", kube_config]) | ||
| get_cmd.extend(["--kubeconfig", kube_config]) | ||
| if kube_context: | ||
| cmd.extend(["--context", kube_context]) | ||
| cmd_output = Popen(cmd, stdout=PIPE, stderr=PIPE) | ||
| _, _ = cmd_output.communicate() | ||
| get_cmd.extend(["--context", kube_context]) | ||
| cmd_output = Popen(get_cmd, stdout=PIPE, stderr=PIPE) | ||
| # Note: communicate() can only be called once on a Popen object, | ||
| # so we must capture stdout here and reuse it below | ||
| stdout, _ = cmd_output.communicate() | ||
|
|
||
| if cmd_output.returncode == 0: | ||
| changed_cmd = json.loads(cmd_output.communicate()[0].strip()) | ||
| status = changed_cmd["status"]["conditions"][-1]["type"] | ||
|
|
||
| if status == "Terminating": | ||
| patch_cmd = [ | ||
| kubectl_client_location, | ||
| "patch", | ||
| "crd", | ||
| full_crds, | ||
| "--type=merge", | ||
| "--patch-file", | ||
| yaml_file_path, | ||
| ] | ||
| if kube_config: | ||
| patch_cmd.extend(["--kubeconfig", kube_config]) | ||
| if kube_context: | ||
| patch_cmd.extend(["--context", kube_context]) | ||
| output_patch_cmd = Popen(patch_cmd, stdout=PIPE, stderr=PIPE) | ||
| _, _ = output_patch_cmd.communicate() | ||
| try: | ||
| changed_cmd = json.loads(stdout.strip()) | ||
| status = changed_cmd["status"]["conditions"][-1]["type"] | ||
|
|
||
| if status == "Terminating": | ||
| patch_cmd = [ | ||
| kubectl_client_location, | ||
| "patch", | ||
| "crd", | ||
| full_crds, | ||
| "--type=merge", | ||
| "--patch-file", | ||
| yaml_file_path, | ||
| ] | ||
| if kube_config: | ||
| patch_cmd.extend(["--kubeconfig", kube_config]) | ||
| if kube_context: | ||
| patch_cmd.extend(["--context", kube_context]) | ||
| output_patch_cmd = Popen(patch_cmd, stdout=PIPE, stderr=PIPE) | ||
| _, _ = output_patch_cmd.communicate() | ||
| except (json.JSONDecodeError, KeyError, IndexError): | ||
| pass | ||
|
|
||
|
|
||
| def check_operation_support(operation_name: str, agent_version: str) -> None: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once tested , can you please add snapshots of the state of CRDs to the pr description as well for reference.