# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

from kubernetes import client

from polyaxon_k8s import constants as k8s_constants

from spawner.templates import constants
from spawner.templates import pods


def get_labels(name, project, role=None):
    labels = {'app': name, 'project': project}
    if role:
        labels['role'] = role
    return labels


def get_project_pod_spec(project,
                         name,
                         volume_mounts,
                         volumes,
                         command=None,
                         args=None,
                         ports=None,
                         resources=None,
                         env_vars=None,
                         restart_policy=None):
    """Pod spec to be used to create pods for project side: tensorboard, notebooks."""
    volume_mounts = volume_mounts or []
    volumes = volumes or []

    volume_mounts = volume_mounts or []
    volumes = volumes or []

    if resources.gpu:
        volume_mounts += pods.get_gpu_volume_mounts()
        volumes += pods.get_gpu_volumes()

    ports = [client.V1ContainerPort(container_port=port) for port in ports]

    container_name = constants.POD_CONTAINER_PROJECT_NAME.format(project=project, name=name)
    containers = [client.V1Container(name=container_name,
                                     image=constants.JOB_DOCKER_NAME,
                                     command=command,
                                     args=args,
                                     ports=ports,
                                     env=env_vars,
                                     resources=pods.get_resources(resources),
                                     volume_mounts=volume_mounts)]
    return client.V1PodSpec(restart_policy=restart_policy, containers=containers,
                            volumes=volumes)


def get_deployment_spec(namespace,
                        name,
                        project,
                        volume_mounts,
                        volumes,
                        command,
                        args,
                        ports,
                        resources=None,
                        role=None,
                        replicas=1):
    labels = get_labels(name, project, role)
    metadata = client.V1ObjectMeta(
        name=constants.DEPLOYMENT_NAME.format(project=project, name=name),
        labels=labels,
        namespace=namespace)
    pod_spec = get_project_pod_spec(project=project,
                                    name=name,
                                    volume_mounts=volume_mounts,
                                    volumes=volumes,
                                    command=command,
                                    args=args,
                                    resources=resources,
                                    ports=ports)
    template_spec = client.V1PodTemplateSpec(metadata=metadata, spec=pod_spec)
    return client.AppsV1beta1DeploymentSpec(replicas=replicas, template=template_spec)


def get_deployment(namespace,
                   name,
                   project,
                   volume_mounts,
                   volumes,
                   command,
                   args,
                   ports,
                   resources=None,
                   role=None,
                   replicas=1):
    labels = get_labels(name, project, role)
    metadata = client.V1ObjectMeta(
        name=constants.DEPLOYMENT_NAME.format(project=project, name=name),
        labels=labels,
        namespace=namespace)
    spec = get_deployment_spec(namespace=namespace,
                               name=name,
                               project=project,
                               volume_mounts=volume_mounts,
                               volumes=volumes,
                               command=command,
                               args=args,
                               ports=ports,
                               resources=resources,
                               role=role,
                               replicas=replicas)
    return client.AppsV1beta1Deployment(api_version=k8s_constants.K8S_API_VERSION_V1_BETA1,
                                        kind=k8s_constants.K8S_DEPLOYMENT_KIND,
                                        metadata=metadata,
                                        spec=spec)

@@ -83,16 +83,49 @@
 nction%0A%0A
+from django.conf import settings%0A
 from kub
@@ -305,21 +305,51 @@
  project
-, rol
+_name, project_uuid, role=None, typ
 e=None):
@@ -384,16 +384,21 @@
 'project
+_name
 ': proje
@@ -399,16 +399,51 @@
  project
+_name, 'project_uuid': project_uuid
 %7D%0A    if
@@ -479,16 +479,59 @@
  = role%0A
+    if type:%0A        labels%5B'type'%5D = type%0A
     retu
@@ -570,24 +570,29 @@
 spec(project
+_uuid
 ,%0A          
@@ -1408,39 +1408,53 @@
 NAME.format(
+%0A        
 project
+_uuid
 =project
 , name=name)
@@ -1433,32 +1433,37 @@
 ect_uuid=project
+_uuid
 , name=name)%0A   
@@ -1560,24 +1560,23 @@
   image=
-constant
+setting
 s.JOB_DO
@@ -2019,36 +2019,8 @@
 ers,
-%0A                           
  vol
@@ -2123,32 +2123,75 @@
          project
+_name,%0A                        project_uuid
 ,%0A              
@@ -2409,32 +2409,67 @@
      role=None,%0A
+                        type=None,%0A
                 
@@ -2513,38 +2513,200 @@
 _labels(name
-, project, rol
+=name,%0A                        project_name=project_name,%0A                        project_uuid=project_uuid,%0A                        role=role,%0A                        type=typ
 e)%0A    metad
@@ -2774,39 +2774,55 @@
 NAME.format(
+name=name, 
 project
+_uuid
 =project
 , name=name)
@@ -2805,35 +2805,29 @@
 uuid=project
-, name=name
+_uuid
 ),%0A        l
@@ -2908,32 +2908,37 @@
 spec(project
+_uuid
 =project
 ,%0A          
@@ -2917,32 +2917,37 @@
 ect_uuid=project
+_uuid
 ,%0A              
@@ -3548,24 +3548,62 @@
      project
+_name,%0A                   project_uuid
 ,%0A          
@@ -3790,32 +3790,62 @@
      role=None,%0A
+                   type=None,%0A
                 
@@ -3892,22 +3892,184 @@
 name
-, project, rol
+=name,%0A                        project_name=project_name,%0A                        project_uuid=project_uuid,%0A                        role=role,%0A                        type=typ
 e)%0A 
@@ -4156,24 +4156,29 @@
 (project
+_uuid
 =project
 , name=n
@@ -4169,16 +4169,21 @@
 =project
+_uuid
 , name=n
@@ -4372,24 +4372,29 @@
  project
+_name
 =project
 ,%0A      
@@ -4385,16 +4385,79 @@
 =project
+_name,%0A                               project_uuid=project_uuid
 ,%0A      
@@ -4778,32 +4778,74 @@
      role=role,%0A
+                               type=type,%0A
                 

Update deployment labels