Skip to content
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

feat(terraform): add support for generic cluster template #22

Merged
merged 6 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint_clean.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ on:
jobs:
fmt_and_lint:
name: Terraform fmt and tflint
uses: truefoundry/github-workflows-public/.github/workflows/terraform-lint-format.yml@v0.1.3
uses: truefoundry/github-workflows-public/.github/workflows/terraform-lint-format.yml@v0.1.3
2 changes: 1 addition & 1 deletion .github/workflows/terraform-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ jobs:
name: Generate terraform docs
uses: truefoundry/github-workflows-public/.github/workflows/terraform-doc-generator.yml@v0.1.3
with:
commit_ref: ${{ github.event.pull_request.head.ref }}
commit_ref: ${{ github.event.pull_request.head.ref }}
2 changes: 1 addition & 1 deletion .github/workflows/terraform-scan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ jobs:
name: Vulnerability scanning
uses: truefoundry/github-workflows-public/.github/workflows/terraform-scan.yml@v0.1.3
secrets:
snyk_token: ${{ secrets.SNYK_TOKEN }}
snyk_token: ${{ secrets.SNYK_TOKEN }}
1 change: 1 addition & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ locals {
"aws-eks" = "${path.module}/templates/cluster/aws.json.tpl"
"azure-aks" = "${path.module}/templates/cluster/azure.json.tpl"
"gcp-gke-standard" = "${path.module}/templates/cluster/gcp.json.tpl"
"generic" = "${path.module}/templates/cluster/generic.json.tpl"
}

# AWS provider configuration
Expand Down
12 changes: 6 additions & 6 deletions scripts/get_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ TRUEFOUNDRY_STDERR_FILE=\(.stderr_log_file)

# Logging functions
function log_info() {
echo "get_environment_name.sh - [INFO] - $1" >> $TRUEFOUNDRY_STDOUT_FILE
echo "get_environment_name.sh - [INFO] - $1" >> "$TRUEFOUNDRY_STDOUT_FILE"
}

function log_error() {
echo "get_environment_name.sh - [ERROR] - $1" >> $TRUEFOUNDRY_STDERR_FILE
echo "get_environment_name.sh - [ERROR] - $1" >> "$TRUEFOUNDRY_STDERR_FILE"
}

# Error handling
Expand All @@ -30,9 +30,9 @@ function handle_error() {
[ -z "${TRUEFOUNDRY_STDERR_FILE}" ] && handle_error "TRUEFOUNDRY_STDERR_FILE is required"


echo "" > $TRUEFOUNDRY_STDOUT_FILE
echo "" > $TRUEFOUNDRY_STDERR_FILE
log_info "Starting script ....\n" >> $TRUEFOUNDRY_STDOUT_FILE
echo "" > "$TRUEFOUNDRY_STDOUT_FILE"
echo "" > "$TRUEFOUNDRY_STDERR_FILE"
log_info "Starting script ....\n" >> "$TRUEFOUNDRY_STDOUT_FILE"

function make_request() {
local method="$1"
Expand Down Expand Up @@ -144,4 +144,4 @@ function main() {

output=$(main)
echo "$output"
log_info "Completed script ..." >> $TRUEFOUNDRY_STDOUT_FILE
log_info "Completed script ..." >> "$TRUEFOUNDRY_STDOUT_FILE"
38 changes: 17 additions & 21 deletions scripts/setup_truefoundry_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ function handle_error() {

# Logging functions
function log_info() {
echo "[INFO] $1" >> "$TRUEFOUNDRY_STDOUT_FILE"
echo "[INFO] $1" >>"$TRUEFOUNDRY_STDOUT_FILE"
}

function log_error() {
echo "[ERROR] $1" >> "$TRUEFOUNDRY_STDERR_FILE"
echo "[ERROR] $1" >>"$TRUEFOUNDRY_STDERR_FILE"
}

# Validate required parameters
Expand Down Expand Up @@ -53,11 +53,11 @@ function make_request() {
[ -n "$body" ] && curl_cmd="$curl_cmd -d '${body}'"

# Execute request and capture response, redirect all output to stderr
eval "${curl_cmd} '${url}'" > "${response_file}" 2>>"$TRUEFOUNDRY_STDERR_FILE"
eval "${curl_cmd} '${url}'" >"${response_file}" 2>>"$TRUEFOUNDRY_STDERR_FILE"
local curl_exit_code=$?

# Get HTTP status code, redirect all output to stderr
eval "${curl_cmd} -o /dev/null -w '%{http_code}' '${url}'" > "${http_code_file}" 2>>"$TRUEFOUNDRY_STDERR_FILE"
eval "${curl_cmd} -o /dev/null -w '%{http_code}' '${url}'" >"${http_code_file}" 2>>"$TRUEFOUNDRY_STDERR_FILE"
local http_code=$(<"${http_code_file}")

# Cleanup
Expand All @@ -75,28 +75,27 @@ function make_request() {
local error_msg="Request failed with status code: ${http_code}"
log_error "$error_msg"
log_error "Response body:"
cat "${response_file}" >> "$TRUEFOUNDRY_STDERR_FILE"
cat "${response_file}" >>"$TRUEFOUNDRY_STDERR_FILE"
rm -f "${response_file}"
handle_error "$error_msg"
return 1
fi

# Output response
cat "${response_file}"
rm -f "${response_file}"
return 0
}


function is_cluster_provisioned() {
log_info "is_cluster_provisioned: Checking if cluster exists and is provisioned..."

# Make GET request to check cluster status
local response=$(make_request "GET" "${CONTROL_PLANE_URL}/api/svc/v1/cluster/${CLUSTER_NAME}" "" "200") || {
local response=$(make_request "GET" "${CONTROL_PLANE_URL}/api/svc/v1/cluster/${CLUSTER_NAME}" "" "200,400") || {
log_info "is_cluster_provisioned: Cluster doesn't exist, creating ${CLUSTER_NAME} in control plane"
return 0
}

# Check if the request was successful
local provisioned
provisioned=$(echo "$response" | jq -r '.provisioned')
Expand All @@ -116,7 +115,7 @@ function create_cluster() {
[ $? -ne 0 ] && handle_error "Failed to decode CLUSTER_CONFIG_BASE64"

log_info "create_cluster: cluster manifest ${manifest}"

local response=$(make_request "PUT" "${CONTROL_PLANE_URL}/api/svc/v1/cluster/" "${manifest}" "200,201")
[ $? -ne 0 ] && handle_error "Failed to create cluster"

Expand All @@ -125,7 +124,6 @@ function create_cluster() {
local cluster_id=$(echo "${response}" | jq -r '.id')
[ $? -ne 0 ] && handle_error "Failed to parse cluster response"


log_info "create_cluster: Cluster ID $cluster_id"
[ -z "${cluster_id}" ] && handle_error "No cluster ID found in response"

Expand All @@ -141,11 +139,11 @@ function get_cluster_token() {
"${CONTROL_PLANE_URL}/api/svc/v1/cluster/${cluster_id}/token" \
"" "200")
[ $? -ne 0 ] && handle_error "Failed to get cluster token"

local token=$(echo "${response}" | jq -r '.clusterToken')
[ $? -ne 0 ] && handle_error "Failed to parse cluster token response"
[ -z "${token}" ] && handle_error "No cluster token found in response" && return 1

echo "${token}"
}

Expand All @@ -154,7 +152,6 @@ function setup_provider_account() {

# Ensure required environment variables are set
[ -z "$PROVIDER_CONFIG_BASE64" ] && handle_error "setup_provider_account: PROVIDER_CONFIG_BASE64 is required"


log_info "setup_provider_account: Creating provider account..."

Expand Down Expand Up @@ -191,13 +188,13 @@ function main() {
local cluster_token

# Check if cluster exists and is provisioned
local cluster_status=$(is_cluster_provisioned)
log_info "main: Cluster status: ${cluster_status}"
local cluster_status=$(is_cluster_provisioned)
log_info "main: Cluster status: ${cluster_status}"

if [ "${cluster_status}" = "true" ]; then
log_info "main: Cluster already exists and is provisioned. Skipping creation."
# Get existing cluster ID from the response
cluster_id=$(make_request "GET" "${CONTROL_PLANE_URL}/api/svc/v1/cluster/${CLUSTER_NAME}" "" "200" | jq -r '.id')
log_info "main: Cluster already exists and is provisioned. Skipping creation."
# Get existing cluster ID from the response
cluster_id=$(make_request "GET" "${CONTROL_PLANE_URL}/api/svc/v1/cluster/${CLUSTER_NAME}" "" "200" | jq -r '.id')
else
if [ "${CLUSTER_TYPE}" != "generic" ]; then
# Setup provider account and create cluster if not provisioned or doesn't exist
Expand All @@ -208,7 +205,6 @@ function main() {
fi
fi


cluster_token=$(get_cluster_token "${cluster_id}") || handle_error "Failed to get cluster token"

log_info "main: cluster_id=$cluster_id"
Expand Down
File renamed without changes.
Loading