2

The question comes from the issue: https://github.com/kubernetes/kubectl/issues/717

Kubectl returns information in a table like this:

$ kubectl get replicasets -n kube-system

NAMESPACE     NAME                       DESIRED   CURRENT   READY   AGE
kube-system   datadog-657c65b75c         2         2         2       11d
kube-system   kube-ops-view-5d8df57fc    1         1         0       33d

To find all deviations in the output without using the json formater, I can execute the command:

grep -v '0         0         0\|1         1         1\|2         2         2\|3         3         3\|4         4         4\|5         5         5'

What grep command can cover all possible cases? The goal is to find any ReplicaSets where DESIRED != CURRENT, CURRENT != READY or DESIRED != READY.

kivagant
  • 123
  • 1
  • 1
  • 6

1 Answers1

5

grep is not very good when your pattern is neither a fixed string nor a regex.

When you want all replica sets where the numbers for 'desired', 'current' and 'ready' are not the same use a better pattern matching language such awk:

   kubectl get replicasets -n kube-system | awk '$3!=$4 || $4!=$5 {print $0}'

which should return both the header and any inconsistent replica sets.

You can omit the header by using:

    awk 'NR!=1 && ($3!=$4 || $4!=$5)  {print $0 }'       

Consistent replica sets will be listed with:

   awk '$3==$4 && $4==$5 {print $0}'  
HBruijn
  • 77,029
  • 24
  • 135
  • 201