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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
from django.conf import settings
import os
import pexpect
import re
import time
import json
from .utils import *
from .remoteregionhandler import RemoteRegionWorker
class Vendor(RemoteRegionWorker):
def __init__(self, logger, dbCoordinationQueue, vmbCoordinationQueue, doneQueue):
self.logger = logger
self.doneQueue = doneQueue
self.dbCoordinationQueue = dbCoordinationQueue
self.vmbCoordinationQueue = vmbCoordinationQueue
self.logger.info(".. created Vendor handler")
def perform_vendor_setup(self, data):
self.logger.info("Checking if any vendor setup needs to be done")
if 'namespace' in data:
namespace = data['namespace']
region_oam_ip = data['region_oam_ip']
network_setup = data['network_setup']
vendor = self._get_vendor(namespace, self.logger)
self.logger.info("Vendor:" + vendor)
self.logger.info("Network setup:" + network_setup)
self._perform_vendor_specific_actions(vendor, namespace, region_oam_ip, network_setup, self.logger)
self.logger.info("Done configuring vendor related things on the remote region")
else:
self.logger.info("Vendor setup not needed for this call.")
self._wait_and_done()
def _wait_and_done(self):
if self.dbCoordinationQueue != '' and self.vmbCoordinationQueue != '' and self.doneQueue != '':
self.logger.info("About to be done..waiting for cleanup")
db_coordination = self.dbCoordinationQueue.get()
self.logger.info(" DB coordination message:" + db_coordination)
vmb_coordination = self.vmbCoordinationQueue.get()
self.logger.info(" VMB coordination message:" + vmb_coordination)
self.doneQueue.put("Done")
self.logger.info("Done")
def _get_vendor(self, namespace, logger):
logger.info(" Inside _get_vendor")
parts = namespace.split('-')
logger.info("parts:" + str(parts))
vendor = "unknown"
if len(parts) >= 5:
if parts[3] == "ss" or parts[4] == "ss":
#vendor_shortform = parts[3]
#if vendor_shortform == "ss":
vendor = "Samsung"
return vendor
def _perform_vendor_specific_actions(self, vendor, namespace, region_oam_ip, network_setup, logger):
logger.info(" Inside _perform_vendor_specific_actions...")
if vendor == "Samsung":
logger.info(" Handling Samsung...")
samsung_provisioner = Samsung(logger)
samsung_provisioner.setup(namespace, region_oam_ip, network_setup)
def setup_network(self, region, logger):
logger.info(" Setting up network...")
remote_region_oam_ip = self._get_remote_region_oam_ip(region, logger)
logger.info(" Received OAM IP: " + region + " " + remote_region_oam_ip)
self.create_host_network(remote_region_oam_ip, logger)
def check_site(self, region, logger):
logger.info(" Checking network...")
remote_region_oam_ip = self._get_remote_region_oam_ip(region, logger)
host_username, host_password, ssh_prefix = get_host_connection_details(remote_region_oam_ip)
cmds = []
cmds.append(ssh_prefix + " kubectl describe nodes controller-0 --kubeconfig=/etc/kubernetes/admin.conf ")
output_lines = self._run_command_get_all_lines(cmds, host_password, logger, block=True)
#logger.info("Returned output lines:")
#logger.info(output_lines)
new_op_lines = []
if len(output_lines) > 0:
for line in output_lines.split("\n"):
if 'Connection to' not in line:
if 'Capacity:' in line or 'Allocatable:' in line or 'Allocated' in line or 'intel.com/pci_sriov_net' in line:
logger.info("LINE:" + line)
new_op_lines.append(line)
namespaces = self.check_namespaces(remote_region_oam_ip, logger)
#new_op_lines.append("----------")
#for namespace_line in namespaces.split("\n"):
# new_op_lines.append(namespace_line)
namespace_secrets = self.check_namespace_secrets(region, remote_region_oam_ip, logger)
namespace_service_accounts = self.check_namespace_serviceaccounts(region, remote_region_oam_ip, logger)
online_status = self.check_online_status(region, remote_region_oam_ip, logger)
return new_op_lines, namespaces, namespace_secrets, namespace_service_accounts, online_status
class Samsung(RemoteRegionWorker):
def __init__(self, logger):
self.logger = logger
self.logger.info(".. created Samsung handler")
def setup(self, namespace, remote_region_oam_ip, network_setup):
self.logger.info(" Inside Samsung setup")
self._create_docker_reg_secret(namespace, remote_region_oam_ip, self.logger)
self._create_serviceaccount(namespace, remote_region_oam_ip, self.logger)
self._add_pac_crd_annotation(remote_region_oam_ip, self.logger)
self.logger.info(" Network setup:" + network_setup)
if network_setup == 'true':
self.create_host_network(remote_region_oam_ip, self.logger)
def _add_pac_crd_annotation(self, remote_region_oam_ip, logger):
logger.info("Inside _add_pac_crd_annotation")
host_username, host_password, ssh_prefix = get_host_connection_details(remote_region_oam_ip)
cmd = " kubectl annotate --kubeconfig=/etc/kubernetes/admin.conf --overwrite crd network-attachment-definitions.k8s.cni.cncf.io "
cmd = cmd + " resource/annotation-relationship=\"on:Pod,key:k8s.v1.cni.cncf.io/networks,value:[{name:INSTANCE.metadata.name}]\""
cmd = ssh_prefix + cmd
logger.info("Annotation cmd:" + cmd)
cmds_annotate = []
cmds_annotate.append(cmd)
run_commands(cmds_annotate, host_password, logger, block=True)
def _create_host_network1(self, remote_region_oam_ip, logger):
logger.info("Inside _create_host_network")
#host_username = settings.HOST_CREDS['username']
#host_password = settings.HOST_CREDS['password']
#ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t ' + host_username + '@' + remote_region_oam_ip
host_username, host_password, ssh_prefix = get_host_connection_details(remote_region_oam_ip)
cmds = []
cmds.append(ssh_prefix + " kubectl get nodes controller-0 --kubeconfig=/etc/kubernetes/admin.conf -o json")
output_lines = self._run_command_get_all_lines(cmds, host_password, logger, block=True)
logger.info("Returned output lines:")
logger.info(output_lines)
new_op_lines = ""
for line in output_lines.split("\n"):
if not 'Connection to' in line:
new_op_lines = new_op_lines + line + "\n"
allocatable_found = False
capacity_found = False
hugepg1G = "hugepages-1Gi"
hugepg2M = "hugepages-2Mi"
logger.info("New o/p lines:" + new_op_lines)
if len(new_op_lines) > 0:
json_op = json.loads(new_op_lines)
logger.info("JSON O/P:" + str(json_op))
status = json_op["status"]
logger.info("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n")
logger.info("Status:" + str(status))
addresses = status["addresses"]
logger.info("###############################\n")
logger.info("Addresses:" + str(addresses))
allocatable = status["allocatable"]
logger.info("********************************\n")
logger.info("Allocatable:" + str(allocatable))
if hugepg1G in allocatable and hugepg2M in allocatable:
logger.info("Allocatable found..")
allocatable_found = True
capacity = status["capacity"]
if hugepg1G in capacity and hugepg2M in capacity:
logger.info("Capacity found..")
capacity_found = True
result = allocatable_found and capacity_found
logger.info("Create host network result:" + str(result))
return result
def _verify_host_network(self, ssh_prefix, basecmd, host_password, logger):
cmds = ["system host-unlock controller-0"]
while True:
unlock_wait = False
for cmd in cmds:
cmd_to_run = ssh_prefix + " " + basecmd + " " + cmd
logger.info(cmd_to_run)
output_lines = self._run_command_get_all_lines([cmd_to_run], host_password, logger, block=True)
logger.info("Returned output lines:")
logger.info(output_lines)
if len(output_lines) > 0:
for line in output_lines.split("\n"):
logger.info("Line:" + line)
if not unlock_wait:
if 'retry host-unlock' in line or 'Rejected' in line:
logger.info("Need to wait to call host-unlock ##### ")
unlock_wait = True
break
if unlock_wait:
time.sleep(60)
else:
break
cmds = ["system host-show controller-0"]
system_available = False
while True:
for cmd in cmds:
cmd_to_run = ssh_prefix + " " + basecmd + " " + cmd
logger.info(cmd_to_run)
output_lines = self._run_command_get_all_lines([cmd_to_run], host_password, logger, block=True)
logger.info("Returned output lines:")
logger.info(output_lines)
if len(output_lines) > 0:
for line in output_lines.split("\n"):
logger.info("Line:" + line)
if 'available' in line:
logger.info("Found available #######")
system_available = True
break
if not system_available:
time.sleep(5)
else:
break
cmds = []
cmds.append(ssh_prefix + " kubectl get nodes controller-0 --kubeconfig=/etc/kubernetes/admin.conf -o json")
output_lines = self._run_command_get_all_lines(cmds, host_password, logger, block=True)
logger.info("Returned output lines:")
logger.info(output_lines)
new_op_lines = ""
for line in output_lines.split("\n"):
if not 'Connection to' in line:
new_op_lines = new_op_lines + line + "\n"
allocatable_found = False
capacity_found = False
f1c = 'intel.com/pci_sriov_net_f1c'
f1u = 'intel.com/pci_sriov_net_f1u'
fh0 = 'intel.com/pci_sriov_net_fh0'
fh0m = 'intel.com/pci_sriov_net_fh0m'
fh1 = 'intel.com/pci_sriov_net_fh1'
logger.info("New o/p lines:" + new_op_lines)
if len(new_op_lines) > 0:
json_op = json.loads(new_op_lines)
logger.info("JSON O/P:" + str(json_op))
status = json_op["status"]
logger.info("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n")
logger.info("Status:" + str(status))
addresses = status["addresses"]
logger.info("###############################\n")
logger.info("Addresses:" + str(addresses))
allocatable = status["allocatable"]
logger.info("********************************\n")
logger.info("Allocatable:" + str(allocatable))
if f1c in allocatable and f1u in allocatable and fh0 in allocatable and fh0m in allocatable and fh1 in allocatable:
logger.info("Allocatable found..")
allocatable_found = True
capacity = status["capacity"]
if f1c in capacity and f1u in capacity and fh0 in capacity and fh0m in capacity and fh1 in capacity:
logger.info("Capacity found..")
capacity_found = True
result = allocatable_found and capacity_found
logger.info("Create host network result:" + str(result))
return result
def _create_serviceaccount(self, namespace, remote_region_oam_ip, logger):
logger.info("Inside _create_serviceaccount")
#host_username = settings.HOST_CREDS['username']
#host_password = settings.HOST_CREDS['password']
#ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t ' + host_username + '@' + remote_region_oam_ip
host_username, host_password, ssh_prefix = get_host_connection_details(remote_region_oam_ip)
saName = 'vran-serviceaccount'
cmds_sa = []
cmds_sa.append(ssh_prefix + ' kubectl create serviceaccount --kubeconfig=/etc/kubernetes/admin.conf ' + saName + ' -n ' + namespace)
run_commands(cmds_sa, host_password, logger, block=True)
def _create_docker_reg_secret(self, namespace, remote_region_oam_ip, logger):
logger.info("Creating Docker registry secret in Namespace:" + namespace)
#host_username = settings.HOST_CREDS['username']
#host_password = settings.HOST_CREDS['password']
#ssh_prefix = 'ssh -o StrictHostKeyChecking=no -t ' + host_username + '@' + remote_region_oam_ip
host_username, host_password, ssh_prefix = get_host_connection_details(remote_region_oam_ip)
dr_username, dr_password = get_docker_reg_connection_details()
#dr_username = settings.CENTRAL_DR_CREDS['username']
#dr_password = settings.CENTRAL_DR_CREDS['password']
secret_name = 'admin-registry-secret'
cmd = ssh_prefix + " kubectl create secret --kubeconfig=/etc/kubernetes/admin.conf docker-registry " + secret_name
cmd = cmd + " --docker-server=registry.local:9001 --docker-username=" + dr_username + " --docker-password=" + dr_password
cmd = cmd + " -n " + namespace
cmds = []
cmds.append(cmd)
run_commands(cmds, host_password, logger, block=True)
|