Search code examples
javascriptdigital-oceanpulumi

How to get the cluster node IPs using Pulumi


Here is an example of creating a managed Kubernetes cluster on DigitalOcean.

import * as pulumi from "@pulumi/pulumi";
import * as digitalocean from "@pulumi/digitalocean";

const foo = new digitalocean.KubernetesCluster("foo", {
    region: "nyc1",
    version: "1.20.2-do.0",
    nodePool: {
        name: "front-end-pool",
        size: "s-2vcpu-2gb",
        nodeCount: 3,
    },
});

Example code taken from the Pulumi DigitalOcean package documentation.

How do I retrieve the droplet node IPv4 addresses for use in say creating DnsRecord resources?

const _default = new digitalocean.Domain("default", {name: "example.com"});

// This code doesn't work because foo.nodePool is just the inputs.
const dnsRecords = foo.nodePool.nodes(node => new digitalocean.DnsRecord("www", {
    domain: _default.name,
    type: "A",
    value: node.ipv4Address,
}));

Solution

  • DigitalOcean doesn't return a list of node IP addresses from the Kubernetes cluster you created. You can retrieve these values using the getDroplet function.

    However, you'll need to do this iteration inside an apply() like so:

    const addresses = foo.nodePool.nodes.apply(
        nodes => nodes.forEach(
            (node) => {
                let n = digitalocean.getDropletOutput({
                    name: node.name
                })
                new digitalocean.DnsRecord("www", {
                    domain: domain.name,
                    type: "A",
                    value: n.ipv4Address,
                })
            }
        )
    )
    

    Using an apply here lets us wait until the foo.nodePool.nodes has been created by the API. We can then iterate over it like a normal array, get the droplet, assign it to the variable n and then create a new DNS record for each of the nodes