-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkpvyaml
executable file
·284 lines (250 loc) · 9.12 KB
/
mkpvyaml
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
278
279
280
281
282
283
284
#!/usr/bin/env python3
#
# MkPVYaml : Generate PV yaml files for iSCSI external vols on IKS
#
# Goal: Autogenerate the "pv.yaml" files needed for worker nodes,
# as documented here: https://github.com/akgunjal/block-volume-attacher
#
# Requirements:
# Ensure the following environment variables are set:
# SL_API_KEY
# SL_USERNAME
# SL_API_KEY
# Make sure to do "bx login"
#
# Input file:
# Assumes an input descriptor file named "yamlgen.yaml" of the following format:
#
# cluster: jeffpx1 # name of IKS cluster
# type: endurance # performance | endurance
# offering: storage_as_a_service # storage_as_a_service | enterprise | performance
# # performance:
# # - iops: 100 # INTEGER between 100 and 1000 in multiples of 100
# endurance:
# - tier: 0.25 # [0.25|2|4|10]
# size: [ 30 ] # Array of disk capacity sizes (ToDo)
#
# Output:
# In a perfect world, this will create a set of "pv.yaml" files as input to the block-attach daemonset.
#
# ToDo:
# - Doesn't currently allow an "array" of disks (i.e. "size" can only be len(1))
# - Only tested "storage_as_a_service" offering
#
#
import os
import time
import sys, traceback
import json
import urllib.request
from os.path import expanduser
import SoftLayer
from pprint import pprint as pp
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
Paraml = "./yamlgen.yaml" # Name of input parameter file
#
# Load the input parameter file
#
def load_cfg():
global doc
with open(Paraml, 'r') as f:
doc = load(f)
#
# Run sanity checks on the input param file before proceeding
#
def check_cfg():
# Sanity: Must have 'type' and 'size' and ('performance' or 'endurance')
if not ('type' in doc and 'size' in doc and 'cluster'):
raise Exception("Must have 'cluster', 'type' and 'size'")
if doc['type'] == 'performance' and not 'performance' in doc:
raise Exception("'performance' type but no 'performance' clause")
if doc['type'] == 'endurance' and not 'endurance' in doc:
raise Exception("'endurance' type but no 'endurance' clause")
if 'performance' in doc and 'endurance' in doc:
raise Exception("Must specify 'performance' OR 'endurance'")
#
# Get the 'id's and 'ips' of the IKS workers.
# The worker 'id's correspond to the VirtualServer "instance" hostnames
#
def get_instances(cluster):
instances = []
home = expanduser("~")
bmxconfig = home + "/.bluemix/config.json"
with open(bmxconfig) as f:
data = json.load(f)
token= data['IAMToken']
headers={'Content-Type': 'application/json', 'X-Region' : doc['region'], 'Authorization' : token }
try:
request = urllib.request.Request(url="https://containers.bluemix.net/v1/clusters/" + cluster + "/workers" , headers=headers)
f = urllib.request.urlopen(request)
response = f.read().decode('utf-8')
print(response)
contents = json.loads(response)
except:
print ("Unable to get cluster workers. Are you logged in (\"bx login\")? Does " + cluster + " actually exist? Are your credentials set?")
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
sys.exit(-1)
for row in contents:
inst = {}
inst['hostname'] = row['id']
inst['id'] = row['id']
inst['ip'] = row['privateIP']
inst['location'] = row['location']
instances.append (inst)
return instances
#
# Return unique ID for IP Address
# (required for authorize_host)
#
def get_ip_id (ipaddr):
result = Nmgr.ip_lookup(ipaddr)
return result['id']
#
# Wait for volume to be created, based on "orderId"
#
def wait4_vol(orderId):
while True:
result = BSmgr.list_block_volumes(mask='billingItem.orderItem.order')
for i in result:
if 'billingItem' in i:
if i['billingItem']['orderItem']['order']['id'] == orderId:
return (i['id'])
print (" No volume yet ... for orderID : ", orderId)
time.sleep(8)
#
# Authorize access to volume (volId) from hostIP
# Loop on exceptions, waiting if needed for volume to be ready
#
def authorize_host_vol(volId, hostIP):
ip_id = get_ip_id (hostIP)
ip_ids = [ ip_id ]
while True:
try:
print (" Granting access to volume: %s for HostIP: %s" % (volId, hostIP))
access = BSmgr.authorize_host_to_volume(volume_id=volId, ip_address_ids=ip_ids)
return access
except:
print (" Vol %s is not yet ready ..." % volId)
time.sleep (10)
#
# Retrieve volume access info for a given volId
#
def get_volume_access (volid):
lba = BSmgr.get_block_volume_access_list(volid)
return (lba['allowedIpAddresses'][0]['allowedHost']['name'],
lba['allowedIpAddresses'][0]['allowedHost']['credential']['username'],
lba['allowedIpAddresses'][0]['allowedHost']['credential']['password'],
lba['allowedIpAddresses'][0]['ipAddress'])
#
# Retrieve volume details for a given volId
#
def get_volume_details (volid):
lv = BSmgr.get_block_volume_details(volid)
return (lv['serviceResourceBackendIpAddress'],
lv['lunId'],
lv['capacityGb'])
#
# Generate the yaml file, needed to attach a given volume to a given worker
#
def mkpvyaml(pv, outfile):
for i in range(len(pv['vols'])):
print ("""
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: %s-pv%s
annotations:
ibm.io/iqn: "%s"
ibm.io/username: "%s"
ibm.io/password: "%s"
ibm.io/targetip: "%s"
ibm.io/lunid: "%s"
ibm.io/nodeip: "%s"
ibm.io/volID: "%s"
spec:
capacity:
storage: %sGi
accessModes:
- ReadWriteOnce
hostPath:
path: /
storageClassName: ibmc-block-attacher
""" % (pv['hostname'], i+1, pv['vols'][i]['iqn'], pv['vols'][i]['username'], pv['vols'][i]['password'],
pv['vols'][i]['targetip'], pv['vols'][i]['lunid'], pv['vols'][i]['nodeip'], pv['vols'][i]['volId'], pv['vols'][i]['capacity']), file=outfile)
### --------------------------------------------------------------------------------
load_cfg()
check_cfg()
client = SoftLayer.Client()
global BSmgr, Nmgr
BSmgr = SoftLayer.BlockStorageManager(client)
Nmgr = SoftLayer.managers.network.NetworkManager(client)
#
# Need API to list instances, given an IKS clusterID
# In the meantime, list instanceIDs for worker nodes explicitly
#
pp (doc)
instances = get_instances(doc['cluster'])
for i in instances:
print (i['hostname'], i['ip'])
#
# For each disk ...
#
i.update ({'vols': []})
for j in doc['size']:
vol = {}
vol.update ({ 'size' : j })
if doc['type'] == 'performance':
iops = doc['performance'][0]['iops']
service_offering = "performance"
tier = ""
else:
tier = doc['endurance'][0]['tier']
service_offering = "enterprise"
iops = ""
print("Creating Vol of size", j , "with type: ", doc['type'] )
try:
iops_param = iops if doc['type'] == 'performance' else None
#
# order the volume
#
print (" Ordering block storage of size: %s for host: %s" % (j, i['hostname']))
result = BSmgr.order_block_volume(storage_type=doc['type'],
location=i['location'],
size=j,
tier_level=tier,
iops=iops_param,
os_type='LINUX',
hourly_billing_flag=True,
service_offering=doc['offering'])
print (" ORDER ID = ", result['orderId'])
vol.update ( {'orderId': result['orderId']} )
i['vols'].append(vol)
except:
raise
# pp (instances)
#
# For all the worker instances ...
#
for i in instances:
for j in i['vols']:
volId = wait4_vol(j['orderId'])
print (" Order ID = ", j['orderId'], "has created VolId = ", volId)
access_info = authorize_host_vol (volId, i['ip'])
iqn, username, password, nodeip = get_volume_access (volId)
targetip, lunid, capacity = get_volume_details (volId)
j.update ({'volId': volId, 'iqn': iqn, 'username': username, 'password': password,
'nodeip': nodeip, 'targetip': targetip, 'lunid': lunid,
'capacity': capacity})
OutFile = "pv-" + doc['cluster'] + ".yaml"
OutF = open (OutFile, "w")
# pp (instances)
for i in instances:
mkpvyaml(i, OutF)
OutF.close()
print ("Output file created as : ", OutFile)