Team Sunny needs to identify some of their Pods in namespace sun. They ask you to add a new label protected: true to all Pods with an existing label type: worker or type: runner. Also add an annotation protected: do not delete this pod to all Pods having the new label protected: true.
➜ k -n sun get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
0509649a 1/1 Running 0 25s type=runner,type_old=messenger
0509649b 1/1 Running 0 24s type=worker
1428721e 1/1 Running 0 23s type=worker
1428721f 1/1 Running 0 22s type=worker
43b9a 1/1 Running 0 22s type=test
4c09 1/1 Running 0 21s type=worker
4c35 1/1 Running 0 20s type=worker
4fe4 1/1 Running 0 19s type=worker
5555a 1/1 Running 0 19s type=messenger
86cda 1/1 Running 0 18s type=runner
8d1c 1/1 Running 0 17s type=messenger
a004a 1/1 Running 0 16s type=runner
a94128196 1/1 Running 0 15s type=runner,type_old=messenger
afd79200c56a 1/1 Running 0 15s type=worker
b667 1/1 Running 0 14s type=worker
fdb2 1/1 Running 0 13s type=worker
If we would only like to get pods with certain labels we can run:
k -n sun get pod -l type=runner # only pods with label runner
We can use this label filtering also when using other commands, like setting new labels:
k label -h # help
k -n sun label pod -l type=runner protected=true # run for label runner
k -n sun label pod -l type=worker protected=true # run for label worker
Or we could run:
k -n sun label pod -l "type in (worker,runner)" protected=true
Let's check the result:
➜ k -n sun get pod --show-labels
NAME ... AGE LABELS
0509649a ... 56s protected=true,type=runner,type_old=messenger
0509649b ... 55s protected=true,type=worker
1428721e ... 54s protected=true,type=worker
1428721f ... 53s protected=true,type=worker
43b9a ... 53s type=test
4c09 ... 52s protected=true,type=worker
4c35 ... 51s protected=true,type=worker
4fe4 ... 50s protected=true,type=worker
5555a ... 50s type=messenger
86cda ... 49s protected=true,type=runner
8d1c ... 48s type=messenger
a004a ... 47s protected=true,type=runner
a94128196 ... 46s protected=true,type=runner,type_old=messenger
afd79200c56a ... 46s protected=true,type=worker
b667 ... 45s protected=true,type=worker
fdb2 ... 44s protected=true,type=worker
Looking good. Finally we set the annotation using the newly assigned label protected: true:
`k -n sun annotate pod -l protected=true protected="do not delete this pod"`
Not requested in the task but for your own control you could run:
`k -n sun get pod -l protected=true -o yaml | grep -A 8 metadata:`