Skip to content

Commit

Permalink
Refactored ${data} using json/jq instead of go-template
Browse files Browse the repository at this point in the history
To enhance readability and maintainability of the script, acquiring the
required parameters in the `$data` variable, was changed from
go-template to json, using `jq` command as its parser.

An updated Dockerfile was provided to install jq in the must-gather
image.

Signed-off-by: Roberto Alfieri <[email protected]>
  • Loading branch information
rebtoor committed May 9, 2024
1 parent 65417e2 commit ec3cfb9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM quay.io/openshift/origin-must-gather:4.14.0 as builder

FROM quay.io/centos/centos:stream9

RUN dnf update -y && dnf install xz rsync python3-pyyaml openssh-clients -y && dnf clean all
RUN dnf update -y && dnf install jq xz rsync python3-pyyaml openssh-clients -y && dnf clean all

COPY --from=builder /usr/bin/oc /usr/bin/oc

Expand Down
26 changes: 21 additions & 5 deletions collection-scripts/gather_edpm_sos
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,31 @@ gather_edpm_sos () {
echo "Finished retrieving SOS Report for ${node}"
}

if [[ ! -x $(command -v jq) ]]; then
echo "jq command wasn't found, please install it!"
return 2
fi

data=$(oc get openstackdataplanenodesets --all-namespaces -o go-template='{{range $indexns,$nodeset := .items}}{{range $index,$node := $nodeset.spec.nodes}}{{printf "%s " $node.hostName}}{{if $node.ansible.ansibleHost}}{{printf "%s " $node.ansible.ansibleHost}}{{else}}{{range $idxnet,$net := $nodeset.status.allIPs}}{{if eq $idxnet $node.hostName}}{{printf "%s " $net.ctlplane}}{{end}}{{end}}{{end}}{{printf "%s %s %s\n" $nodeset.spec.nodeTemplate.ansible.ansibleUser $nodeset.spec.nodeTemplate.ansibleSSHPrivateKeySecret $nodeset.metadata.namespace}}{{end}}{{end}}')
data=$(oc get openstackdataplanenodesets --all-namespaces -o json | jq -j '
.items[] |
.spec.nodes[].hostName as $node |
.status.allIPs[$node].ctlplane as $address |
.spec.nodeTemplate.ansible.ansibleUser as $username |
.spec.nodeTemplate.ansibleSSHPrivateKeySecret as $secret |
.metadata.namespace as $namespace |
$node, " ",
$address, " ",
$username, " ",
$secret, " ",
$namespace, "\n"
')

while read -r node address username secret namespace; do
[[ -z "$node" ]] && continue
[[ -z "${node}" ]] && continue
if [[ "${SOS_EDPM[0]}" == "all" || "${SOS_EDPM[*]}" == *"${node}"* ]]; then
run_bg gather_edpm_sos $node $address $username $secret $namespace
run_bg gather_edpm_sos "${node}" "${address}" "${username}" "${secret}" "${namespace}"
fi
done <<< "$data"
done <<< "${data}"


[[ $CALLED -eq 1 ]] && wait_bg
[[ ${CALLED} -eq 1 ]] && wait_bg

0 comments on commit ec3cfb9

Please sign in to comment.