How should I make a namespace an argument to a Kub...
# general
o
How should I make a namespace an argument to a Kubernetes
ComponentResource
? I would like the component to take a namespace to install into as an argument, but I can't figure out how to extract the
name
of the namespace out of a
k8s.core.v1.Namespace
object.
o
Ty!
@gorgeous-egg-16927 thanks, this was pretty easy. It was my first time using pulumi vs using a Helm chart from scratch, and it was much easier to write abstractions that made sense for Kubernetes. e.g: I defined an interface called
PortSet
and a constructor for it that allowed me to declare the ports I cared about, and then use them in a deployment/daemonset/replicaset context and a service context:
Copy code
export interface PortSet {
  containerPort: pulumi.Input<core.v1.ContainerPort>;
  servicePort: pulumi.Input<core.v1.ServicePort>;
  scrapeTarget: pulumi.Input<string>;
}

export interface PortSetInput {
  containerPort: pulumi.Input<number>;
  servicePort: pulumi.Input<number>;
  nodePort?: pulumi.Input<number>;
}

export function portSet(
  name: pulumi.Input<string>,
  protocol: pulumi.Input<ServiceProtocol>,
  { containerPort, servicePort, nodePort }: PortSetInput
): PortSet {
  const toServicePort = (
    protocol: pulumi.Input<ServiceProtocol>
  ): pulumi.Input<core.v1.ServicePort> => ({
    name,
    protocol,
    targetPort: containerPort,
    nodePort,
    port: servicePort
  });

  return {
    containerPort: {
      name,
      containerPort
    },
    servicePort: pulumi.output(protocol).apply(toServicePort),
    scrapeTarget: pulumi.output(servicePort).apply(x => x.toString()),
  };
}
Then, in the daemonset, I just used, e.g.:
Copy code
const service = new k8s.core.v1.Service(resourceName, {
      metadata: { ...  },
      spec: {
        clusterIP: "None",
        ports: [metricsPortSet.servicePort, metricsSelfPortSet.servicePort],
        selector: kubeStateMetricsLabels
      }
    });

    const deployment = new k8s.apps.v1.Deployment(resourceName, {
      metadata: { ... },
      spec: {
        ...
        template: {
          spec: {
            containers: [
              {
                ports: [
                  metricsPortSet.containerPort,
                  metricsSelfPortSet.containerPort
                ],
The code re-use and giving useful names to what used to be stringly typed values? beautiful. The first deployment with ~15 Kubernetes resources went off without a hitch on the first time.
g
That’s awesome! Glad to hear you got it working. đŸ™‚