Hi all, regarding the *Advanced Kubectl Practice Test Q6*, the correct answer is . . .

Umair Hoodbhoy:
Hi all, regarding the Advanced Kubectl Practice Test Q6, the correct answer is kubectl get pv --sort-by .spec.capacity.storage. As in, that gets me this correct output :white_check_mark::

controlplane ~ ➜  kubectl get pv --sort-by .spec.capacity.storage
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv-log-4   40Mi       RWX            Retain           Available                                   58m
pv-log-1   100Mi      RWX            Retain           Available                                   58m
pv-log-2   200Mi      RWX            Retain           Available                                   58m
pv-log-3   300Mi      RWX            Retain           Available                                   58m

Note that I get the same output if I try kubectl get pv --sort-by=.spec.capacity.storage OR kubectl get pv --sort-by '{.spec.capacity.storage'} OR kubectl get pv --sort-by='{.spec.capacity.storage'}

However, compare that last command with this one that just adds an extra space after the equals sign. i.e. kubectl get pv --sort-by ='{.spec.capacity.storage'} Now the answer is sorted wrong. See:

controlplane ~ ➜  kubectl get pv --sort-by ='{.spec.capacity.storage'}
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv-log-1   100Mi      RWX            Retain           Available                                   57m
pv-log-2   200Mi      RWX            Retain           Available                                   57m
pv-log-3   300Mi      RWX            Retain           Available                                   57m
pv-log-4   40Mi       RWX            Retain           Available                                   57m

Q1) Why is that? I get the same incorrectly sorted output with kubectl get pv --sort-by ='{.items[*].spec.capacity'}See:

controlplane ~ ➜  kubectl get pv --sort-by ='{.items[*].spec.capacity'}
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv-log-1   100Mi      RWX            Retain           Available                                   57m
pv-log-2   200Mi      RWX            Retain           Available                                   57m
pv-log-3   300Mi      RWX            Retain           Available                                   57m
pv-log-4   40Mi       RWX            Retain           Available                                   57m

Q2) Why can’t you get the right answer by using .items[*] at the start? See all these other attempts:

controlplane ~ ➜  kubectl get pv --sort-by='{.items[*].spec.capacity.storage'}
No resources found

controlplane ~ ➜  kubectl get pv --sort-by=.items[*].spec.capacity.storage
No resources found

controlplane ~ ➜  kubectl get pv --sort-by =.items[*].spec.capacity.storage
No resources found

controlplane ~ ➜  kubectl get pv --sort-by .items[*].spec.capacity.storage
No resources found

Alistair Mackay:
Hi @Umair Hoodbhoy

I am surprised that --sort-by='{.spec.capacity.storage'} works at all!

Your quotes and braces don’t match up. The quotes are used to ensure that the shell does not try to interpret characters that are meaningful to it, and braces are one such character.

When you quote an argument value, you must 'enclose the entire value' so it starts and ends with quote

'{.spec.capacity.storage'} :x:
'{.spec.capacity.storage}' :white_check_mark:

As for the items bit. With sorting, items are implicit as you’re always dealing with a list, so you only need specify the path to the individual attribute.