How to add a docker-registry secret for Google Cloud Platform on Windows (that is, in PowerShell, not Bash)?

1.1k Views Asked by At

I'm using Windows, Docker for Windows with Kubernetes enabled and PowerShell (not Bash). This is just for local testing of my Docker images and Kubernetes set up. The problem I'm having is getting access to Google's Container Registry.

I created the service account, hopefully with the right permissions but I'm not there yet. I've got the JSON file with the private key and other details. My problem is when I try to create the secret by running:

kubectl create secret docker-registry gcr-json-key --docker-server=eu.gcr.io --docker-username=_json_key --docker-password="$(cat json_credentials.json)" [email protected]

I get this error:

error: exactly one NAME is required, got 5
See 'kubectl create secret docker-registry -h' for help and examples.

My guess is that after --docker-password=" I end up with line-feeds, quotes, etc. and that is causing the problem. Or is it something else? How am I suppose to do it? Does this work in Bash but not PowerShell?

1

There are 1 best solutions below

0
On

I was able to solve this issue by escaping double quotes and removing new lines from the JSON key file. (Powershell)

$JSON_CREDS = "C:\Path\To\Creds.json"
$REPOSITORY_HOST = "https://gcr.io"
$EMAIL = "[email protected]"
# An arbitrary string to serve as the name of the secret
$SECRET_NAME = "gcr-pull-secret"
$KUB_NAMESPACE = "some-namespace"

$service_json = (Get-Content $JSON_CREDS).replace("\n", "").replace('"', '\"')

$kubePath = "C:\Path\To\kubectl.exe"
$namespaceArg = "--namespace=$KUB_NAMESPACE"
$createArg = "create secret docker-registry $SECRET_NAME"
$serverArg = "--docker-server=$REPOSITORY_HOST"
$usernameArg = "--docker-username=_json_key"
$passwordArg = "--docker-password=""$service_json"""
$emailArg = "--docker-email=$EMAIL"

Start-Process $kubePath -ArgumentList "$namespaceArg $createArg $serverArg $usernameArg $passwordArg $emailArg" -Wait -NoNewWindow

# Optional, used to patch the default service account

$imagePullSecretJson = """{\""imagePullSecrets\"": [{\""name\"": \""$SECRET_NAME\""}]}"""

Start-Process $kubePath -ArgumentList "patch serviceaccount default -p $imagePullSecretJson" -Wait -NoNewWindow