3

I've declared a Kubernetes deployment which has two containers. One is built locally, another needs to be pulled from a private registry.

const appImage = new docker.Image("ledgerImage", {
    imageName: 'us.gcr.io/qwil-build/ledger',
    build: "../../",
});

const ledgerDeployment = new k8s.extensions.v1beta1.Deployment("ledger", {
  spec: {
    template: {
      metadata: {
        labels: {name: "ledger"},
        name: "ledger",
      },
      spec: {
        containers: [
          {
            name: "api",
            image: appImage.imageName,
          },
          {
            name: "ssl-proxy",
            image: "us.gcr.io/qwil-build/monolith-ssl-proxy:latest",
          }
        ],

      }
    }
  }
});

When I run pulumi up it hangs - this is happening because of a complaint that You don't have the needed permissions to perform this operation, and you may have invalid credentials. I see this complain when I run kubectl describe <name of pod>. However, when I run docker pull us.gcr.io/qwil-build/monolith-ssl-proxy:latest it executes just fine. I've re-reun gcloud auth configure-docker and it hasn't helped.

I found https://github.com/pulumi/pulumi-cloud/issues/112 but it seems that docker.Image requires a build arg which suggests to me it's meant for local images, not remote images.

How can I pull an image from a private registry?

EDIT:

Turns out I have a local dockerfile for building the SSL proxy I need. I've declared a new Image with

const sslImage = new docker.Image("sslImage", {
  imageName: 'us.gcr.io/qwil-build/ledger-ssl-proxy',
  build: {
    context: "../../",
    dockerfile: "../../Dockerfile.proxy"
  }
});

And updated the image reference in the Deployment correctly. However, I'm still getting authentication problems.

Paymahn Moghadasian
  • 9,301
  • 13
  • 56
  • 94

4 Answers4

2

I have a solution which uses only code, which I use to retrieve images from a private repository on Gitlab:

config.ts

import { Config } from "@pulumi/pulumi";

//
// Gitlab specific config.
//

const gitlabConfig = new Config("gitlab");

export const gitlab = {
    registry: "registry.gitlab.com",
    user: gitlabConfig.require("user"),
    email: gitlabConfig.require("email"),
    password: gitlabConfig.requireSecret("password"),
}

import * as config from "./config";
import { Base64 } from 'js-base64';
import * as kubernetes from "@pulumi/kubernetes";

[...]
const provider = new kubernetes.Provider("do-k8s", { kubeconfig })

const imagePullSecret = new kubernetes.core.v1.Secret(
  "gitlab-registry",
  {
    type: "kubernetes.io/dockerconfigjson",
    stringData: {
      ".dockerconfigjson": pulumi
        .all([config.gitlab.registry, config.gitlab.user, config.gitlab.password, config.gitlab.email])
        .apply(([server, username, password, email]) => {
          return JSON.stringify({
            auths: {
              [server]: {
                auth: Base64.encode(username + ":" + password),
                username: username,
                email: email,
                password: password
              }
            }
          })
        })
    }
  },
  {
    provider: provider
  }
);

// Then use the imagePullSecret in your deployment like this
deployment = new k8s.apps.v1.Deployment(name, {
            spec: {
                selector: { matchLabels: labels },
                template: {
                    metadata: { labels: labels },
                    spec: {
                        imagePullSecrets: [{ name: args.imagePullSecret.metadata.apply(m => m.name) }],
                        containers: [container]
                    },
                },
            },
        });

ludovicc
  • 444
  • 3
  • 11
1

Turns out running pulumi destroy --yes && pulumi up --skip-preview --yes is what I needed. I guess I was in some weird inconsistent state but this is fixed now.

Paymahn Moghadasian
  • 9,301
  • 13
  • 56
  • 94
0

D'oh! Looks like RemoteImage is the answer: https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/docker/#RemoteImage

EDIT:

I tried

const sslImage = new docker.RemoteImage("sslImage", {
  name: 'us.gcr.io/qwil-build/monolith-ssl-proxy:latest',
})

And I'm still getting authentication errors so this isn't the answer I think.

Paymahn Moghadasian
  • 9,301
  • 13
  • 56
  • 94
0

You need to give your cluster the credentials to your Docker Registry, so that it can pull the images from it.

The manual process would be:

docker login registry.gitlab.com 
cat ~/.docker/config.json | base64

Then create a registry_secret.yaml with the output from above

apiVersion: v1
kind: Secret
metadata:
  name: regsec
data:
  .dockerconfigjson: ewJImF1dGhzIjogewoJCSJyZWdpc3RyeS5naXfRsYWsi7fQoJfSwKCSJIdHRwSGVhZGVycyI6IHsKCQkdiVXNlci1BZ2VudCI6ICJEb2NrZXItQ2xpZW50LzEaLjxzLxjUgKH9yIjogInN3YXJtIgp9
type: kubernetes.io/dockerconfigjson

and then apply it to your cluster with

kubectl apply -f registry_secret.yaml && kubectl get secrets

You can wrap that into Pulumi, as it supports yaml files like

new k8s.yaml.ConfigGroup("docker-secret", {files: "registry_secret.yaml"});

This only works if you have your credentials encoded in .docker/config.json and should not work if you are using a credential store

The alternative would be to create the secret directly by providing your user credentials and extracting the token

kubectl create secret docker-registry regsec \
--docker-server=registry.gitlab.com --docker-username=... \
--docker-email=... --docker-password=... \
--dry-run -o yaml | grep .dockerconfigjson: | sed -e 's/.dockerconfigjson://' | sed -e 's/^[ \t]*//'

This token can now be stored as a pulumi secret with

pulumi config set docker_token --secret <your_token>

and be used like this

import {Secret} from "@pulumi/kubernetes/core/v1";
import {Config} from "@pulumi/pulumi";

/**
 * Creates a docker registry secret to pull images from private registries
 */
export class DockerRegistry {

    constructor(provider: any) {

        const config = new Config();

        const dockerToken = config.require("docker_token");

        new Secret("docker-registry-secret", {
            metadata: {
                name: "docker-registry-secret"
            },
            data: {
                ".dockerconfigjson": dockerToken
            },
            type: "kubernetes.io/dockerconfigjson"
        }, {provider});
    }

}
Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136