Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Optionally add pull secret for private ECR (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippthun authored and knative-prow-robot committed Apr 7, 2019
1 parent 9d6f1a3 commit 63912a6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ecr_helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ By default, the following resources will be provisioned:

* A Kubernetes service account (named `builder` by default) with secrets (`ecr-creds`) to enable pushing to ECR.


To use, simply add a `serviceAccountName: builder` entry to your build definition

```yaml:
Expand Down Expand Up @@ -37,6 +36,12 @@ the namespace and kubernetes service account used:
ecr_helper/helper.sh $MY_NAMESPACE builder-serviceaccount
```

Optionally, a pull secret can be added to the service account which enables pulling images from a private ECR.

```shell
ecr_helper/helper.sh --push-and-pull <namespace> <serviceaccount>
```

This will output a log of operations performed or skipped:

```
Expand Down
26 changes: 26 additions & 0 deletions ecr_helper/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ checkBinary aws
checkBinary jq
checkBinary kubectl

if [[ "$1" == "--push-and-pull" ]]; then
PUSH_AND_PULL_SECRETS=true
shift
fi

readonly KUBECTL_FLAGS="${1:+ -n $1}"

if ! kubectl $KUBECTL_FLAGS get sa >& /dev/null; then
Expand Down Expand Up @@ -104,13 +109,33 @@ if [[ -z $PASSWORD ]]; then
exit 1
fi

if [[ -n "$PUSH_AND_PULL_SECRETS" ]]; then
OPTIONAL_IMAGE_PULL_SECRETS=$(cat <<EOF
imagePullSecrets:
- name: ecr-creds-pull
EOF
)
OPTIONAL_PULL_SECRET=$(cat <<EOF
---
apiVersion: v1
kind: Secret
metadata:
name: ecr-creds-pull
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: $(echo -n "{\"auths\":{\"$ENDPOINT\":{\"Username\":\"$USERNAME\",\"Password\":\"$PASSWORD\",\"Email\":\"noop\"}}}" | openssl base64 -a -A)
EOF
)
fi

cat <<EOF | kubectl $KUBECTL_FLAGS apply -f - 2>&3
apiVersion: v1
kind: ServiceAccount
metadata:
name: $KUBE_SA
secrets:
- name: ecr-creds
$OPTIONAL_IMAGE_PULL_SECRETS
---
apiVersion: v1
kind: Secret
Expand All @@ -122,6 +147,7 @@ type: kubernetes.io/basic-auth
data:
username: $(echo -n $USERNAME | openssl base64 -a -A)
password: $(echo -n $PASSWORD | openssl base64 -a -A)
$OPTIONAL_PULL_SECRET
EOF

readonly EXIT=$?
Expand Down

0 comments on commit 63912a6

Please sign in to comment.