Skip to content

Commit d4bc403

Browse files
authored
Merge pull request #11 from sparkfabrik/3152-create-gitlab-agents-project-from-the-module
3152 create gitlab agents project from the module
2 parents 4cf748d + f173b17 commit d4bc403

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

CHANGELOG.md

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

99
## [Unreleased]
1010

11+
## [0.7.0] - 2024-10-22
12+
13+
[Compare with previous version](https://github.com/sparkfabrik/terraform-gitlab-kubernetes-gitlab-agent/compare/0.6.0...0.7.0)
14+
15+
### Added
16+
17+
- The module can create the gitlab agents project by setting the variable `gitlab_project_name`.
18+
1119
## [0.6.0] - 2024-07-30
1220

1321
[Compare with previous version](https://github.com/sparkfabrik/terraform-gitlab-kubernetes-gitlab-agent/compare/0.5.0...0.6.0)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ If required (`gitlab_agent_grant_access_to_entire_root_namespace` configured to
88

99
**ATTENTION**: you have to manually create the project that will host the Gitlab Agent configuration in Gitlab before running this module.
1010

11+
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.
12+
1113
## RBAC configuration for the Gitlab Agent service account
1214

1315
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.

files/values.yaml.tftpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ additionalLabels:
1010
replicas: ${agent_replicas}
1111

1212
config:
13-
kasAddress: "wss://${agent_kas_address}"
13+
kasAddress: "${agent_kas_address}"
1414
secretName: "${agent_token_secret_name}"
1515

1616
resources:

main.tf

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ locals {
99

1010
final_namespace = var.create_namespace ? resource.kubernetes_namespace_v1.this[0].metadata[0].name : data.kubernetes_namespace_v1.this[0].metadata[0].name
1111

12+
use_existing_project = var.gitlab_project_name == "" ? 1 : 0
13+
project_id = local.use_existing_project == 1 ? data.gitlab_project.this[0].id : gitlab_project.project[0].id
14+
project_path_with_namespace = local.use_existing_project == 1 ? data.gitlab_project.this[0].path_with_namespace : gitlab_project.project[0].path_with_namespace
15+
project_root_namespace = split("/", var.gitlab_project_path_with_namespace)[0]
16+
1217
gitlab_agent_token_name_computed = replace(var.gitlab_agent_token_name, "{{gitlab_agent_name}}", var.gitlab_agent_name)
1318
gitlab_agent_token_description_computed = replace(var.gitlab_agent_token_description, "{{gitlab_agent_name}}", var.gitlab_agent_name)
1419
gitlab_agent_commmit_message_computed = replace(var.gitlab_agent_commmit_message, "{{gitlab_agent_name}}", var.gitlab_agent_name)
@@ -20,26 +25,36 @@ locals {
2025
# Gitlab Agent CI/CD variables
2126
gitlab_agent_kubernetes_context_variables = {
2227
(var.gitlab_agent_variable_name_agent_id) : gitlab_cluster_agent.this.name,
23-
(var.gitlab_agent_variable_name_agent_project) : data.gitlab_project.this.path_with_namespace,
28+
(var.gitlab_agent_variable_name_agent_project) : local.project_path_with_namespace,
2429
}
2530
}
2631

2732
# Gitlab resources
33+
data "gitlab_metadata" "this" {}
34+
2835
data "gitlab_project" "this" {
36+
count = local.use_existing_project
2937
path_with_namespace = var.gitlab_project_path_with_namespace
3038
}
3139

3240
data "gitlab_group" "root_namespace" {
33-
group_id = data.gitlab_project.this.namespace_id
41+
full_path = local.project_root_namespace
42+
}
43+
44+
resource "gitlab_project" "project" {
45+
count = local.use_existing_project == 0 ? 1 : 0
46+
name = var.gitlab_project_name
47+
namespace_id = data.gitlab_group.root_namespace.group_id
3448
}
3549

3650
resource "gitlab_cluster_agent" "this" {
37-
project = data.gitlab_project.this.id
51+
project = local.project_id
3852
name = var.gitlab_agent_name
3953
}
4054

4155
resource "gitlab_cluster_agent_token" "this" {
42-
project = data.gitlab_project.this.id
56+
project = local.project_id
57+
4358
agent_id = gitlab_cluster_agent.this.agent_id
4459
name = local.gitlab_agent_token_name_computed
4560
description = local.gitlab_agent_token_description_computed
@@ -48,7 +63,8 @@ resource "gitlab_cluster_agent_token" "this" {
4863
resource "gitlab_repository_file" "this" {
4964
count = trimspace(local.final_configuration_file_content) != "" ? 1 : 0
5065

51-
project = data.gitlab_project.this.id
66+
project = local.project_id
67+
5268
branch = var.gitlab_agent_branch_name
5369
commit_message = local.gitlab_agent_commmit_message_computed
5470
file_path = ".gitlab/agents/${gitlab_cluster_agent.this.name}/config.yaml"
@@ -127,7 +143,7 @@ resource "helm_release" "this" {
127143
{
128144
k8s_common_labels = local.k8s_common_labels
129145
agent_replicas = var.agent_replicas
130-
agent_kas_address = var.agent_kas_address
146+
agent_kas_address = data.gitlab_metadata.this.kas.external_url
131147
agent_token_secret_name = kubernetes_secret_v1.gitlab_agent_token_secret.metadata[0].name
132148
# Variables used to configure the default podAntiAffinity for the Gitlab Agent
133149
create_default_pod_anti_affinity = var.create_default_pod_anti_affinity

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ output "gitlab_agent_kubernetes_context_variables" {
2121

2222
output "gitlab_agents_project_id" {
2323
description = "The ID of the Gitlab project where the Gitlab Agents are installed."
24-
value = data.gitlab_project.this.id
24+
value = local.project_id
2525
}
2626

2727
output "gitlab_root_namespace_id" {

variables.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
variable "gitlab_project_name" {
2+
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`."
3+
type = string
4+
default = ""
5+
}
6+
17
variable "gitlab_project_path_with_namespace" {
28
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."
39
type = string
@@ -132,12 +138,6 @@ variable "agent_replicas" {
132138
default = 1
133139
}
134140

135-
variable "agent_kas_address" {
136-
description = "The address of the Gitlab Kubernetes Agent Server (KAS)."
137-
type = string
138-
default = "kas.gitlab.com"
139-
}
140-
141141
variable "create_default_pod_anti_affinity" {
142142
description = "Create default podAntiAffinity rules for the Gitlab Agent pods."
143143
type = bool

0 commit comments

Comments
 (0)