Skip to content

Commit 505d0ac

Browse files
authored
Merge pull request #21 from sparkfabrik/platform/3833_add_non_root_group_capabilities
Platform/3833 add non root group capabilities
2 parents d31b668 + c6d7c59 commit 505d0ac

File tree

7 files changed

+583
-28
lines changed

7 files changed

+583
-28
lines changed

CHANGELOG.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,42 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
1010

11+
## [1.0.0] - 2025-10-02
12+
13+
[Compare with previous version](https://github.com/sparkfabrik/terraform-gitlab-kubernetes-gitlab-agent/compare/0.13.0...1.0.0)
14+
15+
### Added
16+
17+
- New variable `operate_at_root_group_level` to simplify configuration and replace the combination of `gitlab_agent_grant_access_to_entire_root_namespace` and `gitlab_agent_create_variables_in_root_namespace`.
18+
- New variable `groups_enabled` to specify groups where the GitLab Agent should be enabled (when not operating at root group level).
19+
- New variable `projects_enabled` to specify projects where the GitLab Agent should be enabled (when not operating at root group level).
20+
- Auto-detection of parent group when `operate_at_root_group_level = false` and no groups/projects are specified.
21+
- Support for creating CI/CD variables in multiple groups and projects simultaneously.
22+
- Dynamic generation of agent configuration file based on enabled groups/projects using `yamlencode()`.
23+
- New outputs: `gitlab_enabled_groups`, `gitlab_enabled_projects`, `gitlab_parent_group_auto_detected`.
24+
25+
### Changed
26+
27+
- Agent configuration file is now dynamically generated based on `operate_at_root_group_level` and enabled groups/projects.
28+
- CI/CD variables can now be created in multiple targets (root group, specific groups, or specific projects) depending on configuration.
29+
- Output `gitlab_root_namespace_id` now returns `null` when not operating at root group level.
30+
31+
### Removed
32+
33+
- **BREAKING CHANGE**: variable `gitlab_agent_grant_access_to_entire_root_namespace` - replaced by `operate_at_root_group_level`.
34+
- **BREAKING CHANGE**: variable `gitlab_agent_create_variables_in_root_namespace` - behavior is now determined by `operate_at_root_group_level`.
35+
- Backward compatibility logic for deprecated variables.
36+
37+
### Migration Guide
38+
39+
If you were using the removed variables, migrate as follows:
40+
41+
- `gitlab_agent_grant_user_access_to_root_namespace = true` -> `operate_at_root_group_level = true` + `gitlab_agent_grant_user_access_to_root_namespace = true`
42+
- `gitlab_agent_grant_access_to_entire_root_namespace = true` + `gitlab_agent_create_variables_in_root_namespace = true``operate_at_root_group_level = true` + `gitlab_agent_grant_user_access_to_root_namespace = true`
43+
- `gitlab_agent_grant_access_to_entire_root_namespace = false` -> `operate_at_root_group_level = false` + configure `groups_enabled` and/or `projects_enabled`
44+
45+
**Note**: user access is now only available when `operate_at_root_group_level = true`. If you need user access to specific groups/projects, this is not currently supported by Gitlab.
46+
1147
## [0.12.0] - 2025-05-19
1248

1349
[Compare with previous version](https://github.com/sparkfabrik/terraform-gitlab-kubernetes-gitlab-agent/compare/0.11.0...0.12.0)

README.md

Lines changed: 68 additions & 11 deletions
Large diffs are not rendered by default.

files/config.yaml.tftpl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
1+
%{~ if operate_at_root_group_level ~}
12
ci_access:
23
groups:
34
- id: ${root_namespace}
4-
55
%{~ if gitlab_agent_grant_user_access_to_root_namespace }
66
user_access:
77
access_as:
88
agent: {}
99
groups:
1010
- id: ${root_namespace}
1111
%{~ endif ~}
12+
%{~ else ~}
13+
%{~ if length(groups_to_enable) > 0 || length(projects_to_enable) > 0 ~}
14+
ci_access:
15+
%{~ if length(groups_to_enable) > 0 ~}
16+
groups:
17+
%{~ for group in groups_to_enable ~}
18+
- id: ${group}
19+
%{~ endfor ~}
20+
%{~ endif ~}
21+
%{~ if length(projects_to_enable) > 0 ~}
22+
projects:
23+
%{~ for project in projects_to_enable ~}
24+
- id: ${project}
25+
%{~ endfor ~}
26+
%{~ endif ~}
27+
%{~ endif ~}
28+
%{~ endif ~}
1229

1330
%{~ if trimspace(gitlab_agent_append_to_config_file) != "" }
1431
${gitlab_agent_append_to_config_file}

main.tf

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,32 @@ 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+
# Determine the parent group of the project
23+
project_path_parts = split("/", var.gitlab_project_path_with_namespace)
24+
parent_group_path = length(local.project_path_parts) > 1 ? join("/", slice(local.project_path_parts, 0, length(local.project_path_parts) - 1)) : ""
25+
26+
# Determine if we are in auto-parent mode
27+
auto_detect_parent = !var.operate_at_root_group_level && length(concat(var.groups_enabled, var.projects_enabled)) == 0
28+
29+
# Final list of groups to enable
30+
groups_to_enable = var.operate_at_root_group_level ? [] : (
31+
local.auto_detect_parent ? [local.parent_group_path] : var.groups_enabled
32+
)
33+
34+
# Final list of projects to enable
35+
projects_to_enable = var.operate_at_root_group_level ? [] : (
36+
local.auto_detect_parent ? [] : var.projects_enabled
37+
)
38+
2239
# 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 }) : "")
40+
final_configuration_file_content = var.gitlab_agent_custom_config_file_content != "" ? var.gitlab_agent_custom_config_file_content : templatefile("${path.module}/files/config.yaml.tftpl", {
41+
operate_at_root_group_level = var.operate_at_root_group_level
42+
gitlab_agent_grant_user_access_to_root_namespace = var.gitlab_agent_grant_user_access_to_root_namespace
43+
root_namespace = data.gitlab_group.root_namespace.path
44+
groups_to_enable = local.groups_to_enable
45+
projects_to_enable = local.projects_to_enable
46+
gitlab_agent_append_to_config_file = var.gitlab_agent_append_to_config_file
47+
})
2448

2549
# Gitlab Agent CI/CD variables
2650
gitlab_agent_kubernetes_context_variables = {
@@ -41,10 +65,28 @@ data "gitlab_group" "root_namespace" {
4165
full_path = local.project_root_namespace
4266
}
4367

68+
# Data source for parent group when auto-detecting
69+
data "gitlab_group" "parent_group" {
70+
count = local.auto_detect_parent ? 1 : 0
71+
full_path = local.parent_group_path
72+
}
73+
74+
# Data source for the specified groups
75+
data "gitlab_group" "enabled_groups" {
76+
for_each = !var.operate_at_root_group_level && !local.auto_detect_parent ? toset(var.groups_enabled) : toset([])
77+
full_path = each.value
78+
}
79+
80+
# Data source for the specified projects
81+
data "gitlab_project" "enabled_projects" {
82+
for_each = !var.operate_at_root_group_level && !local.auto_detect_parent ? toset(var.projects_enabled) : toset([])
83+
path_with_namespace = each.value
84+
}
85+
4486
resource "gitlab_project" "project" {
4587
count = local.use_existing_project == 0 ? 1 : 0
4688
name = var.gitlab_project_name
47-
namespace_id = data.gitlab_group.root_namespace.group_id
89+
namespace_id = var.operate_at_root_group_level ? data.gitlab_group.root_namespace.group_id : data.gitlab_group.parent_group[0].group_id
4890
}
4991

5092
resource "gitlab_cluster_agent" "this" {
@@ -78,8 +120,9 @@ resource "gitlab_repository_file" "this" {
78120
]
79121
}
80122

81-
resource "gitlab_group_variable" "this" {
82-
for_each = var.gitlab_agent_create_variables_in_root_namespace ? local.gitlab_agent_kubernetes_context_variables : {}
123+
# Variables for root group (when operate_at_root_group_level is true)
124+
resource "gitlab_group_variable" "root_namespace" {
125+
for_each = var.operate_at_root_group_level ? local.gitlab_agent_kubernetes_context_variables : {}
83126

84127
group = data.gitlab_group.root_namespace.group_id
85128
key = each.key
@@ -94,6 +137,42 @@ resource "gitlab_group_variable" "this" {
94137
]
95138
}
96139

140+
# Variables for specific groups (when operate_at_root_group_level is false)
141+
resource "gitlab_group_variable" "enabled_groups" {
142+
for_each = !var.operate_at_root_group_level && length(local.groups_to_enable) > 0 ? {
143+
for pair in setproduct(keys(local.gitlab_agent_kubernetes_context_variables), local.groups_to_enable) :
144+
"${pair[1]}__${pair[0]}" => {
145+
group_path = pair[1]
146+
key = pair[0]
147+
value = local.gitlab_agent_kubernetes_context_variables[pair[0]]
148+
}
149+
} : {}
150+
151+
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
152+
key = each.value.key
153+
value = each.value.value
154+
protected = false
155+
masked = false
156+
}
157+
158+
# Variables for specific projects (when operate_at_root_group_level is false)
159+
resource "gitlab_project_variable" "enabled_projects" {
160+
for_each = !var.operate_at_root_group_level && length(local.projects_to_enable) > 0 ? {
161+
for pair in setproduct(keys(local.gitlab_agent_kubernetes_context_variables), local.projects_to_enable) :
162+
"${pair[1]}__${pair[0]}" => {
163+
project_path = pair[1]
164+
key = pair[0]
165+
value = local.gitlab_agent_kubernetes_context_variables[pair[0]]
166+
}
167+
} : {}
168+
169+
project = data.gitlab_project.enabled_projects[each.value.project_path].id
170+
key = each.value.key
171+
value = each.value.value
172+
protected = false
173+
masked = false
174+
}
175+
97176
# Kubernetes resources
98177
resource "kubernetes_namespace_v1" "this" {
99178
count = var.create_namespace ? 1 : 0

outputs.tf

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ 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 = var.operate_at_root_group_level ? data.gitlab_group.root_namespace.group_id : null
3030
}
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+

0 commit comments

Comments
 (0)