Skip to content
Snippets Groups Projects
Commit ab0ce713 authored by Aksel Skaar Leirvaag's avatar Aksel Skaar Leirvaag
Browse files

Merge branch '3-setup-gitlab-ci-pipeline-for-terraform' into 'main'

Resolve "Setup gitlab-ci pipeline for Terraform"

Closes #3

See merge request !6
parents 1ef83c0d 3dab25b9
No related branches found
No related tags found
1 merge request!6Resolve "Setup gitlab-ci pipeline for Terraform"
Showing
with 138 additions and 119 deletions
stages:
- init
- validate
- test
- build
- deploy
- cleanup
include: include:
- local: .gitlab-ci/generated-ci/.gitlab-ci.yml - local: .gitlab-ci/generated-ci/.gitlab-ci.yml
- local: .gitlab-ci/terraform/terraform.yml
\ No newline at end of file
...@@ -5,22 +5,15 @@ let TerraformModules = [ ...@@ -5,22 +5,15 @@ let TerraformModules = [
'flux', 'flux',
] ]
stages: [
'test'
]
{for module in TerraformModules { {for module in TerraformModules {
"\(module)": { "\(module)": {
stage: "test" stage: "test"
image: "akselleirv/terratest:0.0.9" image: "akselleirv/terratest:0.0.9"
before_script: [
". ./IIKG3005_H21_akselsle-openrc.sh",
]
script: [ script: [
"cd modules/\(module)/test", "cd modules/\(module)/test",
"go test -v -timeout 30m 2>&1 | tee test_output.log", "go test -v -timeout 30m 2>&1 | tee test_output.log",
"terratest_log_parser -testlog test_output.log -outputdir test_output", "terratest_log_parser -testlog test_output.log -outputdir test_output",
"cat test_output.log | go-junit-report > report.xml" "cat test_output.log | go-junit-report > report.xml",
] ]
artifacts: { artifacts: {
paths: [ paths: [
...@@ -37,4 +30,3 @@ stages: [ ...@@ -37,4 +30,3 @@ stages: [
} }
} }
} }
stages:
- test
k0s-cluster: k0s-cluster:
stage: test stage: test
image: akselleirv/terratest:0.0.9 image: akselleirv/terratest:0.0.9
before_script:
- . ./IIKG3005_H21_akselsle-openrc.sh
script: script:
- cd modules/k0s-cluster/test - cd modules/k0s-cluster/test
- go test -v -timeout 30m 2>&1 | tee test_output.log - go test -v -timeout 30m 2>&1 | tee test_output.log
...@@ -22,8 +18,6 @@ k0s-cluster: ...@@ -22,8 +18,6 @@ k0s-cluster:
k0s-config-generator: k0s-config-generator:
stage: test stage: test
image: akselleirv/terratest:0.0.9 image: akselleirv/terratest:0.0.9
before_script:
- . ./IIKG3005_H21_akselsle-openrc.sh
script: script:
- cd modules/k0s-config-generator/test - cd modules/k0s-config-generator/test
- go test -v -timeout 30m 2>&1 | tee test_output.log - go test -v -timeout 30m 2>&1 | tee test_output.log
...@@ -41,8 +35,6 @@ k0s-config-generator: ...@@ -41,8 +35,6 @@ k0s-config-generator:
openstack-cluster: openstack-cluster:
stage: test stage: test
image: akselleirv/terratest:0.0.9 image: akselleirv/terratest:0.0.9
before_script:
- . ./IIKG3005_H21_akselsle-openrc.sh
script: script:
- cd modules/openstack-cluster/test - cd modules/openstack-cluster/test
- go test -v -timeout 30m 2>&1 | tee test_output.log - go test -v -timeout 30m 2>&1 | tee test_output.log
...@@ -60,8 +52,6 @@ openstack-cluster: ...@@ -60,8 +52,6 @@ openstack-cluster:
flux: flux:
stage: test stage: test
image: akselleirv/terratest:0.0.9 image: akselleirv/terratest:0.0.9
before_script:
- . ./IIKG3005_H21_akselsle-openrc.sh
script: script:
- cd modules/flux/test - cd modules/flux/test
- go test -v -timeout 30m 2>&1 | tee test_output.log - go test -v -timeout 30m 2>&1 | tee test_output.log
......
image:
name: registry.gitlab.com/gitlab-org/terraform-images/stable:latest
variables:
TF_HTTP_ADDRESS: https://gitlab.stud.idi.ntnu.no/api/v4/projects/12496/terraform/state/project-production
TF_HTTP_USERNAME: simenram
TF_HTTP_PASSWORD: $TF_STATE_PASSWORD
TF_HTTP_UNLOCK_METHOD: DELETE
TF_HTTP_LOCK_METHOD: POST
PROJECT_PROD_DIR: ${CI_PROJECT_DIR}/project-production
TF_STATE_NAME: project-production
cache:
key: "${PROJECT_PROD_DIR}"
paths:
- ${PROJECT_PROD_DIR}/.terraform/
terraform-fmt:
stage: validate
script:
- gitlab-terraform fmt -recursive
allow_failure: true
terraform-validate:
stage: validate
script:
- cd ${PROJECT_PROD_DIR}
- gitlab-terraform validate
terraform-build:
stage: build
script:
- cd ${PROJECT_PROD_DIR}
- gitlab-terraform plan
- gitlab-terraform plan-json
resource_group: ${TF_STATE_NAME}
artifacts:
paths:
- ${PROJECT_PROD_DIR}/plan.cache
reports:
terraform: ${PROJECT_PROD_DIR}/plan.json
terraform-deploy:
stage: deploy
script:
- cd ${PROJECT_PROD_DIR}
- gitlab-terraform apply
resource_group: ${TF_STATE_NAME}
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual
terraform-destroy:
stage: cleanup
script:
- cd ${PROJECT_PROD_DIR}
- gitlab-terraform destroy
resource_group: ${TF_STATE_NAME}
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual
...@@ -2,8 +2,8 @@ module "k0s_cluster" { ...@@ -2,8 +2,8 @@ module "k0s_cluster" {
source = "../../k0s-cluster" source = "../../k0s-cluster"
cluster_name = var.cluster_name cluster_name = var.cluster_name
public_key_path = var.public_key_path
rsa_private_key_path = var.rsa_private_key_path rsa_private_key_path = var.rsa_private_key_path
public_key_openssh = var.public_key_openssh
} }
module "flux" { module "flux" {
......
...@@ -8,9 +8,9 @@ variable "rsa_private_key_path" { ...@@ -8,9 +8,9 @@ variable "rsa_private_key_path" {
description = "The path to the private key which can connect to the VM instances." description = "The path to the private key which can connect to the VM instances."
} }
variable "public_key_path" { variable "public_key_openssh" {
type = string type = string
description = "The path to the public key to be used to SSH to the Openstack instances." description = "The public key to be used to SSH to the Openstack instances."
} }
variable "gitlab_username" { variable "gitlab_username" {
......
...@@ -18,19 +18,15 @@ import ( ...@@ -18,19 +18,15 @@ import (
) )
func TestFlux(t *testing.T) { func TestFlux(t *testing.T) {
privateKeyPath, publicKeyPath, cleanupRSAKeys := generateTempRSAKey(t) privateKeyPath, publicKeyOpenSSH, cleanupRSAKeys := generateTempRSAKey(t)
defer cleanupRSAKeys() defer cleanupRSAKeys()
tfOpts := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ tfOpts := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: "../examples", TerraformDir: "../examples",
Vars: map[string]interface{}{ Vars: map[string]interface{}{
"cluster_name": "k0s-cluster-test-" + strings.ToLower(random.UniqueId()), "cluster_name": "k0s-cluster-test-" + strings.ToLower(random.UniqueId()),
"public_key_path": publicKeyPath, "public_key_openssh": publicKeyOpenSSH,
"rsa_private_key_path": privateKeyPath, "rsa_private_key_path": privateKeyPath,
}, },
EnvVars: map[string]string{
"TF_VAR_gitlab_username": mustGetEnv(t, "gitlab_username"),
"TF_VAR_gitlab_token": mustGetEnv(t, "gitlab_token"),
},
}) })
defer terraform.Destroy(t, tfOpts) defer terraform.Destroy(t, tfOpts)
terraform.InitAndApply(t, tfOpts) terraform.InitAndApply(t, tfOpts)
...@@ -45,26 +41,20 @@ func validateFlux(t *testing.T, kubeConfigFilePath string) { ...@@ -45,26 +41,20 @@ func validateFlux(t *testing.T, kubeConfigFilePath string) {
k8s.WaitUntilPodAvailable(t, k8sOpts, "nginx", 15, 5*time.Second) k8s.WaitUntilPodAvailable(t, k8sOpts, "nginx", 15, 5*time.Second)
} }
func generateTempRSAKey(t *testing.T) (privateKeyPath string, publicKeyPath string, cleanupKeys func()) { func generateTempRSAKey(t *testing.T) (privateKeyPath string, publicKeyOpenSSH string, cleanupKeys func()) {
rsaKey, err := rsa.GenerateKey(rand.Reader, 4096) rsaKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
privateKeyPath = os.TempDir() + "/rsa_private_key" privateKeyPath = os.TempDir() + "/rsa_private_key"
writeFile(t, privateKeyPath, privateKeyToBytes(rsaKey)) writeFile(t, privateKeyPath, privateKeyToBytes(rsaKey))
publicKeyOpenSSH = "ssh-rsa " + rsaKeyToOpenSSHFormat(t, rsaKey)
publicKeyPath = os.TempDir() + "/rsa_public_key" return privateKeyPath, publicKeyOpenSSH, func() {
writeFile(t, publicKeyPath, []byte("ssh-rsa "+rsaKeyToOpenSSHFormat(t, rsaKey)))
return privateKeyPath, publicKeyPath, func() {
err = os.RemoveAll(privateKeyPath) err = os.RemoveAll(privateKeyPath)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = os.RemoveAll(publicKeyPath)
if err != nil {
t.Fatal(err)
}
} }
} }
......
...@@ -2,6 +2,6 @@ module "k0s_cluster" { ...@@ -2,6 +2,6 @@ module "k0s_cluster" {
source = "../" source = "../"
cluster_name = var.cluster_name cluster_name = var.cluster_name
public_key_path = var.public_key_path public_key_openssh = var.public_key_openssh
rsa_private_key_path = var.rsa_private_key_path rsa_private_key_path = var.rsa_private_key_path
} }
...@@ -8,7 +8,7 @@ variable "rsa_private_key_path" { ...@@ -8,7 +8,7 @@ variable "rsa_private_key_path" {
description = "The path to the private key which can connect to the VM instances." description = "The path to the private key which can connect to the VM instances."
} }
variable "public_key_path" { variable "public_key_openssh" {
type = string type = string
description = "The path to the public key to be used to SSH to the Openstack instances." description = "The public key to be used to SSH to the Openstack instances."
} }
\ No newline at end of file
...@@ -2,7 +2,7 @@ module "openstack_cluster" { ...@@ -2,7 +2,7 @@ module "openstack_cluster" {
source = "../openstack-cluster" source = "../openstack-cluster"
cluster_name = var.cluster_name cluster_name = var.cluster_name
number_of_nodes = 2 number_of_nodes = 2
public_key = file(var.public_key_path) public_key = var.public_key_openssh
} }
module "k0s_config_generator" { module "k0s_config_generator" {
......
...@@ -19,12 +19,12 @@ import ( ...@@ -19,12 +19,12 @@ import (
const tfOptsPath = "/tmp/k0s-cluster" const tfOptsPath = "/tmp/k0s-cluster"
func TestK0sCluster(t *testing.T) { func TestK0sCluster(t *testing.T) {
privateKeyPath, publicKeyPath, cleanupRSAKeys := generateTempRSAKey(t) privateKeyPath, publicKeyOpenSSH, cleanupRSAKeys := generateTempRSAKey(t)
tfOpts := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ tfOpts := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: "../examples", TerraformDir: "../examples",
Vars: map[string]interface{}{ Vars: map[string]interface{}{
"cluster_name": "k0s-cluster-test-" + strings.ToLower(random.UniqueId()), "cluster_name": "k0s-cluster-test-" + strings.ToLower(random.UniqueId()),
"public_key_path": publicKeyPath, "public_key_openssh": publicKeyOpenSSH,
"rsa_private_key_path": privateKeyPath, "rsa_private_key_path": privateKeyPath,
}, },
}) })
...@@ -47,7 +47,7 @@ func TestK0sCluster(t *testing.T) { ...@@ -47,7 +47,7 @@ func TestK0sCluster(t *testing.T) {
k8s.GetNodes(t, k8sOpts) k8s.GetNodes(t, k8sOpts)
} }
func generateTempRSAKey(t *testing.T) (privateKeyPath string, publicKeyPath string, cleanupKeys func()) { func generateTempRSAKey(t *testing.T) (privateKeyPath string, publicKeyOpenSSH string, cleanupKeys func()) {
rsaKey, err := rsa.GenerateKey(rand.Reader, 4096) rsaKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -55,18 +55,13 @@ func generateTempRSAKey(t *testing.T) (privateKeyPath string, publicKeyPath stri ...@@ -55,18 +55,13 @@ func generateTempRSAKey(t *testing.T) (privateKeyPath string, publicKeyPath stri
privateKeyPath = os.TempDir() + "/rsa_private_key" privateKeyPath = os.TempDir() + "/rsa_private_key"
writeFile(t, privateKeyPath, privateKeyToBytes(rsaKey)) writeFile(t, privateKeyPath, privateKeyToBytes(rsaKey))
publicKeyPath = os.TempDir() + "/rsa_public_key" publicKeyOpenSSH = "ssh-rsa " + rsaKeyToOpenSSHFormat(t, rsaKey)
writeFile(t, publicKeyPath, []byte("ssh-rsa "+rsaKeyToOpenSSHFormat(t, rsaKey)))
return privateKeyPath, publicKeyPath, func() { return privateKeyPath, publicKeyOpenSSH, func() {
err = os.RemoveAll(privateKeyPath) err = os.RemoveAll(privateKeyPath)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = os.RemoveAll(publicKeyPath)
if err != nil {
t.Fatal(err)
}
} }
} }
......
...@@ -8,7 +8,7 @@ variable "rsa_private_key_path" { ...@@ -8,7 +8,7 @@ variable "rsa_private_key_path" {
description = "The path to the private key which can connect to the VM instances." description = "The path to the private key which can connect to the VM instances."
} }
variable "public_key_path" { variable "public_key_openssh" {
type = string type = string
description = "The path to the public key to be used to SSH to the Openstack instances." description = "The public key to be used to SSH to the Openstack instances."
} }
\ No newline at end of file
module "openstack_cluster" {
source = "../modules/openstack-cluster"
cluster_name = "test"
number_of_nodes = 2
public_key = file("/home/aksel/.ssh/id_rsa.pub")
}
module "k0s_config_generator" {
source = "../modules/k0s-config-generator"
cluster_name = module.openstack_cluster.cluster_name
ips = module.openstack_cluster.ips
rsa_private_key_path = "/home/aksel/.ssh/id_rsa"
}
module "k0s" {
source = "../modules/k0s-apply"
k0s_config_filepath = module.k0s_config_generator.file_path
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment