1 # Copyright 2020 Canonical Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from kubernetes
import client
, config
16 from kubernetes
.client
.rest
import ApiException
21 def __init__(self
, config_file
=None):
22 config
.load_kube_config(config_file
=config_file
)
23 self
.logger
= logging
.getLogger("Kubectl")
25 def get_services(self
, field_selector
=None, label_selector
=None):
28 kwargs
["field_selector"] = field_selector
30 kwargs
["label_selector"] = label_selector
33 v1
= client
.CoreV1Api()
34 result
= v1
.list_service_for_all_namespaces(**kwargs
)
37 "name": i
.metadata
.name
,
38 "cluster_ip": i
.spec
.cluster_ip
,
43 "node_port": p
.node_port
,
45 "protocol": p
.protocol
,
46 "target_port": p
.target_port
,
52 "external_ip": [i
.ip
for i
in i
.status
.load_balancer
.ingress
]
53 if i
.status
.load_balancer
.ingress
58 except ApiException
as e
:
59 self
.logger
.error("Error calling get services: {}".format(e
))