why pulumi kubernetes provider is changing the service and deployment name?

240 views Asked by At

I tried to convert the below working kubernetes manifest from

##namespace
---
apiVersion: v1
kind: Namespace
metadata:
  name: poc

##postgress
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: db
  name: db
  namespace: poc
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
      - image: postgres
        name: postgres
        env:
        - name: POSTGRES_USER
          value: postgres
        - name: POSTGRES_PASSWORD
          value: postgres
        ports:
        - containerPort: 5432
          name: postgres
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: db
  name: db
  namespace: poc
spec:
  type: ClusterIP
  ports:
  - name: "db-service"
    port: 5432
    targetPort: 5432
  selector:
    app: db

##adminer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: ui
  name: ui
  namespace: poc
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ui
  template:
    metadata:
      labels:
        app: ui
    spec:
      containers:
      - image: adminer
        name: adminer
        ports:
        - containerPort: 8080
          name: ui
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: ui
  name: ui
  namespace: poc
spec:
  type: NodePort
  ports:
  - name: "ui-service"
    port: 8080
    targetPort: 8080
  selector:
    app: ui

to

import * as k8s from "@pulumi/kubernetes";
import * as kx from "@pulumi/kubernetesx";

//db
const dbLabels = { app: "db" };
const dbDeployment = new k8s.apps.v1.Deployment("db", {
    spec: {
        selector: { matchLabels: dbLabels },
        replicas: 1,
        template: {
            metadata: { labels: dbLabels },
            spec: { 
                containers: [
                    { 
                        name: "postgres", 
                        image: "postgres",
                        env: [{ name: "POSTGRES_USER", value: "postgres"},{ name: "POSTGRES_PASSWORD", value: "postgres"}],
                        ports: [{containerPort: 5432}]
                    }
                ] 
            }
        }
    }
});

const dbService = new k8s.core.v1.Service("db", {
    metadata: { labels: dbDeployment.spec.template.metadata.labels },
    spec: {
        selector: dbLabels,
        type: "ClusterIP",
        ports: [{ port: 5432, targetPort: 5432, protocol: "TCP" }],
    }
});

//adminer
const uiLabels = { app: "ui" };
const uiDeployment = new k8s.apps.v1.Deployment("ui", {
    spec: {
        selector: { matchLabels: uiLabels },
        replicas: 1,
        template: {
            metadata: { labels: uiLabels },
            spec: { 
                containers: [
                    { 
                        name: "adminer", 
                        image: "adminer",
                        ports: [{containerPort: 8080}],
                    }
                ] 
            }
        }
    }
});

const uiService = new k8s.core.v1.Service("ui", {
    metadata: { labels: uiDeployment.spec.template.metadata.labels },
    spec: {
        selector: uiLabels,
        type: "NodePort",
        ports: [{ port: 8080, targetPort: 8080, protocol: "TCP" }]
    }
});

With this pulumi up -y is SUCCESS without error but the application is not fully UP and RUNNING. Because the adminer image is trying to use Postgres database hostname as db, But looks like pulumi is changing the service name like below:

enter image description here

My question here is, How to make this workable? Is there a way in pulumi to strict with the naming?

Note- I know we can easily pass the hostname as an env variable to the adminer image but I am wondering if there is anything that can allow us to not change the name.

1

There are 1 answers

0
jaxxstorm On BEST ANSWER

Pulumi automatically adds random strings to your resources to help with replacing resource. You can find more information about this in the FAQ

If you'd like to disable this, you can override it using the metadata, like so:

import * as k8s from "@pulumi/kubernetes";
import * as kx from "@pulumi/kubernetesx";

//db
const dbLabels = { app: "db" };
const dbDeployment = new k8s.apps.v1.Deployment("db", {
    spec: {
        selector: { matchLabels: dbLabels },
        replicas: 1,
        template: {
            metadata: { labels: dbLabels },
            spec: { 
                containers: [
                    { 
                        name: "postgres", 
                        image: "postgres",
                        env: [{ name: "POSTGRES_USER", value: "postgres"},{ name: "POSTGRES_PASSWORD", value: "postgres"}],
                        ports: [{containerPort: 5432}]
                    }
                ] 
            }
        }
    }
});
    
const dbService = new k8s.core.v1.Service("db", {
    metadata: { 
        name: "db", // explicitly set a name on the service
        labels: dbDeployment.spec.template.metadata.labels 
    },
    spec: {
        selector: dbLabels,
        type: "ClusterIP",
        ports: [{ port: 5432, targetPort: 5432, protocol: "TCP" }],
    }
});

With that said, it's not always best practice to hardcode names like this, you should, if possible, reference outputs from your resources and pass them to new resources.