示例#1
0
    def NewFromJsonDict(data):
        '''Create a new instance base on a JSON dict
		Args:
		  data: A JSON dict, as converted from the JSON in the kubernetes API
		Returns:
		  A kubernetes.Pod instance
		'''

        desiredState = None
        currentState = None

        if 'desiredState' in data:
            from kubernetes import PodState
            desiredState = PodState.NewFromJsonDict(data['desiredState'])

        if 'currentState' in data:
            from kubernetes import PodState
            currentState = PodState.NewFromJsonDict(data['currentState'])

        return Pod(Kind=data.get('kind', None),
                   ID=data.get('id', None),
                   UID=data.get('uid', None),
                   CreationTimestamp=data.get('creationTimestamp', None),
                   SelfLink=data.get('selfLink', None),
                   ResourceVersion=data.get('resourceVersion', None),
                   APIVersion=data.get('apiVersion', None),
                   Namespace=data.get('namespace', None),
                   Annotations=data.get('annotations', None),
                   Labels=data.get('labels', None),
                   DesiredState=desiredState,
                   CurrentState=currentState)
示例#2
0
    def NewFromJsonDict(data):
        '''Create a new instance base on a JSON dict
        Args:
          data: A JSON dict, as converted from the JSON in the kubernetes API
        Returns:
          A kubernetes.PodList instance
        '''

        items = []
        metadata = data.get('metadata')

        if 'items' in data and data['items']:
            from kubernetes import Pod
            items = [Pod.NewFromJsonDict(pod) for pod in data['items']]

        return PodList(
                    Kind=data.get('kind', None),
                    APIVersion=data.get('apiVersion', None),
                    CreationTimestamp=metadata.get('creationTimestamp', None),
                    SelfLink=metadata.get('selfLink', None),
                    ResourceVersion=metadata.get('resourceVersion', None),
                    Name=metadata.get('name', None),
                    UID=metadata.get('uid', None),
                    Namespace=metadata.get('namespace', None),
                    Annotations=metadata.get('annotations', None),

                    Items=items)
示例#3
0
    def NewFromJsonDict(data):
        '''Create a new instance base on a JSON dict
        Args:
          data: A JSON dict, as converted from the JSON in the kubernetes API
        Returns:
          A kubernetes.Pod instance
        '''

        desiredState = None
        currentState = None
        state = None
        condition = None
        nodeName = None
        containerSpecs = None

        metadata = data.get('metadata', None)
        status = data.get('status', None)

        if 'desiredState' in data:
            from kubernetes import PodState
            desiredState = PodState.NewFromJsonDict(data['desiredState'])

        if 'currentState' in data:
            from kubernetes import PodState
            currentState = PodState.NewFromJsonDict(data['currentState'])

        if 'status' in data:
            from kubernetes import PodState
            state = PodState.NewFromJsonDict(data['status'])
            nodeName = state.HostIP

        if nodeName is None and 'spec' in data:
            nodeName = data['spec'].get('nodeName', None)

        try:
            if len(data['spec']['containers']) > 0:
                containerSpecs = []
                for container in data['spec']['containers']:
                    containerSpec = ContainerSpec(container)
                    containerSpecs.append(containerSpec)
        except:
            containerSpecs = None

        return Pod(
                    Kind=data.get('kind', None),
                    APIVersion=data.get('apiVersion', None),
                    Name=metadata.get('name', None),
                    UID=metadata.get('uid', None),
                    CreationTimestamp=metadata.get('creationTimestamp', None),
                    SelfLink=metadata.get('selfLink', None),
                    ResourceVersion=metadata.get('resourceVersion', None),
                    Namespace=metadata.get('namespace', None),
                    Annotations=metadata.get('annotations', None),
                    Labels=metadata.get('labels', None),
                    Status=state,
                    DesiredState=desiredState,
                    CurrentState=currentState,
                    NodeName=nodeName,
                    ContainerSpecs = containerSpecs)
示例#4
0
    def GetPod(self, name, namespace='default'):
        '''List the specific pod on this cluster'''

        # Make and send requests
        url = ('%(base_url)s/namespaces/%(ns)s/pods/%(name)s' % {
            "base_url": self.base_url,
            "ns": namespace,
            "name": name
        })
        json = self._RequestUrl(url, 'GET')
        if json.status_code == 404:
            #not exit, just return None
            return None
        data = self._ParseAndCheckKubernetes(json.content)
        return Pod.NewFromJsonDict(data)
示例#5
0
    def CreatePod(self, data, namespace='default'):
        '''Create a new Pod'''

        url = ('%(base_url)s/namespaces/%(ns)s/pods' % {
            "base_url": self.base_url,
            "ns": namespace
        })
        json = self._RequestUrl(url, 'PUT', data)
        if json.status_code is not 201:
            raise KubernetesError({
                'message':
                'parsing error [' + simplejson.dumps(json.content) + ']'
            })
        result = self._ParseAndCheckKubernetes(json.content)
        return Pod.NewFromJsonDict(result)
示例#6
0
    def SetNodeSchedulable(self, node_name, is_scheduable=True):
        ''' Make the node as unscheduable '''

        # Make and send requests
        url = ('%(base_url)s/nodes/%(node_name)s' % {
            "base_url": self.base_url,
            "node_name": node_name
        })

        if is_scheduable:
            data = '{"spec":{"unschedulable":false}}'
        else:
            data = '{"spec":{"unschedulable":true}}'

        json = self._RequestUrl(url, 'PATCH', data)
        if json.status_code is not 200:
            raise KubernetesError({
                'message':
                'parsing error [' + simplejson.dumps(json.content) + ']'
            })
        result = self._ParseAndCheckKubernetes(json.content)
        return Pod.NewFromJsonDict(result)