aboutsummaryrefslogtreecommitdiffstats
path: root/sdv/docker/sdvsecurity/nfvsec/utils/k8sclient.py
blob: b1eba5a62a3e0b607d94066367797d5d5bf9dff5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Kubernetes cluster api helper functions
"""


import time

from kubernetes import client, config
from kubernetes.client import Configuration
from kubernetes.client.api import core_v1_api
from kubernetes.client.rest import ApiException
from kubernetes.stream import stream

from kubernetes.stream import stream
import logging
from conf import settings    # pylint: disable=import-error


class K8sClient():
    """
    Class for controlling the pod through PAPI
    """

    def __init__(self):
        """
        Initialisation function.
        """
        self._logger = logging.getLogger(__name__)
        config.load_kube_config(settings.getValue('K8S_CONFIG_FILEPATH'))
        self.api = client.CoreV1Api()

    def get_pod(self, namespace, name):
        """
        Returns json details any one pod with matching label

        :param namespace: namespace to use
        :param namespace: name of the pod (Longest possible).
        :return: pod details
        """
        api_response = self.api.list_namespaced_pod(namespace)
        for pod in api_response.items:
            #print(pod.metadata.name)
            if pod.metadata.name.startswith(name):
                return pod
        return None


    def execute(self, pod, cmd):
        """
        Executes `cmd` inside `pod` and returns response
        :param pod: pod object
        :param cmd: command to execute inside pod
        :return: response from pod
        """
        response = stream(self.api.connect_get_namespaced_pod_exec,
                        pod.metadata.name, pod.metadata.namespace, command=cmd,
                        stderr=True, stdin=False, stdout=True, tty=False)
        return response