| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 1 | ## |
| 2 | # Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U. |
| 3 | # This file is part of OSM |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 15 | # implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | # For those usages not covered by the Apache License, Version 2.0 please |
| 20 | # contact with: nfvlabs@tid.es |
| 21 | ## |
| 22 | |
| 23 | import asyncio |
| 24 | from n2vc.loggable import Loggable |
| 25 | import abc |
| 26 | import time |
| 27 | |
| 28 | |
| 29 | class K8sConnector(abc.ABC, Loggable): |
| 30 | |
| 31 | """ |
| 32 | ################################################################################################## |
| 33 | ########################################## P U B L I C ########################################### |
| 34 | ################################################################################################## |
| 35 | """ |
| 36 | |
| 37 | def __init__( |
| 38 | self, |
| 39 | db: object, |
| 40 | log: object = None, |
| 41 | on_update_db=None |
| 42 | ): |
| 43 | """ |
| 44 | |
| 45 | :param db: database object to write current operation status |
| 46 | :param log: logger for tracing |
| 47 | :param on_update_db: callback called when k8s connector updates database |
| 48 | """ |
| 49 | |
| 50 | # parent class |
| 51 | Loggable.__init__(self, log=log, log_to_console=True, prefix='\nK8S') |
| 52 | |
| tierno | e2bd3da | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 53 | # self.log.info('Initializing generic K8S connector') |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 54 | |
| 55 | # the database and update callback |
| 56 | self.db = db |
| 57 | self.on_update_db = on_update_db |
| 58 | |
| tierno | e2bd3da | 2020-03-26 09:51:11 +0000 | [diff] [blame] | 59 | # self.log.info('K8S generic connector initialized') |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 60 | |
| 61 | @abc.abstractmethod |
| 62 | async def init_env( |
| 63 | self, |
| 64 | k8s_creds: str, |
| 65 | namespace: str = 'kube-system', |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 66 | reuse_cluster_uuid=None |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 67 | ) -> (str, bool): |
| 68 | """ |
| 69 | It prepares a given K8s cluster environment to run Charts or juju Bundles on both sides: |
| 70 | client (OSM) |
| 71 | server (Tiller/Charm) |
| 72 | |
| 73 | :param k8s_creds: credentials to access a given K8s cluster, i.e. a valid '.kube/config' |
| garciadeblas | 2ce889d | 2019-12-13 13:39:03 +0100 | [diff] [blame] | 74 | :param namespace: optional namespace to be used for the K8s engine (helm tiller, juju). |
| 75 | By default, 'kube-system' will be used |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 76 | :param reuse_cluster_uuid: existing cluster uuid for reuse |
| 77 | :return: uuid of the K8s cluster and True if connector has installed some software in the cluster |
| 78 | (on error, an exception will be raised) |
| 79 | """ |
| 80 | |
| 81 | @abc.abstractmethod |
| 82 | async def repo_add( |
| 83 | self, |
| 84 | cluster_uuid: str, |
| 85 | name: str, |
| 86 | url: str, |
| 87 | repo_type: str = 'chart' |
| 88 | ): |
| 89 | """ |
| 90 | Add a new repository to OSM database |
| 91 | |
| 92 | :param cluster_uuid: the cluster |
| 93 | :param name: name for the repo in OSM |
| 94 | :param url: URL of the repo |
| 95 | :param repo_type: either "chart" or "bundle" |
| 96 | :return: True if successful |
| 97 | """ |
| 98 | |
| 99 | @abc.abstractmethod |
| 100 | async def repo_list( |
| 101 | self, |
| 102 | cluster_uuid: str |
| 103 | ): |
| 104 | """ |
| 105 | Get the list of registered repositories |
| 106 | |
| 107 | :param cluster_uuid: the cluster |
| 108 | :return: list of registered repositories: [ (name, url) .... ] |
| 109 | """ |
| 110 | |
| 111 | @abc.abstractmethod |
| 112 | async def repo_remove( |
| 113 | self, |
| 114 | cluster_uuid: str, |
| 115 | name: str |
| 116 | ): |
| 117 | """ |
| 118 | Remove a repository from OSM |
| 119 | |
| 120 | :param name: repo name in OSM |
| 121 | :param cluster_uuid: the cluster |
| 122 | :return: True if successful |
| 123 | """ |
| 124 | |
| 125 | @abc.abstractmethod |
| lloretgalleg | f00dcae | 2020-02-20 12:01:17 +0100 | [diff] [blame] | 126 | async def synchronize_repos( |
| 127 | self, |
| 128 | cluster_uuid: str, |
| 129 | name: str |
| 130 | ): |
| 131 | """ |
| 132 | Synchronizes the list of repositories created in the cluster with |
| 133 | the repositories added by the NBI |
| 134 | |
| 135 | :param cluster_uuid: the cluster |
| 136 | :return: List of repositories deleted from the cluster and dictionary with repos added |
| 137 | """ |
| 138 | |
| 139 | @abc.abstractmethod |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 140 | async def reset( |
| 141 | self, |
| 142 | cluster_uuid: str, |
| 143 | force: bool = False, |
| 144 | uninstall_sw: bool = False |
| 145 | ) -> bool: |
| 146 | """ |
| 147 | Uninstalls Tiller/Charm from a known K8s cluster and removes it from the list of known K8s clusters. |
| 148 | Intended to be used e.g. when the NS instance is deleted. |
| 149 | |
| 150 | :param cluster_uuid: UUID of a K8s cluster known by OSM. |
| 151 | :param force: force deletion, even in case there are deployed releases |
| 152 | :param uninstall_sw: flag to indicate that sw uninstallation from software is needed |
| 153 | :return: str: kdu_instance generated by helm |
| 154 | """ |
| 155 | |
| 156 | @abc.abstractmethod |
| 157 | async def install( |
| 158 | self, |
| 159 | cluster_uuid: str, |
| 160 | kdu_model: str, |
| 161 | atomic: bool = True, |
| 162 | timeout: float = 300, |
| 163 | params: dict = None, |
| Dominik Fleischmann | 12aa084 | 2020-02-04 15:32:42 +0100 | [diff] [blame] | 164 | db_dict: dict = None, |
| tierno | d5d83a4 | 2020-04-07 11:08:16 +0000 | [diff] [blame^] | 165 | kdu_name: str = None, |
| 166 | namespace: str = None |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 167 | ): |
| 168 | """ |
| 169 | Deploys of a new KDU instance. It would implicitly rely on the `install` call to deploy the Chart/Bundle |
| 170 | properly parametrized (in practice, this call would happen before any _initial-config-primitive_ |
| 171 | of the VNF is called). |
| 172 | |
| 173 | :param cluster_uuid: UUID of a K8s cluster known by OSM |
| 174 | :param kdu_model: chart/bundle:version reference (string), which can be either of these options: |
| 175 | - a name of chart/bundle available via the repos known by OSM |
| 176 | - a path to a packaged chart/bundle |
| 177 | - a path to an unpacked chart/bundle directory or a URL |
| 178 | :param atomic: If set, installation process purges chart/bundle on fail, also will wait until |
| 179 | all the K8s objects are active |
| 180 | :param timeout: Time in seconds to wait for the install of the chart/bundle (defaults to |
| 181 | Helm default timeout: 300s) |
| 182 | :param params: dictionary of key-value pairs for instantiation parameters (overriding default values) |
| 183 | :param dict db_dict: where to write into database when the status changes. |
| 184 | It contains a dict with {collection: <str>, filter: {}, path: <str>}, |
| 185 | e.g. {collection: "nsrs", filter: {_id: <nsd-id>, path: "_admin.deployed.K8S.3"} |
| Dominik Fleischmann | 12aa084 | 2020-02-04 15:32:42 +0100 | [diff] [blame] | 186 | :param kdu_name: Name of the KDU instance to be installed |
| tierno | d5d83a4 | 2020-04-07 11:08:16 +0000 | [diff] [blame^] | 187 | :param namespace: K8s namespace to use for the KDU instance |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 188 | :return: True if successful |
| 189 | """ |
| 190 | |
| 191 | @abc.abstractmethod |
| 192 | async def upgrade( |
| 193 | self, |
| 194 | cluster_uuid: str, |
| 195 | kdu_instance: str, |
| 196 | kdu_model: str = None, |
| 197 | atomic: bool = True, |
| 198 | timeout: float = 300, |
| 199 | params: dict = None, |
| 200 | db_dict: dict = None |
| 201 | ): |
| 202 | """ |
| 203 | Upgrades an existing KDU instance. It would implicitly use the `upgrade` call over an existing Chart/Bundle. |
| 204 | It can be used both to upgrade the chart or to reconfigure it. This would be exposed as Day-2 primitive. |
| 205 | |
| 206 | :param cluster_uuid: UUID of a K8s cluster known by OSM |
| 207 | :param kdu_instance: unique name for the KDU instance to be updated |
| 208 | :param kdu_model: new chart/bundle:version reference |
| 209 | :param atomic: rollback in case of fail and wait for pods and services are available |
| 210 | :param timeout: Time in seconds to wait for the install of the chart/bundle (defaults to |
| 211 | Helm default timeout: 300s) |
| 212 | :param params: new dictionary of key-value pairs for instantiation parameters |
| 213 | :param dict db_dict: where to write into database when the status changes. |
| 214 | It contains a dict with {collection: <str>, filter: {}, path: <str>}, |
| 215 | e.g. {collection: "nsrs", filter: {_id: <nsd-id>, path: "_admin.deployed.K8S.3"} |
| 216 | :return: reference to the new revision number of the KDU instance |
| 217 | """ |
| 218 | |
| 219 | @abc.abstractmethod |
| 220 | async def rollback( |
| 221 | self, |
| 222 | cluster_uuid: str, |
| 223 | kdu_instance: str, |
| 224 | revision=0, |
| 225 | db_dict: dict = None |
| 226 | ): |
| 227 | """ |
| 228 | Rolls back a previous update of a KDU instance. It would implicitly use the `rollback` call. |
| 229 | It can be used both to rollback from a Chart/Bundle version update or from a reconfiguration. |
| 230 | This would be exposed as Day-2 primitive. |
| 231 | |
| 232 | :param cluster_uuid: UUID of a K8s cluster known by OSM |
| 233 | :param kdu_instance: unique name for the KDU instance |
| 234 | :param revision: revision to which revert changes. If omitted, it will revert the last update only |
| 235 | :param dict db_dict: where to write into database when the status changes. |
| 236 | It contains a dict with {collection: <str>, filter: {}, path: <str>}, |
| 237 | e.g. {collection: "nsrs", filter: {_id: <nsd-id>, path: "_admin.deployed.K8S.3"} |
| 238 | :return:If successful, reference to the current active revision of the KDU instance after the rollback |
| 239 | """ |
| 240 | |
| 241 | @abc.abstractmethod |
| 242 | async def uninstall( |
| 243 | self, |
| 244 | cluster_uuid: str, |
| 245 | kdu_instance: str |
| 246 | ): |
| 247 | """ |
| 248 | Removes an existing KDU instance. It would implicitly use the `delete` call (this call would happen |
| 249 | after all _terminate-config-primitive_ of the VNF are invoked). |
| 250 | |
| 251 | :param cluster_uuid: UUID of a K8s cluster known by OSM |
| 252 | :param kdu_instance: unique name for the KDU instance to be deleted |
| 253 | :return: True if successful |
| 254 | """ |
| 255 | |
| 256 | @abc.abstractmethod |
| 257 | async def inspect_kdu( |
| 258 | self, |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 259 | kdu_model: str, |
| 260 | repo_url: str = None |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 261 | ) -> str: |
| 262 | """ |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 263 | These calls will retrieve from the Chart/Bundle: |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 264 | |
| 265 | - The list of configurable values and their defaults (e.g. in Charts, it would retrieve |
| 266 | the contents of `values.yaml`). |
| 267 | - If available, any embedded help file (e.g. `readme.md`) embedded in the Chart/Bundle. |
| 268 | |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 269 | :param kdu_model: chart/bundle reference |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 270 | :param repo_url: optional, reposotory URL (None if tar.gz, URl in other cases, even stable URL) |
| 271 | :return: |
| 272 | |
| 273 | If successful, it will return the available parameters and their default values as provided by the backend. |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 274 | """ |
| 275 | |
| 276 | @abc.abstractmethod |
| 277 | async def help_kdu( |
| 278 | self, |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 279 | kdu_model: str, |
| 280 | repo_url: str = None |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 281 | ) -> str: |
| 282 | """ |
| 283 | |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 284 | :param kdu_model: chart/bundle reference |
| quilesj | 1be0630 | 2019-11-29 11:17:11 +0000 | [diff] [blame] | 285 | :param repo_url: optional, reposotory URL (None if tar.gz, URl in other cases, even stable URL) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 286 | :return: If successful, it will return the contents of the 'readme.md' |
| 287 | """ |
| 288 | |
| 289 | @abc.abstractmethod |
| 290 | async def status_kdu( |
| 291 | self, |
| 292 | cluster_uuid: str, |
| 293 | kdu_instance: str |
| 294 | ) -> str: |
| 295 | """ |
| 296 | This call would retrieve tha current state of a given KDU instance. It would be would allow to retrieve |
| 297 | the _composition_ (i.e. K8s objects) and _specific values_ of the configuration parameters applied |
| 298 | to a given instance. This call would be based on the `status` call. |
| 299 | |
| 300 | :param cluster_uuid: UUID of a K8s cluster known by OSM |
| 301 | :param kdu_instance: unique name for the KDU instance |
| 302 | :return: If successful, it will return the following vector of arguments: |
| 303 | - K8s `namespace` in the cluster where the KDU lives |
| 304 | - `state` of the KDU instance. It can be: |
| 305 | - UNKNOWN |
| 306 | - DEPLOYED |
| 307 | - DELETED |
| 308 | - SUPERSEDED |
| 309 | - FAILED or |
| 310 | - DELETING |
| 311 | - List of `resources` (objects) that this release consists of, sorted by kind, and the status of those resources |
| 312 | - Last `deployment_time`. |
| 313 | |
| 314 | """ |
| 315 | |
| 316 | """ |
| 317 | ################################################################################################## |
| 318 | ########################################## P R I V A T E ######################################### |
| 319 | ################################################################################################## |
| 320 | """ |
| 321 | |
| 322 | async def write_app_status_to_db( |
| 323 | self, |
| 324 | db_dict: dict, |
| 325 | status: str, |
| 326 | detailed_status: str, |
| 327 | operation: str |
| 328 | ) -> bool: |
| 329 | |
| 330 | if not self.db: |
| 331 | self.warning('No db => No database write') |
| 332 | return False |
| 333 | |
| 334 | if not db_dict: |
| 335 | self.warning('No db_dict => No database write') |
| 336 | return False |
| 337 | |
| Dominik Fleischmann | bc269eb | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 338 | self.log.debug('status={}'.format(status)) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 339 | |
| 340 | try: |
| 341 | |
| 342 | the_table = db_dict['collection'] |
| 343 | the_filter = db_dict['filter'] |
| 344 | the_path = db_dict['path'] |
| 345 | if not the_path[-1] == '.': |
| 346 | the_path = the_path + '.' |
| 347 | update_dict = { |
| 348 | the_path + 'operation': operation, |
| 349 | the_path + 'status': status, |
| 350 | the_path + 'detailed-status': detailed_status, |
| 351 | the_path + 'status-time': str(time.time()), |
| 352 | } |
| 353 | |
| 354 | self.db.set_one( |
| 355 | table=the_table, |
| 356 | q_filter=the_filter, |
| 357 | update_dict=update_dict, |
| 358 | fail_on_empty=True |
| 359 | ) |
| 360 | |
| 361 | # database callback |
| 362 | if self.on_update_db: |
| 363 | if asyncio.iscoroutinefunction(self.on_update_db): |
| 364 | await self.on_update_db(the_table, the_filter, the_path, update_dict) |
| 365 | else: |
| 366 | self.on_update_db(the_table, the_filter, the_path, update_dict) |
| 367 | |
| 368 | return True |
| 369 | |
| 370 | except Exception as e: |
| Dominik Fleischmann | bc269eb | 2020-02-27 10:04:34 +0100 | [diff] [blame] | 371 | self.log.info('Exception writing status to database: {}'.format(e)) |
| quilesj | a049b7c | 2019-10-28 18:08:00 +0100 | [diff] [blame] | 372 | return False |