forked from crc-org/snc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnc.sh
executable file
·191 lines (160 loc) · 7.33 KB
/
snc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/sh
INSTALL_DIR=crc-tmp-install-data
INSTALLER_RELEASE=v0.14.0
JQ=${JQ:-jq}
OC=${OC:-oc}
YQ=${YQ:-yq}
OPENSHIFT_INSTALL=${OPENSHIFT_INSTALL:-./openshift-install}
OPENSHIFT_RELEASE_VERSION=$(git describe --abbrev=0 HEAD 2>/dev/null) || OPENSHIFT_RELEASE_VERSION=
CRC_VM_NAME=${CRC_VM_NAME:-crc}
BASE_DOMAIN=${CRC_BASE_DOMAIN:-testing}
CRC_PV_DIR="/mnt/pv-data"
function create_json_description {
openshiftInstallerVersion=$(${OPENSHIFT_INSTALL} version)
sncGitHash=$(git describe --abbrev=4 HEAD 2>/dev/null || git rev-parse --short=4 HEAD)
echo {} | ${JQ} '.version = "1.0"' \
| ${JQ} '.type = "snc"' \
| ${JQ} ".buildInfo.buildTime = \"$(date -u --iso-8601=seconds)\"" \
| ${JQ} ".buildInfo.openshiftInstallerVersion = \"${openshiftInstallerVersion}\"" \
| ${JQ} ".buildInfo.sncVersion = \"git${sncGitHash}\"" \
| ${JQ} ".clusterInfo.clusterName = \"${CRC_VM_NAME}\"" \
| ${JQ} ".clusterInfo.baseDomain = \"${BASE_DOMAIN}\"" \
| ${JQ} ".clusterInfo.appsDomain = \"apps-${CRC_VM_NAME}.${BASE_DOMAIN}\"" >${INSTALL_DIR}/crc-bundle-info.json
# |${JQ} '.buildInfo.ocGetCo = "snc"' >${INSTALL_DIR}/crc-bundle-info.json
}
function generate_pv() {
local pvdir="${1}"
local name="${2}"
cat <<EOF
apiVersion: v1
kind: PersistentVolume
metadata:
name: ${name}
labels:
volume: ${name}
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
- ReadWriteMany
- ReadOnlyMany
hostPath:
path: ${pvdir}
persistentVolumeReclaimPolicy: Recycle
EOF
}
function setup_pv_dirs() {
local dir="${1}"
local count="${2}"
ssh="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i id_rsa_crc"
${ssh} core@api.${CRC_VM_NAME}.${BASE_DOMAIN} 'sudo bash -x -s' <<EOF
for pvsubdir in \$(seq -f "pv%04g" 1 ${count}); do
mkdir -p "${dir}/\${pvsubdir}"
done
if ! chcon -R -t svirt_sandbox_file_t "${dir}" &> /dev/null; then
echo "Failed to set SELinux context on ${dir}"
fi
chmod -R 770 ${dir}
EOF
}
function create_pvs() {
local pvdir="${1}"
local count="${2}"
setup_pv_dirs "${pvdir}" "${count}"
for pvname in $(seq -f "pv%04g" 1 ${count}); do
if ! ${OC} get pv "${pvname}" &> /dev/null; then
generate_pv "${pvdir}/${pvname}" "${pvname}" | ${OC} create -f -
else
echo "persistentvolume ${pvname} already exists"
fi
done
}
# Download the oc binary if not present in current directory
if ! which $OC; then
if [[ ! -e oc ]] ; then
if [ "${OPENSHIFT_RELEASE_VERSION}" != "" ]; then
curl -L https://mirror.openshift.com/pub/openshift-v4/clients/ocp/${OPENSHIFT_RELEASE_VERSION}/openshift-client-linux-${OPENSHIFT_RELEASE_VERSION}.tar.gz | tar zx oc
else
curl -L https://mirror.openshift.com/pub/openshift-v4/clients/oc/latest/linux/oc.tar.gz | tar zx oc
fi
fi
OC=./oc
fi
# Download yq for manipulating in place yaml configs
if ! which $YQ; then
if [[ ! -e yq ]]; then
curl -L https://github.com/mikefarah/yq/releases/download/2.2.1/yq_linux_amd64 -o yq
chmod +x yq
fi
YQ=./yq
fi
if ! which ${JQ}; then
sudo yum -y install /usr/bin/jq
fi
# Destroy an existing cluster and resources
${OPENSHIFT_INSTALL} --dir $INSTALL_DIR destroy cluster --log-level debug
if [ "${OPENSHIFT_PULL_SECRET}" = "" ]; then
echo "OpenShift pull secret must be specified through the OPENSHIFT_PULL_SECRET environment variable"
exit 1
fi
# Use the release payload for the latest known openshift release as indicated by git tags
if [ "${OPENSHIFT_RELEASE_VERSION}" != "" ]; then
OPENSHIFT_INSTALL_RELEASE_IMAGE_OVERRIDE=quay.io/openshift-release-dev/ocp-release:${OPENSHIFT_RELEASE_VERSION}
export OPENSHIFT_INSTALL_RELEASE_IMAGE_OVERRIDE
echo "Setting OPENSHIFT_INSTALL_RELEASE_IMAGE_OVERRIDE to ${OPENSHIFT_INSTALL_RELEASE_IMAGE_OVERRIDE}"
fi
# Generate a new ssh keypair for this cluster
rm id_rsa_crc* || true
ssh-keygen -N "" -f id_rsa_crc
# Create the INSTALL_DIR for the installer and copy the install-config
rm -fr $INSTALL_DIR && mkdir $INSTALL_DIR && cp install-config.yaml $INSTALL_DIR
${YQ} write --inplace $INSTALL_DIR/install-config.yaml baseDomain $BASE_DOMAIN
${YQ} write --inplace $INSTALL_DIR/install-config.yaml metadata.name $CRC_VM_NAME
${YQ} write --inplace $INSTALL_DIR/install-config.yaml compute[0].replicas 0
${YQ} write --inplace $INSTALL_DIR/install-config.yaml pullSecret "${OPENSHIFT_PULL_SECRET}"
${YQ} write --inplace $INSTALL_DIR/install-config.yaml sshKey "$(cat id_rsa_crc.pub)"
# Create the manifests using the INSTALL_DIR
${OPENSHIFT_INSTALL} --dir $INSTALL_DIR create manifests || exit 1
# Copy the config which removes taint from master
cp 99_master-kubelet-no-taint.yaml $INSTALL_DIR/openshift/
# Add worker label to master machine config
${YQ} write --inplace $INSTALL_DIR/openshift/99_openshift-cluster-api_master-machines-0.yaml spec.metadata.labels[node-role.kubernetes.io/worker] ""
# Add custom domain to cluster-ingress
${YQ} write --inplace $INSTALL_DIR/manifests/cluster-ingress-02-config.yml spec[domain] apps-${CRC_VM_NAME}.${BASE_DOMAIN}
# Start the cluster with 10GB memory and 4 CPU create and wait till it finish
export TF_VAR_libvirt_master_memory=10192
export TF_VAR_libvirt_master_vcpu=4
${OPENSHIFT_INSTALL} --dir $INSTALL_DIR create cluster --log-level debug
if [ $? -ne 0 ]; then
echo "This is known to fail with:
'pool master is not ready - timed out waiting for the condition'
see https://github.com/openshift/machine-config-operator/issues/579"
fi
# Set the VM static hostname to crc-xxxxx-master-0 instead of localhost.localdomain
SSH="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i id_rsa_crc"
HOSTNAME=$(${SSH} core@api.${CRC_VM_NAME}.${BASE_DOMAIN} hostnamectl status --transient)
${SSH} core@api.${CRC_VM_NAME}.${BASE_DOMAIN} sudo hostnamectl set-hostname ${HOSTNAME}
create_json_description
# export the kubeconfig
export KUBECONFIG=$INSTALL_DIR/auth/kubeconfig
# Create persistent volumes
create_pvs "${CRC_PV_DIR}" 30
# Once it is finished, disable the CVO
${OC} scale --replicas 0 -n openshift-cluster-version deployments/cluster-version-operator
# Disable the deployment/replicaset/statefulset config for openshift-monitoring namespace
${OC} scale --replicas=0 replicaset --all -n openshift-monitoring
${OC} scale --replicas=0 deployment --all -n openshift-monitoring
${OC} scale --replicas=0 statefulset --all -n openshift-monitoring
# Disable the deployment/replicaset/statefulset config for openshift-marketplace namespace
${OC} scale --replicas=0 deployment --all -n openshift-marketplace
${OC} scale --replicas=0 replicaset --all -n openshift-marketplace
# Delete the pods which are there in Complete state
${OC} delete pod -l 'app in (installer, pruner)' -n openshift-kube-apiserver
${OC} delete pods -l 'app in (installer, pruner)' -n openshift-kube-scheduler
${OC} delete pods -l 'app in (installer, pruner)' -n openshift-kube-controller-manager
# Disable the deployment/replicaset for openshift-machine-api and openshift-machine-config-operator
${OC} scale --replicas=0 deployment --all -n openshift-machine-api
${OC} scale --replicas=0 replicaset --all -n openshift-machine-api
${OC} scale --replicas=0 deployment --all -n openshift-machine-config-operator
${OC} scale --replicas=0 replicaset --all -n openshift-machine-config-operator