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

[ci][terraform][cloud] tags/labels with metadata #792

Merged
merged 20 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
9 changes: 8 additions & 1 deletion .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,14 @@ def withCloudTestEnv(Closure body) {
}
// Masking
withEnvMask(vars: maskedVars) {
body()
// Env variables to be populated as tag/labels for the cloud resources
// NOTE: values can contain only lowercase letters, numeric characters, underscores, and dashes
withEnv(["TF_VAR_CREATED_DATE_TIME=${getCurrentBuildTime().toLowerCase().replaceAll(':', '_')}",
v1v marked this conversation as resolved.
Show resolved Hide resolved
"TF_VAR_CREATED_DATE=${new Date().format('yyyy-MM-dd')}",
v1v marked this conversation as resolved.
Show resolved Hide resolved
"TF_VAR_BRANCH_NAME=${env.BRANCH_NAME.toLowerCase()}",
"TF_VAR_ENVIRONMENT=ci", "TF_VAR_REPO=${env.REPO.toLowerCase()}"]) {
v1v marked this conversation as resolved.
Show resolved Hide resolved
body()
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions docs/howto/system_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,14 @@ Injecting credentials can be achieved with functions from the [`apm-pipeline-lib

Leveraging Terraform to create cloud resources is useful but risks creating leftover resources that are difficult to remove.

There are 4 environment variables that should be leveraged to overcome this issue; these variables are already injected to be used by Terraform (through `TF_VAR_`):
There are some specific environment variables that should be leveraged to overcome this issue; these variables are already injected to be used by Terraform (through `TF_VAR_`):
- `TF_VAR_TEST_RUN_ID`: a unique identifier for the test run, allows to distinguish each run
- `REPO_NAME`: the repository name the CI run is linked to
- `CHANGE_ID`: the PR number the CI run is linked to
- `BUILD_NUMBER`: incremental number providing the current CI run number
- `CREATED_DATE_TIME`: the creation date and time when the resource was created
- `CREATED_DATE`: the creation date when the resource was created
- `BRANCH_NAME`: the branch name or PR number the CI run is linked to
- `BUILD_ID`: incremental number providing the current CI run number
- `ENVIRONMENT`: what environment created the resource (`ci`)
- `REPO_NAME`: the GitHub repository name (`elastic-package`)

### Kubernetes service deployer

Expand Down
9 changes: 6 additions & 3 deletions internal/install/_static/terraform_deployer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ services:
stop_grace_period: 5m
environment:
- TF_VAR_TEST_RUN_ID=${TF_VAR_TEST_RUN_ID:-detached}
- TF_VAR_REPO_NAME=${REPO_NAME:-unknown}
- TF_VAR_PULL_REQUEST=pr-${CHANGE_ID:-unknown}
- TF_VAR_CI_BUILD_NUMBER=${BUILD_NUMBER:-unknown}
- TF_VAR_CREATED_DATE_TIME=${TF_VAR_CREATED_DATE_TIME:-unknown}
- TF_VAR_CREATED_DATE=${TF_VAR_CREATED_DATE:-unknown}
- TF_VAR_BRANCH_NAME=${TF_VAR_BRANCH_NAME:-unknown}
- TF_VAR_BUILD_ID=${BUILD_ID:-unknown}
mtojek marked this conversation as resolved.
Show resolved Hide resolved
- TF_VAR_ENVIRONMENT=${TF_VAR_ENVIRONMENT:-unknown}
- TF_VAR_REPO_NAME=${TF_VAR_REPO_NAME:-unknown}
volumes:
- ${TF_DIR}:/stage
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ variable "TEST_RUN_ID" {
default = "detached"
}

provider "aws" {}
provider "aws" {
default_tags {
tags = {
run_id = var.TEST_RUN_ID
v1v marked this conversation as resolved.
Show resolved Hide resolved
environment = var.ENVIRONMENT
repo_name = var.REPO_NAME
branch = var.BRANCH_NAME
build = var.BUILD_ID
created_date = var.CREATED_DATE
created_date_time = var.CREATED_DATE_TIME
}
}
}

resource "aws_instance" "i" {
ami = data.aws_ami.latest-amzn.id
Expand All @@ -20,4 +32,4 @@ data "aws_ami" "latest-amzn" {
name = "name"
values = ["amzn2-ami-minimal-hvm-*-ebs"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
variable "BRANCH_NAME" {
description = "Branch name for tagging purposes"
default = "unknown-branch"
}

variable "BUILD_ID" {
description = "Build ID in the CI for tagging purposes"
default = "unknown-build"
}

variable "CREATED_DATE" {
description = "Creation date for tagging purposes"
default = "unknown-date"
}

variable "CREATED_DATE_TIME" {
description = "Creation date and time for tagging purposes"
default = "unknown-date-time"
}

variable "ENVIRONMENT" {
default = "unknown-environment"
}

variable "REPO_NAME" {
default = "unknown-repo-name"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ data "google_compute_image" "default" {

resource "google_compute_instance" "default" {
name = "elastic-package-system-test-${var.TEST_RUN_ID}"
// NOTE: e2 instance type is required to collect instance/memory/balloon/*
// NOTE: e2 instance type is required to collect instance/memory/balloon/*
// metrics, available only on those instances.
// https://cloud.google.com/monitoring/api/metrics_gcp
machine_type = "e2-micro"
zone = var.zone

labels = {
run_id = var.TEST_RUN_ID
repo_name = var.REPO_NAME
pull_request = var.PULL_REQUEST
ci_build_number = var.CI_BUILD_NUMBER
run_id = var.TEST_RUN_ID
v1v marked this conversation as resolved.
Show resolved Hide resolved
environment = var.ENVIRONMENT
repo_name = var.REPO_NAME
branch = var.BRANCH_NAME
build = var.BUILD_ID
created_date = var.CREATED_DATE
created_date_time = var.CREATED_DATE_TIME
}

boot_disk {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@ variable "TEST_RUN_ID" {
default = "detached"
}

variable "REPO_NAME" {
default = "unknown-repo"
variable "BRANCH_NAME" {
description = "Branch name for tagging purposes"
default = "unknown-branch"
}

variable "PULL_REQUEST" {
default = "unknown-pr"
variable "BUILD_ID" {
v1v marked this conversation as resolved.
Show resolved Hide resolved
description = "Build ID in the CI for tagging purposes"
default = "unknown-build"
}

variable "CI_BUILD_NUMBER" {
default = "unknown-build"
variable "CREATED_DATE" {
description = "Creation date for tagging purposes"
default = "unknown-date"
}

variable "CREATED_DATE_TIME" {
description = "Creation date and time for tagging purposes"
default = "unknown-date-time"
}

variable "ENVIRONMENT" {
default = "unknown-environment"
}

variable "REPO_NAME" {
default = "unknown-repo-name"
}

variable "gcp_project_id" {
Expand Down