Skip to content

Commit

Permalink
fix the bug of generate_globalnode.sh
Browse files Browse the repository at this point in the history
Signed-off-by: duanmengkk <[email protected]>
  • Loading branch information
duanmengkk committed May 11, 2024
1 parent 71c767f commit 51913dd
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions hack/generate_globalnode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,36 @@ if [ -z "$KUBECONFIG" ]; then
exit 1
fi

# Creating a directory for logs
mkdir -p kube_apply_logs

nodes=$(kubectl get nodes -o jsonpath='{.items[*].metadata.name}')
for node in ${nodes}; do
nodeIP=$(kubectl get node ${node} -o jsonpath='{.status.addresses[0].address}')
labels=$(kubectl get node ${node} -o jsonpath='{.metadata.labels}')
labelsFormatted=$(echo "$labels" | jq -r 'to_entries | .[] | " \(.key): \(.value)"')
echo "

# Use jq to ensure all values are strings, but also explicitly add quotes in the YAML formatting step below
labelsFormatted=$(echo "$labels" | jq -r 'to_entries | map(.value |= tostring) | .[] | " \(.key): \"\(.value)\""')

yamlContent="
apiVersion: kosmos.io/v1alpha1
kind: GlobalNode
metadata:
name: ${node}
spec:
state: \"reserved\"
nodeIP: \"${nodeIP}\"
labels:
$(echo "${labelsFormatted}" | sed 's/=/": "/g' | awk '{print " " $0}')
" | kubectl apply -f -
$(echo "${labelsFormatted}" | awk '{print " " $0}')
"

# Log the YAML content to a file for inspection
echo "$yamlContent" > kube_apply_logs/${node}.yaml

# Apply the YAML
echo "$yamlContent" | kubectl apply -f -

# clear resources
rm -rf kube_apply_logs

done

0 comments on commit 51913dd

Please sign in to comment.