diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b473b0..9419fa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,14 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +## [0.7.0] - 2024-10-22 + +[Compare with previous version](https://github.com/sparkfabrik/terraform-gitlab-kubernetes-gitlab-agent/compare/0.6.0...0.7.0) + +### Added + +- The module can create the gitlab agents project by setting the variable `gitlab_project_name`. + ## [0.6.0] - 2024-07-30 [Compare with previous version](https://github.com/sparkfabrik/terraform-gitlab-kubernetes-gitlab-agent/compare/0.5.0...0.6.0) diff --git a/README.md b/README.md index ce3a35a..cce0b65 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ If required (`gitlab_agent_grant_access_to_entire_root_namespace` configured to **ATTENTION**: you have to manually create the project that will host the Gitlab Agent configuration in Gitlab before running this module. +From version `0.7.0`, if you set `gitlab_project_name` the module will create Gitlab project automatically. This new behavior requires the provider to have the proper permissions to create the project in the namespace. + ## RBAC configuration for the Gitlab Agent service account This module uses the default configuration of the Gitlab Agent Helm chart. The default configuration grants to the Gitlab Agent service account the `cluster-admin` ClusterRole. If you want to change this configuration, you can use the `helm_additional_values` variable to pass additional values to the Helm chart. diff --git a/files/values.yaml.tftpl b/files/values.yaml.tftpl index 874ced3..9224686 100644 --- a/files/values.yaml.tftpl +++ b/files/values.yaml.tftpl @@ -10,7 +10,7 @@ additionalLabels: replicas: ${agent_replicas} config: - kasAddress: "wss://${agent_kas_address}" + kasAddress: "${agent_kas_address}" secretName: "${agent_token_secret_name}" resources: diff --git a/main.tf b/main.tf index 051d87e..cc7987e 100644 --- a/main.tf +++ b/main.tf @@ -9,6 +9,11 @@ locals { final_namespace = var.create_namespace ? resource.kubernetes_namespace_v1.this[0].metadata[0].name : data.kubernetes_namespace_v1.this[0].metadata[0].name + use_existing_project = var.gitlab_project_name == "" ? 1 : 0 + project_id = local.use_existing_project == 1 ? data.gitlab_project.this[0].id : gitlab_project.project[0].id + project_path_with_namespace = local.use_existing_project == 1 ? data.gitlab_project.this[0].path_with_namespace : gitlab_project.project[0].path_with_namespace + project_root_namespace = split("/", var.gitlab_project_path_with_namespace)[0] + gitlab_agent_token_name_computed = replace(var.gitlab_agent_token_name, "{{gitlab_agent_name}}", var.gitlab_agent_name) gitlab_agent_token_description_computed = replace(var.gitlab_agent_token_description, "{{gitlab_agent_name}}", var.gitlab_agent_name) gitlab_agent_commmit_message_computed = replace(var.gitlab_agent_commmit_message, "{{gitlab_agent_name}}", var.gitlab_agent_name) @@ -20,26 +25,36 @@ locals { # Gitlab Agent CI/CD variables gitlab_agent_kubernetes_context_variables = { (var.gitlab_agent_variable_name_agent_id) : gitlab_cluster_agent.this.name, - (var.gitlab_agent_variable_name_agent_project) : data.gitlab_project.this.path_with_namespace, + (var.gitlab_agent_variable_name_agent_project) : local.project_path_with_namespace, } } # Gitlab resources +data "gitlab_metadata" "this" {} + data "gitlab_project" "this" { + count = local.use_existing_project path_with_namespace = var.gitlab_project_path_with_namespace } data "gitlab_group" "root_namespace" { - group_id = data.gitlab_project.this.namespace_id + full_path = local.project_root_namespace +} + +resource "gitlab_project" "project" { + count = local.use_existing_project == 0 ? 1 : 0 + name = var.gitlab_project_name + namespace_id = data.gitlab_group.root_namespace.group_id } resource "gitlab_cluster_agent" "this" { - project = data.gitlab_project.this.id + project = local.project_id name = var.gitlab_agent_name } resource "gitlab_cluster_agent_token" "this" { - project = data.gitlab_project.this.id + project = local.project_id + agent_id = gitlab_cluster_agent.this.agent_id name = local.gitlab_agent_token_name_computed description = local.gitlab_agent_token_description_computed @@ -48,7 +63,8 @@ resource "gitlab_cluster_agent_token" "this" { resource "gitlab_repository_file" "this" { count = trimspace(local.final_configuration_file_content) != "" ? 1 : 0 - project = data.gitlab_project.this.id + project = local.project_id + branch = var.gitlab_agent_branch_name commit_message = local.gitlab_agent_commmit_message_computed file_path = ".gitlab/agents/${gitlab_cluster_agent.this.name}/config.yaml" @@ -127,7 +143,7 @@ resource "helm_release" "this" { { k8s_common_labels = local.k8s_common_labels agent_replicas = var.agent_replicas - agent_kas_address = var.agent_kas_address + agent_kas_address = data.gitlab_metadata.this.kas.external_url agent_token_secret_name = kubernetes_secret_v1.gitlab_agent_token_secret.metadata[0].name # Variables used to configure the default podAntiAffinity for the Gitlab Agent create_default_pod_anti_affinity = var.create_default_pod_anti_affinity diff --git a/outputs.tf b/outputs.tf index bf12857..e6166f2 100644 --- a/outputs.tf +++ b/outputs.tf @@ -21,7 +21,7 @@ output "gitlab_agent_kubernetes_context_variables" { output "gitlab_agents_project_id" { description = "The ID of the Gitlab project where the Gitlab Agents are installed." - value = data.gitlab_project.this.id + value = local.project_id } output "gitlab_root_namespace_id" { diff --git a/variables.tf b/variables.tf index e7b7d86..0fc19c3 100644 --- a/variables.tf +++ b/variables.tf @@ -1,3 +1,9 @@ +variable "gitlab_project_name" { + description = "The name of the Gitlab project that hosts the Gitlab Agent configuration. If not provided, the module will use the project defined in `gitlab_project_path_with_namespace`." + type = string + default = "" +} + variable "gitlab_project_path_with_namespace" { description = "The path with namespace of the Gitlab project that hosts the Gitlab Agent configuration. The project must be created in Gitlab before running this module. The configured Gitlab provider must have write access to the project." type = string @@ -132,12 +138,6 @@ variable "agent_replicas" { default = 1 } -variable "agent_kas_address" { - description = "The address of the Gitlab Kubernetes Agent Server (KAS)." - type = string - default = "kas.gitlab.com" -} - variable "create_default_pod_anti_affinity" { description = "Create default podAntiAffinity rules for the Gitlab Agent pods." type = bool