Capture creation of cluster into Helm chart
[icn.git] / deploy / clusters / clusters.sh
1 #!/usr/bin/env bash
2 set -eux -o pipefail
3
4 SCRIPTDIR="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
5 LIBDIR="$(dirname $(dirname ${SCRIPTDIR}))/env/lib"
6
7 source $LIBDIR/logging.sh
8 source $LIBDIR/common.sh
9
10 FLANNEL_VERSION="v0.15.0"
11
12 # This may be used to update the in-place addon YAML files from the
13 # upstream projects
14 function build_source {
15     mkdir -p ${SCRIPTDIR}/addons
16
17     # Flannel
18     curl -sL https://raw.githubusercontent.com/coreos/flannel/${FLANNEL_VERSION}/Documentation/kube-flannel.yml -o ${SCRIPTDIR}/addons/flannel.yaml
19     cat <<EOF >${SCRIPTDIR}/templates/flannel-addon.yaml
20 {{- range \$clusterName, \$cluster := .Values.clusters }}
21 {{- if eq \$cluster.cni "flannel" }}
22 ---
23 $(kubectl create configmap flannel-addon --from-file=${SCRIPTDIR}/addons/flannel.yaml -o yaml --dry-run=client)
24 {{- end }}
25 {{- end }}
26 EOF
27     sed -i -e 's/  name: flannel-addon/  name: {{ $clusterName }}-flannel-addon/' ${SCRIPTDIR}/templates/flannel-addon.yaml
28     sed -i -e 's/10.244.0.0\/16/{{ $cluster.podCidr }}/' ${SCRIPTDIR}/templates/flannel-addon.yaml
29
30     # Flux
31     flux install --export >${SCRIPTDIR}/addons/flux-system.yaml
32     # The name "sync" must be sorted after "flux-system" to ensure
33     # Flux CRDs are instantiated first
34     cat <<'EOF' >${SCRIPTDIR}/addons/sync.yaml
35 ---
36 apiVersion: source.toolkit.fluxcd.io/v1beta1
37 kind: GitRepository
38 metadata:
39   name: {{ $cluster.flux.repositoryName }}
40   namespace: flux-system
41 spec:
42   gitImplementation: go-git
43   interval: 1m0s
44   ref:
45     branch: {{ $cluster.flux.branch }}
46   timeout: 20s
47   url: {{ $cluster.flux.url }}
48 ---
49 apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
50 kind: Kustomization
51 metadata:
52   name: {{ $clusterName }}-flux-sync
53   namespace: flux-system
54 spec:
55   interval: 10m0s
56   path: {{ $cluster.flux.path }}
57   prune: true
58   sourceRef:
59     kind: GitRepository
60     name: {{ $cluster.flux.repositoryName }}
61 EOF
62     cat <<EOF >${SCRIPTDIR}/templates/flux-addon.yaml
63 {{- range \$clusterName, \$cluster := .Values.clusters }}
64 {{- if \$cluster.flux }}
65 ---
66 $(kubectl create configmap flux-addon --from-file=${SCRIPTDIR}/addons/flux-system.yaml,${SCRIPTDIR}/addons/sync.yaml -o yaml --dry-run=client)
67 {{- end }}
68 {{- end }}
69 EOF
70     sed -i -e 's/  name: flux-addon/  name: {{ $clusterName }}-flux-addon/' ${SCRIPTDIR}/templates/flux-addon.yaml
71 }
72
73 case $1 in
74     "build-source") build_source ;;
75     *) cat <<EOF
76 Usage: $(basename $0) COMMAND
77
78 Commands:
79   build-source  - Rebuild the in-tree addon YAML files
80 EOF
81        ;;
82 esac