Skip to content

Commit 5ee7809

Browse files
committed
Remove mkNCSI and apply labels explicitly to all resources
Removes the mkNCSI helper function that was automatically applying labels to all resources. Instead, labels are now applied explicitly to each resource: - Added cfg.matchLabels for selector matching (without version label) - Updated cfg.labels to include version for metadata.labels - Changed all kubenix modules to apply labels directly: - Added metadata.labels = labels where labels = cfg.labels // { component: "..." } - Updated selector.matchLabels to use cfg.matchLabels // labels for specificity - Moved cfg.metadata from _module.args to an internal cfg option - Removed mkNCSI function entirely This makes label application explicit and easier to understand in each resource.
1 parent 139ad95 commit 5ee7809

10 files changed

Lines changed: 172 additions & 130 deletions

File tree

kubenix/builder.nix

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
x86Pkgs,
77
armPkgs,
88
curPkgs,
9-
mkNCSI,
109
...
1110
}:
1211
let
@@ -85,9 +84,10 @@ in
8584
};
8685
config =
8786
let
88-
baseLabels = {
87+
labels = cfg.labels // {
8988
"app.kubernetes.io/component" = "builder";
9089
};
90+
matchLabels = cfg.matchLabels // labels;
9191
in
9292
lib.mkIf (cfg.enable && cfg.builders.enable) {
9393
nix-csi.builders.nixConfig.settings.sandbox = cfg.builders.privilegedSandboxedBuilds;
@@ -195,31 +195,33 @@ in
195195
n: v:
196196
let
197197
v2 = lib.recursiveUpdate v {
198-
labels = baseLabels // {
198+
labels = labels // {
199199
"kubernetes.io/arch" = v.arch;
200200
"nix.csi/deployment" = n;
201201
};
202202
};
203203
in
204-
mkNCSI {
204+
{
205+
metadata.labels = v2.labels;
205206
spec = {
206207
replicas = v.replicas;
207-
selector.matchLabels = v2.labels;
208+
selector.matchLabels = cfg.matchLabels // v2.labels;
208209
template = podTemplate v2;
209210
};
210211
}
211212
) (lib.filterAttrs (n: v: v.enable) cfg.builders.deployments))
212213
// {
213-
proxy = lib.mkIf (cfg.builders.loadBalancerPort != null) {
214-
spec =
215-
let
216-
labels = {
217-
"app.kubernetes.io/component" = "proxy";
218-
};
219-
in
220-
{
214+
proxy = lib.mkIf (cfg.builders.loadBalancerPort != null) (
215+
let
216+
labels = cfg.labels // {
217+
"app.kubernetes.io/component" = "proxy";
218+
};
219+
in
220+
{
221+
metadata.labels = labels;
222+
spec = {
221223
replicas = 1;
222-
selector.matchLabels = labels;
224+
selector.matchLabels = cfg.matchLabels // labels;
223225
template = {
224226
metadata.labels = labels;
225227
metadata.annotations = {
@@ -279,22 +281,24 @@ in
279281
};
280282
};
281283
};
282-
};
284+
}
285+
);
283286
};
284287

285288
DaemonSet = lib.mapAttrs (
286289
n: v:
287290
let
288291
v2 = lib.recursiveUpdate v {
289-
labels = baseLabels // {
292+
labels = labels // {
290293
"kubernetes.io/arch" = v.arch;
291294
"nix.csi/daemonset" = n;
292295
};
293296
};
294297
in
295-
mkNCSI {
298+
{
299+
metadata.labels = v2.labels;
296300
spec = {
297-
selector.matchLabels = v2.labels;
301+
selector.matchLabels = cfg.matchLabels // v2.labels;
298302
template = lib.recursiveUpdate (podTemplate v2) {
299303
spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms = [
300304
{
@@ -312,32 +316,32 @@ in
312316
) (lib.filterAttrs (n: v: v.enable) cfg.builders.daemonsets);
313317

314318
# Headless service for DNS discovery of individual builder pods
315-
Service.nix-csi-builders = mkNCSI {
319+
Service.nix-csi-builders = {
320+
metadata.labels = labels;
316321
spec = {
317322
clusterIP = "None";
318-
selector = baseLabels;
323+
selector = matchLabels;
319324
ports = lib.mkNamedList {
320325
ssh.port = 22;
321326
};
322327
};
323328
};
324329

325-
Service.nix-proxy =
326-
lib.mkIf (cfg.builders.enable && cfg.builders.loadBalancerPort != null)
327-
(mkNCSI {
328-
spec = {
329-
selector = baseLabels // {
330-
"nix.csi/proxy" = "true";
331-
};
332-
ports = lib.mkNamedList {
333-
ssh = {
334-
port = cfg.builders.loadBalancerPort;
335-
targetPort = "ssh";
336-
};
337-
};
338-
type = "LoadBalancer";
330+
Service.nix-proxy = lib.mkIf (cfg.builders.enable && cfg.builders.loadBalancerPort != null) {
331+
metadata.labels = labels;
332+
spec = {
333+
selector = matchLabels // {
334+
"nix.csi/proxy" = "true";
335+
};
336+
ports = lib.mkNamedList {
337+
ssh = {
338+
port = cfg.builders.loadBalancerPort;
339+
targetPort = "ssh";
339340
};
340-
});
341+
};
342+
type = "LoadBalancer";
343+
};
344+
};
341345
};
342346
};
343347
}

kubenix/cache.nix

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
x86Pkgs,
77
armPkgs,
88
curPkgs,
9-
mkNCSI,
109
...
1110
}:
1211
let
@@ -36,13 +35,15 @@ in
3635
};
3736
config =
3837
let
39-
labels = {
38+
labels = cfg.labels // {
4039
"app.kubernetes.io/component" = "cache";
4140
};
41+
matchLabels = cfg.matchLabels // labels;
4242
in
4343
lib.mkIf (cfg.enable && cfg.cache.enable) {
4444
kubernetes.resources.${cfg.namespace} = {
45-
StatefulSet.nix-cache = mkNCSI {
45+
StatefulSet.nix-cache = {
46+
metadata.labels = labels;
4647
spec = {
4748
serviceName = "nix-cache";
4849
replicas = 1;
@@ -169,9 +170,10 @@ in
169170
};
170171
};
171172

172-
Service.nix-cache = mkNCSI {
173+
Service.nix-cache = {
174+
metadata.labels = labels;
173175
spec = {
174-
selector = labels;
176+
selector = matchLabels;
175177
ports = lib.mkNamedList {
176178
ssh = {
177179
port = 22;
@@ -181,9 +183,10 @@ in
181183
type = "ClusterIP";
182184
};
183185
};
184-
Service.nix-cache-lb = lib.mkIf (cfg.cache.loadBalancerPort != null) (mkNCSI {
186+
Service.nix-cache-lb = lib.mkIf (cfg.cache.loadBalancerPort != null) {
187+
metadata.labels = labels;
185188
spec = {
186-
selector = labels;
189+
selector = matchLabels;
187190
ports = lib.mkNamedList {
188191
ssh = {
189192
port = cfg.cache.loadBalancerPort;
@@ -192,7 +195,7 @@ in
192195
};
193196
type = "LoadBalancer";
194197
};
195-
});
198+
};
196199
};
197200
};
198201
}

kubenix/config.nix

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ in
4646
};
4747
};
4848
kubernetes.resources.${cfg.namespace} = {
49-
ConfigMap.push = lib.mkIf cfg.push (mkNCSI {
49+
ConfigMap.push = lib.mkIf cfg.push {
50+
metadata.labels = cfg.labels;
5051
data = {
5152
builder-aarch64-linux = armPkgs.nix-csi-builder-env;
5253
builder-x86_64-linux = x86Pkgs.nix-csi-builder-env;
@@ -57,20 +58,23 @@ in
5758
proxy-aarch64-linux = armPkgs.nix-csi-proxy-env;
5859
proxy-x86_64-linux = x86Pkgs.nix-csi-proxy-env;
5960
};
60-
});
61-
ConfigMap.nix-node = mkNCSI {
61+
};
62+
ConfigMap.nix-node = {
63+
metadata.labels = cfg.labels;
6264
data = {
6365
"nix.conf" = builtins.readFile (cfg.node.nixConfig.nixConf);
6466
"logging.json" = builtins.toJSON cfg.loggingConfig;
6567
};
6668
};
67-
ConfigMap.nix-cache = mkNCSI {
69+
ConfigMap.nix-cache = {
70+
metadata.labels = cfg.labels;
6871
data = {
6972
"nix.conf" = builtins.readFile (cfg.cache.nixConfig.nixConf);
7073
"logging.json" = builtins.toJSON cfg.loggingConfig;
7174
};
7275
};
73-
ConfigMap.nix-builder = mkNCSI {
76+
ConfigMap.nix-builder = {
77+
metadata.labels = cfg.labels;
7478
data = {
7579
"nix.conf" = builtins.readFile (cfg.builders.nixConfig.nixConf);
7680
"logging.json" = builtins.toJSON cfg.loggingConfig;

kubenix/csidriver.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ let
1111
in
1212
{
1313
config = lib.mkIf cfg.enable {
14-
kubernetes.resources.none.CSIDriver."nix.csi.store" = mkNCSI {
14+
kubernetes.resources.none.CSIDriver."nix.csi.store" = {
15+
metadata.labels = cfg.labels;
1516
spec = {
1617
attachRequired = false;
1718
podInfoOnMount = true;

kubenix/daemonset.nix

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
x86Pkgs,
77
armPkgs,
88
curPkgs,
9-
mkNCSI,
109
...
1110
}:
1211
let
@@ -25,13 +24,15 @@ in
2524
};
2625
config =
2726
let
28-
labels = {
27+
labels = cfg.labels // {
2928
"app.kubernetes.io/component" = "node";
3029
};
30+
matchLabels = cfg.matchLabels // labels;
3131
in
3232
lib.mkIf cfg.enable {
3333
kubernetes.resources.${cfg.namespace} = {
34-
DaemonSet.nix-node = mkNCSI {
34+
DaemonSet.nix-node = {
35+
metadata.labels = labels;
3536
spec = {
3637
updateStrategy = {
3738
type = "RollingUpdate";
@@ -209,10 +210,11 @@ in
209210
};
210211
};
211212
# DNS for pods
212-
Service.${cfg.internalServiceName} = mkNCSI {
213+
Service.${cfg.internalServiceName} = {
214+
metadata.labels = labels;
213215
spec = {
214216
clusterIP = "None";
215-
selector = labels;
217+
selector = matchLabels;
216218
ports = lib.mkNamedList {
217219
ssh.port = 22;
218220
};

kubenix/namespace.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ let
1212
in
1313
{
1414
config = lib.mkIf cfg.enable {
15-
kubernetes.resources.none.Namespace.${namespace} = mkNCSI { };
15+
kubernetes.resources.none.Namespace.${namespace} = {
16+
metadata.labels = cfg.labels;
17+
};
1618
};
1719
}

kubenix/options.nix

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ in
5353
apply = lib.mapAttrs (n: v: lib.trim (if lib.typeOf v == "path" then builtins.readFile v else v));
5454
default = { };
5555
};
56-
labels = lib.mkOption {
57-
description = "Labels added to nix-csi resources";
58-
type = lib.types.attrsOf lib.types.str;
56+
metadata = lib.mkOption {
57+
description = "Metadata (labels, annotations) applied to nix-csi resources";
58+
type = (curPkgs.formats.json { }).type;
5959
default = { };
6060
};
6161
version = lib.mkOption {
@@ -148,6 +148,16 @@ in
148148
internal = true;
149149
default = inputs.dinix;
150150
};
151+
labels = lib.mkOption {
152+
type = lib.types.attrsOf lib.types.str;
153+
internal = true;
154+
description = "All nix-csi labels including version (for metadata.labels)";
155+
};
156+
matchLabels = lib.mkOption {
157+
type = lib.types.attrsOf lib.types.str;
158+
internal = true;
159+
description = "nix-csi base labels without version (for selector.matchLabels)";
160+
};
151161
};
152162
config =
153163
let
@@ -182,14 +192,19 @@ in
182192
x86Pkgs = mkPkgs "x86_64-linux";
183193
armPkgs = mkPkgs "aarch64-linux";
184194
curPkgs = mkPkgs builtins.currentSystem;
185-
mkNCSI =
186-
attrs:
187-
lib.recursiveUpdate {
188-
metadata.labels = cfg.labels;
189-
} attrs;
190195
subPath = spath: lib.removePrefix "/" (toString spath);
191196
};
192197

198+
# Set internal label options using the derived values
199+
nix-csi.matchLabels = {
200+
"app.kubernetes.io/name" = "nix-csi";
201+
"app.kubernetes.io/part-of" = "nix-csi";
202+
"app.kubernetes.io/managed-by" = "nix";
203+
};
204+
nix-csi.labels = cfg.matchLabels // {
205+
"app.kubernetes.io/version" = cfg.version;
206+
};
207+
193208
kubernetes.transformers = lib.optional (!cfg.push) (
194209
resource:
195210
let
@@ -207,12 +222,5 @@ in
207222
else
208223
resource
209224
);
210-
211-
nix-csi.labels = {
212-
"app.kubernetes.io/name" = "nix-csi";
213-
"app.kubernetes.io/part-of" = "nix-csi";
214-
"app.kubernetes.io/managed-by" = "nix";
215-
"app.kubernetes.io/version" = cfg.version;
216-
};
217225
};
218226
}

0 commit comments

Comments
 (0)