| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 3 | # |
| 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 |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 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 |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | ####################################################################################### |
| 17 | |
| 18 | |
| 19 | import base64 |
| 20 | |
| 21 | |
| 22 | async def create_secret(self, secret_name, secret_namespace, secret_key, secret_value): |
| 23 | async def check_secret(secret_name, secret_namespace, secret_key, secret_value): |
| 24 | self.logger.info(f"Checking content of secret {secret_name} ...") |
| 25 | returned_secret_data = await self._kubectl.get_secret_content( |
| 26 | name=secret_name, |
| 27 | namespace=secret_namespace, |
| 28 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 29 | returned_secret_value = base64.b64decode( |
| 30 | returned_secret_data[secret_key] |
| 31 | ).decode("utf-8") |
| garciadeblas | ee34b49 | 2025-03-05 17:06:49 +0100 | [diff] [blame] | 32 | # self.logger.debug(f"secret_data_original={secret_value}") |
| 33 | # self.logger.debug(f"secret_data_received={returned_secret_value}") |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 34 | self.logger.info( |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 35 | f"Result of secret comparison: {secret_value==returned_secret_value}" |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | self.logger.info( |
| 39 | f"Creating secret {secret_name} in namespace {secret_namespace} ..." |
| 40 | ) |
| 41 | secret_data = {secret_key: base64.b64encode(secret_value.encode()).decode("utf-8")} |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 42 | self.logger.info( |
| 43 | f"Calling N2VC kubectl to create secret. Namespace: {secret_namespace}. Secret name: {secret_name}. Secret data:{secret_data}." |
| 44 | ) |
| garciadeblas | 96b94f5 | 2024-07-08 16:18:21 +0200 | [diff] [blame] | 45 | await self._kubectl.create_secret( |
| 46 | name=secret_name, |
| 47 | data=secret_data, |
| 48 | namespace=secret_namespace, |
| 49 | secret_type="Opaque", |
| 50 | ) |
| 51 | self.logger.info(f"Secret {secret_name} CREATED") |
| 52 | |
| 53 | await check_secret(secret_name, secret_namespace, secret_key, secret_value) |
| garciadeblas | 28bff0f | 2024-09-16 12:53:07 +0200 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def delete_secret(self, secret_name, secret_namespace): |
| 57 | try: |
| 58 | self._kubectl.delete_secret(name=secret_name, namespace=secret_namespace) |
| 59 | self.logger.info( |
| 60 | f"Deleted secret {secret_name} in namespace {secret_namespace}" |
| 61 | ) |
| 62 | except Exception as e: |
| 63 | self.logger.error( |
| 64 | f"Could not delete secret {secret_name} in namespace {secret_namespace}: {e}" |
| 65 | ) |
| rshri | f8911b9 | 2025-06-11 18:19:07 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | async def create_configmap(self, configmap_name, configmap_namespace, data): |
| 69 | self.logger.info(f"Checking content of configmap {data} ...") |
| 70 | self.logger.info( |
| 71 | f"Calling N2VC kubectl to create configmap. Namespace: {configmap_namespace}. configmap name: {configmap_name}. data:{data}." |
| 72 | ) |
| 73 | await self._kubectl.create_configmap( |
| 74 | name=configmap_name, |
| 75 | data=data, |
| 76 | namespace=configmap_namespace, |
| 77 | ) |
| 78 | self.logger.info(f"configmap {configmap_name} CREATED") |
| 79 | |
| 80 | |
| 81 | def delete_configmap(self, configmap_name, configmap_namespace): |
| 82 | try: |
| 83 | self._kubectl.delete_configmap( |
| 84 | name=configmap_name, namespace=configmap_namespace |
| 85 | ) |
| 86 | self.logger.info( |
| 87 | f"Deleted configmap {configmap_name} in namespace {configmap_namespace}" |
| 88 | ) |
| 89 | except Exception as e: |
| 90 | self.logger.error( |
| 91 | f"Could not delete configmap {configmap_name} in namespace {configmap_namespace}: {e}" |
| 92 | ) |