Make API Proxy optional and avoid replacing existing SSH Keys in the provisioner
[osm/N2VC.git] / n2vc / kubectl.py
1 # Copyright 2020 Canonical Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from kubernetes import client, config
16 from kubernetes.client.rest import ApiException
17 import logging
18
19
20 class Kubectl:
21 def __init__(self, config_file=None):
22 config.load_kube_config(config_file=config_file)
23 self.logger = logging.getLogger("Kubectl")
24
25 def get_services(self, field_selector=None, label_selector=None):
26 kwargs = {}
27 if field_selector:
28 kwargs["field_selector"] = field_selector
29 if label_selector:
30 kwargs["label_selector"] = label_selector
31
32 try:
33 v1 = client.CoreV1Api()
34 result = v1.list_service_for_all_namespaces(**kwargs)
35 return [
36 {
37 "name": i.metadata.name,
38 "cluster_ip": i.spec.cluster_ip,
39 "type": i.spec.type,
40 "ports": i.spec.ports,
41 "external_ip": [i.ip for i in i.status.load_balancer.ingress]
42 if i.status.load_balancer.ingress
43 else None,
44 }
45 for i in result.items
46 ]
47 except ApiException as e:
48 self.logger.error("Error calling get services: {}".format(e))
49 raise e