diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 38b8b209..db2723dc 100755 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -66,8 +66,19 @@ jobs: - name: Check out code into the Go module directory uses: actions/checkout@v2 - - name: Create eks-charts PR - run: make ekscharts-sync-release + - name: Sync Helm Chart Catalog information + run: make sync-catalog-information-for-helm-chart + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_SESSION_TOKEN: ${{ secrets.AWS_SESSION_TOKEN }} + + - name: Sync Helm Chart to ECR Public + run: make push-helm-chart + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_SESSION_TOKEN: ${{ secrets.AWS_SESSION_TOKEN }} - name: Sync Readme to ECR Public run: make sync-readme-to-ecr-public diff --git a/Makefile b/Makefile index bff5cbd9..8f928866 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,12 @@ VERSION = $(shell git describe --tags --always --dirty) LATEST_RELEASE_TAG=$(shell git describe --tags --abbrev=0) +LATEST_COMMIT_HASH=$(shell git rev-parse HEAD) +LATEST_COMMIT_CHART_VERSION=$(shell git --no-pager show ${LATEST_COMMIT_HASH}:config/helm/aws-node-termination-handler/Chart.yaml | grep 'version:' | cut -d' ' -f2 | tr -d '[:space:]') PREVIOUS_RELEASE_TAG=$(shell git describe --abbrev=0 --tags `git rev-list --tags --skip=1 --max-count=1`) REPO_FULL_NAME=aws/aws-node-termination-handler ECR_REGISTRY ?= public.ecr.aws/aws-ec2 ECR_REPO ?= ${ECR_REGISTRY}/aws-node-termination-handler +ECR_REPO_CHART ?= aws-node-termination-handler IMG ?= amazon/aws-node-termination-handler IMG_TAG ?= ${VERSION} IMG_W_TAG = ${IMG}:${IMG_TAG} @@ -56,9 +59,16 @@ push-docker-images-windows: @ECR_REGISTRY=${ECR_REGISTRY} ${MAKEFILE_PATH}/scripts/ecr-public-login ${MAKEFILE_PATH}/scripts/push-docker-images -p ${SUPPORTED_PLATFORMS_WINDOWS} -r ${ECR_REPO} -v ${VERSION} -m +push-helm-chart: + @ECR_REGISTRY=${ECR_REGISTRY} ${MAKEFILE_PATH}/scripts/helm-login + ${MAKEFILE_PATH}/scripts/push-helm-chart -r ${ECR_REPO_CHART} -v ${LATEST_COMMIT_CHART_VERSION} -h ${ECR_REGISTRY} + version: @echo ${VERSION} +chart-version: + @echo ${LATEST_COMMIT_CHART_VERSION} + latest-release-tag: @echo ${LATEST_RELEASE_TAG} @@ -98,16 +108,13 @@ license-test: $(GOLICENSES) go-linter: golangci-lint run -helm-sync-test: - ${MAKEFILE_PATH}/test/helm-sync-test/run-helm-sync-test - helm-version-sync-test: ${MAKEFILE_PATH}/test/helm-sync-test/run-helm-version-sync-test helm-lint: ${MAKEFILE_PATH}/test/helm/helm-lint -helm-validate-eks-versions: +helm-validate-chart-versions: ${MAKEFILE_PATH}/test/helm/validate-chart-versions build-binaries: @@ -129,11 +136,9 @@ sync-readme-to-ecr-public: @ECR_REGISTRY=${ECR_REGISTRY} ${MAKEFILE_PATH}/scripts/ecr-public-login ${MAKEFILE_PATH}/scripts/sync-readme-to-ecr-public -ekscharts-sync: - ${MAKEFILE_PATH}/scripts/sync-to-aws-eks-charts -b ${BINARY_NAME} -r ${REPO_FULL_NAME} - -ekscharts-sync-release: - ${MAKEFILE_PATH}/scripts/sync-to-aws-eks-charts -b ${BINARY_NAME} -r ${REPO_FULL_NAME} -n +sync-catalog-information-for-helm-chart: + @ECR_REGISTRY=${ECR_REGISTRY} ${MAKEFILE_PATH}/scripts/helm-login + ${MAKEFILE_PATH}/scripts/sync-catalog-information-for-helm-chart unit-test: go test -bench=. ${MAKEFILE_PATH}/... -v -coverprofile=coverage.txt -covermode=atomic -outputdir=${BUILD_DIR_PATH} @@ -149,7 +154,7 @@ spellcheck: build: compile -helm-tests: helm-version-sync-test helm-lint helm-validate-eks-versions +helm-tests: helm-version-sync-test helm-lint helm-validate-chart-versions eks-cluster-test: ${MAKEFILE_PATH}/test/eks-cluster-test/run-test @@ -158,7 +163,7 @@ release: build-binaries build-docker-images push-docker-images generate-k8s-yaml release-windows: build-binaries-windows build-docker-images-windows push-docker-images-windows upload-resources-to-github-windows -test: spellcheck shellcheck unit-test e2e-test compatibility-test license-test go-linter helm-sync-test helm-version-sync-test helm-lint +test: spellcheck shellcheck unit-test e2e-test compatibility-test license-test go-linter helm-version-sync-test helm-lint help: @grep -E '^[a-zA-Z_-]+:.*$$' $(MAKEFILE_LIST) | sort @@ -173,19 +178,19 @@ create-local-release-tag-minor: create-local-release-tag-patch: ${MAKEFILE_PATH}/scripts/create-local-tag-for-release -p -create-release-prep-pr: +create-release-pr: ${MAKEFILE_PATH}/scripts/prepare-for-release -create-release-prep-pr-draft: +create-release-pr-draft: ${MAKEFILE_PATH}/scripts/prepare-for-release -d -release-prep-major: create-local-release-tag-major create-release-prep-pr +release-prep-major: create-local-release-tag-major create-release-pr -release-prep-minor: create-local-release-tag-minor create-release-prep-pr +release-prep-minor: create-local-release-tag-minor create-release-pr -release-prep-patch: create-local-release-tag-patch create-release-prep-pr +release-prep-patch: create-local-release-tag-patch create-release-pr release-prep-custom: # Run make NEW_VERSION=v1.2.3 release-prep-custom to prep for a custom release version ifdef NEW_VERSION - $(shell echo "${MAKEFILE_PATH}/scripts/create-local-tag-for-release -v $(NEW_VERSION) && echo && make create-release-prep-pr") + $(shell echo "${MAKEFILE_PATH}/scripts/create-local-tag-for-release -v $(NEW_VERSION) && echo && make create-release-pr") endif diff --git a/README.md b/README.md index 827dc354..60ea5715 100644 --- a/README.md +++ b/README.md @@ -123,22 +123,25 @@ For a full list of releases and associated artifacts see our [releases page](htt #### Helm -The easiest way to configure the various options of the termination handler is via [helm](https://helm.sh/). The chart for this project is hosted in the [eks-charts](https://github.com/aws/eks-charts) repository. +The easiest way to configure the various options of the termination handler is via [helm](https://helm.sh/). The chart for this project is hosted in [helm/aws-node-termination-handler](https://gallery.ecr.aws/aws-ec2/helm/aws-node-termination-handler) -To get started you need to add the eks-charts repo to helm +To get started you need to authenticate your helm client ``` -helm repo add eks https://aws.github.io/eks-charts +aws ecr-public get-login-password \ + --region us-east-1 | helm registry login \ + --username AWS \ + --password-stdin public.ecr.aws ``` -Once that is complete you can install the termination handler. We've provided some sample setup options below. +Once that is complete you can install the termination handler. We've provided some sample setup options below. Make sure to replace CHART_VERSION with the version you want to install. Zero Config: ```sh helm upgrade --install aws-node-termination-handler \ --namespace kube-system \ - eks/aws-node-termination-handler + oci://public.ecr.aws/aws-ec2/helm/aws-node-termination-handler --version $CHART_VERSION ``` Enabling Features: @@ -149,7 +152,7 @@ helm upgrade --install aws-node-termination-handler \ --set enableSpotInterruptionDraining="true" \ --set enableRebalanceMonitoring="true" \ --set enableScheduledEventDraining="false" \ - eks/aws-node-termination-handler + oci://public.ecr.aws/aws-ec2/helm/aws-node-termination-handler --version $CHART_VERSION ``` The `enable*` configuration flags above enable or disable IMDS monitoring paths. @@ -160,7 +163,7 @@ Running Only On Specific Nodes: helm upgrade --install aws-node-termination-handler \ --namespace kube-system \ --set nodeSelector.lifecycle=spot \ - eks/aws-node-termination-handler + oci://public.ecr.aws/aws-ec2/helm/aws-node-termination-handler --version $CHART_VERSION ``` Webhook Configuration: @@ -169,7 +172,7 @@ Webhook Configuration: helm upgrade --install aws-node-termination-handler \ --namespace kube-system \ --set webhookURL=https://hooks.slack.com/services/YOUR/SLACK/URL \ - eks/aws-node-termination-handler + oci://public.ecr.aws/aws-ec2/helm/aws-node-termination-handler --version $CHART_VERSION ``` Alternatively, pass Webhook URL as a Secret: @@ -183,10 +186,10 @@ kubectl create secret -n kube-system generic webhooksecret --from-literal=$WEBHO helm upgrade --install aws-node-termination-handler \ --namespace kube-system \ --set webhookURLSecretName=webhooksecret \ - eks/aws-node-termination-handler + oci://public.ecr.aws/aws-ec2/helm/aws-node-termination-handler --version $CHART_VERSION ``` -For a full list of configuration options see our [Helm readme](https://github.com/aws/eks-charts/tree/master/stable/aws-node-termination-handler). +For a full list of configuration options see our [Helm readme](https://github.com/aws/aws-node-termination-handler/blob/v1.19.0/config/helm/aws-node-termination-handler#readme). @@ -391,15 +394,18 @@ When using Kubernetes [Pod Security Admission](https://kubernetes.io/docs/concep #### Helm -The easiest and most commonly used method to configure the termination handler is via [helm](https://helm.sh/). The chart for this project is hosted in the [eks-charts](https://github.com/aws/eks-charts) repository. +The easiest way to configure the various options of the termination handler is via [helm](https://helm.sh/). The chart for this project is hosted in [helm/aws-node-termination-handler](https://gallery.ecr.aws/aws-ec2/helm/aws-node-termination-handler) -To get started you need to add the eks-charts repo to helm +To get started you need to authenticate your helm client ``` -helm repo add eks https://aws.github.io/eks-charts +aws ecr-public get-login-password \ + --region us-east-1 | helm registry login \ + --username AWS \ + --password-stdin public.ecr.aws ``` -Once that is complete you can install the termination handler. We've provided some sample setup options below. +Once that is complete you can install the termination handler. We've provided some sample setup options below. Make sure to replace CHART_VERSION with the version you want to install. Minimal Config: @@ -408,7 +414,7 @@ helm upgrade --install aws-node-termination-handler \ --namespace kube-system \ --set enableSqsTerminationDraining=true \ --set queueURL=https://sqs.us-east-1.amazonaws.com/0123456789/my-term-queue \ - eks/aws-node-termination-handler + oci://public.ecr.aws/aws-ec2/helm/aws-node-termination-handler --version $CHART_VERSION ``` Webhook Configuration: @@ -419,7 +425,7 @@ helm upgrade --install aws-node-termination-handler \ --set enableSqsTerminationDraining=true \ --set queueURL=https://sqs.us-east-1.amazonaws.com/0123456789/my-term-queue \ --set webhookURL=https://hooks.slack.com/services/YOUR/SLACK/URL \ - eks/aws-node-termination-handler + oci://public.ecr.aws/aws-ec2/helm/aws-node-termination-handler --version $CHART_VERSION ``` Alternatively, pass Webhook URL as a Secret: @@ -435,10 +441,10 @@ helm upgrade --install aws-node-termination-handler \ --set enableSqsTerminationDraining=true \ --set queueURL=https://sqs.us-east-1.amazonaws.com/0123456789/my-term-queue \ --set webhookURLSecretName=webhooksecret \ - eks/aws-node-termination-handler + oci://public.ecr.aws/aws-ec2/helm/aws-node-termination-handler --version $CHART_VERSION ``` -For a full list of configuration options see our [Helm readme](https://github.com/aws/eks-charts/tree/master/stable/aws-node-termination-handler). +For a full list of configuration options see our [Helm readme](https://github.com/aws/aws-node-termination-handler/blob/v1.19.0/config/helm/aws-node-termination-handler#readme). #### Kubectl Apply diff --git a/config/helm/aws-node-termination-handler/Chart.yaml b/config/helm/aws-node-termination-handler/Chart.yaml index b486f647..d5976c37 100644 --- a/config/helm/aws-node-termination-handler/Chart.yaml +++ b/config/helm/aws-node-termination-handler/Chart.yaml @@ -11,11 +11,10 @@ keywords: - ec2 - node-termination - spot -home: https://github.com/aws/eks-charts +home: https://github.com/aws/aws-node-termination-handler/ icon: https://raw.githubusercontent.com/aws/eks-charts/master/docs/logo/aws.png sources: - https://github.com/aws/aws-node-termination-handler/ - - https://github.com/aws/eks-charts/ maintainers: - name: Brandon Wagner url: https://github.com/bwagner5 diff --git a/config/helm/aws-node-termination-handler/README.md b/config/helm/aws-node-termination-handler/README.md index 11491272..e37f8288 100644 --- a/config/helm/aws-node-termination-handler/README.md +++ b/config/helm/aws-node-termination-handler/README.md @@ -8,22 +8,24 @@ AWS Node Termination Handler Helm chart for Kubernetes. For more information on ## Installing the Chart -Before you can install the chart you will need to add the `aws` repo to [Helm](https://helm.sh/). - +Before you can install the chart you will need to authenticate your Helm client. ```shell -helm repo add eks https://aws.github.io/eks-charts/ +aws ecr-public get-login-password \ + --region us-east-1 | helm registry login \ + --username AWS \ + --password-stdin public.ecr.aws ``` -After you've installed the repo you can install the chart, the following command will install the chart with the release name `aws-node-termination-handler` and the default configuration to the `kube-system` namespace. +Once the helm registry login succeeds, use the following command to install the chart with the release name `aws-node-termination-handler` and the default configuration to the `kube-system` namespace. In the below command, add the CHART_VERSION that you want to install. ```shell -helm upgrade --install --namespace kube-system aws-node-termination-handler eks/aws-node-termination-handler +helm upgrade --install --namespace kube-system aws-node-termination-handler oci://public.ecr.aws/aws-ec2/helm/aws-node-termination-handler --version $CHART_VERSION ``` To install the chart on an EKS cluster where the AWS Node Termination Handler is already installed, you can run the following command. ```shell -helm upgrade --install --namespace kube-system aws-node-termination-handler eks/aws-node-termination-handler --recreate-pods --force +helm upgrade --install --namespace kube-system aws-node-termination-handler oci://public.ecr.aws/aws-ec2/helm/aws-node-termination-handler --version $CHART_VERSION --recreate-pods --force ``` If you receive an error similar to the one below simply rerun the above command. @@ -33,7 +35,7 @@ If you receive an error similar to the one below simply rerun the above command. To uninstall the `aws-node-termination-handler` chart installation from the `kube-system` namespace run the following command. ```shell -helm delete --namespace kube-system aws-node-termination-handler +helm uninstall --namespace kube-system aws-node-termination-handler ``` ## Configuration diff --git a/pkg/ec2metadata/ec2metadata.go b/pkg/ec2metadata/ec2metadata.go index 4c67c89d..f7f5ade8 100644 --- a/pkg/ec2metadata/ec2metadata.go +++ b/pkg/ec2metadata/ec2metadata.go @@ -16,7 +16,7 @@ package ec2metadata import ( "encoding/json" "fmt" - "io/ioutil" + "io" "math/rand" "net/http" "strconv" @@ -202,7 +202,7 @@ func (e *Service) GetMetadataInfo(path string) (info string, err error) { } if resp != nil { defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", fmt.Errorf("Unable to parse http response. Status code: %d. %w", resp.StatusCode, err) } @@ -284,7 +284,7 @@ func (e *Service) getV2Token() (string, int, error) { if resp.StatusCode < 200 || resp.StatusCode >= 300 { return "", -1, fmt.Errorf("Received an http status code %d", resp.StatusCode) } - token, err := ioutil.ReadAll(resp.Body) + token, err := io.ReadAll(resp.Body) if err != nil { return "", -1, fmt.Errorf("Unable to read token response from IMDSv2: %w", err) } diff --git a/pkg/ec2metadata/ec2metadata_internal_test.go b/pkg/ec2metadata/ec2metadata_internal_test.go index e03c51a5..0e3999e4 100644 --- a/pkg/ec2metadata/ec2metadata_internal_test.go +++ b/pkg/ec2metadata/ec2metadata_internal_test.go @@ -16,7 +16,7 @@ package ec2metadata import ( "bytes" "errors" - "io/ioutil" + "io" "net/http" "net/http/httptest" "strconv" @@ -35,7 +35,7 @@ func TestRetry(t *testing.T) { requestCount++ return &http.Response{ StatusCode: 400, - Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)), + Body: io.NopCloser(bytes.NewBufferString(`OK`)), Header: make(http.Header), }, errors.New(errorMsg) } diff --git a/pkg/ec2metadata/ec2metadata_test.go b/pkg/ec2metadata/ec2metadata_test.go index fc0aaa8a..6fe58301 100644 --- a/pkg/ec2metadata/ec2metadata_test.go +++ b/pkg/ec2metadata/ec2metadata_test.go @@ -15,7 +15,7 @@ package ec2metadata_test import ( "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "testing" @@ -46,7 +46,7 @@ func TestRequestV1(t *testing.T) { defer resp.Body.Close() h.Equals(t, http.StatusOK, resp.StatusCode) - responseData, err := ioutil.ReadAll(resp.Body) + responseData, err := io.ReadAll(resp.Body) if err != nil { t.Error("Unable to parse response.") } @@ -80,7 +80,7 @@ func TestRequestV2(t *testing.T) { defer resp.Body.Close() h.Equals(t, http.StatusOK, resp.StatusCode) - responseData, err := ioutil.ReadAll(resp.Body) + responseData, err := io.ReadAll(resp.Body) if err != nil { t.Error("Unable to parse response.") } diff --git a/pkg/uptime/common.go b/pkg/uptime/common.go index e67f13c2..b29e59da 100644 --- a/pkg/uptime/common.go +++ b/pkg/uptime/common.go @@ -15,7 +15,7 @@ package uptime import ( "fmt" - "io/ioutil" + "os" "strconv" "strings" ) @@ -26,7 +26,7 @@ type UptimeFuncType func() (int64, error) // UptimeFromFile reads system uptime information from filepath and returns // the number of seconds since last system boot. func UptimeFromFile(filepath string) (int64, error) { - data, err := ioutil.ReadFile(filepath) + data, err := os.ReadFile(filepath) if err != nil { return 0, fmt.Errorf("Not able to read %s: %w", filepath, err) } diff --git a/pkg/uptime/common_test.go b/pkg/uptime/common_test.go index 7b694574..23fad24d 100644 --- a/pkg/uptime/common_test.go +++ b/pkg/uptime/common_test.go @@ -14,7 +14,6 @@ package uptime import ( - "io/ioutil" "os" "testing" @@ -25,7 +24,7 @@ const testFile = "test.out" func TestUptimeFromFileSuccess(t *testing.T) { d1 := []byte("350735.47 234388.90") - err := ioutil.WriteFile(testFile, d1, 0644) + err := os.WriteFile(testFile, d1, 0644) h.Ok(t, err) value, err := UptimeFromFile(testFile) @@ -41,7 +40,7 @@ func TestUptimeFromFileReadFail(t *testing.T) { func TestUptimeFromFileBadData(t *testing.T) { d1 := []byte("Something not time") - err := ioutil.WriteFile(testFile, d1, 0644) + err := os.WriteFile(testFile, d1, 0644) h.Ok(t, err) _, err = UptimeFromFile(testFile) diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go index 760d78c4..f3446f81 100644 --- a/pkg/webhook/webhook.go +++ b/pkg/webhook/webhook.go @@ -17,9 +17,9 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "net/http" "net/url" + "os" "text/template" "time" @@ -41,7 +41,7 @@ func Post(additionalInfo ec2metadata.NodeMetadata, event *monitor.InterruptionEv var webhookTemplateContent string if nthConfig.WebhookTemplateFile != "" { - content, err := ioutil.ReadFile(nthConfig.WebhookTemplateFile) + content, err := os.ReadFile(nthConfig.WebhookTemplateFile) if err != nil { log.Err(err). Str("webhook_template_file", nthConfig.WebhookTemplateFile). @@ -131,7 +131,7 @@ func ValidateWebhookConfig(nthConfig config.Config) error { var webhookTemplateContent string if nthConfig.WebhookTemplateFile != "" { - content, err := ioutil.ReadFile(nthConfig.WebhookTemplateFile) + content, err := os.ReadFile(nthConfig.WebhookTemplateFile) if err != nil { return fmt.Errorf("Webhook Error: Could not read template file %w", err) } diff --git a/pkg/webhook/webhook_test.go b/pkg/webhook/webhook_test.go index c24e729f..ecd073dd 100644 --- a/pkg/webhook/webhook_test.go +++ b/pkg/webhook/webhook_test.go @@ -17,7 +17,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "testing" @@ -88,7 +88,7 @@ func TestPostSuccess(t *testing.T) { h.Equals(t, req.Header.Get("Content-type"), headerMap["Content-type"]) // Test request body - requestBody, err := ioutil.ReadAll(req.Body) + requestBody, err := io.ReadAll(req.Body) if err != nil { t.Error("Unable to read request body.") } diff --git a/scripts/ecr-template-for-helm-chart.json b/scripts/ecr-template-for-helm-chart.json new file mode 100644 index 00000000..f604538c --- /dev/null +++ b/scripts/ecr-template-for-helm-chart.json @@ -0,0 +1,5 @@ +{ + "description": "Helm Charts for AWS Node Termination Handler", + "aboutText": "# Helm Chart for Node Termination Handler\n\n AWS NTH gracefully handles EC2 instance shutdown within Kubernetes.\n\nThis repository contains helm-charts for Node Termination Handler.\n\nFor more information on this project, see the project repo at [AWS Node Termination Handler](https://github.com/aws/aws-node-termination-handler)", + "usageText": "# We can install AWS NTH using the helm chart from this repository.\n\nWe need to authenticate our helm client to ECR registry and install NTH chart using helm chart URI, detailed information on how to install helm chart can be found here [HelmChart ReadMe](https://github.com/aws/aws-node-termination-handler/tree/main/config/helm/aws-node-termination-handler#readme)" +} \ No newline at end of file diff --git a/scripts/helm-login b/scripts/helm-login new file mode 100755 index 00000000..7050ee05 --- /dev/null +++ b/scripts/helm-login @@ -0,0 +1,20 @@ +#!/bin/bash +set -euo pipefail + +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +BUILD_DIR=$SCRIPTPATH/../build/ +export PATH="${BUILD_DIR}:${PATH}" + +if [[ -z "${ECR_REGISTRY}" ]]; then + echo "The env var ECR_REGISTRY must be set" + exit 1 +fi + +function exit_and_fail() { + echo "❌ Failed to login to ECR Public Repo!" +} + +trap exit_and_fail INT TERM ERR + +export HELM_EXPERIMENTAL_OCI=1 +helm registry login --username AWS --password="$(aws ecr-public get-login-password --region us-east-1)" "${ECR_REGISTRY}" \ No newline at end of file diff --git a/scripts/prepare-for-release b/scripts/prepare-for-release index 4673dc2c..8eb93d83 100755 --- a/scripts/prepare-for-release +++ b/scripts/prepare-for-release @@ -55,7 +55,6 @@ HELP=$(cat << 'EOM' -d create a draft pr -r target repo full name for the pr (default: aws/aws-node-termination-handler) -h help - Examples: prepare-for-release -d update release version in repo and create a draft pr against aws/aws-node-termination-handler prepare-for-release -r username/aws-node-termination-handler update release version in repo and create a pr against username/aws-node-termination-handler diff --git a/scripts/push-helm-chart b/scripts/push-helm-chart new file mode 100755 index 00000000..ca4c6414 --- /dev/null +++ b/scripts/push-helm-chart @@ -0,0 +1,51 @@ +#!/bin/bash +set -euo pipefail + +SCRIPTPATH="$( cd "$(dirname "$0")";pwd -P )" + +REPO_ROOT_PATH=$SCRIPTPATH/../ +MAKE_FILE_PATH=$REPO_ROOT_PATH/Makefile +CHART_VERSION=$(make -s -f $MAKE_FILE_PATH chart-version) +HELM_CHART_PATH=$REPO_ROOT_PATH/config/helm/aws-node-termination-handler + +USAGE=$(cat << 'EOM' + Usage: push-helm-chart + Pushes helm charts + Optional: + -h HELM CHART REGISTRY: set the helm chart registry + -v CHART VERSION: The chart version [DEFAULT: output of `make chart-version`] + -r HELM CHART REPOSITORY: Set the helm chart repository +EOM +) + +# Process our input arguments +while getopts "r:v:h:" opt; do + case ${opt} in + r ) # Helm Chart Repository + HELM_CHART_REPOSITORY="$OPTARG" + ;; + v ) # Image Version + CHART_VERSION="$OPTARG" + ;; + h ) # Helm Chart Registry + ECR_REGISTRY="$OPTARG" + ;; + \? ) + echo "$USAGE" 1>&2 + exit + ;; + esac +done + +CHART_EXISTS=$(aws ecr-public describe-images --repository-name "helm/$HELM_CHART_REPOSITORY" --region us-east-1 --query "imageDetails[?contains(imageTags, '$CHART_VERSION')].imageTags[]" --output text) + +if [[ -n "$CHART_EXISTS" ]]; then + echo "chart with version $CHART_VERSION already exists in the repository, skipping pushing of chart..." + exit 0 +fi + +echo "chart with version $CHART_VERSION not found in repository, pushing new chart..." +#Package the chart +helm package $HELM_CHART_PATH --destination $REPO_ROOT_PATH/build +#Pushing helm chart +helm push $REPO_ROOT_PATH/build/$HELM_CHART_REPOSITORY-$CHART_VERSION.tgz oci://$ECR_REGISTRY/helm diff --git a/scripts/sync-catalog-information-for-helm-chart b/scripts/sync-catalog-information-for-helm-chart new file mode 100644 index 00000000..55be89e0 --- /dev/null +++ b/scripts/sync-catalog-information-for-helm-chart @@ -0,0 +1,16 @@ +#!/bin/bash +set -euo pipefail + +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +REPO_NAME="helm/aws-node-termination-handler" +REPO_ROOT_PATH=$SCRIPTPATH/../ +TEMPLATE_PATH=$REPO_ROOT_PATH/scripts/ecr-template-for-helm-chart.json +CATALOG_DATA=$(cat "$TEMPLATE_PATH") + +if aws ecr-public describe-repositories --region us-east-1 --repository-names "$REPO_NAME" > /dev/null 2>&1; then + echo "The repository $REPO_NAME exists, update it with template..." + aws ecr-public put-repository-catalog-data --region us-east-1 --repository-name "$REPO_NAME" --catalog-data "$CATALOG_DATA" +else + echo "The repository $REPO_NAME does not exist, create it with template..." + aws ecr-public create-repository --region us-east-1 --repository-name "$REPO_NAME" --catalog-data "$CATALOG_DATA" +fi \ No newline at end of file diff --git a/scripts/sync-to-aws-eks-charts b/scripts/sync-to-aws-eks-charts deleted file mode 100755 index 128c3fdb..00000000 --- a/scripts/sync-to-aws-eks-charts +++ /dev/null @@ -1,162 +0,0 @@ -#!/bin/bash -set -euo pipefail - -SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" -BUILD_DIR="${SCRIPTPATH}/../build" - -CHARTS_REPO="aws/eks-charts" -CHARTS_REPO_NAME=$(echo ${CHARTS_REPO} | cut -d'/' -f2) -REPO=$(make -s -f "${SCRIPTPATH}/../Makefile" repo-full-name) -NTH_HELM_CHART_DIR="${SCRIPTPATH}/../config/helm/aws-node-termination-handler" -PR_ID=$(uuidgen | cut -d '-' -f1) - -SYNC_DIR="${BUILD_DIR}/eks-charts-sync" -FORK_DIR="${SYNC_DIR}/${CHARTS_REPO_NAME}" - -BINARY_BASE="" -INCLUDE_NOTES=0 - -GH_CLI_VERSION="0.10.1" -GH_CLI_CONFIG_PATH="${HOME}/.config/gh/config.yml" -KERNEL=$(uname -s | tr '[:upper:]' '[:lower:]') -OS="${KERNEL}" -if [[ "${KERNEL}" == "darwin" ]]; then - OS="macOS" -fi - -VERSION=$(make -s -f "${SCRIPTPATH}/../Makefile" version) - -USAGE=$(cat << EOM - Usage: sync-to-aws-eks-charts -r - Syncs Helm chart to aws/eks-charts - - Example: sync-to-aws-eks-charts -r "aws/aws-node-termination-handler" - Required: - -b Binary basename (i.e. -b "node-termination-handler") - - Optional: - -r Github repo to sync to in the form of "org/name" (i.e. -r "aws/aws-node-termination-handler") [DEFAULT: output of \`make repo-full-name\`] - -n Include application release notes in the sync PR -EOM -) - -# Process our input arguments -while getopts "b:r:n" opt; do - case ${opt} in - r ) # Github repo - REPO="$OPTARG" - ;; - b ) # binary basename - BINARY_BASE="$OPTARG" - ;; - n ) # Include release notes - INCLUDE_NOTES=1 - ;; - \? ) - echo "$USAGE" 1>&2 - exit - ;; - esac -done - - -if [[ -z "${REPO}" ]]; then - echo "Repo (-r) must be specified if no \"make repo-full-name\" target exists" -fi - -if [[ -z $(command -v gh) ]] || [[ ! $(gh --version) =~ $GH_CLI_VERSION ]]; then - mkdir -p "${BUILD_DIR}"/gh - curl -Lo "${BUILD_DIR}"/gh/gh.tar.gz "https://github.com/cli/cli/releases/download/v${GH_CLI_VERSION}/gh_${GH_CLI_VERSION}_${OS}_amd64.tar.gz" - tar -C "${BUILD_DIR}"/gh -xvf "${BUILD_DIR}/gh/gh.tar.gz" - export PATH="${BUILD_DIR}/gh/gh_${GH_CLI_VERSION}_${OS}_amd64/bin:$PATH" - if [[ ! $(gh --version) =~ $GH_CLI_VERSION ]]; then - echo "❌ Failed install of github cli" - exit 4 - fi -fi - -function restore_gh_config() { - mv -f "${GH_CLI_CONFIG_PATH}.bkup" "${GH_CLI_CONFIG_PATH}" || : -} - -if [[ -n $(env | grep GITHUB_TOKEN) ]] && [[ -n "${GITHUB_TOKEN}" ]]; then - trap restore_gh_config EXIT INT TERM ERR - mkdir -p "${HOME}/.config/gh" - cp -f "${GH_CLI_CONFIG_PATH}" "${GH_CLI_CONFIG_PATH}.bkup" || : - cat << EOF > "${GH_CLI_CONFIG_PATH}" -hosts: - github.com: - oauth_token: ${GITHUB_TOKEN} - user: ${GITHUB_USERNAME} -EOF -fi - -function fail() { - echo "❌ EKS charts sync failed" - exit 5 -} - -trap fail ERR TERM INT - -rm -rf "${SYNC_DIR}" -mkdir -p "${SYNC_DIR}" - -cd "${SYNC_DIR}" -gh repo fork $CHARTS_REPO --clone --remote -cd "${FORK_DIR}" -git remote set-url origin https://"${GITHUB_USERNAME}":"${GITHUB_TOKEN}"@github.com/"${GITHUB_USERNAME}"/"${CHARTS_REPO_NAME}".git -DEFAULT_BRANCH=$(git rev-parse --abbrev-ref HEAD | tr -d '\n') - - -if diff -x ".*" -r "$NTH_HELM_CHART_DIR/" "${FORK_DIR}/stable/aws-node-termination-handler/" &> /dev/null ; then - echo " ✅ Charts already in sync; no updates needed" - exit -else - echo "📊 Charts are NOT in sync proceeding with PR" -fi - -git config user.name "ec2-bot 🤖" -git config user.email "ec2-bot@users.noreply.github.com" - -# Sync the fork -git pull upstream "${DEFAULT_BRANCH}" -git push -u origin "${DEFAULT_BRANCH}" - -FORK_RELEASE_BRANCH="${BINARY_BASE}-${VERSION}-${PR_ID}" -git checkout -b "${FORK_RELEASE_BRANCH}" upstream/"${DEFAULT_BRANCH}" - -rm -rf "${FORK_DIR}"/stable/aws-node-termination-handler/ -cp -r "$NTH_HELM_CHART_DIR/" "${FORK_DIR}/stable/aws-node-termination-handler/" - -git add --all -git commit -m "${BINARY_BASE}: ${VERSION}" - -PR_BODY=$(cat << EOM -## ${BINARY_BASE} ${VERSION} Automated Chart Sync! 🤖🤖 -EOM -) - -if [[ "${INCLUDE_NOTES}" -eq 1 ]]; then - RELEASE_ID=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ - https://api.github.com/repos/"${REPO}"/releases | \ - jq --arg VERSION "$VERSION" '.[] | select(.tag_name==$VERSION) | .id') - - RELEASE_NOTES=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \ - https://api.github.com/repos/"${REPO}"/releases/"${RELEASE_ID}" | \ - jq -r '.body') - - PR_BODY=$(cat << EOM - ## ${BINARY_BASE} ${VERSION} Automated Chart Sync! 🤖🤖 - - ### Release Notes 📝: - - ${RELEASE_NOTES} -EOM -) -fi - - git push -u origin "${FORK_RELEASE_BRANCH}" - gh pr create --title "🥳 ${BINARY_BASE} ${VERSION} Automated Release! 🥑" \ - --body "${PR_BODY}" - -echo "✅ EKS charts sync complete" \ No newline at end of file diff --git a/test/helm-sync-test/run-helm-sync-test b/test/helm-sync-test/run-helm-sync-test deleted file mode 100755 index 7eff19ca..00000000 --- a/test/helm-sync-test/run-helm-sync-test +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash -set -euo pipefail - -SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" -TEST_ID=$(uuidgen | cut -d'-' -f1 | tr '[:upper:]' '[:lower:]') -TMP_DIR=$SCRIPTPATH/../../build/helm-sync-$TEST_ID -GITHUB_CURL_AUTH="" -if [[ ! -z $GITHUB_TOKEN ]]; then - GITHUB_CURL_AUTH="-u $GITHUB_TOKEN:x-oauth-basic" -fi - -mkdir -p $TMP_DIR -cd $TMP_DIR - -prs=$(curl -s $GITHUB_CURL_AUTH https://api.github.com/repos/aws/eks-charts/pulls) - -branches=() -while IFS='' read -r line; do - branches+=("$line"); -done < <(echo $prs | jq -r '.[].head.ref') - -clone_urls=() -while IFS='' read -r line; do - clone_urls+=("$line"); -done < <(echo $prs | jq -r '.[].head.repo.clone_url') - -branches+=("master") -clone_urls+=("https://github.com/aws/eks-charts.git") - -for i in "${!branches[@]}"; do - git clone --branch "${branches[$i]}" "${clone_urls[$i]}" $TMP_DIR/pr$i || continue - if [[ ! -d $TMP_DIR/pr$i/stable/aws-node-termination-handler ]]; then - continue - fi - diff -r $TMP_DIR/pr$i/stable/aws-node-termination-handler $SCRIPTPATH/../../config/helm/aws-node-termination-handler | tee $TMP_DIR/chart-diff-pr$i.txt || continue - - if [[ $i -eq 0 ]]; then - echo "✅ AWS Node Termination Handler helm chart is in-sync with the eks-charts repo!" - else - echo "✅ AWS Node Termination Handler helm chart is in-sync with a PR (${clone_urls[$i]} -branch ${branches[$i]}) in eks-charts repo!" - fi - exit 0 -done - -echo "❌ The Helm chart is NOT in-sync with the eks-charts repo. Please submit a PR to eks-charts via 'make ekscharts-sync' before merging this code." -echo "Check $TMP_DIR for more details" - -exit 1 diff --git a/test/helm/validate-chart-versions b/test/helm/validate-chart-versions index 6666c60f..a9388d22 100755 --- a/test/helm/validate-chart-versions +++ b/test/helm/validate-chart-versions @@ -2,13 +2,6 @@ set -euo pipefail NTH_HELM_DIR=config/helm/aws-node-termination-handler -CHARTS_REPO="aws/eks-charts" -CHARTS_REPO_NAME=$(echo ${CHARTS_REPO} | cut -d'/' -f2) -SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" -BUILD_DIR="${SCRIPTPATH}/../build" -SYNC_DIR="${BUILD_DIR}/eks-charts-sync" -FORK_DIR="${SYNC_DIR}/${CHARTS_REPO_NAME}" -STABLE="${FORK_DIR}/stable" function version { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; } @@ -20,21 +13,13 @@ if [[ -z $HELM_FILES_CHANGED ]]; then exit $EXIT_CODE fi -echo "📝 Helm chart changes detected! Checking for updated Chart versions since the last eks-charts release" +echo "📝 Helm chart changes detected! Checking for updated Chart versions since the last release" LATEST_COMMIT_HASH=$(git rev-parse HEAD) LATEST_COMMIT_CHART_VERSION=$(git --no-pager show "$LATEST_COMMIT_HASH":"$NTH_HELM_DIR"/Chart.yaml | grep 'version:' | xargs | cut -d' ' -f2 | tr -d '[:space:]') -rm -rf "${SYNC_DIR}" -mkdir -p "${SYNC_DIR}" - -cd "${SYNC_DIR}" -gh repo fork $CHARTS_REPO --clone --remote -cd "${FORK_DIR}" - TAG=$(git describe HEAD --tags | grep -Eo "^v[0-9]+(\.[0-9]+)*") LAST_RELEASE_HASH=$(git rev-list -1 "$TAG") -cd "${STABLE}" -LAST_RELEASE_CHART_VERSION=$(git --no-pager show "$LAST_RELEASE_HASH":stable/aws-node-termination-handler/Chart.yaml | grep 'version:' | xargs | cut -d' ' -f2 | tr -d '[:space:]') +LAST_RELEASE_CHART_VERSION=$(git --no-pager show "$LAST_RELEASE_HASH":"$NTH_HELM_DIR"/Chart.yaml | grep 'version:' | xargs | cut -d' ' -f2 | tr -d '[:space:]') if [[ $LAST_RELEASE_CHART_VERSION == "$LATEST_COMMIT_CHART_VERSION" ]]; then echo "❌ This commit's NTH Chart has the same Chart version as the latest release $LATEST_COMMIT_CHART_VERSION -- please increment Chart version in NTH" EXIT_CODE=1 @@ -42,7 +27,7 @@ elif [[ $(version $LATEST_COMMIT_CHART_VERSION) -lt $(version $LAST_RELEASE_CHAR echo "❌ This commit's NTH Chart version $LATEST_COMMIT_CHART_VERSION is BEHIND the latest release's chart version $LAST_RELEASE_CHART_VERSION -- please increment Chart version in NTH" EXIT_CODE=1 else - echo "✅ This commit's NTH Chart has a different version since the last eks-charts release ($LAST_RELEASE_CHART_VERSION -> $LATEST_COMMIT_CHART_VERSION)" + echo "✅ This commit's NTH Chart has a different version since the last release ($LAST_RELEASE_CHART_VERSION -> $LATEST_COMMIT_CHART_VERSION)" fi exit $EXIT_CODE \ No newline at end of file