Skip to content

Instantly share code, notes, and snippets.

@jgato
Created December 19, 2023 15:17
Show Gist options
  • Save jgato/11f9ae8dd101392181bfe53824080ff9 to your computer and use it in GitHub Desktop.
Save jgato/11f9ae8dd101392181bfe53824080ff9 to your computer and use it in GitHub Desktop.
#! /bin/bash
#
# This script takes a sitconfig file and it will move
# for each node
# the nodeNetwork section into a separated NMStateconfig
#
# It can be helpful to reduce a siteconfig file, moving
# the network configuration to separate NMStateconfig files
# Then everything will be merged together with ArgoCD
# to start a deployment
#
# The final result should not be any difference from ArgoCD
# perspective. Same objects generated.
#
# Finally, the script includes the NMStateconfig into your
# kustomization file.
#
# You cannot run the script twice. So, if somethig goes wrong
# just restore everything. Actually, you should be doing this
# over a git repository with siteconfigs
#
# Author: Jose Gato Luis <jgato@redhat.com>
if [ $# -lt 1 ]
then
echo "Usage: ./script.sh <SITECONFIG_FILE>"
exit 1
fi
CLUSTERNAME=$(yq -r ' explode(.) | .metadata.name' "$1")
export CLUSTERNAME
mkdir -p NMStates
for node in $(yq -r ' explode(.) | .spec.clusters[0].nodes[].hostName' "$1")
do
export NODENAME=${node}
NODENETWORK=$(yq -r ' explode(.) | .spec.clusters[0].nodes[] | select(.hostName == env(NODENAME)) | .nodeNetwork' "$1")
export NODENETWORK
cat <<"EOF" | envsubst > /tmp/nmstate.yaml
apiVersion: agent-install.openshift.io/v1beta1
kind: NMStateConfig
metadata:
annotations:
argocd.argoproj.io/sync-wave: "1"
ran.openshift.io/ztp-gitops-generated: '{}'
labels:
app.kubernetes.io/instance: clusters
nmstate-label: ${CLUSTERNAME}
name: ${NODENAME}
namespace: ${CLUSTERNAME}
spec:
EOF
echo "Writing NMStateConfig of ${NODENAME} into ./NMStates/nmstate-${NODENAME}.yaml"
yq -r '.spec = env(NODENETWORK) ' /tmp/nmstate.yaml > ./NMStates/nmstate-"${NODENAME}.yaml"
echo "Linking the file into kustomization.yaml"
CREATEDFILE="./NMStates/nmstate-${NODENAME}.yaml"
export CREATEDFILE
yq -i '.bases += [ env(CREATEDFILE) ]' kustomization.yaml
done
echo "Deleting nodeNetwork info from ${1}"
yq -i ' explode(.) | del(.spec.clusters[0].nodes[].nodeNetwork)' "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment