Skip to content

Commit

Permalink
Refactor to only call kubectl once
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Trent <[email protected]>
  • Loading branch information
trent-s committed Dec 12, 2023
1 parent 6673cde commit da2b0ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 8 additions & 5 deletions test/labelgroups.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ namespace=default

t_start=$(date +%s.%N)

for labelgroup in $(kubectl -n ${namespace} get labelgroups -o custom-columns=':{.metadata.name}')
alldata=$(kubectl -n ${namespace} get labelgroups -o json)

for labelgroup in $(echo ${alldata} | jq -cr '.items[].metadata.name')
do
totalEnergy=$(kubectl -n ${namespace} get labelgroup ${labelgroup} -o jsonpath='{.status.totalEnergy}')
susqlPrometheusQuery=$(kubectl -n ${namespace} get labelgroup ${labelgroup} -o jsonpath='{.status.susqlPrometheusQuery}')
phase=$(kubectl -n ${namespace} get labelgroup ${labelgroup} -o jsonpath='{.status.phase}')
labels=$(kubectl -n ${namespace} get labelgroup ${labelgroup} -o jsonpath='{.spec.labels}')
newdata=$(echo ${alldata} | jq '.items[] | select(.metadata.name=="'${labelgroup}'")')
totalEnergy=$(echo ${newdata} | jq -cr '.status.totalEnergy')
susqlPrometheusQuery=$(echo ${newdata} | jq -cr '.status.susqlPrometheusQuery')
phase=$(echo ${newdata} | jq -cr '.status.phase')
labels=$(echo ${newdata} | jq -cr '.spec.labels')

echo "LabelGroup: ${labelgroup}"
echo " - Labels: ${labels}"
Expand Down
11 changes: 7 additions & 4 deletions test/susqltop
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
# usage command -n "comma,delimited,list,of,namespaces" to limit to specified namespaces
# usage command to display SusQL energy data on all namespaces that have such data

alldata=$(kubectl get labelgroups -o json --all-namespaces)

if [[ ${#} -gt 1 && ${1} -eq '-n' ]]
then
ns=$(echo ${2} | tr ',' ' ')
else
ns=$(oc projects -q)
ns=$(echo ${alldata} | jq -r '.items[].metadata.namespace' | uniq)
fi

printf '%-20s%-40s%-40s%-20s\n' NameSpace LabelGroup Labels TotalEnergy

for namespace in ${ns}
do
for labelgroup in $(kubectl -n ${namespace} get labelgroups -o custom-columns=':{.metadata.name}')
for labelgroup in $(echo ${alldata} | jq '.items[] | select(.metadata.namespace=="'${namespace}'")' | jq -r '.metadata.name')
do
totalEnergy=$(kubectl -n ${namespace} get labelgroup ${labelgroup} -o jsonpath='{.status.totalEnergy}')
labels=$(kubectl -n ${namespace} get labelgroup ${labelgroup} -o jsonpath='{.spec.labels}')
newdata=$(echo ${alldata} | jq '.items[] | select(.metadata.namespace=="'${namespace}'" and .metadata.name=="'${labelgroup}'")')
totalEnergy=$(echo ${newdata} | jq -cr '.status.totalEnergy')
labels=$(echo ${newdata} | jq -cr '.spec.labels')
printf '%-20s%-40s%-40s%-20s\n' ${namespace} ${labelgroup} ${labels} ${totalEnergy}
done
done | sort -nr -k4 | head -n 20

0 comments on commit da2b0ed

Please sign in to comment.