2 # Copyright 2020 Telefonica Investigacion y Desarrollo, S.A.U.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
25 from grpclib
.client
import Channel
27 from osm_lcm
.frontend_pb2
import PrimitiveRequest
28 from osm_lcm
.frontend_pb2
import SshKeyRequest
, SshKeyReply
29 from osm_lcm
.frontend_grpc
import FrontendExecutorStub
30 from osm_lcm
.lcm_utils
import LcmBase
32 from osm_lcm
.data_utils
.database
.database
import Database
33 from osm_lcm
.data_utils
.filesystem
.filesystem
import Filesystem
35 from n2vc
.n2vc_conn
import N2VCConnector
36 from n2vc
.k8s_helm_conn
import K8sHelmConnector
37 from n2vc
.k8s_helm3_conn
import K8sHelm3Connector
38 from n2vc
.exceptions
import N2VCBadArgumentsException
, N2VCException
, N2VCExecutionException
40 from osm_lcm
.lcm_utils
import deep_get
43 def retryer(max_wait_time_var
="_initial_retry_time", delay_time_var
="_retry_delay"):
46 ConnectionRefusedError
49 @functools.wraps(func
)
50 async def wrapped(*args
, **kwargs
):
51 # default values for wait time and delay_time
55 # obtain arguments from variable names
57 if self
.__dict
__.get(max_wait_time_var
):
58 max_wait_time
= self
.__dict
__.get(max_wait_time_var
)
59 if self
.__dict
__.get(delay_time_var
):
60 delay_time
= self
.__dict
__.get(delay_time_var
)
62 wait_time
= max_wait_time
65 return await func(*args
, **kwargs
)
66 except retry_exceptions
:
67 wait_time
= wait_time
- delay_time
68 await asyncio
.sleep(delay_time
)
71 return ConnectionRefusedError
76 class LCMHelmConn(N2VCConnector
, LcmBase
):
77 _KUBECTL_OSM_NAMESPACE
= "osm"
78 _KUBECTL_OSM_CLUSTER_NAME
= "_system-osm-k8s"
79 _EE_SERVICE_PORT
= 50050
81 # Initial max retry time
82 _MAX_INITIAL_RETRY_TIME
= 600
83 # Max retry time for normal operations
85 # Time beetween retries, retry time after a connection error is raised
93 vca_config
: dict = None,
96 Initialize EE helm connector.
99 self
.db
= Database().instance
.db
100 self
.fs
= Filesystem().instance
.fs
102 # parent class constructor
103 N2VCConnector
.__init
__(
109 vca_config
=vca_config
,
110 on_update_db
=on_update_db
,
115 self
.log
.debug("Initialize helm N2VC connector")
116 self
.log
.debug("initial vca_config: {}".format(vca_config
))
118 # TODO - Obtain data from configuration
119 self
._ee
_service
_port
= self
._EE
_SERVICE
_PORT
121 self
._retry
_delay
= self
._EE
_RETRY
_DELAY
123 if self
.vca_config
and self
.vca_config
.get("eegrpcinittimeout"):
124 self
._initial
_retry
_time
= self
.vca_config
.get("eegrpcinittimeout")
125 self
.log
.debug("Initial retry time: {}".format(self
._initial
_retry
_time
))
127 self
._initial
_retry
_time
= self
._MAX
_INITIAL
_RETRY
_TIME
128 self
.log
.debug("Applied default retry time: {}".format(self
._initial
_retry
_time
))
130 if self
.vca_config
and self
.vca_config
.get("eegrpctimeout"):
131 self
._max
_retry
_time
= self
.vca_config
.get("eegrpctimeout")
132 self
.log
.debug("Retry time: {}".format(self
._max
_retry
_time
))
134 self
._max
_retry
_time
= self
._MAX
_RETRY
_TIME
135 self
.log
.debug("Applied default retry time: {}".format(self
._max
_retry
_time
))
137 # initialize helm connector for helmv2 and helmv3
138 self
._k
8sclusterhelm
2 = K8sHelmConnector(
139 kubectl_command
=self
.vca_config
.get("kubectlpath"),
140 helm_command
=self
.vca_config
.get("helmpath"),
147 self
._k
8sclusterhelm
3 = K8sHelm3Connector(
148 kubectl_command
=self
.vca_config
.get("kubectlpath"),
149 helm_command
=self
.vca_config
.get("helm3path"),
156 self
._system
_cluster
_id
= None
157 self
.log
.info("Helm N2VC connector initialized")
159 # TODO - ¿reuse_ee_id?
160 async def create_execution_environment(self
,
163 reuse_ee_id
: str = None,
164 progress_timeout
: float = None,
165 total_timeout
: float = None,
167 artifact_path
: str = None,
168 vca_type
: str = None,
169 *kargs
, **kwargs
) -> (str, dict):
171 Creates a new helm execution environment deploying the helm-chat indicated in the
173 :param str namespace: This param is not used, all helm charts are deployed in the osm
175 :param dict db_dict: where to write to database when the status changes.
176 It contains a dictionary with {collection: str, filter: {}, path: str},
177 e.g. {collection: "nsrs", filter: {_id: <nsd-id>, path:
178 "_admin.deployed.VCA.3"}
179 :param str reuse_ee_id: ee id from an older execution. TODO - right now this params is not used
180 :param float progress_timeout:
181 :param float total_timeout:
182 :param dict config: General variables to instantiate KDU
183 :param str artifact_path: path of package content
184 :param str vca_type: Type of vca, must be type helm or helm-v3
185 :returns str, dict: id of the new execution environment including namespace.helm_id
186 and credentials object set to None as all credentials should be osm kubernetes .kubeconfig
190 "create_execution_environment: namespace: {}, artifact_path: {}, db_dict: {}, "
191 "reuse_ee_id: {}".format(
192 namespace
, artifact_path
, db_dict
, reuse_ee_id
)
195 # Validate artifact-path is provided
196 if artifact_path
is None or len(artifact_path
) == 0:
197 raise N2VCBadArgumentsException(
198 message
="artifact_path is mandatory", bad_args
=["artifact_path"]
201 # Validate artifact-path exists and sync path
202 from_path
= os
.path
.split(artifact_path
)[0]
203 self
.fs
.sync(from_path
)
205 # remove / in charm path
206 while artifact_path
.find("//") >= 0:
207 artifact_path
= artifact_path
.replace("//", "/")
210 if self
.fs
.file_exists(artifact_path
):
211 helm_chart_path
= artifact_path
213 msg
= "artifact path does not exist: {}".format(artifact_path
)
214 raise N2VCBadArgumentsException(message
=msg
, bad_args
=["artifact_path"])
216 if artifact_path
.startswith("/"):
217 full_path
= self
.fs
.path
+ helm_chart_path
219 full_path
= self
.fs
.path
+ "/" + helm_chart_path
221 while full_path
.find("//") >= 0:
222 full_path
= full_path
.replace("//", "/")
225 # Call helm conn install
226 # Obtain system cluster id from database
227 system_cluster_uuid
= await self
._get
_system
_cluster
_id
()
228 # Add parameter osm if exist to global
229 if config
and config
.get("osm"):
230 if not config
.get("global"):
231 config
["global"] = {}
232 config
["global"]["osm"] = config
.get("osm")
234 self
.log
.debug("install helm chart: {}".format(full_path
))
235 if vca_type
== "helm":
236 helm_id
= await self
._k
8sclusterhelm
2.install(system_cluster_uuid
, kdu_model
=full_path
,
237 namespace
=self
._KUBECTL
_OSM
_NAMESPACE
,
240 timeout
=progress_timeout
)
242 helm_id
= await self
._k
8sclusterhelm
3.install(system_cluster_uuid
, kdu_model
=full_path
,
243 namespace
=self
._KUBECTL
_OSM
_NAMESPACE
,
246 timeout
=progress_timeout
)
248 ee_id
= "{}:{}.{}".format(vca_type
, self
._KUBECTL
_OSM
_NAMESPACE
, helm_id
)
250 except N2VCException
:
252 except Exception as e
:
253 self
.log
.error("Error deploying chart ee: {}".format(e
), exc_info
=True)
254 raise N2VCException("Error deploying chart ee: {}".format(e
))
256 async def register_execution_environment(self
, namespace
: str, credentials
: dict, db_dict
: dict,
257 progress_timeout
: float = None, total_timeout
: float = None,
258 *kargs
, **kwargs
) -> str:
262 async def install_configuration_sw(self
,
266 progress_timeout
: float = None,
267 total_timeout
: float = None,
275 async def add_relation(self
, ee_id_1
: str, ee_id_2
: str, endpoint_1
: str, endpoint_2
: str):
279 async def remove_relation(self
):
283 async def get_status(self
, namespace
: str, yaml_format
: bool = True):
284 # not used for this connector
287 async def get_ee_ssh_public__key(self
, ee_id
: str, db_dict
: dict, progress_timeout
: float = None,
288 total_timeout
: float = None) -> str:
290 Obtains ssh-public key from ee executing GetSShKey method from the ee.
292 :param str ee_id: the id of the execution environment returned by
293 create_execution_environment or register_execution_environment
295 :param float progress_timeout:
296 :param float total_timeout:
297 :returns: public key of the execution environment
301 "get_ee_ssh_public_key: ee_id: {}, db_dict: {}".format(
306 if ee_id
is None or len(ee_id
) == 0:
307 raise N2VCBadArgumentsException(
308 message
="ee_id is mandatory", bad_args
=["ee_id"]
312 # Obtain ip_addr for the ee service, it is resolved by dns from the ee name by kubernetes
313 version
, namespace
, helm_id
= self
._get
_ee
_id
_parts
(ee_id
)
314 ip_addr
= socket
.gethostbyname(helm_id
)
316 # Obtain ssh_key from the ee, this method will implement retries to allow the ee
317 # install libraries and start successfully
318 ssh_key
= await self
._get
_ssh
_key
(ip_addr
)
320 except Exception as e
:
321 self
.log
.error("Error obtaining ee ssh_key: {}".format(e
), exc_info
=True)
322 raise N2VCException("Error obtaining ee ssh_ke: {}".format(e
))
324 async def exec_primitive(self
, ee_id
: str, primitive_name
: str, params_dict
: dict, db_dict
: dict = None,
325 progress_timeout
: float = None, total_timeout
: float = None) -> str:
327 Execute a primitive in the execution environment
329 :param str ee_id: the one returned by create_execution_environment or
330 register_execution_environment with the format namespace.helm_id
331 :param str primitive_name: must be one defined in the software. There is one
332 called 'config', where, for the proxy case, the 'credentials' of VM are
334 :param dict params_dict: parameters of the action
335 :param dict db_dict: where to write into database when the status changes.
336 It contains a dict with
337 {collection: <str>, filter: {}, path: <str>},
338 e.g. {collection: "nslcmops", filter:
339 {_id: <nslcmop_id>, path: "_admin.VCA"}
340 It will be used to store information about intermediate notifications
341 :param float progress_timeout:
342 :param float total_timeout:
343 :returns str: primitive result, if ok. It raises exceptions in case of fail
346 self
.log
.info("exec primitive for ee_id : {}, primitive_name: {}, params_dict: {}, db_dict: {}".format(
347 ee_id
, primitive_name
, params_dict
, db_dict
351 if ee_id
is None or len(ee_id
) == 0:
352 raise N2VCBadArgumentsException(
353 message
="ee_id is mandatory", bad_args
=["ee_id"]
355 if primitive_name
is None or len(primitive_name
) == 0:
356 raise N2VCBadArgumentsException(
357 message
="action_name is mandatory", bad_args
=["action_name"]
359 if params_dict
is None:
363 version
, namespace
, helm_id
= self
._get
_ee
_id
_parts
(ee_id
)
364 ip_addr
= socket
.gethostbyname(helm_id
)
365 except Exception as e
:
366 self
.log
.error("Error getting ee ip ee: {}".format(e
))
367 raise N2VCException("Error getting ee ip ee: {}".format(e
))
369 if primitive_name
== "config":
371 # Execute config primitive, higher timeout to check the case ee is starting
372 status
, detailed_message
= await self
._execute
_config
_primitive
(ip_addr
, params_dict
, db_dict
=db_dict
)
373 self
.log
.debug("Executed config primitive ee_id_ {}, status: {}, message: {}".format(
374 ee_id
, status
, detailed_message
))
376 self
.log
.error("Error configuring helm ee, status: {}, message: {}".format(
377 status
, detailed_message
))
378 raise N2VCExecutionException(
379 message
="Error configuring helm ee_id: {}, status: {}, message: {}: ".format(
380 ee_id
, status
, detailed_message
382 primitive_name
=primitive_name
,
384 except Exception as e
:
385 self
.log
.error("Error configuring helm ee: {}".format(e
))
386 raise N2VCExecutionException(
387 message
="Error configuring helm ee_id: {}, {}".format(
390 primitive_name
=primitive_name
,
396 status
, detailed_message
= await self
._execute
_primitive
(ip_addr
, primitive_name
,
397 params_dict
, db_dict
=db_dict
)
398 self
.log
.debug("Executed primitive {} ee_id_ {}, status: {}, message: {}".format(
399 primitive_name
, ee_id
, status
, detailed_message
))
400 if status
!= "OK" and status
!= "PROCESSING":
402 "Execute primitive {} returned not ok status: {}, message: {}".format(
403 primitive_name
, status
, detailed_message
)
405 raise N2VCExecutionException(
406 message
="Execute primitive {} returned not ok status: {}, message: {}".format(
407 primitive_name
, status
, detailed_message
409 primitive_name
=primitive_name
,
411 except Exception as e
:
413 "Error executing primitive {}: {}".format(primitive_name
, e
)
415 raise N2VCExecutionException(
416 message
="Error executing primitive {} into ee={} : {}".format(
417 primitive_name
, ee_id
, e
419 primitive_name
=primitive_name
,
421 return detailed_message
423 async def deregister_execution_environments(self
):
427 async def delete_execution_environment(self
, ee_id
: str, db_dict
: dict = None, total_timeout
: float = None):
429 Delete an execution environment
430 :param str ee_id: id of the execution environment to delete, included namespace.helm_id
431 :param dict db_dict: where to write into database when the status changes.
432 It contains a dict with
433 {collection: <str>, filter: {}, path: <str>},
434 e.g. {collection: "nsrs", filter:
435 {_id: <nsd-id>, path: "_admin.deployed.VCA.3"}
436 :param float total_timeout:
439 self
.log
.info("ee_id: {}".format(ee_id
))
443 raise N2VCBadArgumentsException(
444 message
="ee_id is mandatory", bad_args
=["ee_id"]
449 # Obtain cluster_uuid
450 system_cluster_uuid
= await self
._get
_system
_cluster
_id
()
453 version
, namespace
, helm_id
= self
._get
_ee
_id
_parts
(ee_id
)
455 # Uninstall chart, for backward compatibility we must assume that if there is no
456 # version it is helm-v2
457 if version
== "helm-v3":
458 await self
._k
8sclusterhelm
3.uninstall(system_cluster_uuid
, helm_id
)
460 await self
._k
8sclusterhelm
2.uninstall(system_cluster_uuid
, helm_id
)
461 self
.log
.info("ee_id: {} deleted".format(ee_id
))
462 except N2VCException
:
464 except Exception as e
:
465 self
.log
.error("Error deleting ee id: {}: {}".format(ee_id
, e
), exc_info
=True)
466 raise N2VCException("Error deleting ee id {}: {}".format(ee_id
, e
))
468 async def delete_namespace(self
, namespace
: str, db_dict
: dict = None, total_timeout
: float = None):
469 # method not implemented for this connector, execution environments must be deleted individually
472 async def install_k8s_proxy_charm(
478 progress_timeout
: float = None,
479 total_timeout
: float = None,
485 @retryer(max_wait_time_var
="_initial_retry_time", delay_time_var
="_retry_delay")
486 async def _get_ssh_key(self
, ip_addr
):
487 channel
= Channel(ip_addr
, self
._ee
_service
_port
)
489 stub
= FrontendExecutorStub(channel
)
490 self
.log
.debug("get ssh key, ip_addr: {}".format(ip_addr
))
491 reply
: SshKeyReply
= await stub
.GetSshKey(SshKeyRequest())
496 @retryer(max_wait_time_var
="_initial_retry_time", delay_time_var
="_retry_delay")
497 async def _execute_config_primitive(self
, ip_addr
, params
, db_dict
=None):
498 return await self
._execute
_primitive
_internal
(ip_addr
, "config", params
, db_dict
=db_dict
)
500 @retryer(max_wait_time_var
="_max_retry_time", delay_time_var
="_retry_delay")
501 async def _execute_primitive(self
, ip_addr
, primitive_name
, params
, db_dict
=None):
502 return await self
._execute
_primitive
_internal
(ip_addr
, primitive_name
, params
, db_dict
=db_dict
)
504 async def _execute_primitive_internal(self
, ip_addr
, primitive_name
, params
, db_dict
=None):
506 channel
= Channel(ip_addr
, self
._ee
_service
_port
)
508 stub
= FrontendExecutorStub(channel
)
509 async with stub
.RunPrimitive
.open() as stream
:
510 primitive_id
= str(uuid
.uuid1())
512 self
.log
.debug("Execute primitive internal: id:{}, name:{}, params: {}".
513 format(primitive_id
, primitive_name
, params
))
514 await stream
.send_message(
515 PrimitiveRequest(id=primitive_id
, name
=primitive_name
, params
=yaml
.dump(params
)), end
=True)
516 async for reply
in stream
:
517 self
.log
.debug("Received reply: {}".format(reply
))
519 # If db_dict provided write notifs in database
521 self
._write
_op
_detailed
_status
(db_dict
, reply
.status
, reply
.detailed_message
)
523 return reply
.status
, reply
.detailed_message
525 return "ERROR", "No result received"
529 def _write_op_detailed_status(self
, db_dict
, status
, detailed_message
):
531 # write ee_id to database: _admin.deployed.VCA.x
533 the_table
= db_dict
["collection"]
534 the_filter
= db_dict
["filter"]
535 update_dict
= {"detailed-status": "{}: {}".format(status
, detailed_message
)}
536 # self.log.debug('Writing ee_id to database: {}'.format(the_path))
540 update_dict
=update_dict
,
543 except asyncio
.CancelledError
:
545 except Exception as e
:
546 self
.log
.error("Error writing detailedStatus to database: {}".format(e
))
548 async def _get_system_cluster_id(self
):
549 if not self
._system
_cluster
_id
:
550 db_k8cluster
= self
.db
.get_one("k8sclusters", {"name": self
._KUBECTL
_OSM
_CLUSTER
_NAME
})
551 k8s_hc_id
= deep_get(db_k8cluster
, ("_admin", "helm-chart-v3", "id"))
554 # backward compatibility for existing clusters that have not been initialized for helm v3
555 cluster_id
= db_k8cluster
.get("_id")
556 k8s_credentials
= yaml
.safe_dump(db_k8cluster
.get("credentials"))
557 k8s_hc_id
, uninstall_sw
= await self
._k
8sclusterhelm
3.init_env(k8s_credentials
,
558 reuse_cluster_uuid
=cluster_id
)
559 db_k8scluster_update
= {"_admin.helm-chart-v3.error_msg": None,
560 "_admin.helm-chart-v3.id": k8s_hc_id
,
561 "_admin.helm-chart-v3}.created": uninstall_sw
,
562 "_admin.helm-chart-v3.operationalState": "ENABLED"}
563 self
.update_db_2("k8sclusters", cluster_id
, db_k8scluster_update
)
564 except Exception as e
:
565 self
.log
.error("error initializing helm-v3 cluster: {}".format(str(e
)))
566 raise N2VCException("K8s system cluster '{}' has not been initialized for helm-chart-v3".format(
568 self
._system
_cluster
_id
= k8s_hc_id
569 return self
._system
_cluster
_id
571 def _get_ee_id_parts(self
, ee_id
):
573 Parses ee_id stored at database that can be either 'version:namespace.helm_id' or only
574 namespace.helm_id for backward compatibility
575 If exists helm version can be helm-v3 or helm (helm-v2 old version)
577 version
, _
, part_id
= ee_id
.rpartition(':')
578 namespace
, _
, helm_id
= part_id
.rpartition('.')
579 return version
, namespace
, helm_id