Esempio n. 1
0
 def _create(self, app_spec, selector, labels):
     LOG.info("Creating/updating service for %s with labels: %s",
              app_spec.name, labels)
     ports = [
         self._make_service_port(port_spec) for port_spec in app_spec.ports
     ]
     try:
         svc = Service.get(app_spec.name, app_spec.namespace)
         ports = self._merge_ports(svc.spec.ports, ports)
     except NotFound:
         pass
     service_name = app_spec.name
     custom_labels = merge_dicts(app_spec.labels.service, labels)
     custom_annotations = merge_dicts(
         app_spec.annotations.service,
         self._make_tcp_port_annotation(app_spec))
     metadata = ObjectMeta(name=service_name,
                           namespace=app_spec.namespace,
                           labels=custom_labels,
                           annotations=custom_annotations)
     spec = ServiceSpec(selector=selector,
                        ports=ports,
                        type=self._service_type)
     svc = Service.get_or_create(metadata=metadata, spec=spec)
     self._owner_references.apply(svc, app_spec)
     svc.save()
Esempio n. 2
0
    def action():
        for kind in kinds:
            assert kind.get(name)
        dep = Deployment.get(name)
        assert dep.spec.template.spec.containers[0].image == image
        svc = Service.get(name)
        assert svc.spec.type == service_type

        for kind, expected_dict in expected.items():
            actual = kind.get(name)
            assert_k8s_resource_matches(actual, expected_dict, image, service_type, deployment_id, strongbox_groups)
Esempio n. 3
0
    def test_get_or_create_service_not_new(self, put, get):
        service = create_default_service()

        mock_response = mock.Mock()
        mock_response.json.return_value = {
            "kind": "Service", "apiVersion": "v1", "metadata": {
                "name": SERVICE_NAME,
                "namespace": SERVICE_NAMESPACE,
                "selfLink": "/api/v1/namespaces/" + SERVICE_NAMESPACE + "/services/my-name",
                "uid": "cc562581-cbf5-11e5-b6ef-247703d2e388",
                "resourceVersion": "817",
                "creationTimestamp": "2016-02-05T10:47:06Z",
                "labels": {
                    "app": "test"
                },
            },
            "spec": {
                "ports": [
                    {
                        "name": "my-port", "protocol": "TCP", "port": 80, "targetPort": "name"
                    }
                ],
                "clusterIP": "10.0.0.54", "type": "ClusterIP", "sessionAffinity": "None"
            },
            "status": {
                "loadBalancer": {}
            }
        }
        get.return_value = mock_response

        metadata = ObjectMeta(name=SERVICE_NAME, namespace=SERVICE_NAMESPACE, labels={"app": "test"})
        port = ServicePort(name="my-port", port=80, targetPort="name")
        spec = ServiceSpec(ports=[port])

        from_api = Service.get_or_create(metadata=metadata, spec=spec)
        assert not from_api._new
        assert from_api.metadata.labels
        assert from_api.metadata.name == service.metadata.name
        call_params = from_api.as_dict()
        put.return_value.json.return_value = call_params

        from_api.save()
        pytest.helpers.assert_any_call(put, SERVICES_URI + SERVICE_NAME, call_params)
Esempio n. 4
0
 def delete(self, app_spec):
     LOG.info("Deleting service for %s", app_spec.name)
     try:
         Service.delete(app_spec.name, app_spec.namespace)
     except NotFound:
         pass
Esempio n. 5
0
def create_default_service():
    metadata = ObjectMeta(name=SERVICE_NAME, namespace=SERVICE_NAMESPACE, labels={"app": "test"})
    port = ServicePort(name="my-port", port=80, targetPort="name")
    spec = ServiceSpec(ports=[port])
    return Service(metadata=metadata, spec=spec)
Esempio n. 6
0
    def test_list_services(self, get):
        service_list = {
            "apiVersion": "v1",
            "kind": "List",
            "metadata": {},
            "resourceVersion": "",
            "selflink": "",
            "items": [
                {
                    "kind": "Service",
                    "apiVersion": "v1",
                    "metadata": {
                        "name": "foo",
                        "namespace": "default",
                        "selfLink": "/api/v1/namespaces/default/services/foo",
                        "uid": "cc562581-cbf5-11e5-b6ef-247703d2e388",
                        "resourceVersion": "817",
                        "creationTimestamp": "2016-02-05T10:47:06Z",
                    },
                    "spec": {
                        "ports": [
                            {
                                "name": "https", "protocol": "TCP", "port": 443, "targetPort": "https"
                            }
                        ],
                        "clusterIP": "10.0.0.1", "type": "ClusterIP", "sessionAffinity": "None"
                    },
                    "status": {
                        "loadBalancer": {}
                    }
                },
                {
                    "kind": "Service",
                    "apiVersion": "v1",
                    "metadata": {
                        "name": "bar",
                        "namespace": "default",
                        "selfLink": "/api/v1/namespaces/default/services/bar",
                        "uid": "4d00cb9e-30d2-11e7-ba70-7a4531eb635c",
                        "resourceVersion": "13608",
                        "creationTimestamp": "2017-05-04T14:02:25Z",
                    },
                    "spec": {
                        "ports": [
                            {
                                "name": "http", "protocol": "TCP", "port": 80, "targetPort": "8080"
                            }
                        ],
                        "clusterIP": "10.0.0.2", "type": "ClusterIP", "sessionAffinity": "None"
                    },
                    "status": {
                        "loadBalancer": {}
                    }
                }
            ]
        }
        mock_response = mock.Mock()
        mock_response.json.return_value = service_list
        get.return_value = mock_response

        services = Service.list(namespace="default")
        assert services[0].metadata.name == "foo"
        assert services[0].metadata.namespace == "default"
        assert services[0].spec.ports[0].name == "https"
        assert services[0].spec.ports[0].port == 443
        assert services[0].spec.ports[0].targetPort == "https"
        assert services[1].metadata.name == "bar"
        assert services[1].metadata.namespace == "default"
        assert services[1].spec.ports[0].name == "http"
        assert services[1].spec.ports[0].port == 80
        assert services[1].spec.ports[0].targetPort == "8080"
Esempio n. 7
0
    def test_service_deleted(self, delete):
        Service.delete(SERVICE_NAME, SERVICE_NAMESPACE)

        # call delete with service_name
        pytest.helpers.assert_any_call(delete, (SERVICES_URI + SERVICE_NAME))