Example #1
0
async def api_client(context=None, api_client_kwargs=None):
    await config.load_kube_config(config_file=KUBECONFIG_FILE)
    context = context or {}
    context['core_api'] = True
    api_client_kwargs = api_client_kwargs or {}
    api_client_kwargs.setdefault('request_timeout', 50)
    api_cl = ApiClient(**api_client_kwargs)
    user_context = {
        'core_api': client.CoreV1Api(api_cl),
        'apps_api': client.AppsV1Api(api_cl),
        'storage_api': client.StorageV1Api(api_cl),
        'batch_api': client.BatchV1Api(api_cl),
        'cronjob_batch_api': client.BatchV1beta1Api(api_cl),
        'custom_object_api': client.CustomObjectsApi(api_cl),
        'extensions_api': client.ApiextensionsV1Api(api_cl),
    }

    try:
        for k in filter(lambda k: context[k], context):
            if k == 'node':
                user_context[k] = await get_node(user_context['core_api'])

        yield api_cl, user_context
    finally:
        await api_cl.close()
Example #2
0
 def client(self) -> k8s_client.AppsV1Api:
     if self._client is None:
         a_configuration = k8s_client.Configuration(
             host=self.configuration.host,
             api_key={"authorization": self.configuration.token},
         )
         a_configuration.api_key_prefix["authorization"] = "Bearer"
         a_configuration.verify_ssl = False
         self._client = k8s_client.AppsV1Api(k8s_client.ApiClient(a_configuration))
     return self._client
async def statefulsets():
    v1apps = client.AppsV1Api()
    async with watch.Watch().stream(
            v1apps.list_stateful_set_for_all_namespaces) as stream:
        async for event in stream:
            print("Event: %s %s %s" % (event['type'], event['object'].kind,
                                       event['object'].metadata.name))
            await slack(eventtype=event['type'],
                        eventkind=event['object'].kind,
                        eventname=event['object'].metadata.name,
                        eventns=event['object'].metadata.namespace)
Example #4
0
async def api_client(context=None, api_client_kwargs=None):
    await config.load_kube_config(config_file=KUBECONFIG_FILE)
    context = context or {}
    context['core_api'] = True
    api_cl = ApiClient(**(api_client_kwargs or {}))
    user_context = {
        'core_api': client.CoreV1Api(api_cl),
        'apps_api': client.AppsV1Api(api_cl),
        'storage_api': client.StorageV1Api(api_cl),
    }
    for k in filter(lambda k: context[k], context):
        if k == 'node':
            user_context[k] = await get_node(user_context['core_api'])

    try:
        yield api_cl, user_context
    finally:
        await api_cl.close()
Example #5
0
    async def delete_deployment(self, deployment_id: str):
        assert self.auth_client
        assert self.cluster_endpoint

        cfg = client.Configuration(
            host=f"https://{self.cluster_endpoint}:443",
            api_key={
                "authorization": f"Bearer {await self.auth_client.get()}"
            },
        )
        cfg.verify_ssl = False

        async with ApiClient(configuration=cfg) as kube_api:
            apps_api = client.AppsV1Api(kube_api)
            core_api = client.CoreV1Api(kube_api)

            # Delete service
            service_id = f"{deployment_id}-svc"
            await core_api.delete_namespaced_service(name=service_id,
                                                     namespace=KUBE_NAMESPACE)

            # Delete deployment
            await apps_api.delete_namespaced_deployment(
                name=deployment_id, namespace=KUBE_NAMESPACE)
Example #6
0
async def watch_statefulsets(queue):
    v1 = client.AppsV1Api()
    async for event in watch.Watch().stream(
            v1.list_stateful_set_for_all_namespaces):
        await queue.put(event)
Example #7
0
async def watch_deployments(queue):
    v1 = client.AppsV1Api()
    async for event in watch.Watch().stream(
            v1.list_deployment_for_all_namespaces):
        await queue.put(event)
Example #8
0
 def __init__(self, api_client=None):
     self.apps_api_client = client.AppsV1Api(api_client=api_client)
     self.api_client = self.apps_api_client.api_client
Example #9
0
    async def create_deployment(
        self,
        container: str,
        num_replicas: int,
        cpus: float = 1.0,
        memory: float = 1.0,
    ) -> Tuple[str, str]:
        assert self.auth_client
        assert self.cluster_endpoint

        cfg = client.Configuration(
            host=f"https://{self.cluster_endpoint}:443",
            api_key={
                "authorization": f"Bearer {await self.auth_client.get()}"
            },
        )
        cfg.verify_ssl = False

        async with ApiClient(configuration=cfg) as kube_api:
            apps_api = client.AppsV1Api(kube_api)
            core_api = client.CoreV1Api(kube_api)

            # Create deployment
            deployment_id = f"dep-{uuid.uuid4()}"
            deployment = client.V1Deployment(
                api_version="apps/v1",
                kind="Deployment",
                metadata=client.V1ObjectMeta(name=deployment_id),
                spec=client.V1DeploymentSpec(
                    replicas=num_replicas,
                    selector={"matchLabels": {
                        "dep": deployment_id
                    }},
                    template=client.V1PodTemplateSpec(
                        metadata=client.V1ObjectMeta(
                            labels={"dep": deployment_id}),
                        spec=client.V1PodSpec(containers=[
                            client.V1Container(
                                name=deployment_id,
                                env=[
                                    client.V1EnvVar(name="PORT",
                                                    value=str(INTERNAL_PORT))
                                ],
                                image=container,
                                resources=client.V1ResourceRequirements(
                                    requests={
                                        "cpu": str(cpus),
                                        "memory": f"{int(memory * 1024)}M",
                                    }),
                                ports=[
                                    client.V1ContainerPort(
                                        container_port=INTERNAL_PORT)
                                ],
                            )
                        ]),
                    ),
                ),
            )
            await apps_api.create_namespaced_deployment(
                namespace=KUBE_NAMESPACE, body=deployment)

            # Create service
            service_id = f"{deployment_id}-svc"
            service_port = self.get_unassigned_port()
            service = client.V1Service(
                api_version="v1",
                kind="Service",
                metadata=client.V1ObjectMeta(
                    name=service_id,
                    # annotations={"cloud.google.com/load-balancer-type": "Internal"},
                ),
                spec=client.V1ServiceSpec(
                    selector={"dep": deployment_id},
                    ports=[
                        client.V1ServicePort(
                            protocol="TCP",
                            port=service_port,
                            target_port=INTERNAL_PORT,
                        )
                    ],
                    type="LoadBalancer",
                ),
            )
            await core_api.create_namespaced_service(namespace=KUBE_NAMESPACE,
                                                     body=service)

            # Poll for external URL
            service_ip = None
            while not service_ip:
                await asyncio.sleep(POLL_INTERVAL)
                ingress = (await core_api.read_namespaced_service(
                    name=service_id,
                    namespace=KUBE_NAMESPACE)).status.load_balancer.ingress
                if ingress:
                    service_ip = ingress[0].ip

        service_url = f"http://{service_ip}:{service_port}"
        print(f"Started deployment {deployment_id} at {service_url}")

        return deployment_id, service_url