-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.nix
More file actions
928 lines (873 loc) · 33.4 KB
/
Copy pathcontroller.nix
File metadata and controls
928 lines (873 loc) · 33.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
# Seed controller — reconciles instance definitions into Kata pods
#
# Deploys as k8s-native components:
# - seed-controller: Deployment pod (reconciliation engine + webhook)
# - seed-host-agent: DaemonSet pod (privileged, manages swtpm on host)
# - seed-builder: Jobs created by controller for nix build/eval
#
# The controller runs inside the cluster with proper RBAC, communicates
# via standard k8s primitives (CRDs, ConfigMaps, Jobs), and self-heals.
{ config, lib, pkgs, ... }:
let
cfg = config.seed.controller;
# SeedHostTask CRD definition
seedHostTaskCRD = pkgs.writeText "seed-hosttask-crd.yaml" (builtins.toJSON {
apiVersion = "apiextensions.k8s.io/v1";
kind = "CustomResourceDefinition";
metadata.name = "seedhosttasks.seed.loom.farm";
spec = {
group = "seed.loom.farm";
versions = [{
name = "v1alpha1";
served = true;
storage = true;
schema.openAPIV3Schema = {
type = "object";
properties = {
spec = {
type = "object";
properties = {
type = { type = "string"; };
instance = { type = "string"; };
namespace = { type = "string"; };
};
required = [ "type" "instance" "namespace" ];
};
status = {
type = "object";
properties = {
ready = { type = "boolean"; };
socketPath = { type = "string"; };
message = { type = "string"; };
};
};
};
};
subresources.status = {};
}];
scope = "Namespaced";
names = {
plural = "seedhosttasks";
singular = "seedhosttask";
kind = "SeedHostTask";
shortNames = [ "sht" ];
};
};
});
# SeedFlake CRD definition — dynamic flake registry with invite codes
seedFlakeCRD = pkgs.writeText "seed-flake-crd.yaml" (builtins.toJSON {
apiVersion = "apiextensions.k8s.io/v1";
kind = "CustomResourceDefinition";
metadata.name = "seedflakes.seed.loom.farm";
spec = {
group = "seed.loom.farm";
versions = [{
name = "v1alpha1";
served = true;
storage = true;
schema.openAPIV3Schema = {
type = "object";
properties = {
spec = {
type = "object";
properties = {
inviteCode = { type = "string"; };
flakeUri = { type = "string"; };
identity = { type = "string"; };
};
};
status = {
type = "object";
properties = {
namespace = { type = "string"; };
state = { type = "string"; enum = [ "pending" "active" ]; };
generation = { type = "string"; };
lastReconciled = { type = "string"; };
};
};
};
};
subresources.status = {};
}];
scope = "Cluster";
names = {
plural = "seedflakes";
singular = "seedflake";
kind = "SeedFlake";
shortNames = [ "sf" ];
};
};
});
# SeedDNSRecord CRD definition
seedDNSRecordCRD = pkgs.writeText "seed-dnsrecord-crd.yaml" (builtins.toJSON {
apiVersion = "apiextensions.k8s.io/v1";
kind = "CustomResourceDefinition";
metadata.name = "seeddnsrecords.seed.loom.farm";
spec = {
group = "seed.loom.farm";
versions = [{
name = "v1alpha1";
served = true;
storage = true;
schema.openAPIV3Schema = {
type = "object";
properties = {
spec = {
type = "object";
properties = {
name = { type = "string"; };
type = { type = "string"; enum = [ "A" "AAAA" "CNAME" ]; };
ttl = { type = "integer"; };
records = {
type = "array";
items = {
type = "object";
properties = {
content = { type = "string"; };
};
required = [ "content" ];
};
};
sourceRef = {
type = "object";
properties = {
kind = { type = "string"; };
name = { type = "string"; };
};
required = [ "kind" "name" ];
};
domainRef = {
type = "object";
properties = {
name = { type = "string"; };
};
required = [ "name" ];
};
};
required = [ "name" "type" "ttl" ];
};
status = {
type = "object";
properties = {
synced = { type = "boolean"; };
resolvedRecords = {
type = "array";
items = {
type = "object";
properties = {
content = { type = "string"; };
};
};
};
message = { type = "string"; };
lastSyncedAt = { type = "string"; };
};
};
};
};
subresources.status = {};
additionalPrinterColumns = [
{ name = "DNS Name"; type = "string"; jsonPath = ".spec.name"; }
{ name = "Type"; type = "string"; jsonPath = ".spec.type"; }
{ name = "Synced"; type = "boolean"; jsonPath = ".status.synced"; }
{ name = "Age"; type = "date"; jsonPath = ".metadata.creationTimestamp"; }
];
}];
scope = "Namespaced";
names = {
plural = "seeddnsrecords";
singular = "seeddnsrecord";
kind = "SeedDNSRecord";
shortNames = [ "sdr" ];
};
};
});
# SeedDomain CRD definition — domain registration + zone lifecycle
seedDomainCRD = pkgs.writeText "seed-domain-crd.yaml" (builtins.toJSON {
apiVersion = "apiextensions.k8s.io/v1";
kind = "CustomResourceDefinition";
metadata.name = "seeddomains.seed.loom.farm";
spec = {
group = "seed.loom.farm";
versions = [{
name = "v1alpha1";
served = true;
storage = true;
schema.openAPIV3Schema = {
type = "object";
properties = {
spec = {
type = "object";
properties = {
name = { type = "string"; };
register = { type = "boolean"; };
registrar = { type = "string"; enum = [ "namesilo" ]; };
};
required = [ "name" ];
};
status = {
type = "object";
properties = {
phase = { type = "string"; enum = [
"Pending" "Registering" "Registered" "Delegating"
"Delegated" "ZoneReady" "Error"
]; };
registered = { type = "boolean"; };
nsConfigured = { type = "boolean"; };
zoneReady = { type = "boolean"; };
registrarDomainId = { type = "string"; };
expiresAt = { type = "string"; };
message = { type = "string"; };
lastSyncedAt = { type = "string"; };
};
};
};
};
subresources.status = {};
additionalPrinterColumns = [
{ name = "Domain"; type = "string"; jsonPath = ".spec.name"; }
{ name = "Phase"; type = "string"; jsonPath = ".status.phase"; }
{ name = "Zone Ready"; type = "boolean"; jsonPath = ".status.zoneReady"; }
{ name = "Expires"; type = "string"; jsonPath = ".status.expiresAt"; }
{ name = "Age"; type = "date"; jsonPath = ".metadata.creationTimestamp"; }
];
}];
scope = "Namespaced";
names = {
plural = "seeddomains";
singular = "seeddomain";
kind = "SeedDomain";
shortNames = [ "sd" ];
};
};
});
# Namespace for seed system components
seedSystemNS = "seed-system";
# Standard labels for system components
systemLabels = component: {
"app.kubernetes.io/name" = "seed-${component}";
"app.kubernetes.io/part-of" = "seed";
"seed.loom.farm/managed-by" = "seed";
"seed.loom.farm/component" = component;
};
# ServiceAccount for controller
controllerSA = pkgs.writeText "seed-controller-sa.yaml" (builtins.toJSON {
apiVersion = "v1";
kind = "ServiceAccount";
metadata = {
name = "seed-controller";
namespace = seedSystemNS;
};
});
# ServiceAccount for builder Jobs
builderSA = pkgs.writeText "seed-builder-sa.yaml" (builtins.toJSON {
apiVersion = "v1";
kind = "ServiceAccount";
metadata = {
name = "seed-builder";
namespace = seedSystemNS;
};
});
# ClusterRole for controller
controllerRole = pkgs.writeText "seed-controller-role.yaml" (builtins.toJSON {
apiVersion = "rbac.authorization.k8s.io/v1";
kind = "ClusterRole";
metadata.name = "seed-controller";
rules = [
{
apiGroups = [ "" ];
resources = [ "namespaces" "pods" "persistentvolumeclaims" "services" "configmaps" "endpoints" "events" "nodes" "secrets" ];
verbs = [ "get" "list" "watch" "create" "update" "patch" "delete" ];
}
{
apiGroups = [ "" ];
resources = [ "pods/log" ];
verbs = [ "get" ];
}
{
apiGroups = [ "apps" ];
resources = [ "deployments" ];
verbs = [ "get" "list" "watch" "create" "update" "patch" "delete" ];
}
{
apiGroups = [ "batch" ];
resources = [ "jobs" ];
verbs = [ "get" "list" "watch" "create" "delete" ];
}
{
apiGroups = [ "node.k8s.io" ];
resources = [ "runtimeclasses" ];
verbs = [ "get" "create" "update" "patch" ];
}
{
apiGroups = [ "metallb.io" ];
resources = [ "ipaddresspools" "l2advertisements" "bgppeers" "bgpadvertisements" ];
verbs = [ "get" "list" "create" "update" "patch" "delete" ];
}
{
apiGroups = [ "seed.loom.farm" ];
resources = [ "seedhosttasks" "seedhosttasks/status" ];
verbs = [ "get" "list" "watch" "create" "update" "patch" "delete" ];
}
{
apiGroups = [ "seed.loom.farm" ];
resources = [ "seedflakes" "seedflakes/status" ];
verbs = [ "get" "list" "watch" "create" "update" "patch" "delete" ];
}
{
apiGroups = [ "seed.loom.farm" ];
resources = [ "seeddnsrecords" "seeddnsrecords/status" ];
verbs = [ "get" "list" "watch" "create" "update" "patch" "delete" ];
}
{
apiGroups = [ "seed.loom.farm" ];
resources = [ "seeddomains" "seeddomains/status" ];
verbs = [ "get" "list" "watch" "create" "update" "patch" "delete" ];
}
{
apiGroups = [ "cert-manager.io" ];
resources = [ "certificaterequests" ];
verbs = [ "get" "create" "delete" ];
}
];
});
# ClusterRoleBinding for controller
controllerRoleBinding = pkgs.writeText "seed-controller-rolebinding.yaml" (builtins.toJSON {
apiVersion = "rbac.authorization.k8s.io/v1";
kind = "ClusterRoleBinding";
metadata.name = "seed-controller";
subjects = [{
kind = "ServiceAccount";
name = "seed-controller";
namespace = seedSystemNS;
}];
roleRef = {
apiGroup = "rbac.authorization.k8s.io";
kind = "ClusterRole";
name = "seed-controller";
};
});
# ClusterRole for builder (needs configmap write in instance namespace)
builderRole = pkgs.writeText "seed-builder-role.yaml" (builtins.toJSON {
apiVersion = "rbac.authorization.k8s.io/v1";
kind = "ClusterRole";
metadata.name = "seed-builder";
rules = [
{
apiGroups = [ "" ];
resources = [ "configmaps" ];
verbs = [ "get" "create" "update" "patch" ];
}
];
});
builderRoleBinding = pkgs.writeText "seed-builder-rolebinding.yaml" (builtins.toJSON {
apiVersion = "rbac.authorization.k8s.io/v1";
kind = "ClusterRoleBinding";
metadata.name = "seed-builder";
subjects = [{
kind = "ServiceAccount";
name = "seed-builder";
namespace = seedSystemNS;
}];
roleRef = {
apiGroup = "rbac.authorization.k8s.io";
kind = "ClusterRole";
name = "seed-builder";
};
});
# Controller Deployment
controllerDeployment = pkgs.writeText "seed-controller-deployment.yaml" (builtins.toJSON {
apiVersion = "apps/v1";
kind = "Deployment";
metadata = {
name = "seed-controller";
namespace = seedSystemNS;
labels = systemLabels "controller";
};
spec = {
replicas = 1;
selector.matchLabels."app.kubernetes.io/name" = "seed-controller";
template = {
metadata.labels = systemLabels "controller";
spec = {
serviceAccountName = "seed-controller";
# Pin to this node — controller needs hostPath secrets (sops-nix)
nodeSelector."kubernetes.io/hostname" = config.networking.hostName;
# Default runtime — controller doesn't need Kata
containers = [{
name = "controller";
image = "nix:0${cfg.controllerImage}";
command = [ "${pkgs.nodejs_22}/bin/node" "/app/controller.mjs" ];
# Cluster-level config (reserved IPs, etc.) from ConfigMap.
# Created by provisioner after cluster bootstrap. Optional so the
# controller starts even before the ConfigMap exists.
envFrom = [{
configMapRef = {
name = "seed-cluster-config";
optional = true;
};
}];
env = [
{ name = "SEED_FLAKE_PATHS"; value = builtins.concatStringsSep "," cfg.flakePaths; }
{ name = "SEED_WEBHOOK_PORT"; value = toString cfg.webhook.port; }
{ name = "SEED_SWTPM_ENABLED"; value = if cfg.swtpmEnabled then "1" else ""; }
{ name = "SEED_BUILDER_IMAGE"; value = cfg.builderImage; }
# nix needs flakes + nix-command enabled, and daemon mode (store is read-only mount)
{ name = "NIX_CONFIG"; value = "experimental-features = nix-command flakes"; }
{ name = "NIX_REMOTE"; value = "daemon"; }
# CA certs for HTTPS (nix fetching from GitHub)
{ name = "NIX_SSL_CERT_FILE"; value = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; }
{ name = "SSL_CERT_FILE"; value = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; }
{ name = "GIT_SSL_CAINFO"; value = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; }
# PATH: nix + git + coreutils (for nix eval/build)
{ name = "PATH"; value = lib.makeBinPath [ pkgs.nix pkgs.git pkgs.coreutils pkgs.gnutar pkgs.gzip pkgs.xz pkgs.age pkgs.age-plugin-tpm ]; }
{ name = "SEED_SILO_HOST"; value = cfg.siloHost; }
] ++ lib.optional (cfg.ipv4Address != "") {
name = "SEED_IPV4_ADDRESS"; value = cfg.ipv4Address;
} ++ lib.optional (cfg.ipv6Block != "") {
name = "SEED_IPV6_BLOCK"; value = cfg.ipv6Block;
} ++ lib.optionals cfg.bgp.enable [
{ name = "SEED_BGP_MY_ASN"; value = toString cfg.bgp.myASN; }
{ name = "SEED_BGP_PEER_ASN"; value = toString cfg.bgp.peerASN; }
{ name = "SEED_BGP_PEER_ADDRESS"; value = cfg.bgp.peerAddress; }
{ name = "SEED_BGP_PEER_ADDRESS_IPV6"; value = cfg.bgp.peerAddressIPv6; }
{ name = "SEED_BGP_PASSWORD"; value = cfg.bgp.password; }
] ++ lib.optional (cfg.dns.apiUrl != "") {
name = "SEED_PDNS_API_URL"; value = cfg.dns.apiUrl;
} ++ lib.optional (cfg.dns.apiKeyFile != "") {
name = "SEED_PDNS_API_KEY_FILE"; value = cfg.dns.apiKeyFile;
} ++ lib.optional (cfg.dns.zone != "loom.farm.") {
name = "SEED_PDNS_ZONE"; value = cfg.dns.zone;
} ++ lib.optional (cfg.dns.instanceDomain != "seed.loom.farm") {
name = "SEED_INSTANCE_DOMAIN"; value = cfg.dns.instanceDomain;
} ++ lib.optional (cfg.registrar.apiKeyFile != "") {
name = "SEED_NAMESILO_API_KEY_FILE"; value = cfg.registrar.apiKeyFile;
} ++ lib.optional (cfg.webhook.secretFile != "") {
name = "SEED_WEBHOOK_SECRET_FILE"; value = cfg.webhook.secretFile;
} ++ lib.optional (cfg.acme.accountKey != "") {
name = "SEED_ACME_ACCOUNT_KEY_FILE"; value = "/etc/seed/acme/account-key.pem";
} ++ lib.optional (cfg.acme.accountKey != "") {
name = "SEED_ACME_ENABLED"; value = "1";
} ++ lib.optional cfg.poolManager.enable {
name = "SEED_POOL_MANAGER_URL"; value = "http://seed-pool-manager.${seedSystemNS}.svc.cluster.local:${toString cfg.poolManager.port}";
};
ports = [{
containerPort = cfg.webhook.port;
name = "webhook";
protocol = "TCP";
}];
volumeMounts = [
{ name = "nix-daemon"; mountPath = "/nix/var/nix/daemon-socket"; }
{ name = "nix-store"; mountPath = "/nix/store"; readOnly = true; }
{ name = "secrets"; mountPath = "/etc/seed/secrets"; readOnly = true; }
{ name = "tpm-state"; mountPath = "/var/lib/seed-controller/tpm"; readOnly = true; }
{ name = "tls"; mountPath = "/etc/seed/tls"; readOnly = true; }
] ++ lib.optional (cfg.acme.accountKey != "") {
name = "acme-account-key";
mountPath = "/etc/seed/acme";
readOnly = true;
};
}];
volumes = [
{ name = "nix-daemon"; hostPath.path = "/nix/var/nix/daemon-socket"; }
{ name = "nix-store"; hostPath.path = "/nix/store"; }
{ name = "secrets"; secret = { secretName = "seed-controller-secrets"; optional = true; }; }
{ name = "tpm-state"; hostPath = { path = "/var/lib/seed-controller/tpm"; type = "DirectoryOrCreate"; }; }
{ name = "tls"; secret.secretName = "seed-controller-tls"; }
] ++ lib.optional (cfg.acme.accountKey != "") {
name = "acme-account-key";
secret.secretName = "seed-acme-account-key";
};
};
};
};
});
# Controller webhook Service (for Caddy reverse proxy)
controllerService = pkgs.writeText "seed-controller-service.yaml" (builtins.toJSON {
apiVersion = "v1";
kind = "Service";
metadata = {
name = "seed-controller";
namespace = seedSystemNS;
};
spec = {
selector."app.kubernetes.io/name" = "seed-controller";
ports = [{
port = cfg.webhook.port;
targetPort = cfg.webhook.port;
protocol = "TCP";
name = "webhook";
}];
};
});
# Host agent DaemonSet (privileged — manages swtpm on host)
hostAgentDaemonSet = pkgs.writeText "seed-host-agent-daemonset.yaml" (builtins.toJSON {
apiVersion = "apps/v1";
kind = "DaemonSet";
metadata = {
name = "seed-host-agent";
namespace = seedSystemNS;
labels = systemLabels "host-agent";
};
spec = {
selector.matchLabels."app.kubernetes.io/name" = "seed-host-agent";
template = {
metadata.labels = systemLabels "host-agent";
spec = {
serviceAccountName = "seed-controller";
# Default runtime — host agent needs host access, not Kata
hostPID = false;
hostNetwork = true;
containers = [{
name = "host-agent";
image = "nix:0${cfg.hostAgentImage}";
command = [ "${pkgs.nodejs_22}/bin/node" "/app/host-agent.mjs" ];
securityContext.privileged = true;
env = [
{ name = "SEED_NETPOL_ENABLED"; value = if cfg.netpol.enable then "1" else ""; }
{ name = "SEED_CLUSTER_CIDR"; value = "10.42.0.0/16,fd00::/56"; }
{ name = "SEED_SERVICE_CIDR"; value = "10.43.0.0/16,fd01::/108"; }
{ name = "SEED_DNS_IP"; value = "10.43.0.10"; }
{
name = "SEED_NODE_NAME";
valueFrom.fieldRef.fieldPath = "spec.nodeName";
}
];
volumeMounts = [
{ name = "tpm-state"; mountPath = "/var/lib/seed-controller/tpm"; }
{ name = "swtpm-sockets"; mountPath = "/run/swtpm"; }
];
}];
volumes = [
{ name = "tpm-state"; hostPath = { path = "/var/lib/seed-controller/tpm"; type = "DirectoryOrCreate"; }; }
{ name = "swtpm-sockets"; hostPath = { path = "/run/swtpm"; type = "DirectoryOrCreate"; }; }
];
};
};
};
});
# Pool manager DaemonSet (privileged — manages CLH VMs on host)
poolManagerDaemonSet = pkgs.writeText "seed-pool-manager-daemonset.yaml" (builtins.toJSON {
apiVersion = "apps/v1";
kind = "DaemonSet";
metadata = {
name = "seed-pool-manager";
namespace = seedSystemNS;
labels = systemLabels "pool-manager";
};
spec = {
selector.matchLabels."app.kubernetes.io/name" = "seed-pool-manager";
template = {
metadata.labels = systemLabels "pool-manager";
spec = {
serviceAccountName = "seed-controller";
hostPID = false;
hostNetwork = true;
containers = [{
name = "pool-manager";
image = "nix:0${cfg.poolManager.image}";
command = [ "${pkgs.nodejs_22}/bin/node" "/app/pool-manager.mjs" ];
securityContext.privileged = true;
env = [
{ name = "SEED_POOL_SIZE"; value = toString cfg.poolManager.poolSize; }
{ name = "SEED_POOL_PORT"; value = toString cfg.poolManager.port; }
{ name = "NIX_REMOTE"; value = "daemon"; }
{ name = "NIX_CONFIG"; value = "experimental-features = nix-command flakes"; }
{ name = "NIX_SSL_CERT_FILE"; value = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; }
{ name = "SSL_CERT_FILE"; value = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; }
{ name = "GIT_SSL_CAINFO"; value = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; }
{ name = "PATH"; value = lib.makeBinPath [ pkgs.nix pkgs.git pkgs.coreutils pkgs.gnutar pkgs.gzip pkgs.xz pkgs.socat ]; }
];
ports = [{
containerPort = cfg.poolManager.port;
name = "api";
protocol = "TCP";
}];
volumeMounts = [
{ name = "nix-store"; mountPath = "/nix/store"; readOnly = true; }
{ name = "nix-var"; mountPath = "/nix/var/nix"; }
{ name = "nix-cache"; mountPath = "/root/.cache/nix"; }
{ name = "dev-kvm"; mountPath = "/dev/kvm"; }
{ name = "dev-vhost-vsock"; mountPath = "/dev/vhost-vsock"; }
{ name = "pool-state"; mountPath = "/run/seed-pool"; }
{ name = "k3s-storage"; mountPath = "/var/lib/rancher/k3s/storage"; }
];
}];
volumes = [
{ name = "nix-store"; hostPath.path = "/nix/store"; }
{ name = "nix-var"; hostPath = { path = "/nix/var/nix"; type = "Directory"; }; }
{ name = "nix-cache"; hostPath = { path = "/root/.cache/nix"; type = "DirectoryOrCreate"; }; }
{ name = "dev-kvm"; hostPath = { path = "/dev/kvm"; type = "CharDevice"; }; }
{ name = "dev-vhost-vsock"; hostPath = { path = "/dev/vhost-vsock"; type = "CharDevice"; }; }
{ name = "pool-state"; hostPath = { path = "/var/lib/seed-pool"; type = "DirectoryOrCreate"; }; }
{ name = "k3s-storage"; hostPath = { path = "/var/lib/rancher/k3s/storage"; type = "DirectoryOrCreate"; }; }
];
};
};
};
});
# Pool manager Service — internalTrafficPolicy: Local ensures requests
# from instance pods always reach the pool manager on the same node.
# This is critical because shoots mount node-local PVC storage via virtiofs.
poolManagerService = pkgs.writeText "seed-pool-manager-service.yaml" (builtins.toJSON {
apiVersion = "v1";
kind = "Service";
metadata = {
name = "seed-pool-manager";
namespace = seedSystemNS;
};
spec = {
selector."app.kubernetes.io/name" = "seed-pool-manager";
internalTrafficPolicy = "Local";
ports = [{
port = cfg.poolManager.port;
targetPort = cfg.poolManager.port;
protocol = "TCP";
name = "api";
}];
};
});
# TLS Certificate for the controller (issued by seed-ca via cert-manager)
controllerCert = pkgs.writeText "seed-controller-cert.yaml" (builtins.toJSON {
apiVersion = "cert-manager.io/v1";
kind = "Certificate";
metadata = {
name = "seed-controller-tls";
namespace = seedSystemNS;
};
spec = {
secretName = "seed-controller-tls";
duration = "2160h"; # 90 days
renewBefore = "720h"; # 30 days before expiry
privateKey = {
algorithm = "ECDSA";
size = 256;
};
dnsNames = [
"seed-controller"
"seed-controller.${seedSystemNS}"
"seed-controller.${seedSystemNS}.svc"
"seed-controller.${seedSystemNS}.svc.cluster.local"
];
issuerRef = {
name = "seed-ca";
kind = "ClusterIssuer";
group = "cert-manager.io";
};
};
});
# Namespace manifest
seedSystemNamespace = pkgs.writeText "seed-system-namespace.yaml" (builtins.toJSON {
apiVersion = "v1";
kind = "Namespace";
metadata.name = seedSystemNS;
});
# ACME account key k8s Secret — JSON with sops placeholder for runtime substitution.
# The calling profile creates sops.templates from this and passes the rendered
# path via extraManifestPaths. Same pattern as mkCephCsiRbd.secretManifestJSON.
acmeSecretJSON = builtins.toJSON {
apiVersion = "v1";
kind = "Secret";
metadata = {
name = "seed-acme-account-key";
namespace = seedSystemNS;
};
data = {
"account-key.pem" = cfg.acme.accountKey;
};
};
# Combined manifests directory for seed.k8s.services
controllerManifests = pkgs.linkFarm "seed-controller-manifests" (
[
{ name = "01-namespace.json"; path = seedSystemNamespace; }
{ name = "02-hosttask-crd.json"; path = seedHostTaskCRD; }
{ name = "02-flake-crd.json"; path = seedFlakeCRD; }
{ name = "02-dnsrecord-crd.json"; path = seedDNSRecordCRD; }
{ name = "02-domain-crd.json"; path = seedDomainCRD; }
{ name = "03-controller-sa.json"; path = controllerSA; }
{ name = "03-builder-sa.json"; path = builderSA; }
{ name = "04-controller-role.json"; path = controllerRole; }
{ name = "04-controller-rolebinding.json"; path = controllerRoleBinding; }
{ name = "04-builder-role.json"; path = builderRole; }
{ name = "04-builder-rolebinding.json"; path = builderRoleBinding; }
{ name = "05-controller-cert.json"; path = controllerCert; }
{ name = "05-controller-deployment.json"; path = controllerDeployment; }
{ name = "05-controller-service.json"; path = controllerService; }
]
++ lib.optional cfg.swtpmEnabled
{ name = "05-host-agent-daemonset.json"; path = hostAgentDaemonSet; }
++ lib.optionals cfg.poolManager.enable [
{ name = "05-pool-manager-daemonset.json"; path = poolManagerDaemonSet; }
{ name = "05-pool-manager-service.json"; path = poolManagerService; }
]
);
in {
options.seed.controller = {
enable = lib.mkEnableOption "Seed instance controller";
flakePaths = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = "Bootstrap flake paths containing seeds.* outputs. Additional flakes can be registered dynamically via SeedFlake CRDs.";
};
ipv4Address = lib.mkOption {
type = lib.types.str;
default = "";
description = "Reserved IPv4 address for public LoadBalancer services.";
};
ipv6Block = lib.mkOption {
type = lib.types.str;
default = "";
description = "Reserved IPv6 /64 block for public LoadBalancer services (e.g. 2001:db8::/64).";
};
bgp = {
enable = lib.mkEnableOption "BGP peering with upstream router (e.g. Vultr) for reserved IP announcement";
myASN = lib.mkOption {
type = lib.types.int;
default = 64512;
description = "Local (cluster-side) BGP ASN. Use a private ASN unless you have your own.";
};
peerASN = lib.mkOption {
type = lib.types.int;
description = "Upstream router BGP ASN (e.g. Vultr account ASN).";
};
peerAddress = lib.mkOption {
type = lib.types.str;
default = "169.254.1.1";
description = "IPv4 BGP peer address (upstream router). 169.254.1.1 for Vultr bare metal, 169.254.169.254 for Vultr VMs.";
};
peerAddressIPv6 = lib.mkOption {
type = lib.types.str;
default = "2001:19f0:ffff::1";
description = "IPv6 BGP peer address (upstream router).";
};
password = lib.mkOption {
type = lib.types.str;
default = "";
description = "BGP TCP MD5 authentication password.";
};
sourceAddress = lib.mkOption {
type = lib.types.str;
default = "";
description = "Source IPv4 address for BGP peering (node's public IP).";
};
sourceAddressIPv6 = lib.mkOption {
type = lib.types.str;
default = "";
description = "Source IPv6 address for BGP peering (node's SLAAC address).";
};
};
controllerImage = lib.mkOption {
type = lib.types.str;
description = "Nix store path to the controller OCI image.";
};
hostAgentImage = lib.mkOption {
type = lib.types.str;
description = "Nix store path to the host agent OCI image.";
};
builderImage = lib.mkOption {
type = lib.types.str;
default = "";
description = "Nix image ref for builder Job pods. Empty = controller runs nix directly.";
};
swtpmEnabled = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Enable vTPM (swtpm) for all instances via SeedHostTask CRDs.";
};
registrar = {
apiKeyFile = lib.mkOption {
type = lib.types.str;
default = "";
description = "Path to file containing the NameSilo API key for domain registration.";
};
};
dns = {
apiUrl = lib.mkOption {
type = lib.types.str;
default = "";
description = "PowerDNS HTTP API URL for DNS auto-registration. Empty = disabled.";
};
apiKeyFile = lib.mkOption {
type = lib.types.str;
default = "";
description = "Path to file containing the PowerDNS API key.";
};
zone = lib.mkOption {
type = lib.types.str;
default = "loom.farm.";
description = "PowerDNS zone name (for API calls).";
};
instanceDomain = lib.mkOption {
type = lib.types.str;
default = "seed.loom.farm";
description = "Domain suffix for instance FQDNs (<instance>.<namespace>.<instanceDomain>).";
};
};
acme = {
accountKey = lib.mkOption {
type = lib.types.str;
default = "";
description = ''
Base64-encoded Let's Encrypt account key PEM. Stored as base64 in
sops to avoid multi-line values breaking JSON templates. The value
is placed in a k8s Secret `data` field (which expects base64),
rendered by sops.templates at activation time.
'';
};
_secretManifestJSON = lib.mkOption {
type = lib.types.str;
internal = true;
default = "";
description = "Generated k8s Secret JSON for sops.templates wiring.";
};
};
netpol = {
enable = lib.mkEnableOption "Network policy enforcement via iptables in host-agent";
};
webhook = {
enable = lib.mkEnableOption "Seed webhook for cache-busting reconciliation";
port = lib.mkOption {
type = lib.types.port;
default = 9876;
description = "Port for the webhook HTTP listener.";
};
secretFile = lib.mkOption {
type = lib.types.str;
default = "";
description = ''
Path to file containing the HMAC-SHA256 secret for GitHub webhook verification.
Empty = no authentication (accept all requests).
'';
};
};
siloHost = lib.mkOption {
type = lib.types.str;
default = "silo.loom.farm";
description = "Hostname of the silo git server, used to expand silo: shorthand in plant command.";
};
poolManager = {
enable = lib.mkEnableOption "Pool manager for hardware-isolated nix eval/build";
poolSize = lib.mkOption {
type = lib.types.int;
default = 2;
description = "Number of warm VM slots per node.";
};
port = lib.mkOption {
type = lib.types.port;
default = 9877;
description = "Port for the pool manager HTTP API.";
};
image = lib.mkOption {
type = lib.types.str;
default = "";
description = "Nix store path to the pool manager OCI image.";
};
};
};
config = lib.mkIf cfg.enable {
# Deploy controller manifests via kubectl apply on activation
seed.k8s.services.seed-controller.manifests = controllerManifests;
# Expose ACME secret JSON for sops.templates wiring in infra profiles
seed.controller.acme._secretManifestJSON = lib.mkIf (cfg.acme.accountKey != "") acmeSecretJSON;
};
}