Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for ECR private images in charts #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ NAME := $(REPO)
CHARTNAME := $(shell ls -1d */ | sed 's\#/\#\#' | grep -v 'tools')
K3SVER := v0.7.0

REGISTRYID := 000000000000
REGISTRYREGION := us-east-1
ECRTOKEN := $(shell aws ecr get-login --region $(REGISTRYREGION) --registry-ids $(REGISTRYID) | cut -d' ' -f6)

help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

Expand All @@ -24,8 +28,10 @@ run: ## Creates a docker network, runs this module image with volumes and defau
@bash -c "docker exec -t $(NAME) sh -c 'iptables -t nat -I POSTROUTING -s 10.42.0.0/16 -d 10.42.0.0/16 -j MASQUERADE'"
@bash -c "docker cp tools/helm $(NAME):/bin/helm"
@bash -c "docker cp tools/tiller $(NAME):/bin/tiller"
@bash -c "docker cp tools/imagepullsecret.sh $(NAME):/bin/imagepullsecret"
@bash -c "docker exec -t $(NAME) sh -c 'sleep 10'"
@bash -c "docker exec -t $(NAME) sh -c 'mkdir -p /.kube && kubectl config view --raw > /.kube/config'"
@bash -c "docker exec -t $(NAME) sh -c 'imagepullsecret $(CHARTNAME) $(REGISTRYID) $(REGISTRYREGION) $(ECRTOKEN)'"
@bash -c "docker exec -t $(NAME) sh -c 'kubectl create clusterrolebinding system:default --clusterrole=cluster-admin --serviceaccount=kube-system:default'"
@bash -c "docker exec -t $(NAME) sh -c 'helm init --wait'"

Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,26 @@ For example to exclude a `ci` and `docs` directory that you want to add update t
```
CHARTNAME := $(shell ls -1d */ | sed 's\#/\#\#' | grep -v 'tools\|ci\|docs')
```

# Using with ECR

Change the `REGISTRYID` and `REGISTRYREGION` variable at the top of the `Makefile`.

Before running `make` ensure you have the correct AWS credentials set that allow the `aws ecr get-login` command to work.

We copy `tools/imagepullsecret.sh` into the k3s container and execute it so it creates a pull secret called `aws-registry` in the same namespace that the chart is installed into.

We also set `imagePullSecrets: ["aws-registry"]` in the values.yaml in the root of the repo so that this is only applied to charts when run under K3s and not when deployed to AWS clusters that will use an IAM role.

Finally, inside your chart you will need to add the following to your template. Under `spec.template.spec` add.

```
{{- if .Values.imagePullSecrets }}
imagePullSecrets:
{{- range $sec := .Values.imagePullSecrets }}
- name: {{$sec | quote }}
{{- end }}
{{- end }}
```

This sets the imagePullSecret if the value is set.
7 changes: 7 additions & 0 deletions tools/imagepullsecret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
kubectl create ns $1
kubectl -n $1 create secret docker-registry aws-registry \
--docker-server=https://$2.dkr.ecr.$3.amazonaws.com \
--docker-username=AWS \
--docker-password=$4 \
[email protected]
1 change: 1 addition & 0 deletions values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
imagePullSecrets: ["aws-registry"]