Skip to content

3152 create gitlab agents project from the module #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
23f7ef1
refs platform/#3152: add variable for project
Stevesibilia Oct 21, 2024
a44ed5d
refs platform/#3152: fix
Stevesibilia Oct 21, 2024
854207b
ref platform/#3152: fix condition
Stevesibilia Oct 21, 2024
e10062b
refs platform/#3152: fix condition
Stevesibilia Oct 21, 2024
a93014a
ref platform/#3152: typo
Stevesibilia Oct 21, 2024
c4664a1
refs platform/#3152: add root namespace
Stevesibilia Oct 21, 2024
1a73e44
refs platform/#3152: fix condition
Stevesibilia Oct 21, 2024
747523e
refs platform/#3152: add project creation
Stevesibilia Oct 21, 2024
fc344bd
refs platfomr/#3152: fix
Stevesibilia Oct 21, 2024
6cd5345
ref platform/#3152: fix count
Stevesibilia Oct 21, 2024
534eb75
fix
Stevesibilia Oct 21, 2024
3500b81
fix
Stevesibilia Oct 21, 2024
81feff5
fix
Stevesibilia Oct 21, 2024
368c9bf
fix
Stevesibilia Oct 21, 2024
f3302f1
fix
Stevesibilia Oct 21, 2024
96a552f
refs platform/#3152: cleaned conditions
Stevesibilia Oct 21, 2024
f6f5718
fix
Stevesibilia Oct 21, 2024
f21fd59
fix
Stevesibilia Oct 21, 2024
369b82a
refs platform/#3152: remove unused variables
Stevesibilia Oct 21, 2024
7e492ae
refs platform/#3152: add docs
Stevesibilia Oct 21, 2024
1f503cc
ref platform/#3152: change kas address to metadata.kas.external_url
Stevesibilia Oct 21, 2024
ff98fc1
refs platform/#3152: fix kass address
Stevesibilia Oct 21, 2024
70bd82c
refs platform/#3152: remove var agent_kas_address
Stevesibilia Oct 21, 2024
c829084
Update CHANGELOG.md
Stevesibilia Oct 22, 2024
092706b
Update main.tf
Stevesibilia Oct 22, 2024
cac9b3a
Update README.md
Stevesibilia Oct 22, 2024
1bae978
Update main.tf
Stevesibilia Oct 22, 2024
622fd6c
Update main.tf
Stevesibilia Oct 22, 2024
f173b17
refs platform/#3152: change gitlab_root_namespace with local project_…
Stevesibilia Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion files/values.yaml.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ additionalLabels:
replicas: ${agent_replicas}

config:
kasAddress: "wss://${agent_kas_address}"
kasAddress: "${agent_kas_address}"
secretName: "${agent_token_secret_name}"

resources:
Expand Down
28 changes: 22 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
12 changes: 6 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down