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 :
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