Rushil Joshi
Rushil Joshi

Reputation: 1

How to tell Tilt to identify a deployment / service created by an existing pod?

In my Tiltfile, I create a frontend and backend deployment, with a service for each one. I also configure the images used in each deployment to synchronize live updates:

# Identify deployment and service yaml for frontend
k8s_yaml('k8s-config/frontend-deployment.yaml')
k8s_yaml('k8s-config/frontend-service.yaml')


# Identify deployment and service yaml for backend
k8s_yaml('k8s-config/backend-deployment.yaml')
k8s_yaml('k8s-config/backend-service.yaml')

# Build frontend image with live update setup
docker_build(
    ref='zz/frontend-test',
    context='../frontend',
    only=['../frontend/zz-frontend'],
    live_update=[
        sync('../frontend/zz-frontend/', '/app/zz-frontend/'),
    ]
)
# Build backend image with live update setup
docker_build(
    ref='zz/backend-test',
    context='../backend',
    only=['../backend'],
    live_update=[
        sync('../backend', '/app/'),
    ]
) 

However, the backend is also able to create or delete services and deployments, using the following Python code (utils.loadTemplate() reads a default kubernetes yaml file and loads it as a dictionary):

from kubernetes import config, client
import utils

# Create an ID for the session
newId = "ID000000"


# Load kubernetes APIs
print(config.load_incluster_config()) # NOTE: This is used for loading the in-cluster configuration of k8s
k8sApps = client.AppsV1Api()
k8sCore = client.CoreV1Api()
print("K8S Config Loaded!")

# Load k8s deployment and service templates from yaml file as dictionaries
deploymentTemplate = utils.loadTemplate(name="jupyter", templateType="deployment")
serviceTemplate = utils.loadTemplate(name="jupyter", templateType="service")

    
# Modify relevant template metadata
deploymentTemplate['metadata']['name'] = f"jupyter-{newId}"
serviceTemplate['metadata']['name'] = f"jupyter-{newId}"
# ...other modification of template metadata

# Attempt deployment creation
deployment = k8sApps.create_namespaced_deployment(
    body=deploymentTemplate, 
    namespace="default"
)
print(deployment)

# Attempt service creation
service = k8sCore.create_namespaced_service(
    body=serviceTemplate, 
    namespace="default"
)
print(service)


While this works, Tilt does not track deployments/services created in-cluster via the Kubernetes API. I would like all deployments created programmatically by the backend to also be tracked by Tilt, and synchronize live updates to code like the frontend and backend do. How can I achieve this?

From this page that outlines managing custom resource definitions, I attempted using k8s_kind('jupyter', image_json_path='{.spec.image}', pod_readiness='wait'), I was not able to get this working. Not sure if this is the solution.

Upvotes: 0

Views: 97

Answers (0)

Related Questions