2

I have some projects in OC, and the names of the pods are generated in base of a chain and a commit. So, I want create an instruction in AzureDevOps to delete all the pods by an incomplete name the name with an specific characters, but finishing with others.

Example:

root@oc-jump-pod:/# oc get po
NAME                           READY     STATUS    RESTARTS   AGE
podA-74rt7                     1/1       Running   0          20h
podB-6744849c59                1/1       Running   0          20h
podB-6746378213                1/1       Running   0          20h

I need use something like:

oc delete po podB*
Error from server (NotFound): pods "podB*" not found

How can I filter the deletion with a couple of characters and not the complete name of the pod?

DeployConfig added:

root@oc-jump-pod-pre:/# oc describe deploy NAME
Name:                   NAME
Namespace:              NAME-pre
CreationTimestamp:      Mon, 25 May 2020 07:01:14 +0000
Labels:                 app.kubernetes.io/instance=NAME
                        app.kubernetes.io/managed-by=Helm
                        app.kubernetes.io/name=NAME
                        app.kubernetes.io/version=latest
                        helm.sh/chart=NAME-1.0.0
Annotations:            deployment.kubernetes.io/revision=3
                        meta.helm.sh/release-name=NAME
                        meta.helm.sh/release-namespace=sda-NAME-pre
Selector:               app.kubernetes.io/instance=NAME,app.kubernetes.io/name=NAME
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:           app.kubernetes.io/instance=NAME
                    app.kubernetes.io/name=NAME
  Service Account:  default
  Containers:
   NAME:
    Image:  registry/sda-NAME-dev/test-NAME-java:0.0.2
    Port:   8080/TCP
    Limits:
      cpu:     150m
      memory:  1444Mi
    Requests:
      cpu:     100m
      memory:  1Gi
    Environment:
      APP_NAME:                     NAME
      JAVA_OPTS_EXT:                ....
      SPRING_CLOUD_CONFIG_PROFILE:  pre
      TZ:                           Europe/Madrid
      WILY_MOM_PORT:                5001
      spring_application_name:      NAME_pre
      spring_cloud_config_uri:      https://config.d.cluster.local
    Mounts:
      /etc/jks from jks (ro)
      /etc/secret-vol from secretvol (ro)
      /etc/truststore from jssecacerts (ro)
  Volumes:
....
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  NAME-6744849c59 (1/1 replicas created)
NewReplicaSet:   <none>
Events:          <none>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
RCat
  • 157
  • 2
  • 5
  • 17

3 Answers3

8

For regex based approach following commands will delete all the pods starting with "podB"

oc get pods | awk '/^podB/{system("oc delete pod " $1)}'

Anyway i would recommend using the method provided by Dashrath Mundkar. For openshift you do not need to provide any namespace if you just want to access resource in your current project so you can just remove the "-n namespace" from the commands like

oc get pod -l labelname=value
oc delete pod -l labelname=value

Just make sure the labels you provided are unique to pods you want to delete

Vishal
  • 145
  • 7
  • the first command works perfectly. The problem in this case is that the name changes at the end (as CHAR was designed), and to speed up a deployment script of certain changes that it collects from a YAML file, it is necessary to kill the current pod. As projects are separated, there is no risk of deleting from other places than the current project. – RCat Jul 27 '20 at 09:43
  • if you delete deployment then it will also delete associated pods with that deployment so focus on deleting deployment – Dashrath Mundkar Jul 27 '20 at 09:45
5

I use the below command to delete pods in bulk. This single command helps me to delete selective pods of my choice. It is really handy and fast and saves lot of time. Happy coding!

oc get pods -o wide -n <namespace> | grep -i -E "<search_string_1>|<search_string_2>" | grep -v "<exclude_results_with_search_string_3>" | awk '{print $1}' | xargs oc delete pod
Vikd
  • 121
  • 1
  • 8
1

First get pod name if it belongs to deploymentConfig like this, I don't think using wildcard you can delete pods, I would suggest use labels and selectors for that.

oc get po -n namespace -l labelname=value

then delete those pods

oc delete po -l labelname=value -n namespace
Dashrath Mundkar
  • 7,956
  • 2
  • 28
  • 42
  • great, but my user cannot list by namespace caused by RBAC policies – RCat Jul 24 '20 at 09:03
  • Add role and role binding to that user. ```oc create role podview --verb=get,list,create,delete,watch --resource=pod -n namespace``` and bind that role to user using ```oc adm policy add-role-to-user podview --role-namespace=namespace -n namespace ``` for info refer this doc section https://docs.openshift.com/container-platform/3.7/admin_guide/manage_rbac.html – Dashrath Mundkar Jul 24 '20 at 09:09
  • and to verify if the user can get,create or delete pod run ```oc auth can-i delete pods -n namespace --as ``` – Dashrath Mundkar Jul 24 '20 at 09:16
  • And make sure that role and role bindings are with namespace scope. and if your user needs to get all pods from whole cluster then you have to create cluster-role for that user – Dashrath Mundkar Jul 24 '20 at 09:24
  • Hi again @Dashrath! the policies of my project, cannot edited by me:` is forbidden: User "ME" cannot impersonate users at the cluster scope: no RBAC policy matchedbbv I can delete pods, create it...but have no powers with namespaces and roles – RCat Jul 24 '20 at 11:35