Ejemplo n.º 1
0
    def _generate_api_endpoint(self, item=None, namespace=None, name=None):
        api_tail = ""
        if item in self.item_v1:
            api_base = API_V1_BASE
        elif item in self.item_v1_beta:
            api_base = API_EXT_BASE
        elif item in self.item_v1_auto_scale:
            api_base = API_AUTO_SCALE_BASE
        else:
            api_base = API_BATCH_BASE
        api_base += "watch/"

        if name:
            if not namespace and item not in KUBE_NO_NAMESPACE_SET:
                namespace = "default"
            api_tail = "/" + name

        if namespace:
            if item in KUBE_NO_NAMESPACE_SET:
                raise AXKubeApiException(
                    "Cannot watch namespaced {}".format(item))
            api_base += "namespaces/{namespace}/".format(namespace=namespace)

        endpoint = api_base + item + api_tail
        return endpoint
Ejemplo n.º 2
0
    def watch(self, item, namespace=None, name=None, **kwargs):
        """
        For API v1, we can watch (and watch namespaced)
        1. configmaps
        2. endpoints
        3. events
        4. limitranges
        5. namespaces (should NOT specify namespace)
        6. nodes (should NOT specify namespace)
        7. persistentvolumeclaims
        8. persistentvolumes (should NOT specify namespace)
        9. pods
        10. podtemplates
        11. replicationcontrollers
        12. resourcequotas
        13. secrets
        14. serviceaccounts
        15. services

        For API extensions, we can watch (and watch namespaced)
        1. daemonsets
        2. deployments
        3. horizontalpodautoscalers
        4. ingresses
        5. jobs
        6. replicasets

        We can watch object by specifying its name. In this case, if namespace is not
        provided explicitly, we will watch the namespace "default"

        We can specify query parameters "labelSelector, timeoutSeconds and fieldSelector". Others are
        either not relevant to us or is not supported by Kubernetes

        Example:
        label = [
           "key1=value1",
           "key2=value2"
        ]
        timeout = 10
        pods = client.watch(item="pods", label_selector=label, timeout_seconds=timeout)

        Only get events about Pods. Note that the value of field_selector is case sensitive.
        events = client.watch(item="events", field_selector="involvedObject.kind=Pod")

        :param item string: one of the items specified above
        :param namespace string: kubernetes namespace
        :param name string: name of the object
        :param kwargs string: label_selector or timeout_seconds
        :return:
        """
        if not self.validate_object(item):
            msg = "Invalid item: {}. Supported items are {}, {}, {} with autoscaling API and {} with batch API".format(
                item, str(self.item_v1), str(self.item_v1_beta),
                str(self.item_v1_auto_scale), str(self.item_v1_batch))
            raise AXKubeApiException(msg)

        endpoint = self._generate_api_endpoint(item, namespace, name)
        return self._call_api(endpoint, **kwargs)
Ejemplo n.º 3
0
 def swagger_exception_handler(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except ApiException as e:
         if e.status == 404:
             raise AXNotFoundException(e.reason, detail=e.body)
         elif e.status == 409:
             raise AXConflictException(e.reason, detail=e.body)
         raise AXKubeApiException(e.reason, detail=e.body)
Ejemplo n.º 4
0
    def _stream_helper(response):
        """
        Generate iterables from kubernetes stream API

        :param response: HTTP get response
        :return: iterables with json objects
        """
        try:
            response.raise_for_status()
        except requests.exceptions.HTTPError as e:
            raise AXKubeApiException(str(e))

        # this assumes that there is no newline character
        # within one json object. Note Kubernetes does retusn
        # json that contains "\" and "n", but thats two characters
        for line in response.iter_lines(chunk_size=None):
            try:
                if py3env:
                    yield json.loads(line.decode("utf-8"))
                else:
                    yield json.loads(line)
            except Exception as e:
                msg = str(e) + " Content: " + line
                raise AXKubeApiException(msg)
Ejemplo n.º 5
0
    def _generate_api_params(self, **kwargs):
        data = {}
        valid_params = frozenset(
            ["label_selector", "timeout_seconds", "field_selector"])

        # Try to keep argument name same as that of other swagger
        # generated apis
        swagger_to_kube = {
            "label_selector": "labelSelector",
            "timeout_seconds": "timeoutSeconds",
            "field_selector": "fieldSelector"
        }
        for k, v in kwargs.items():
            if k not in valid_params:
                raise AXKubeApiException(
                    "Stream API only support {} for now".format(
                        str(valid_params)))
            data[swagger_to_kube[k]] = v
        return data