-
Notifications
You must be signed in to change notification settings - Fork 91
Feat/delete os pod #206
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
alvlkov
wants to merge
5
commits into
openshift:main
Choose a base branch
from
alvlkov:feat/delete-os-pod-osd-20528
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
Feat/delete os pod #206
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ad4789
adding managed script for os pod deletion
a4275c1
adding SREP to allowedGroups
eca9f00
adding --force flag and replicaset ownership check
f65f957
adding replicaset check and --force flag for bypassing it, fixed scri…
86eaec2
Update scripts/CEE/delete-pod/metadata.yaml
alvlkov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Delete Openshift Pod Script | ||
|
|
||
| ## Purpose | ||
|
|
||
| This script is designed to delete a pod from OpenShift cluster core namespace. | ||
|
|
||
| ## Usage | ||
|
|
||
| Parameters: | ||
| - POD_NAME: Name of pod to delete. | ||
| - NAMESPACE: Namespace name where por to delete is running, must start with openshift-*. | ||
| - FLAGS: Optional flags, currently only accepts --force. | ||
|
|
||
| ```bash | ||
| ocm backplane managedjob create CEE/delete-pod -p POD_NAME: dns-default-h7l2w -p NAMESPACE=openshift-dns -p FLAGS="--force" | ||
| ``` | ||
|
|
||
| ## Important Notes | ||
|
|
||
| - The script utilizes the `oc` command-line tool, and the user running the script should have the necessary permissions to access the cluster. | ||
| - Ensure that the required tools (`oc`) are available in the environment where the script is executed. | ||
| - The script requires pod to be bound to a replicaset. Otherwise pod cannot be deleted. | ||
| - The script provides force flag to bypass replicaset check. |
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| file: script.sh | ||
| name: delete-pod | ||
| shortDescription: Deletes a pod from openshift namespace | ||
| description: Deletes a single pod from openshift's reserved namespace. | ||
| author: Alex Volkov | ||
| allowedGroups: | ||
| - SREP | ||
| - MCSTierTwo | ||
| rbac: | ||
| clusterRoleRules: | ||
| - apiGroups: | ||
| - "" | ||
| resources: | ||
| - "pods" | ||
| verbs: | ||
| - "delete" | ||
| - "get" | ||
|
|
||
| envs: | ||
| - key: POD_NAME | ||
| description: Name of the pod to delete | ||
| optional: false | ||
| - key: NAMESPACE | ||
| description: Namespace name where pod to delete is running, must start with openshift-* | ||
| optional: false | ||
| - key: FLAGS | ||
| description: Flag to bypass ReplicaSet validation | ||
| optional: true | ||
|
|
||
| language: bash | ||
| customerDataAccess: false | ||
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
| set -o errexit | ||
| set -o nounset | ||
| set -o pipefail | ||
|
|
||
| ## Input validation | ||
| if ! declare -p FLAGS &>/dev/null || [[ -z "${FLAGS}" ]]; then | ||
| FLAGS="" | ||
| fi | ||
|
|
||
| # If --force is in FLAGS, set FORCE_FLAG to true | ||
| FORCE_FLAG=false | ||
| if [[ "$FLAGS" =~ --force ]]; then | ||
| FORCE_FLAG=true | ||
| fi | ||
|
|
||
| if [[ -z "${POD_NAME:-}" ]]; then | ||
| echo 'Variable POD_NAME cannot be blank' | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [[ -z "${NAMESPACE:-}" ]]; then | ||
| echo 'Variable NAMESPACE cannot be blank' | ||
| exit 1 | ||
| fi | ||
|
|
||
| ### Check namespace is "openshift-*" | ||
| if [[ ! "$NAMESPACE" =~ ^openshift-.*$ ]]; then | ||
| echo "The namespace must start with 'openshift-'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| ## Validate if pod is owned by a replicaset | ||
| check_owned_by_replicaset(){ | ||
| echo -e "\n[INFO] Checking replicaset owning the pod \"${POD_NAME}\" from \"${NAMESPACE}\" namespace." | ||
|
|
||
| local owner_kind | ||
| owner_kind=$(oc get pod "$POD_NAME" -n "$NAMESPACE" -o jsonpath='{.metadata.ownerReferences[0].kind}' 2>/dev/null || echo "") | ||
|
|
||
| if [[ "$owner_kind" == "ReplicaSet" ]]; then | ||
| echo "[INFO] Pod '${POD_NAME}' is owned by a ReplicaSet." | ||
| else | ||
| echo "[WARN] Pod '${POD_NAME}' is not owned by a ReplicaSet." | ||
|
|
||
| if [[ "$FORCE_FLAG" != true ]]; then | ||
| echo "[ERROR] Deletion blocked. Use --force to override." >&2 | ||
| exit 1 | ||
| else | ||
| echo "[INFO] --force flag detected. Proceeding with deletion." | ||
| fi | ||
| fi | ||
| } | ||
|
|
||
| ## Delete pod | ||
| delete_pod(){ | ||
| echo -e "\n[INFO] Deleting pod \"${POD_NAME}\" from \"${NAMESPACE}\" namespace." | ||
| oc delete pod "$POD_NAME" -n "$NAMESPACE" | ||
|
|
||
| if [ $? -eq 0 ]; then | ||
| echo -e "\n[SUCCESS] Pod '$POD_NAME' successfully deleted from namespace '$NAMESPACE'." | ||
| else | ||
| echo -e "\n[ERROR] Failed to delete pod '$POD_NAME' from namespace '$NAMESPACE'." | ||
| fi | ||
| } | ||
|
|
||
| main(){ | ||
| check_owned_by_replicaset | ||
| delete_pod | ||
| } | ||
|
|
||
| main |
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.
This is valid for all namespaces. There's no limitation to
from openshift's reserved namespace.as mentioned above.This permission extends beyond the scope even SRE-P has.
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.
The above limitation applies to the NAMESPACE parameter, to avoid deleting Openshift related pods. AFAIK I cant scope namespaces within clusterRoleRules. Please elaborate about the suggestion.