Skip to content

Commit e22aa99

Browse files
fsismondimodridi
andcommitted
feat: app to node pool selection
* introduces app_node_selector * enables devops stack apps to node pools using node labels Co-authored-by: modridi <mohamed-amine.dridi@camptocamp.com>
1 parent a9e2ff9 commit e22aa99

6 files changed

Lines changed: 136 additions & 22 deletions

File tree

argocd/aad-pod-identity/templates/namespace.yaml

Lines changed: 0 additions & 13 deletions
This file was deleted.

modules/aks/azure/main.tf

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ locals {
1919
principal_id = azurerm_user_assigned_identity.this[format("%s.%s", v.namespace, v.name)].principal_id
2020
}
2121
}
22+
23+
namespaces = merge(
24+
{ for i in local.azureidentities : i.namespace => null },
25+
var.app_node_selectors
26+
)
2227
}
2328

2429
provider "helm" {
@@ -65,7 +70,7 @@ module "cluster" {
6570
network_policy = var.network_policy
6671
vnet_subnet_id = var.vnet_subnet_id
6772
agents_pool_name = var.agents_pool_name
68-
agents_labels = var.agents_labels
73+
agents_labels = merge({ "devops-stack.io/nodepool" = var.agents_pool_name }, var.agents_labels)
6974
agents_count = var.agents_count
7075
agents_size = var.agents_size
7176
agents_max_pods = var.agents_max_pods
@@ -100,7 +105,7 @@ resource "azurerm_kubernetes_cluster_node_pool" "this" {
100105
os_disk_size_gb = lookup(each.value, "os_disk_size_gb", null)
101106
os_type = lookup(each.value, "os_type", "Linux")
102107
vnet_subnet_id = lookup(each.value, "vnet_subnet_id", var.vnet_subnet_id)
103-
node_labels = lookup(each.value, "node_labels", null)
108+
node_labels = merge({ "devops-stack.io/nodepool" = each.key }, lookup(each.value, "node_labels", null))
104109
mode = lookup(each.value, "mode", null)
105110
}
106111

@@ -150,13 +155,14 @@ module "argocd" {
150155
loki_account_name = azurerm_storage_account.this.name
151156
loki_account_key = azurerm_storage_account.this.primary_access_key
152157
azureidentities = local.azureidentities
158+
namespaces = local.namespaces
153159
}
154160
),
155161
var.app_of_apps_values_overrides,
156162
]
157163

158164
depends_on = [
159-
module.cluster,
165+
azurerm_kubernetes_cluster_node_pool.this, # node pools creation must precede apps creation for the pod to node assignation
160166
]
161167
}
162168

modules/aks/azure/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ output "azureidentities" {
3030
value = local.azureidentities
3131
}
3232

33+
output "namespaces" {
34+
value = local.namespaces
35+
}
36+
3337
output "kubelet_identity" {
3438
value = module.cluster.kubelet_identity
3539
}

modules/aks/azure/values.tmpl.yaml

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ apps:
77
metrics-server:
88
enabled: false
99

10+
${yamlencode({"namespaces": namespaces})}
11+
1012
aad-pod-identity:
1113
azureIdentities:
12-
%{for k, v in azureidentities~}
14+
%{ for k, v in azureidentities ~}
1315
${k}:
1416
namespace: "${v.namespace}"
1517
name: "${v.name}"
@@ -19,7 +21,21 @@ aad-pod-identity:
1921
binding:
2022
name: "${v.name}"
2123
selector: "${v.name}"
22-
%{endfor~}
24+
%{ endfor ~}
25+
%{ if length(lookup(namespaces, "aad-pod-identity", {})) > 0 ~}
26+
mic:
27+
affinity:
28+
nodeAffinity:
29+
requiredDuringSchedulingIgnoredDuringExecution:
30+
nodeSelectorTerms:
31+
- matchExpressions:
32+
%{ for k, v in lookup(namespaces, "aad-pod-identity") ~}
33+
- key: ${k}
34+
operator: In
35+
values:
36+
- ${v}
37+
%{ endfor ~}
38+
%{ endif ~}
2339

2440
argo-cd:
2541
controller:
@@ -125,6 +141,19 @@ kube-prometheus-stack:
125141
requests:
126142
cpu: 100m
127143
memory: 32Mi
144+
%{ if length(lookup(namespaces, "kube-prometheus-stack", {})) > 0 ~}
145+
affinity:
146+
nodeAffinity:
147+
requiredDuringSchedulingIgnoredDuringExecution:
148+
nodeSelectorTerms:
149+
- matchExpressions:
150+
%{ for k, v in lookup(namespaces, "kube-prometheus-stack") ~}
151+
- key: ${k}
152+
operator: In
153+
values:
154+
- ${v}
155+
%{ endfor ~}
156+
%{ endif ~}
128157

129158
alertmanager:
130159
alertmanagerSpec:
@@ -134,6 +163,19 @@ kube-prometheus-stack:
134163
requests:
135164
cpu: 10m
136165
memory: 16Mi
166+
%{ if length(lookup(namespaces, "kube-prometheus-stack", {})) > 0 ~}
167+
affinity:
168+
nodeAffinity:
169+
requiredDuringSchedulingIgnoredDuringExecution:
170+
nodeSelectorTerms:
171+
- matchExpressions:
172+
%{ for k, v in lookup(namespaces, "kube-prometheus-stack") ~}
173+
- key: ${k}
174+
operator: In
175+
values:
176+
- ${v}
177+
%{ endfor ~}
178+
%{ endif ~}
137179

138180
prometheus:
139181
azureIdentity:
@@ -157,6 +199,19 @@ kube-prometheus-stack:
157199
requests:
158200
memory: 32Mi
159201
cpu: 10m
202+
%{ if length(lookup(namespaces, "kube-prometheus-stack",{})) > 0 ~}
203+
affinity:
204+
nodeAffinity:
205+
requiredDuringSchedulingIgnoredDuringExecution:
206+
nodeSelectorTerms:
207+
- matchExpressions:
208+
%{ for k, v in lookup(namespaces, "kube-prometheus-stack") ~}
209+
- key: ${k}
210+
operator: In
211+
values:
212+
- ${v}
213+
%{ endfor ~}
214+
%{ endif ~}
160215

161216
kube-state-metrics:
162217
resources:
@@ -167,6 +222,19 @@ kube-prometheus-stack:
167222
memory: 16Mi
168223
podSecurityPolicy:
169224
enabled: false
225+
%{ if length(lookup(namespaces, "kube-prometheus-stack", {})) > 0 ~}
226+
affinity:
227+
nodeAffinity:
228+
requiredDuringSchedulingIgnoredDuringExecution:
229+
nodeSelectorTerms:
230+
- matchExpressions:
231+
%{ for k, v in lookup(namespaces, "kube-prometheus-stack") ~}
232+
- key: ${k}
233+
operator: In
234+
values:
235+
- ${v}
236+
%{ endfor ~}
237+
%{ endif ~}
170238

171239
prometheus-node-exporter:
172240
resources:
@@ -194,6 +262,19 @@ kube-prometheus-stack:
194262
requests:
195263
cpu: 10m
196264
memory: 96Mi
265+
%{ if length(lookup(namespaces, "kube-prometheus-stack", {})) > 0 ~}
266+
affinity:
267+
nodeAffinity:
268+
requiredDuringSchedulingIgnoredDuringExecution:
269+
nodeSelectorTerms:
270+
- matchExpressions:
271+
%{ for k, v in lookup(namespaces, "kube-prometheus-stack") ~}
272+
- key: ${k}
273+
operator: In
274+
values:
275+
- ${v}
276+
%{ endfor ~}
277+
%{ endif ~}
197278

198279
loki-stack:
199280
loki:
@@ -207,7 +288,6 @@ loki-stack:
207288
index:
208289
prefix: index_
209290
period: 24h
210-
211291
storage_config:
212292
azure:
213293
container_name: ${loki_container_name}
@@ -218,6 +298,19 @@ loki-stack:
218298
active_index_directory: /data/loki/index
219299
shared_store: azure
220300
cache_location: /data/loki/boltdb-cache
301+
%{ if length(lookup(namespaces, "loki-stack", {})) > 0 ~}
302+
affinity:
303+
nodeAffinity:
304+
requiredDuringSchedulingIgnoredDuringExecution:
305+
nodeSelectorTerms:
306+
%{ for k, v in lookup(namespaces, "loki-stack") ~}
307+
- matchExpressions:
308+
- key: ${k}
309+
operator: In
310+
values:
311+
- ${v}
312+
%{ endfor ~}
313+
%{ endif ~}
221314

222315
traefik:
223316
resources:

modules/aks/azure/variables.tf

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ variable "network_policy" {
8585

8686
variable "node_pools" {
8787
default = {}
88-
description = "List of node pools with minimal configuration"
88+
description = "Map of node pools"
8989
type = map(any)
9090
}
9191

@@ -106,3 +106,29 @@ variable "sku_tier" {
106106
default = "Free"
107107
type = string
108108
}
109+
110+
variable "app_node_selectors" {
111+
/* Example:
112+
app_node_selectors = {
113+
aad-pod-identity = {
114+
"kubernetes.azure.com/agentpool" = "default"
115+
}
116+
argocd = {
117+
"odoo.camptocamp.io/nodepool" = "mutualized"
118+
"odoo.camptocamp.io/environment" = "prod"
119+
}
120+
non-existing-app = {
121+
"odoo.camptocamp.io/nodepool" = "mutualized"
122+
"odoo.camptocamp.io/environment" = "misc-apps"
123+
}
124+
kube-prometheus-stack = {
125+
"kubernetes.azure.com/agentpool" = "default"
126+
}
127+
loki-stack = {}
128+
#cert-manager = {}
129+
}
130+
*/
131+
description = "Map of argoCD apps to node selector"
132+
default = {}
133+
type = map(map(string))
134+
}

modules/values.tmpl.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ apps:
3737
enabled: ${metrics_server.enable}
3838
minio:
3939
enabled: ${minio.enable}
40-
namespaces:
41-
enabled: true
4240
secrets-store-csi-driver:
4341
enabled: false
4442
traefik:

0 commit comments

Comments
 (0)