Update documentation for Cluster-API and Flux
[icn.git] / tools / migration / to_r6.sh
1 #!/bin/bash
2 set -eu -o pipefail
3
4 SCRIPTDIR="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
5 LIBDIR="${SCRIPTDIR}/../../env/lib"
6
7 source ${LIBDIR}/common.sh
8
9 function usage {
10     cat <<EOF
11 Usage: $(basename $0) -n nodes.json -p provisioning.yaml >site.yaml
12
13 This tool assists in migrating ICN R5 and earlier configurations to R6
14 by translating an existing nodes.json and Provisioning resource YAML
15 into values files to provide to the ICN machine and cluster Helm
16 charts.
17
18 IMPORTANT: The tool is only intended to be a starting point.  The
19 following limitations should be noted:
20 - The Kubernetes control plane endpoint must be explicitly specified
21   with the controlPlaneEndpoint and controlPlanePrefix values in the
22   cluster values YAML.
23 - The value of image_name in nodes.json is ignored.
24 - The SSH authorized key that will copied to the provisioned nodes is
25   ${HOME}/.ssh/id_rsa.pub.
26 - spec.KUDPlugins in the Provisioning resource is ignored.  This
27   functionality is accomplished in R6 with Flux.
28
29 After reviewing and updating the migrated site YAML as needed, the
30 YAML secrets may be encrypted with the below command before committing
31 to source control for use with Flux:
32
33   $(readlink -f ${SCRIPTDIR}/../../deploy/site/site.sh) sops-encrypt-site site.yaml key-name
34
35 EOF
36     exit 1
37 }
38
39 function migrate {
40     cat <<EOF
41 apiVersion: v1
42 kind: Namespace
43 metadata:
44   name: metal3
45 ---
46 apiVersion: source.toolkit.fluxcd.io/v1beta1
47 kind: GitRepository
48 metadata:
49   name: icn
50   namespace: metal3
51 spec:
52   gitImplementation: go-git
53   interval: 1m0s
54   ref:
55     branch: master
56   timeout: 20s
57   url: https://gerrit.akraino.org/r/icn
58 EOF
59     list_nodes | while IFS=',' read -r name ipmi_username ipmi_password ipmi_address boot_mac os_username os_password os_image_name; do
60         cat <<EOF
61 ---
62 apiVersion: helm.toolkit.fluxcd.io/v2beta1
63 kind: HelmRelease
64 metadata:
65   name: ${name}
66   namespace: metal3
67 spec:
68   interval: 5m
69   chart:
70     spec:
71       chart: deploy/machine
72       sourceRef:
73         kind: GitRepository
74         name: icn
75       interval: 1m
76   values:
77 EOF
78         node_machine_values | sed -e 's/^/    /'
79     done
80     cat <<EOF
81 ---
82 apiVersion: helm.toolkit.fluxcd.io/v2beta1
83 kind: HelmRelease
84 metadata:
85   name: $(provisioning_json | jq -r '.metadata.name')
86   namespace: metal3
87 spec:
88   interval: 5m
89   chart:
90     spec:
91       chart: deploy/cluster
92       sourceRef:
93         kind: GitRepository
94         name: icn
95       interval: 1m
96     values:
97 EOF
98     cluster_values | sed -e 's/^/    /'
99 }
100
101 function cluster_values {
102     cat <<EOF
103 clusterName: $(cluster_name)
104 clusterLabels:
105   owner: $(provisioning_json | jq -r '.metadata.labels.owner')
106   provider: $(provisioning_json | jq -r '.metadata.labels.owner')
107 EOF
108     if [[ $(cluster_type) != "null" ]]; then
109         cat <<EOF
110   cluster-type: $(cluster_type)
111 EOF
112     fi
113     cat <<EOF
114 numControlPlaneMachines: $(provisioning_json | jq -r '.spec.masters | length')
115 numWorkerMachines: $(provisioning_json | jq -r '.spec.workers | length')
116 controlPlaneEndpoint: # TODO
117 controlPlanePrefix: # TODO
118 controlPlaneHostSelector:
119   matchExpressions:
120     key: machine
121     operator: In
122     values:
123 $(provisioning_json | jq -r '.spec.masters[] | keys[0]' | awk '{print "    - " $0}')
124 workersHostSelector:
125   matchExpressions:
126     key: machine
127     operator: In
128     values:
129 $(provisioning_json | jq -r '.spec.workers[] | keys[0]' | awk '{print "    - " $0}')
130 userData: {}
131 # TODO
132 #flux:
133 #  url: https://gerrit.akraino.org/r/icn
134 #  branch: master
135 #  path: ./deploy/site/cluster-icn
136 EOF
137 }
138
139 function cluster_name {
140     provisioning_json | jq -r '.metadata.labels.cluster'
141 }
142
143 function cluster_type {
144     provisioning_json | jq -r '.metadata.labels."cluster-type"'
145 }
146
147 function provisioning_json {
148     cat ${PROVISIONING_YAML} | python3 -c 'import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin.read())))'
149 }
150
151 while getopts ":n:o:p:" opt; do
152     case "${opt}" in
153         n)
154             NODES_JSON=${OPTARG}
155             ;;
156         p)
157             PROVISIONING_YAML=${OPTARG}
158             ;;
159         *)
160             usage
161             ;;
162     esac
163 done
164 shift $((OPTIND-1))
165
166 if [[ -z "${NODES_JSON}" ]] || [[ -z "${PROVISIONING_YAML}" ]]; then
167     usage
168 fi
169
170 export NODES_FILE=${NODES_JSON}
171 migrate