1. Answers
  2. Await Kubernetes Service Status Change with Pulumi

How do I make Pulumi await Kubernetes service status change?

In this example, we will see how to use Pulumi to create a Kubernetes service and wait for its status to change to Active. This is useful for ensuring that the service is fully provisioned and ready before proceeding with further operations.

The program will:

  1. Create a Kubernetes namespace.
  2. Deploy a Kubernetes service.
  3. Wait for the service to become active by using Pulumi’s Output.apply method.
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Create a Kubernetes namespace
const namespace = new k8s.core.v1.Namespace("example-namespace", {
    metadata: { name: "example-namespace" },
});

// Create a Kubernetes service
const service = new k8s.core.v1.Service("example-service", {
    metadata: {
        namespace: namespace.metadata.name,
        name: "example-service",
    },
    spec: {
        selector: { app: "example-app" },
        ports: [{ port: 80, targetPort: 80 }],
    },
});

// Wait for the service to be active
const serviceStatus = service.status.apply(status => {
    if (status.loadBalancer && status.loadBalancer.ingress) {
        return status.loadBalancer.ingress[0].ip;
    }
    return pulumi.output("Service is not yet active").apply(console.log);
});

// Export the IP address of the service
export const serviceIp = serviceStatus;

Key Points:

  • The k8s.core.v1.Namespace resource is used to create a namespace.
  • The k8s.core.v1.Service resource is used to create a service within the namespace.
  • Pulumi’s Output.apply method is used to wait for the service status to change, checking for the presence of an IP address in the loadBalancer.ingress field.
  • The IP address of the service is exported once it becomes available.

Summary:

In this example, we demonstrated how to create a Kubernetes service using Pulumi and wait for its status to change to Active. This ensures that the service is fully provisioned before proceeding with further operations. By using Pulumi’s Output.apply method, we can easily wait for specific conditions to be met, such as the availability of an IP address for the service.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up