Skip to content

Commit

Permalink
ROX-16090: Start data plane CD on integration environment. (#1190)
Browse files Browse the repository at this point in the history
This change also splits the idp setup script to resolve some dependency loops
between it and the terraform config.
  • Loading branch information
porridge authored Aug 24, 2023
1 parent deb10a6 commit 47653e4
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 78 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/deploy-integration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Deploy Integration Env

on:
push:
branches:
- main

jobs:
call-deploy-workflow:
uses: ./.github/workflows/deploy-data-plane.yaml
secrets: inherit # pragma: allowlist secret
with:
acs_environment: integration
github_environment: integration
deploy_clusters: "acs-int-us-01"
probe_clusters: "acs-int-us-01"
8 changes: 5 additions & 3 deletions docs/development/setup-osd-cluster-idp.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## Pre-reqs

1. `ocm` installed.
1. `ocm` installed
2. Secrets `oidc_client_id` and `oidc_client_secret` set in AWS secrets manager in `us-east-1`.
3. Parameter `oidc_user_list` set by [terraform](https://github.com/stackrox/acs-fleet-manager-aws-config) in `us-east-1`.

Additionally, you will require access to the environment specific AWS account.

Expand All @@ -14,11 +16,11 @@ The following IdPs will be created:
- OIDC IdP using auth.redhat.com as backend.

Before executing the script that manages the IdP setup, you have to ensure you are logged in with OCM.
Based on the environment, you have to choose between `rhacs-managed-service-stage` or `rhacs-managed-service-prod` account.
Based on the environment, you have to choose between `rhacs-managed-service-integration`, `rhacs-managed-service-stage` or `rhacs-managed-service-prod` account.

Afterwards, you can call the script and adjust the parameters based on your needs:
```shell
./dp-terraform/osd-cluster-idp-setup.sh "stage|prod" "cluster-name"
AWS_REGION=us-east-1 AWS_SAML_ROLE=<aws_account_id>-poweruser ./dp-terraform/osd-cluster-idp-setup.sh "integration|stage|prod" "cluster-name"
```

The script will handle the following:
Expand Down
141 changes: 141 additions & 0 deletions dp-terraform/cd-robot-account-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

# shellcheck source=scripts/lib/external_config.sh
source "$SCRIPT_DIR/../scripts/lib/external_config.sh"

if [[ $# -ne 2 ]]; then
echo "Usage: $0 [environment] [cluster]" >&2
echo "Known environments: integration stage prod"
echo "Cluster typically looks like: acs-{env}-dp-01"
echo "Description:"
echo "This script will create and configure a ServiceAccount for the data plane continuous deployment."
echo "It should be safe to run this script many times, it should be idempotent."
echo
echo "Note: you need to be logged into OCM for your environment's administrator"
echo "Note: you need access to AWS account of the selected environment"
exit 2
fi

ENVIRONMENT=$1
CLUSTER_NAME=$2

export AWS_AUTH_HELPER="${AWS_AUTH_HELPER:-aws-saml}"

save_cluster_parameter() {
local key="$1"
local value="$2"
echo "Saving parameter '/cluster-${CLUSTER_NAME}/${key}' in AWS parameter store..."
chamber write "cluster-${CLUSTER_NAME}" "${key}" "${value}" --skip-unchanged
}

save_cluster_secret() {
local key="$1"
local value="$2"
echo "Saving parameter '/cluster-${CLUSTER_NAME}/${key}' in AWS Secrets Manager..."
chamber write -b secretsmanager "cluster-${CLUSTER_NAME}" "${key}" "${value}" --skip-unchanged
}

case $ENVIRONMENT in
integration)
EXPECT_OCM_ID="2QVFzUvsbMGheHhoUDjtG0tpJ08"
;;

stage)
EXPECT_OCM_ID="2ECw6PIE06TzjScQXe6QxMMt3Sa"
;;

prod)
# TODO: Fetch OCM token and log in as appropriate user as part of script.
EXPECT_OCM_ID="2BBslbGSQs5PS2HCfJKqOPcCN4r"
;;

*)
echo "Unknown environment ${ENVIRONMENT}"
exit 2
;;
esac

ACTUAL_OCM_ID=$(ocm whoami | jq -r '.id')
if [[ "${EXPECT_OCM_ID}" != "${ACTUAL_OCM_ID}" ]]; then
echo "Must be logged into rhacs-managed-service-$ENVIRONMENT account in OCM to get cluster ID"
exit 1
fi
CLUSTER_ID=$(ocm list cluster "${CLUSTER_NAME}" --no-headers --columns="ID")

init_chamber

# Retrieve the cluster token from the configured IdP interactively.
echo "Login to the cluster using the OIDC IdP and obtain a token."
ocm cluster login "${CLUSTER_NAME}" --token
# This requires users to paste the token, since the command only opens the browser but doesn't retrieve the token itself.
echo "Paste the token (it will not be echoed to the screen):"
read -r -s CLUSTER_TOKEN

# The ocm command likes to return trailing whitespace, so try and trim it:
CLUSTER_URL="$(ocm list cluster "${CLUSTER_NAME}" --no-headers --columns api.url | awk '{print $1}')"

# Use a temporary KUBECONFIG to avoid storing credentials in and changing current context in user's day-to-day kubeconfig.
KUBECONFIG="$(mktemp)"
export KUBECONFIG
trap 'rm -f "${KUBECONFIG}"' EXIT

echo "Logging into cluster ${CLUSTER_NAME}..."
oc login "${CLUSTER_URL}" --token="${CLUSTER_TOKEN}"

ROBOT_NS="acscs-dataplane-cd"
ROBOT_SA="acscs-cd-robot"
ROBOT_TOKEN_RESOURCE="robot-token"

echo "Provisioning robot account and configuring its permissions..."
# We use `apply` rather than `create` for idempotence.
oc apply -f - <<END
apiVersion: v1
kind: Namespace
metadata:
name: ${ROBOT_NS}
END
oc apply -f - <<END
apiVersion: v1
kind: ServiceAccount
metadata:
name: ${ROBOT_SA}
namespace: ${ROBOT_NS}
END
oc adm policy -n "${ROBOT_NS}" --rolebinding-name="acscs-cd-robot-admin" add-cluster-role-to-user cluster-admin -z "${ROBOT_SA}"
oc apply -n "${ROBOT_NS}" -f - <<END
apiVersion: v1
kind: Secret
metadata:
name: ${ROBOT_TOKEN_RESOURCE}
annotations:
kubernetes.io/service-account.name: "${ROBOT_SA}"
type: kubernetes.io/service-account-token
END

save_cluster_parameter "id" "$CLUSTER_ID"
save_cluster_parameter "url" "$CLUSTER_URL"

echo "Polling for token to be provisioned."
attempt=0
while true
do
attempt=$((attempt+1))
ROBOT_TOKEN="$(oc get secret "${ROBOT_TOKEN_RESOURCE}" -n "$ROBOT_NS" -o json | jq -r 'if (has("data") and (.data|has("token"))) then (.data.token|@base64d) else "" end')"
if [[ -n $ROBOT_TOKEN ]]; then
save_cluster_secret "robot_oc_token" "$ROBOT_TOKEN"
break
fi
if [[ $attempt -gt 30 ]]; then
echo "Timed out waiting for a token to be provisioned in the ${ROBOT_TOKEN_RESOURCE} secret."
exit 1
fi
sleep 1
done

echo "The following cluster parameters are currently stored in AWS Parameter Store:"
chamber list "cluster-${CLUSTER_NAME}"
echo "The following cluster parameters are currently stored in AWS Secrets Manager:"
chamber list "cluster-${CLUSTER_NAME}" -b secretsmanager
19 changes: 17 additions & 2 deletions dp-terraform/helm/rhacs-terraform/terraform_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ source "$SCRIPT_DIR/../../../scripts/lib/helm.sh"

if [[ $# -ne 2 ]]; then
echo "Usage: $0 [environment] [cluster]" >&2
echo "Known environments: stage prod"
echo "Cluster typically looks like: acs-{environment}-dp-01"
echo "Known environments: integration stage prod"
echo "Cluster typically looks like: acs-{env}-dp-01"
exit 2
fi

Expand Down Expand Up @@ -45,6 +45,21 @@ case $ENVIRONMENT in
SECURED_CLUSTER_ENABLED="true"
;;

integration)
FM_ENDPOINT="https://qj3layty4dynlnz.api.integration.openshift.com"
OBSERVABILITY_GITHUB_TAG="master"
OBSERVABILITY_OBSERVATORIUM_GATEWAY="https://observatorium-mst.api.stage.openshift.com"
OBSERVABILITY_OPERATOR_VERSION="v4.2.1"
OPERATOR_USE_UPSTREAM="false"
OPERATOR_CHANNEL="stable"
OPERATOR_VERSION="v4.1.0"
FLEETSHARD_SYNC_CPU_REQUEST="${FLEETSHARD_SYNC_CPU_REQUEST:-"200m"}"
FLEETSHARD_SYNC_MEMORY_REQUEST="${FLEETSHARD_SYNC_MEMORY_REQUEST:-"1024Mi"}"
FLEETSHARD_SYNC_CPU_LIMIT="${FLEETSHARD_SYNC_CPU_LIMIT:-"1000m"}"
FLEETSHARD_SYNC_MEMORY_LIMIT="${FLEETSHARD_SYNC_MEMORY_LIMIT:-"1024Mi"}"
SECURED_CLUSTER_ENABLED="false" # TODO(ROX-18908): enable
;;

stage)
FM_ENDPOINT="https://xtr6hh3mg6zc80v.api.stage.openshift.com"
OBSERVABILITY_GITHUB_TAG="master"
Expand Down
83 changes: 10 additions & 73 deletions dp-terraform/osd-cluster-idp-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ source "$SCRIPT_DIR/../scripts/lib/external_config.sh"

if [[ $# -ne 2 ]]; then
echo "Usage: $0 [environment] [cluster]" >&2
echo "Known environments: stage prod"
echo "Cluster typically looks like: acs-{environment}-dp-01"
echo "Known environments: integration stage prod"
echo "Cluster typically looks like: acs-{env}-dp-01"
echo "Description: This script will create identity providers for the OSD cluster:"
echo "- OIDC provider using auth.redhat.com"
echo "It will also create and configure a ServiceAccount for the data plane continuous deployment."
echo "See additional documentation in docs/development/setup-osd-cluster-idp.md"
echo
echo "It will NOT create a ServiceAccount for the data plane continuous deployment."
echo "See the cd-robot-account-setup.sh for that."
echo
echo "Note: you need to be logged into OCM for your environment's administrator"
echo "Note: you need access to AWS account of the selected environment"
exit 2
Expand All @@ -25,20 +27,6 @@ CLUSTER_NAME=$2

export AWS_AUTH_HELPER="${AWS_AUTH_HELPER:-aws-saml}"

save_cluster_parameter() {
local key="$1"
local value="$2"
echo "Saving parameter '/cluster-${CLUSTER_NAME}/${key}' in AWS parameter store..."
chamber write "cluster-${CLUSTER_NAME}" "${key}" "${value}" --skip-unchanged
}

save_cluster_secret() {
local key="$1"
local value="$2"
echo "Saving parameter '/cluster-${CLUSTER_NAME}/${key}' in AWS Secrets Manager..."
chamber write -b secretsmanager "cluster-${CLUSTER_NAME}" "${key}" "${value}" --skip-unchanged
}

export_cluster_environment() {
init_chamber
load_external_config "osd" OSD_
Expand Down Expand Up @@ -66,6 +54,10 @@ setup_oidc_provider() {
}

case $ENVIRONMENT in
integration)
EXPECT_OCM_ID="2QVFzUvsbMGheHhoUDjtG0tpJ08"
;;

stage)
EXPECT_OCM_ID="2ECw6PIE06TzjScQXe6QxMMt3Sa"
;;
Expand Down Expand Up @@ -95,7 +87,7 @@ setup_oidc_provider
echo "Login to the cluster using the OIDC IdP and obtain a token."
ocm cluster login "${CLUSTER_NAME}" --token
# This requires users to paste the token, since the command only opens the browser but doesn't retrieve the token itself.
echo "Paste the token:"
echo "Paste the token (it will not be echoed to the screen):"
read -r -s CLUSTER_TOKEN

# The ocm command likes to return trailing whitespace, so try and trim it:
Expand Down Expand Up @@ -154,58 +146,3 @@ roleRef:
kind: ClusterRole
name: cluster-monitoring-view
END

ROBOT_NS="acscs-dataplane-cd"
ROBOT_SA="acscs-cd-robot"
ROBOT_TOKEN_RESOURCE="robot-token"

echo "Provisioning robot account and configuring its permissions..."
# We use `apply` rather than `create` for idempotence.
oc apply -f - <<END
apiVersion: v1
kind: Namespace
metadata:
name: ${ROBOT_NS}
END
oc apply -f - <<END
apiVersion: v1
kind: ServiceAccount
metadata:
name: ${ROBOT_SA}
namespace: ${ROBOT_NS}
END
oc adm policy -n "${ROBOT_NS}" --rolebinding-name="acscs-cd-robot-admin" add-cluster-role-to-user cluster-admin -z "${ROBOT_SA}"
oc apply -n "${ROBOT_NS}" -f - <<END
apiVersion: v1
kind: Secret
metadata:
name: ${ROBOT_TOKEN_RESOURCE}
annotations:
kubernetes.io/service-account.name: "${ROBOT_SA}"
type: kubernetes.io/service-account-token
END

save_cluster_parameter "id" "$CLUSTER_ID"
save_cluster_parameter "url" "$CLUSTER_URL"

echo "Polling for token to be provisioned."
attempt=0
while true
do
attempt=$((attempt+1))
ROBOT_TOKEN="$(oc get secret "${ROBOT_TOKEN_RESOURCE}" -n "$ROBOT_NS" -o json | jq -r 'if (has("data") and (.data|has("token"))) then (.data.token|@base64d) else "" end')"
if [[ -n $ROBOT_TOKEN ]]; then
save_cluster_secret "robot_oc_token" "$ROBOT_TOKEN"
break
fi
if [[ $attempt -gt 30 ]]; then
echo "Timed out waiting for a token to be provisioned in the ${ROBOT_TOKEN_RESOURCE} secret."
exit 1
fi
sleep 1
done

echo "The following cluster parameters are currently stored in AWS Parameter Store:"
chamber list "cluster-${CLUSTER_NAME}"
echo "The following cluster parameters are currently stored in AWS Secrets Manager:"
chamber list "cluster-${CLUSTER_NAME}" -b secretsmanager

0 comments on commit 47653e4

Please sign in to comment.