Skip to content

Commit 58f5f90

Browse files
committed
feat: enhance GitLab Agent configuration with backward compatibility and new variable mappings
1 parent d31b668 commit 58f5f90

File tree

3 files changed

+137
-9
lines changed

3 files changed

+137
-9
lines changed

main.tf

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,47 @@ locals {
1919
gitlab_agent_commmit_message_computed = replace(var.gitlab_agent_commmit_message, "{{gitlab_agent_name}}", var.gitlab_agent_name)
2020
k8s_gitlab_agent_token_secret_name_computed = replace(var.k8s_gitlab_agent_token_secret_name, "{{gitlab_agent_name}}", var.gitlab_agent_name)
2121

22+
# Backward compatibility: map old variables to new operate_at_root_group_level
23+
operate_at_root_group_level_computed = var.operate_at_root_group_level != null ? var.operate_at_root_group_level : (
24+
var.gitlab_agent_grant_user_access_to_root_namespace != null ? var.gitlab_agent_grant_user_access_to_root_namespace : true
25+
)
26+
27+
# Determina il parent group del progetto
28+
project_path_parts = split("/", var.gitlab_project_path_with_namespace)
29+
parent_group_path = length(local.project_path_parts) > 1 ? join("/", slice(local.project_path_parts, 0, length(local.project_path_parts) - 1)) : local.project_root_namespace
30+
31+
# Determina se siamo in modalità auto-parent
32+
auto_detect_parent = !local.operate_at_root_group_level_computed && length(concat(var.groups_enabled, var.projects_enabled)) == 0
33+
34+
# Lista finale di gruppi da abilitare
35+
groups_to_enable = local.operate_at_root_group_level_computed ? [local.project_root_namespace] : (
36+
local.auto_detect_parent ? [local.parent_group_path] : var.groups_enabled
37+
)
38+
39+
# Lista finale di progetti da abilitare
40+
projects_to_enable = local.operate_at_root_group_level_computed ? [] : (
41+
local.auto_detect_parent ? [] : var.projects_enabled
42+
)
43+
2244
# Gitlab Agent configuration file
23-
final_configuration_file_content = var.gitlab_agent_custom_config_file_content != "" ? var.gitlab_agent_custom_config_file_content : (var.gitlab_agent_grant_access_to_entire_root_namespace ? templatefile("${path.module}/files/config.yaml.tftpl", { root_namespace = data.gitlab_group.root_namespace.path, gitlab_agent_append_to_config_file = var.gitlab_agent_append_to_config_file, gitlab_agent_grant_user_access_to_root_namespace = var.gitlab_agent_grant_user_access_to_root_namespace }) : "")
45+
final_configuration_file_content = var.gitlab_agent_custom_config_file_content != "" ? var.gitlab_agent_custom_config_file_content : (
46+
local.operate_at_root_group_level_computed ? templatefile("${path.module}/files/config.yaml.tftpl", {
47+
root_namespace = data.gitlab_group.root_namespace.path,
48+
gitlab_agent_append_to_config_file = var.gitlab_agent_append_to_config_file,
49+
gitlab_agent_grant_user_access_to_root_namespace = var.gitlab_agent_grant_user_access_to_root_namespace
50+
}) : (
51+
length(local.groups_to_enable) > 0 || length(local.projects_to_enable) > 0 ? yamlencode({
52+
ci_access = merge(
53+
length(local.groups_to_enable) > 0 ? {
54+
groups = [for g in local.groups_to_enable : { id = g }]
55+
} : {},
56+
length(local.projects_to_enable) > 0 ? {
57+
projects = [for p in local.projects_to_enable : { id = p }]
58+
} : {}
59+
)
60+
}) : ""
61+
)
62+
)
2463

2564
# Gitlab Agent CI/CD variables
2665
gitlab_agent_kubernetes_context_variables = {
@@ -41,6 +80,24 @@ data "gitlab_group" "root_namespace" {
4180
full_path = local.project_root_namespace
4281
}
4382

83+
# Data source per parent group quando auto-detect
84+
data "gitlab_group" "parent_group" {
85+
count = local.auto_detect_parent ? 1 : 0
86+
full_path = local.parent_group_path
87+
}
88+
89+
# Data source per i gruppi specificati
90+
data "gitlab_group" "enabled_groups" {
91+
for_each = !local.operate_at_root_group_level_computed && !local.auto_detect_parent ? toset(var.groups_enabled) : toset([])
92+
full_path = each.value
93+
}
94+
95+
# Data source per i progetti specificati
96+
data "gitlab_project" "enabled_projects" {
97+
for_each = !local.operate_at_root_group_level_computed && !local.auto_detect_parent ? toset(var.projects_enabled) : toset([])
98+
path_with_namespace = each.value
99+
}
100+
44101
resource "gitlab_project" "project" {
45102
count = local.use_existing_project == 0 ? 1 : 0
46103
name = var.gitlab_project_name
@@ -78,8 +135,9 @@ resource "gitlab_repository_file" "this" {
78135
]
79136
}
80137

81-
resource "gitlab_group_variable" "this" {
82-
for_each = var.gitlab_agent_create_variables_in_root_namespace ? local.gitlab_agent_kubernetes_context_variables : {}
138+
# Variabili per root group (quando operate_at_root_group_level è true)
139+
resource "gitlab_group_variable" "root_namespace" {
140+
for_each = local.operate_at_root_group_level_computed ? local.gitlab_agent_kubernetes_context_variables : {}
83141

84142
group = data.gitlab_group.root_namespace.group_id
85143
key = each.key
@@ -94,6 +152,44 @@ resource "gitlab_group_variable" "this" {
94152
]
95153
}
96154

155+
# Variabili per gruppi specifici (quando operate_at_root_group_level è false)
156+
resource "gitlab_group_variable" "enabled_groups" {
157+
for_each = !local.operate_at_root_group_level_computed && length(local.groups_to_enable) > 0 ? {
158+
for pair in setproduct(keys(local.gitlab_agent_kubernetes_context_variables), local.groups_to_enable) :
159+
"${pair[1]}_${pair[0]}" => {
160+
group_path = pair[1]
161+
key = pair[0]
162+
value = local.gitlab_agent_kubernetes_context_variables[pair[0]]
163+
}
164+
} : {}
165+
166+
group = local.auto_detect_parent && each.value.group_path == local.parent_group_path ? data.gitlab_group.parent_group[0].group_id : data.gitlab_group.enabled_groups[each.value.group_path].group_id
167+
key = each.value.key
168+
value = each.value.value
169+
protected = false
170+
masked = false
171+
172+
}
173+
174+
# Variabili per progetti specifici (quando operate_at_root_group_level è false)
175+
resource "gitlab_project_variable" "enabled_projects" {
176+
for_each = !local.operate_at_root_group_level_computed && length(local.projects_to_enable) > 0 ? {
177+
for pair in setproduct(keys(local.gitlab_agent_kubernetes_context_variables), local.projects_to_enable) :
178+
"${pair[1]}_${pair[0]}" => {
179+
project_path = pair[1]
180+
key = pair[0]
181+
value = local.gitlab_agent_kubernetes_context_variables[pair[0]]
182+
}
183+
} : {}
184+
185+
project = data.gitlab_project.enabled_projects[each.value.project_path].id
186+
key = each.value.key
187+
value = each.value.value
188+
protected = false
189+
masked = false
190+
191+
}
192+
97193
# Kubernetes resources
98194
resource "kubernetes_namespace_v1" "this" {
99195
count = var.create_namespace ? 1 : 0

outputs.tf

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ output "gitlab_agents_project_id" {
2525
}
2626

2727
output "gitlab_root_namespace_id" {
28-
description = "The ID of the root namespace of the Gitlab Agents project."
29-
value = data.gitlab_group.root_namespace.group_id
28+
description = "The ID of the root namespace of the Gitlab Agents project. Only available when operate_at_root_group_level is true."
29+
value = local.operate_at_root_group_level_computed ? data.gitlab_group.root_namespace.group_id : null
30+
}
31+
32+
output "gitlab_enabled_groups" {
33+
description = "List of groups where the GitLab Agent has been enabled with variables."
34+
value = local.groups_to_enable
35+
}
36+
37+
output "gitlab_enabled_projects" {
38+
description = "List of projects where the GitLab Agent has been enabled with variables."
39+
value = local.projects_to_enable
40+
}
41+
42+
output "gitlab_parent_group_auto_detected" {
43+
description = "Whether the parent group was automatically detected."
44+
value = local.auto_detect_parent
45+
}
46+
47+
output "operate_at_root_group_level" {
48+
description = "The computed value of operate_at_root_group_level (includes backward compatibility)."
49+
value = local.operate_at_root_group_level_computed
3050
}

variables.tf

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,24 @@ variable "gitlab_agent_token_description" {
3131
default = "Token for the Gitlab Agent {{gitlab_agent_name}}."
3232
}
3333

34-
variable "gitlab_agent_grant_access_to_entire_root_namespace" {
35-
description = "Grant access to the entire root namespace. If false, you can provide a custom configuration file content using the variable `gitlab_agent_custom_config_file_content`. Otherwise, you will have to manually manage the access to the Gitlab Agent committing the proper configuration to the Gitlab project."
34+
variable "operate_at_root_group_level" {
35+
description = "Operate at root group level. If true, grants access to entire root namespace and creates variables in root group. If false, behavior depends on groups_enabled and projects_enabled. This replaces gitlab_agent_grant_access_to_entire_root_namespace and gitlab_agent_create_variables_in_root_namespace."
3636
type = bool
3737
default = true
3838
}
3939

40+
variable "groups_enabled" {
41+
description = "List of group paths where the GitLab Agent should be enabled. Only used when operate_at_root_group_level is false. If empty and projects_enabled is also empty, the parent group of the agent project will be used automatically."
42+
type = list(string)
43+
default = []
44+
}
45+
46+
variable "projects_enabled" {
47+
description = "List of project paths (with namespace) where the GitLab Agent should be enabled. Only used when operate_at_root_group_level is false. If empty and groups_enabled is also empty, the parent group of the agent project will be used automatically."
48+
type = list(string)
49+
default = []
50+
}
51+
4052
variable "gitlab_agent_grant_user_access_to_root_namespace" {
4153
description = "Grant `user_access` to the root namespace."
4254
type = bool
@@ -69,9 +81,9 @@ variable "gitlab_agent_branch_name" {
6981
}
7082

7183
variable "gitlab_agent_create_variables_in_root_namespace" {
72-
description = "Create two Gitlab CI/CD variables in the root namespace useful to configure the Kubernetes context and use the Gitlab Agent. These variables are created in the root namespace of the project defined in `gitlab_project_path_with_namespace`, which is the project that hosts the Gitlab Agent configuration."
84+
description = "DEPRECATED: Use operate_at_root_group_level instead. Create two Gitlab CI/CD variables in the root namespace useful to configure the Kubernetes context and use the Gitlab Agent. These variables are created in the root namespace of the project defined in `gitlab_project_path_with_namespace`, which is the project that hosts the Gitlab Agent configuration."
7385
type = bool
74-
default = true
86+
default = null
7587
}
7688

7789
variable "gitlab_agent_variable_name_agent_id" {

0 commit comments

Comments
 (0)