1 # ============LICENSE_START=======================================================
2 # Copyright (C) 2023 Nordix Foundation.
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
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 implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=========================================================
19 # @author Waqas Ikram (waqas.ikram@est.tech)
21 from robot.api import logger
22 from kubernetes import client, config
23 from robot.api.deco import library
26 @library(scope="GLOBAL", auto_keywords=True)
27 class KubernetesClient:
29 self.api_client = None
31 def create_api_client(self, config_file):
32 logger.console("Initializing api client ..")
33 self.api_client = config.new_client_from_config(config_file)
35 def get_number_of_stateful_set_in_namespace(self, namespace="default", label_selector=""):
36 self.check_if_api_client_is_initialized()
37 api_client = client.AppsV1Api(api_client=self.api_client)
38 result = api_client.list_namespaced_stateful_set(namespace, watch=False, label_selector=label_selector)
39 if result.items is None:
41 return len(result.items)
43 def get_number_of_services_in_namespace(self, namespace="default", label_selector=""):
44 self.check_if_api_client_is_initialized()
45 api_client = client.CoreV1Api(api_client=self.api_client)
46 result = api_client.list_namespaced_service(namespace, watch=False, label_selector=label_selector)
47 if result.items is None:
49 return len(result.items)
51 def get_number_of_deployments_in_namespace(self, namespace="default", label_selector=""):
52 self.check_if_api_client_is_initialized()
53 api_client = client.AppsV1Api(api_client=self.api_client)
54 result = api_client.list_namespaced_deployment(namespace, watch=False, label_selector=label_selector)
55 if result.items is None:
57 return len(result.items)
59 def check_if_api_client_is_initialized(self):
60 if self.api_client is None:
61 raise TypeError("'api_client' is null")