How to make Pulumi await Kubernetes service status change?
TypeScriptIn order to make Pulumi wait for a Kubernetes Service status change, we can leverage the watch property of Kubernetes 'Service' type in the Pulumi Kubernetes provider.
When the
watch
property is set totrue
, Pulumi will watch the status of the resource in Kubernetes and wait until the status is ready before the program continues.Here's an example of how to create a Kubernetes Service with Pulumi and set the watch property:
import * as k8s from "@pulumi/kubernetes"; const myappLabels = { app: "myapp", }; const myappDeployment = new k8s.apps.v1.Deployment("myappDeployment", { spec: { selector: { matchLabels: myappLabels }, replicas: 1, template: { metadata: { labels: myappLabels }, spec: { containers: [{ name: "myapp", image: "nginx", }], }, }, }, }); // Create a Kubernetes Service to expose the Deployment. const frontEnd = new k8s.core.v1.Service("frontEnd", { spec: { selector: myappLabels, ports: [{ port: 80 }], type: "LoadBalancer", }, },{ // This will watch the status of the Service until it's ready // before marking the Pulumi program as done. watch: true, });
In the above code, we first create a Kubernetes Deployment that consists of the
myapp
application. Then, we have a Kubernetes Service that selects the pods of our application and exposes them on aLoadBalancer
at port 80. Whenwatch
is set totrue
, Pulumi will wait for the Service to be ready before marking the program as done. This essentially makes Pulumi await the service status change.You can see the status of the Kubernetes resources by running
pulumi up
.To check that our service is running and discover the public IP address provisioned for our service, we can export the frontEnd status:
// Export the (cluster-private) IP address of the Service. export const serviceIP = frontEnd.status.apply(status => status.loadBalancer.ingress[0].ip);
Please consult the Kubernetes documentation for more detailed usage of the Service type in Pulumi.