forked from openshift/windows-machine-config-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machineset.sh
executable file
·277 lines (249 loc) · 8.82 KB
/
machineset.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env bash
# machineset.sh - create a Windows MachineSet for Windows Machine Config Operator
# Creates a MachineSet.yaml file and apply/delete the MachineSet if optional action is provided
#
# USAGE
# machineset.sh
# OPTIONS
# $1 Action (Optional) apply/delete the MachineSet
# PREREQUISITES
# oc to fetch cluster info and apply/delete MachineSets on the cluster(cluster should be logged in)
# aws to fetch Windows AMI id for AWS platform (only required for clusters running on AWS)
set -euo pipefail
WMCO_ROOT=$(dirname "${BASH_SOURCE}")/..
source $WMCO_ROOT/hack/common.sh
ACTION=${1:-}
# get_spec returns the template yaml common for all cloud providers
get_spec() {
if [ "$#" -lt 3 ]; then
error-exit incorrect parameter count for get_spec $#
fi
local infraID=$1
local az=$2
local provider=$3
# set machineset name, short name for Azure and vSphere due to
# the limit in the number of characters for VM name
machineSetName="winworker"
# check provider
if [ "$provider" = "aws" ]; then
# improve machineset name for aws provider
machineSetName="$infraID"-"$machineSetName"-"$az"
fi
cat <<EOF
apiVersion: machine.openshift.io/v1beta1
kind: MachineSet
metadata:
labels:
machine.openshift.io/cluster-api-cluster: ${infraID}
name: ${machineSetName}
namespace: openshift-machine-api
spec:
replicas: 1
selector:
matchLabels:
machine.openshift.io/cluster-api-cluster: ${infraID}
machine.openshift.io/cluster-api-machineset: ${machineSetName}
template:
metadata:
labels:
machine.openshift.io/cluster-api-cluster: ${infraID}
machine.openshift.io/cluster-api-machine-role: worker
machine.openshift.io/cluster-api-machine-type: worker
machine.openshift.io/cluster-api-machineset: ${machineSetName}
machine.openshift.io/os-id: Windows
spec:
metadata:
labels:
node-role.kubernetes.io/worker: ""
EOF
}
# get_aws_ms creates a MachineSet for AWS Cloud Provider
get_aws_ms() {
if [ "$#" -lt 4 ]; then
error-exit incorrect parameter count for get_aws_ms $#
fi
local infraID=$1
local region=$2
local az=$3
local provider=$4
# get the AMI id for the Windows VM
ami_id=$(aws ec2 describe-images --region ${region} --filters "Name=name,Values=Windows_Server-2019*English*Full*Containers*" "Name=is-public,Values=true" --query "reverse(sort_by(Images, &CreationDate))[*].{name: Name, id: ImageId}" --output json | jq -r '.[0].id')
if [ -z "$ami_id" ]; then
error-exit "unable to find AMI ID for Windows Server 2019 1809"
fi
cat <<EOF
$(get_spec $infraID $az $provider)
providerSpec:
value:
ami:
id: ${ami_id}
apiVersion: awsproviderconfig.openshift.io/v1beta1
blockDevices:
- ebs:
iops: 0
volumeSize: 120
volumeType: gp2
credentialsSecret:
name: aws-cloud-credentials
deviceIndex: 0
iamInstanceProfile:
id: ${infraID}-worker-profile
instanceType: m5a.large
kind: AWSMachineProviderConfig
placement:
availabilityZone: ${az}
region: ${region}
securityGroups:
- filters:
- name: tag:Name
values:
- ${infraID}-worker-sg
subnet:
filters:
- name: tag:Name
values:
- ${infraID}-private-${az}
tags:
- name: kubernetes.io/cluster/${infraID}
value: owned
userDataSecret:
name: windows-user-data
EOF
}
# get_azure_ms creates a MachineSet for Azure Cloud Provider
get_azure_ms() {
if [ "$#" -lt 4 ]; then
error-exit incorrect parameter count for get_azure_ms $#
fi
local infraID=$1
local region=$2
local az=$3
local provider=$4
cat <<EOF
$(get_spec $infraID $az $provider)
providerSpec:
value:
apiVersion: azureproviderconfig.openshift.io/v1beta1
credentialsSecret:
name: azure-cloud-credentials
namespace: openshift-machine-api
image:
offer: WindowsServer
publisher: MicrosoftWindowsServer
resourceID: ""
sku: 2019-Datacenter-with-Containers
version: latest
kind: AzureMachineProviderSpec
location: ${region}
managedIdentity: ${infraID}-identity
metadata:
creationTimestamp: null
networkResourceGroup: ${infraID}-rg
osDisk:
diskSizeGB: 128
managedDisk:
storageAccountType: Premium_LRS
osType: Windows
publicIP: false
resourceGroup: ${infraID}-rg
subnet: ${infraID}-worker-subnet
userDataSecret:
name: windows-user-data
namespace: openshift-machine-api
vmSize: Standard_D2s_v3
vnet: ${infraID}-vnet
zone: "${az}"
EOF
}
# get_vsphere_ms creates a MachineSet for vSphere Cloud Provider
get_vsphere_ms() {
if [ "$#" -lt 2 ]; then
error-exit incorrect parameter count for get_vsphere_ms $#
fi
local infraID=$1
local provider=$2
# set golden image template name
# TODO: read from parameter
template="windows-golden-images/windows-server-2004-template"
# TODO: Reduce the number of API calls, make just one call
# to `oc get machines` and pass the data around. This is the
# 3rd call being introduced across the script and can be avoided
providerSpec=$(oc get machines \
-n openshift-machine-api \
-l machine.openshift.io/cluster-api-machine-role=worker \
-o jsonpath="{.items[0].spec.providerSpec.value}" \
) || {
error-exit "error getting providerSpec for ${provider} cluster ${infraID}"
}
if [ -z "$providerSpec" ]; then
error-exit "cannot find providerSpec for ${provider} cluster ${infraID}"
fi
# get credentialsSecret
credentialsSecret=$(echo "${providerSpec}" | jq -r '.credentialsSecret.name')
# get network name TODO: review when devices > 1
networkName=$(echo "${providerSpec}" | jq -r '.network.devices[0].networkName')
# get workspace specs
datacenter=$(echo "${providerSpec}" | jq -r '.workspace.datacenter')
datastore=$(echo "${providerSpec}" | jq -r '.workspace.datastore')
folder=$(echo "${providerSpec}" | jq -r '.workspace.folder')
resourcePool=$(echo "${providerSpec}" | jq -r '.workspace.resourcePool')
server=$(echo "${providerSpec}" | jq -r '.workspace.server')
# build machineset
cat <<EOF
$(get_spec $infraID "" $provider)
providerSpec:
value:
apiVersion: vsphereprovider.openshift.io/v1beta1
credentialsSecret:
name: ${credentialsSecret}
diskGiB: 128
kind: VSphereMachineProviderSpec
memoryMiB: 16384
network:
devices:
- networkName: ${networkName}
numCPUs: 4
numCoresPerSocket: 1
snapshot: ""
template: ${template}
workspace:
datacenter: ${datacenter}
datastore: ${datastore}
folder: ${folder}
resourcePool: ${resourcePool}
server: ${server}
EOF
}
# Retrieves the Cloud Provider for the OpenShift Cluster
provider="$(oc -n openshift-kube-apiserver get configmap config -o json | jq -r '.data."config.yaml"' | jq '.apiServerArguments."cloud-provider"' | jq -r '.[]')"
# Gets the Infrastructure Id for the cluster like `pmahajan-azure-68p9l-gv45m`
infraID="$(oc get -o jsonpath='{.status.infrastructureName}{"\n"}' infrastructure cluster)"
# Determines the region based on existing MachinesSets like `us-east-1` for aws or `centralus` for azure
region="$(oc get machines -n openshift-machine-api | grep -w "Running" | awk '{print $4}' | head -1)"
# Determines the availability zone based on existing MachinesSets like `us-east-1a` for aws or `2` for azure
az="$(oc get machines -n openshift-machine-api | grep -w "Running" | awk '{print $5}' | head -1)"
# Creates/deletes a MachineSet for Cloud Provider
case "$provider" in
aws)
ms=$(get_aws_ms $infraID $region $az $provider)
;;
azure)
ms=$(get_azure_ms $infraID $region $az $provider)
;;
vsphere)
ms=$(get_vsphere_ms $infraID $provider)
;;
*)
error-exit "platform '$provider' is not yet supported by this script"
;;
esac
# If action like apply/delete is provided, directly apply the MachineSet else create a yaml file
if [ -n "$ACTION" ]; then
if [[ ! "$ACTION" =~ ^apply|delete$ ]]; then
echo "$ms" > MachineSet.yaml
error-exit "Action (1st parameter) must be \"apply\" or \"delete\". Creating a yaml file"
fi
echo "$ms" | oc $ACTION -n openshift-machine-api -f -
else
echo "$ms" > MachineSet.yaml
fi