|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +cd "$(dirname "$0")" |
| 4 | + |
| 5 | +CLUSTER_NAME=$1 |
| 6 | +REGION=$2 |
| 7 | +NAMESPACE=${3:-default} |
| 8 | +echo "Enabling Application Signals for EKS Cluster ${CLUSTER_NAME} in ${REGION} for namespace ${NAMESPACE}" |
| 9 | + |
| 10 | +# Check if the current context points to the new cluster in the correct region |
| 11 | +kub_config=$(kubectl config current-context) |
| 12 | +if [[ $kub_config != *"$CLUSTER_NAME"* ]] || [[ $kub_config != *"$REGION"* ]]; then |
| 13 | + echo "Your current cluster context is not set to $CLUSTER_NAME $REGION. Please switch to the correct context first before running this script" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +check_if_step_failed_and_exit() { |
| 18 | + if [ $? -ne 0 ]; then |
| 19 | + echo $1 |
| 20 | + exit 1 |
| 21 | + fi |
| 22 | +} |
| 23 | + |
| 24 | +check_if_loop_failed_and_exit() { |
| 25 | + if [ $1 -ne 0 ]; then |
| 26 | + echo $2 |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +# Check if the namespace exists |
| 32 | +kubectl get namespace $NAMESPACE > /dev/null 2>&1 |
| 33 | + |
| 34 | +# $? is a special variable that stores the exit status of the last command |
| 35 | +if [ $? -ne 0 ]; then |
| 36 | + # If namespace does not exist, create it |
| 37 | + echo "Namespace '$NAMESPACE' does not exist. Creating it..." |
| 38 | + kubectl create namespace $NAMESPACE |
| 39 | +else |
| 40 | + # If namespace exists, print a message |
| 41 | + echo "Namespace '$NAMESPACE' already exists." |
| 42 | +fi |
| 43 | + |
| 44 | +# Create service linked role in the account |
| 45 | +aws iam create-service-linked-role --aws-service-name application-signals.cloudwatch.amazonaws.com |
| 46 | + |
| 47 | +# Enable OIDC to allow IAM role authN/Z with service account |
| 48 | +eksctl utils associate-iam-oidc-provider --cluster ${CLUSTER_NAME} --region ${REGION} --approve |
| 49 | +check_if_step_failed_and_exit "There was an error enabling the OIDC, exiting" |
| 50 | + |
| 51 | +# Create Service Account with the proper IAM permissions |
| 52 | +echo "Creating ServiceAccount" |
| 53 | +eksctl create iamserviceaccount \ |
| 54 | + --name appsignals-collector \ |
| 55 | + --namespace ${NAMESPACE} \ |
| 56 | + --cluster ${CLUSTER_NAME} \ |
| 57 | + --region ${REGION} \ |
| 58 | + --attach-policy-arn arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess \ |
| 59 | + --attach-policy-arn arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy \ |
| 60 | + --attach-policy-arn arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy \ |
| 61 | + --approve \ |
| 62 | + --override-existing-serviceaccounts |
| 63 | +check_if_step_failed_and_exit "There was an error creating the ServiceAccount, exiting" |
| 64 | + |
| 65 | + |
| 66 | +# Install OpenTelemetry Operator |
| 67 | +kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.12.0/cert-manager.yaml |
| 68 | +kubectl apply -f https://github.com/open-telemetry/opentelemetry-operator/releases/latest/download/opentelemetry-operator.yaml |
0 commit comments