From c839641910b20778472cf1b31457c739e369c7a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Aufort?= Date: Wed, 23 Nov 2022 23:37:16 +0100 Subject: [PATCH] feat: add custom aws-cli Helm chart (#3) Deploy a custom AWS CLI application via Helm on Kubernetes. This custom application uses IRSA thanks to the EKS annotation set on the ServiceAccount. Add steps in linter workflow to lint aws-cli Helm chart and deactivate lint of YAML files with super linter. Update README.md to explain how to deploy the custom Helm chart onto Kubernetes. --- .github/workflows/linter.yml | 12 ++++- README.md | 21 ++++++++ helm/aws-cli/.helmignore | 23 ++++++++ helm/aws-cli/Chart.yaml | 25 +++++++++ helm/aws-cli/templates/_helpers.tpl | 62 ++++++++++++++++++++++ helm/aws-cli/templates/deployment.yaml | 52 ++++++++++++++++++ helm/aws-cli/templates/serviceaccount.yaml | 13 +++++ helm/aws-cli/values.yaml | 56 +++++++++++++++++++ 8 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 helm/aws-cli/.helmignore create mode 100644 helm/aws-cli/Chart.yaml create mode 100644 helm/aws-cli/templates/_helpers.tpl create mode 100644 helm/aws-cli/templates/deployment.yaml create mode 100644 helm/aws-cli/templates/serviceaccount.yaml create mode 100644 helm/aws-cli/values.yaml diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index b54b278..d0c2ba8 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -18,9 +18,19 @@ jobs: with: fetch-depth: 0 - - name: Lint Code Base + - name: Lint with super linter uses: github/super-linter/slim@v4 env: VALIDATE_ALL_CODEBASE: false DEFAULT_BRANCH: main GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + VALIDATE_KUBERNETES_KUBEVAL: false + VALIDATE_YAML: false + + - name: Setup Helm + uses: azure/setup-helm@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Helm lint aws-cli chart + run: helm lint --set awsAccountId=123456789123 helm/aws-cli diff --git a/README.md b/README.md index 2ef913a..85538b8 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ You need several tools to be able to interact with the infrastructure of this pr - Terragrunt - aws CLI v2 - kubectl +- helm v3 You can install those tools on your computer thanks to [tfswitch](https://github.com/warrensbox/terraform-switcher) and to [tgswitch](https://github.com/warrensbox/tgswitch). @@ -53,3 +54,23 @@ Then, you can interact with the Kubernetes cluster with `kubectl` commands: ```bash kubectl get pods -n kube-system ``` + +## How to deploy aws-cli Helm chart? + +You can install the `aws-cli` Helm chart as such (after replacing `` with your account ID): +```bash +helm install --create-namespace --namespace aws-eks-irsa --set awsAccountId= aws-cli helm/aws-cli +``` + +Then, you can check your resources were created into the EKS cluster: +```bash +kubectl get all -n aws-eks-irsa +``` + +When the `aws-cli` pod is running, connect to it and execute an AWS CLI command as such: +```bash +# Exec into the pod +kubectl exec -n aws-eks-irsa -it $(kubectl get pods -n aws-eks-irsa -o=name) -- bash +# Once inside the aws-cli pod, try to describe EC2 instances ;) +aws ec2 describe-instances --filters Name=tag:aws:eks:cluster-name,Values=aws-eks-irsa +``` diff --git a/helm/aws-cli/.helmignore b/helm/aws-cli/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/helm/aws-cli/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/helm/aws-cli/Chart.yaml b/helm/aws-cli/Chart.yaml new file mode 100644 index 0000000..f434570 --- /dev/null +++ b/helm/aws-cli/Chart.yaml @@ -0,0 +1,25 @@ +--- +apiVersion: v2 +name: aws-cli +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/helm/aws-cli/templates/_helpers.tpl b/helm/aws-cli/templates/_helpers.tpl new file mode 100644 index 0000000..f5d7422 --- /dev/null +++ b/helm/aws-cli/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "aws-cli.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "aws-cli.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "aws-cli.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "aws-cli.labels" -}} +helm.sh/chart: {{ include "aws-cli.chart" . }} +{{ include "aws-cli.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "aws-cli.selectorLabels" -}} +app.kubernetes.io/name: {{ include "aws-cli.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "aws-cli.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "aws-cli.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/helm/aws-cli/templates/deployment.yaml b/helm/aws-cli/templates/deployment.yaml new file mode 100644 index 0000000..fbac396 --- /dev/null +++ b/helm/aws-cli/templates/deployment.yaml @@ -0,0 +1,52 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "aws-cli.fullname" . }} + labels: + {{- include "aws-cli.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "aws-cli.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "aws-cli.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "aws-cli.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + command: + - "sh" + - "-c" + - "tail -f /dev/null" + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/helm/aws-cli/templates/serviceaccount.yaml b/helm/aws-cli/templates/serviceaccount.yaml new file mode 100644 index 0000000..e2b1138 --- /dev/null +++ b/helm/aws-cli/templates/serviceaccount.yaml @@ -0,0 +1,13 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "aws-cli.serviceAccountName" . }} + labels: + {{- include "aws-cli.labels" . | nindent 4 }} + annotations: + eks.amazonaws.com/role-arn: arn:aws:iam::{{ .Values.awsAccountId }}:role/irsa/aws-cli-describe-ec2-instances + {{- with .Values.serviceAccount.annotations }} + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/helm/aws-cli/values.yaml b/helm/aws-cli/values.yaml new file mode 100644 index 0000000..fd5e48a --- /dev/null +++ b/helm/aws-cli/values.yaml @@ -0,0 +1,56 @@ +# Default values for aws-cli. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +awsAccountId: + +replicaCount: 1 + +image: + repository: amazon/aws-cli + pullPolicy: IfNotPresent + tag: "2.9.0" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +nodeSelector: {} + +tolerations: [] + +affinity: {}