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 6 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
6 changes: 5 additions & 1 deletion .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,11 @@ def withCloudTestEnv(Closure body) {
}
// Masking
withEnvMask(vars: maskedVars) {
body()
withEnv(["TF_VAR_BUILD_ID=${BUILD_ID}",
"TF_VAR_BRANCH_NAME=${BRANCH_NAME}",
"TF_VAR_CREATED_DATE=${getCurrentBuildTime()}"]) {
body()
}
v1v marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
6 changes: 3 additions & 3 deletions docs/howto/system_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ Leveraging Terraform to create cloud resources is useful but risks creating left

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_`):
- `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`: 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

### Kubernetes service deployer

Expand Down
6 changes: 3 additions & 3 deletions internal/install/_static/terraform_deployer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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=${CREATED_DATE:-unknown}
- TF_VAR_BRANCH_NAME=${BRANCH_NAME:-unknown}
- TF_VAR_BUILD_ID=${BUILD_ID:-unknown}
mtojek marked this conversation as resolved.
Show resolved Hide resolved
volumes:
- ${TF_DIR}:/stage
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ variable "TEST_RUN_ID" {
default = "detached"
}

provider "aws" {}
provider "aws" {
default_tags {
tags = {
RunId = var.TEST_RUN_ID
Environment = var.ENVIRONMENT
Owner = var.OWNER
Branch = var.BRANCH_NAME
Build = var.BUILD_ID
CreatedDate = var.CREATED_DATE
}
}
}

resource "aws_instance" "i" {
ami = data.aws_ami.latest-amzn.id
Expand All @@ -20,4 +31,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,22 @@
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 "ENVIRONMENT" {
default = "CI"
}

variable "OWNER" {
default = "elastic-package"
}
v1v marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ 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
RunId = var.TEST_RUN_ID
Environment = var.ENVIRONMENT
Owner = var.OWNER
Branch = var.BRANCH_NAME
Build = var.BUILD_ID
CreatedDate = var.CREATED_DATE
}

boot_disk {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ 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 "ENVIRONMENT" {
default = "CI"
}

variable "OWNER" {
default = "elastic-package"
}

variable "gcp_project_id" {
Expand Down