-
Notifications
You must be signed in to change notification settings - Fork 45
feat: Added helm chart for updating AWS ECR secrets in Kubernetes #228
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
Open
vishu247
wants to merge
8
commits into
main
Choose a base branch
from
renew-ecr-k8s-creds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2c6b58e
Helm chart for updating AWS ECR secrets in Kubernetes
vishu247 134009d
update the job.yaml
vishu247 34d8a20
Update role.yaml
vishu247 5288f84
Update rolebinding.yaml
vishu247 f2196b6
Update script-configmap.yaml
vishu247 29c72c2
Update serviceaccount.yaml
vishu247 b648725
Update values.yaml
vishu247 10adc7e
Update README.md
vishu247 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apiVersion: v2 | ||
name: renew-ecr-k8s-creds | ||
description: A Helm chart for updating AWS ECR secrets in Kubernetes | ||
version: 0.1.0 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# renew-ecr-k8s-cred | ||
|
||
A Helm chart for renewing AWS ECR credentials and updating them in a Kubernetes secret. | ||
|
||
## Introduction | ||
|
||
This Helm chart creates a CronJob in Kubernetes to periodically renew AWS Elastic Container Registry (ECR) credentials and update them in a Kubernetes secret. This is useful for ensuring that your ECR credentials are always up to date, especially in environments where long-running workloads need continuous access to private ECR repositories. | ||
|
||
## Prerequisites | ||
|
||
- Kubernetes 1.16+ | ||
- Helm 3.0+ | ||
- An AWS account with permissions to assume the specified role and access ECR | ||
- The AWS CLI installed in the container image | ||
|
||
## Installation | ||
|
||
### Add the Helm Repository | ||
|
||
```sh | ||
# AWS credentials configuration | ||
aws: | ||
accessKey: "your-aws-access-key" # AWS Access Key ID of IAM role for authentication | ||
secretKey: "your-aws-secret-key" # AWS Secret Access Key of IAM role for authentication | ||
region: "your-aws-region" # AWS region where your resources are located | ||
roleArn: "your-aws-role-arn" # ARN of the AWS role to assume for getting temporary credentials | ||
sessionName: "your-session-name" # Session name for the assumed role | ||
|
||
# Kubernetes configuration | ||
kubernetes: | ||
secretName: "your-secret-name" # Name of the Kubernetes secret to create or update with ECR credentials | ||
|
||
# ECR (Elastic Container Registry) configuration | ||
ecr: | ||
account: "your-aws-account" # AWS account ID where your ECR is located | ||
region: "your-ecr-region" # AWS region of your ECR | ||
|
||
# CronJob configuration | ||
cronjob: | ||
schedule: "0 */12 * * *" # Cron schedule for the job to run (every 12 hours) | ||
|
||
# ServiceAccount configuration | ||
serviceAccount: | ||
name: "renew-ecr-k8s-creds-sa" # Name of the ServiceAccount to create or use | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apiVersion: batch/v1 | ||
kind: CronJob | ||
metadata: | ||
name: renew-ecr-k8s-creds | ||
namespace: {{ .Release.Namespace }} | ||
spec: | ||
schedule: "{{ .Values.cronjob.schedule }}" | ||
jobTemplate: | ||
spec: | ||
template: | ||
spec: | ||
serviceAccountName: {{ .Values.serviceAccount.name }} | ||
containers: | ||
- name: renew-ecr-k8s-creds | ||
image: amazon/aws-cli:2.13.15 | ||
command: ["/bin/bash", "/scripts/update-secret.sh"] | ||
volumeMounts: | ||
- name: script | ||
mountPath: /scripts | ||
restartPolicy: Never | ||
volumes: | ||
- name: script | ||
configMap: | ||
name: renew-ecr-k8s-creds-script |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: Role | ||
metadata: | ||
namespace: {{ .Release.Namespace }} | ||
name: renew-ecr-k8s-creds-role | ||
rules: | ||
- apiGroups: [""] | ||
resources: ["secrets"] | ||
verbs: ["get", "create", "patch"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: RoleBinding | ||
metadata: | ||
name: renew-ecr-k8s-creds-rolebinding | ||
namespace: {{ .Release.Namespace }} | ||
subjects: | ||
- kind: ServiceAccount | ||
name: {{ .Values.serviceAccount.name }} | ||
namespace: {{ .Release.Namespace }} | ||
roleRef: | ||
kind: Role | ||
name: renew-ecr-k8s-creds-role | ||
apiGroup: rbac.authorization.k8s.io |
56 changes: 56 additions & 0 deletions
56
charts/renew-ecr-k8s-creds/templates/script-configmap.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: renew-ecr-k8s-creds-script | ||
namespace: {{ .Release.Namespace }} | ||
data: | ||
update-secret.sh: | | ||
#!/bin/bash | ||
|
||
# Install kubectl | ||
echo "Installing kubectl..." | ||
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" | ||
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl | ||
|
||
# Install jq | ||
echo "Installing jq..." | ||
yum install -y jq | ||
|
||
secret_exists() { | ||
kubectl get secret "{{ .Values.kubernetes.secretName }}" --namespace="{{ .Release.Namespace }}" &> /dev/null | ||
return $? | ||
} | ||
|
||
echo "Aws Configure..." | ||
aws configure set aws_access_key_id "{{ .Values.aws.accessKey }}" | ||
aws configure set aws_secret_access_key "{{ .Values.aws.secretKey }}" | ||
aws configure set region "{{ .Values.aws.region }}" | ||
aws configure set output "json" | ||
|
||
echo "Assuming a Role..." | ||
ROLE_OUTPUT=$(aws sts assume-role --role-arn "{{ .Values.aws.roleArn }}" --role-session-name "{{ .Values.aws.sessionName }}") | ||
|
||
echo "Now fetching the access_key, secret_key & session_token..." | ||
AWS_ACCESS_KEY_ID=$(echo "$ROLE_OUTPUT" | jq -r '.Credentials.AccessKeyId') | ||
AWS_SECRET_ACCESS_KEY=$(echo "$ROLE_OUTPUT" | jq -r '.Credentials.SecretAccessKey') | ||
AWS_SESSION_TOKEN=$(echo "$ROLE_OUTPUT" | jq -r '.Credentials.SessionToken') | ||
|
||
export AWS_ACCESS_KEY_ID | ||
export AWS_SECRET_ACCESS_KEY | ||
export AWS_SESSION_TOKEN | ||
|
||
if secret_exists; then | ||
echo "Secret already exists, patching the secret..." | ||
kubectl patch secret "{{ .Values.kubernetes.secretName }}" --namespace="{{ .Release.Namespace }}" --type='json' -p='[{"op": "replace", "path": "/data/.dockerconfigjson", "value":"'$(echo -n "{\"auths\":{\"{{ .Values.ecr.account }}.dkr.ecr.{{ .Values.ecr.region }}.amazonaws.com\":{\"username\":\"AWS\",\"password\":\"$(aws ecr get-login-password)\"}}}" | base64 | tr -d '\n')'"}]' | ||
else | ||
echo "Secret does not exist, creating the secret..." | ||
kubectl create secret docker-registry "{{ .Values.kubernetes.secretName }}" \ | ||
--docker-server="{{ .Values.ecr.account }}.dkr.ecr.{{ .Values.ecr.region }}.amazonaws.com" \ | ||
--docker-username=AWS \ | ||
--docker-password="$(aws ecr get-login-password)" \ | ||
--namespace="{{ .Release.Namespace }}" | ||
fi | ||
|
||
unset AWS_ACCESS_KEY_ID | ||
unset AWS_SECRET_ACCESS_KEY | ||
unset AWS_SESSION_TOKEN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: {{ .Values.serviceAccount.name }} | ||
namespace: {{ .Release.Namespace }} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# AWS credentials configuration | ||
aws: | ||
accessKey: "your-aws-access-key" # AWS Access Key ID of IAM role for authentication | ||
secretKey: "your-aws-secret-key" # AWS Secret Access Key of IAM role for authentication | ||
region: "your-aws-region" # AWS region where your resources are located | ||
roleArn: "your-aws-role-arn" # ARN of the AWS role to assume for getting temporary credentials | ||
sessionName: "your-session-name" # Session name for the assumed role | ||
|
||
# Kubernetes configuration | ||
kubernetes: | ||
secretName: "your-secret-name" # Name of the Kubernetes secret to create or update with ECR credentials | ||
|
||
# ECR (Elastic Container Registry) configuration | ||
ecr: | ||
account: "your-aws-account" # AWS account ID where your ECR is located | ||
region: "your-ecr-region" # AWS region of your ECR | ||
|
||
# CronJob configuration | ||
cronjob: | ||
schedule: "0 */12 * * *" # Cron schedule for the job to run (every 12 hours) | ||
|
||
# ServiceAccount configuration | ||
serviceAccount: | ||
name: "renew-ecr-k8s-creds-sa" # Name of the ServiceAccount to create or use |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.