コード例 #1
0
ファイル: backend.py プロジェクト: lslebodn/conu
    def list_deployments(self, namespace=None):
        """
        List all available deployments.

        :param namespace: str, if not specified list deployments for all namespaces
        :return: collection of instances of :class:`conu.backend.k8s.deployment.Deployment`
        """

        if namespace:
            return [
                Deployment(
                    name=d.metadata.name,
                    namespace=d.metadata.namespace,
                    labels=d.metadata.labels,
                    selector=d.spec.selector,
                    image_metadata=ImageMetadata(
                        name=d.spec.template.spec.containers[0].name.split(
                            "-", 1)[0]))
                for d in self.apps_api.list_namespaced_deployment(
                    namespace, watch=False).items
            ]

        return [
            Deployment(name=d.metadata.name,
                       namespace=d.metadata.namespace,
                       labels=d.metadata.labels,
                       selector=d.spec.selector,
                       image_metadata=ImageMetadata(
                           name=d.spec.template.spec.containers[0].name.split(
                               "-", 1)[0]))
            for d in self.apps_api.list_deployment_for_all_namespaces(
                watch=False).items
        ]
コード例 #2
0
ファイル: test_k8s.py プロジェクト: abompard/conu
    def test_list_deployments(self):
        api_key = get_oc_api_token()
        with K8sBackend(api_key=api_key) as k8s_backend:

            namespace = k8s_backend.create_namespace()

            with DockerBackend() as backend:
                postgres_image = backend.ImageClass("centos/postgresql-10-centos7")

                postgres_image_metadata = postgres_image.get_metadata()

                # set up env variables
                db_env_variables = {"POSTGRESQL_USER": "******",
                                    "POSTGRESQL_PASSWORD": "******",
                                    "POSTGRESQL_DATABASE": "db"}

                postgres_image_metadata.env_variables.update(db_env_variables)

                db_labels = {"app": "postgres"}

                db_deployment = Deployment(name="database", selector=db_labels, labels=db_labels,
                                           image_metadata=postgres_image_metadata,
                                           namespace=namespace,
                                           create_in_cluster=True)

                try:
                    db_deployment.wait(200)
                    assert db_deployment.all_pods_ready()
                    assert any(db_deployment.name == d.name for d in k8s_backend.list_deployments())
                finally:
                    db_deployment.delete()
                    k8s_backend.delete_namespace(namespace)
コード例 #3
0
def test_database_deployment():
    with K8sBackend() as k8s_backend:

        namespace = k8s_backend.create_namespace()

        with DockerBackend() as backend:
            postgres_image = backend.ImageClass("centos/postgresql-10-centos7")

            postgres_image_metadata = postgres_image.get_metadata()

            # set up env variables
            db_env_variables = {"POSTGRESQL_USER": "******",
                                "POSTGRESQL_PASSWORD": "******",
                                "POSTGRESQL_DATABASE": "db"}

            postgres_image_metadata.env_variables.update(db_env_variables)

            db_labels = {"app": "postgres"}

            db_service = Service(name="database", ports=["5432"], selector=db_labels,
                                 namespace=namespace,
                                 create_in_cluster=True)

            db_deployment = Deployment(name="database", selector=db_labels, labels=db_labels,
                                       image_metadata=postgres_image_metadata, namespace=namespace,
                                       create_in_cluster=True)

            try:
                db_deployment.wait(200)
                assert db_deployment.all_pods_ready()
            finally:
                db_deployment.delete()
                db_service.delete()
                k8s_backend.delete_namespace(namespace)
コード例 #4
0
ファイル: test_k8s.py プロジェクト: fridex/conu
def test_deployment_from_template():
    with K8sBackend() as k8s_backend:

        namespace = k8s_backend.create_namespace()

        template = """
        apiVersion: apps/v1
        kind: Deployment
        metadata:
          name: nginx-deployment
          labels:
            app: nginx
        spec:
          replicas: 3
          selector:
            matchLabels:
              app: nginx
          template:
            metadata:
              labels:
                app: nginx
            spec:
              containers:
              - name: nginx
                image: nginx:1.7.9
                ports:
                - containerPort: 80
        """

        test_deployment = Deployment(namespace=namespace,
                                     from_template=template,
                                     create_in_cluster=True)

        try:
            test_deployment.wait(200)
            assert test_deployment.all_pods_ready()
        finally:
            test_deployment.delete()
            k8s_backend.delete_namespace(namespace)
コード例 #5
0
ファイル: test_k8s.py プロジェクト: lslebodn/conu
    def test_deployment_from_template(self):
        api_key = get_oc_api_token()
        with K8sBackend(api_key=api_key) as k8s_backend:

            namespace = k8s_backend.create_namespace()

            template = """
            apiVersion: apps/v1
            kind: Deployment
            metadata:
              name: hello-world
              labels:
                app: hello-world
            spec:
              replicas: 3
              selector:
                matchLabels:
                  app: hello-world
              template:
                metadata:
                  labels:
                    app: hello-world
                spec:
                  containers:
                  - name: hello-openshift
                    image: openshift/hello-openshift
            """

            test_deployment = Deployment(namespace=namespace,
                                         from_template=template,
                                         create_in_cluster=True)

            try:
                test_deployment.wait(200)
                assert test_deployment.all_pods_ready()
            finally:
                test_deployment.delete()
                k8s_backend.delete_namespace(namespace)
コード例 #6
0
ファイル: k8s_deployment.py プロジェクト: fasashen/conu
    kind: Deployment
    metadata:
      name: hello-world
      labels:
        app: hello-world
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: hello-world
      template:
        metadata:
          labels:
            app: hello-world
        spec:
          containers:
          - name: hello-openshift
            image: openshift/hello-openshift
    """

    test_deployment = Deployment(namespace=namespace,
                                 from_template=template,
                                 create_in_cluster=True)

    try:
        test_deployment.wait(200)
        assert test_deployment.all_pods_ready()
    finally:
        test_deployment.delete()
        k8s_backend.delete_namespace(namespace)
コード例 #7
0
    def deploy(self,
               img: ImageSpec,
               env_vars,
               mounts,
               cargos,
               ports,
               cmd: list,
               name: str,
               tasks: int = 1,
               allow_probe=False,
               delay_readiness: int = 0):

        [self.load_vol(v, name) for v in cargos]
        vol_refs, vol_defs = self.kube_vols(cargos)
        mount_refs, mount_defs = self.kube_mounts(mounts)
        port_refs, port_defs = self.kube_svc_ports(name, ports)

        container = dict(name=name,
                         image=img.target,
                         imagePullPolicy='Always',
                         command=cmd,
                         resources=self.kube_resources(),
                         volumeMounts=vol_refs + mount_refs,
                         env=self.kube_env_vars(env_vars),
                         ports=port_refs,
                         livenessProbe=self.kube_healthcheck(allow_probe),
                         readinessProbe=self.kube_readiness(delay_readiness))

        template = self.cleaner(
            dict(apiVersion="apps/v1",
                 kind="Deployment",
                 metadata={'name': name},
                 spec=dict(replicas=tasks,
                           selector={'matchLabels': {
                               'app': name
                           }},
                           template=dict(metadata={
                               'labels': {
                                   'app': name
                               },
                               'annotations': {
                                   'updated':
                                   datetime.now().strftime(DateFmt.READABLE)
                               }
                           },
                                         spec={
                                             'containers': [container],
                                             'volumes':
                                             vol_defs + mount_defs,
                                             'imagePullSecrets': [{
                                                 'name':
                                                 self.secret
                                             }]
                                         }))))

        if self.find_depl(name) is None:
            self.LOG.info("Creating deployment '{}'".format(name))
            self.LOG.debug(template)
            yaml = Kaptan().import_config(template).export(handler='yaml')
            depl = Deployment(namespace=self.namespace,
                              create_in_cluster=True,
                              from_template=yaml)
        else:
            self.LOG.info("Updating deployment '{}'".format(name))
            self.LOG.debug(template)
            with K8sBackend(logging_level=logging.ERROR) as k8s_backend:
                k8s_backend.apps_api.replace_namespaced_deployment(
                    name, self.namespace, template)
            depl = self.find_depl(name)

        self.handle_svc(name, port_defs)
        self.handle_autoscaler(name)

        return depl