Beispiel #1
0
def _create_pod():
    object_meta = ObjectMeta(name=NAME,
                             namespace=NAMESPACE,
                             labels={
                                 "test": "true",
                                 "app": NAME
                             })
    container_port = ContainerPort(name="http5000", containerPort=5000)
    secrets_volume_mounts = [
        VolumeMount(
            name=NAME,
            readOnly=True,
            mountPath="/var/run/secrets/kubernetes.io/kubernetes-secrets")
    ]
    secret_volumes = [
        Volume(name=NAME, secret=SecretVolumeSource(secretName=NAME))
    ]
    container = Container(name="container",
                          image="dummy_image",
                          ports=[container_port],
                          volumeMounts=secrets_volume_mounts)
    image_pull_secret = LocalObjectReference(name="image_pull_secret")
    pod_spec = PodSpec(containers=[container],
                       imagePullSecrets=[image_pull_secret],
                       volumes=secret_volumes,
                       serviceAccountName="default")
    first = Pod(metadata=object_meta, spec=pod_spec)
    return first
Beispiel #2
0
 def __call__(self, deployment_config, channel, spec_config=None):
     namespace = deployment_config.namespace
     bootstrap_counter.inc()
     LOG.info("Bootstrapping %s in %s", deployment_config.name, namespace)
     try:
         Pod.delete(name=BOOTSTRAP_POD_NAME, namespace=namespace)
     except NotFound:
         pass
     pod_spec = _create_pod_spec(self._cmd_args, channel, namespace,
                                 spec_config)
     pod_metadata = _create_pod_metadata(namespace, spec_config)
     pod = Pod(metadata=pod_metadata, spec=pod_spec)
     pod.save()
Beispiel #3
0
    def test_lifecycle(self, logger):
        object_meta = ObjectMeta(name=NAME, namespace=NAMESPACE, labels={"test": "true", "app": NAME})
        container_port = ContainerPort(name="http5000", containerPort=5000)
        secrets_volume_mounts = [VolumeMount(name=NAME, readOnly=True, mountPath="/var/run/secrets/kubernetes.io/kubernetes-secrets")]
        secret_volumes = [Volume(name=NAME, secret=SecretVolumeSource(secretName=NAME))]
        container = Container(name="container", image="dummy_image", ports=[container_port], volumeMounts=secrets_volume_mounts)
        image_pull_secret = LocalObjectReference(name="image_pull_secret")
        pod_spec = PodSpec(containers=[container], imagePullSecrets=[image_pull_secret],
                           volumes=secret_volumes, serviceAccountName="default")
        first = Pod(metadata=object_meta, spec=pod_spec)
        logger.debug(pformat(first.as_dict()))
        first.save()

        pods = Pod.find(NAME, NAMESPACE)
        assert len(pods) == 1
        second = pods[0]
        assert first.metadata.name == second.metadata.name
        assert first.metadata.namespace == second.metadata.namespace
Beispiel #4
0
    def test_get_or_create_pod_not_new(self, put, get):
        mock_response = mock.Mock()
        mock_response.json.return_value = {
            'apiVersion': 'v1',
            'kind': 'Pod',
            'metadata': {
                'creationTimestamp': '2017-10-03T10:36:20Z',
                'labels': {
                    'app': 'my-name', 'test': 'true'
                },
                'name': 'my-name',
                'namespace': 'my-namespace',
                'resourceVersion': '852',
                'selfLink': '/api/v1/namespaces/my-namespace/pods/my-name',
                'uid': 'b1e35ab5-a826-11e7-ba76-0800273598c9'
            },
            'spec': {
                'containers': [{
                    'image': 'dummy_image',
                    'imagePullPolicy': 'IfNotPresent',
                    'name': 'container',
                    'ports': [{
                        'containerPort': 5000,
                        'name': 'http5000',
                        'protocol': 'TCP'
                    }],
                    'resources': {},
                    'volumeMounts': [{
                        'mountPath': '/var/run/secrets/kubernetes.io/kubernetes-secrets',
                        'name': 'my-name',
                        'readOnly': True
                    }, {
                        'mountPath': '/var/run/secrets/kubernetes.io/serviceaccount',
                        'name': 'default-token-0g73b',
                        'readOnly': True
                    }]
                }],
                'imagePullSecrets': [{
                    'name': 'image_pull_secret'
                }],
                'restartPolicy': 'Always',
                'serviceAccount': 'default',
                'serviceAccountName': 'default',
                'terminationGracePeriodSeconds': 30,
                'volumes': [{
                    'name': 'my-name',
                    'secret': {
                        'defaultMode': 420,
                        'secretName': 'my-name'
                    }}, {
                    'name': 'default-token-0g73b',
                    'secret': {
                        'defaultMode': 420,
                        'secretName': 'default-token-0g73b'
                    }}]
            },
            'status': {
                'conditions': [{
                    'lastProbeTime': None,
                    'lastTransitionTime': '2017-10-03T10:36:20Z',
                    'status': 'True',
                    'type': 'PodScheduled'
                }],
                'phase': 'Pending',
                'qosClass': 'BestEffort'
            }
        }
        get.return_value = mock_response
        pod = Pod.get(name=NAME, namespace=NAMESPACE)
        assert not pod._new
        assert pod.metadata.name == NAME
        assert pod.spec.containers[0].ports[0].name == "http5000"
        call_params = pod.as_dict()
        put.return_value.json.return_value = call_params

        pod.save()
        pytest.helpers.assert_any_call(put, POD_URI + NAME, call_params)
Beispiel #5
0
    def test_pod_deleted(self, delete):
        Pod.delete(NAME, NAMESPACE)

        # call delete with service_name
        pytest.helpers.assert_any_call(delete, POD_URI + NAME)
Beispiel #6
0
 def teardown(self):
     for name, namespace in ((NAME, "default"), (NAME, NAMESPACE)):
         try:
             Pod.delete(name, namespace)
         except:
             pass