-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstatefulset.go
More file actions
994 lines (904 loc) · 33.1 KB
/
Copy pathstatefulset.go
File metadata and controls
994 lines (904 loc) · 33.1 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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
package resources
import (
"encoding/json"
"fmt"
"strings"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
paperclipv1alpha1 "github.com/paperclipinc/paperclip-operator/api/v1alpha1"
)
// BuildStatefulSet constructs the Paperclip server StatefulSet.
func BuildStatefulSet(instance *paperclipv1alpha1.Instance, extraPodAnnotations map[string]string) *appsv1.StatefulSet {
selectorLabels := SelectorLabels(instance)
replicas := WorkloadReplicas(instance)
sts := &appsv1.StatefulSet{
ObjectMeta: ObjectMeta(instance, StatefulSetName(instance)),
Spec: appsv1.StatefulSetSpec{
Replicas: &replicas,
ServiceName: ServiceName(instance),
RevisionHistoryLimit: Ptr(int32(10)),
Selector: &metav1.LabelSelector{
MatchLabels: selectorLabels,
},
Template: BuildServerPodTemplate(instance, extraPodAnnotations),
UpdateStrategy: appsv1.StatefulSetUpdateStrategy{
Type: appsv1.RollingUpdateStatefulSetStrategyType,
},
},
}
return sts
}
func buildMainContainer(instance *paperclipv1alpha1.Instance) corev1.Container {
image := containerImage(instance)
port := servicePort(instance)
container := corev1.Container{
Name: ContainerName,
Image: image,
Ports: []corev1.ContainerPort{
{
Name: "http",
ContainerPort: port,
Protocol: corev1.ProtocolTCP,
},
},
Env: buildEnvVars(instance),
EnvFrom: instance.Spec.EnvFrom,
Resources: instance.Spec.Resources,
ImagePullPolicy: imagePullPolicy(instance),
TerminationMessagePath: "/dev/termination-log",
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
VolumeMounts: buildVolumeMounts(instance),
}
// Container security context
container.SecurityContext = paperclipContainerSecurityContext(instance)
if container.SecurityContext.ReadOnlyRootFilesystem == nil {
container.SecurityContext = container.SecurityContext.DeepCopy()
container.SecurityContext.ReadOnlyRootFilesystem = Ptr(false) // Paperclip needs writable filesystem for node_modules, etc.
}
// Always override the image ENTRYPOINT (docker-entrypoint.sh) and exec the
// server directly. The image entrypoint gosu-drops from root to "node", which
// fails under our runAsNonRoot / runAsUser:1000 / drop-ALL securityContext
// ("failed switching to node: operation not permitted"). We already run as the
// node uid, so there is nothing to drop to.
container.Command = []string{"/bin/sh", "-c"}
script := "exec " + DefaultPaperclipEntrypoint
// Multi-replica heartbeat gating: only pod-0 runs the scheduler. Uses a shell
// wrapper that checks the StatefulSet ordinal in $HOSTNAME. Applied only for
// the "ordinal" gating mode on the StatefulSet workload: "lease" delegates
// leadership to the app's lease-based leader election, and Deployment pods
// have no stable ordinals for the wrapper to match (the controller surfaces
// that combination via the SchedulerGatingValid condition).
if instance.Spec.Heartbeat.Enabled && EffectiveReplicas(instance) > 1 &&
SchedulerGatingMode(instance) == "ordinal" && !EffectiveWorkloadIsDeployment(instance) {
script = `case "$HOSTNAME" in *-0) export HEARTBEAT_SCHEDULER_ENABLED=true ;; *) export HEARTBEAT_SCHEDULER_ENABLED=false ;; esac; ` + script
}
container.Args = []string{script}
// Probes
container.LivenessProbe = buildLivenessProbe(instance, port)
container.ReadinessProbe = buildReadinessProbe(instance, port)
container.StartupProbe = buildStartupProbe(instance, port)
return container
}
func buildEnvVars(instance *paperclipv1alpha1.Instance) []corev1.EnvVar {
port := servicePort(instance)
vars := []corev1.EnvVar{
{Name: "PORT", Value: fmt.Sprintf("%d", port)},
{Name: "PAPERCLIP_BIND", Value: "custom"},
{Name: "PAPERCLIP_BIND_HOST", Value: "0.0.0.0"},
{Name: "PAPERCLIP_HOME", Value: DataMountPath},
{Name: "SERVE_UI", Value: "true"},
{Name: "PAPERCLIP_DEPLOYMENT_MODE", Value: instance.Spec.Deployment.Mode},
{Name: "PAPERCLIP_DEPLOYMENT_EXPOSURE", Value: instance.Spec.Deployment.Exposure},
}
// OpenTelemetry - load instrumentation before the app so OTEL can hook into
// HTTP/Express/pg modules at require time. Only inject this when observability
// is enabled: the --import preload references ./server/dist/instrumentation.js,
// which is only present in app images built with instrumentation; forcing it
// unconditionally crashes the app (ERR_MODULE_NOT_FOUND) on images without it.
if instance.Spec.Observability.Metrics.Enabled {
vars = append(vars,
corev1.EnvVar{Name: "NODE_OPTIONS", Value: "--import ./server/dist/instrumentation.js"},
corev1.EnvVar{Name: "OTEL_EXPORTER_OTLP_ENDPOINT", Value: "http://otel-collector.observability.svc.cluster.local:4317"},
corev1.EnvVar{Name: "OTEL_SERVICE_NAME", Value: instance.Name},
corev1.EnvVar{
Name: "OTEL_RESOURCE_ATTRIBUTES",
Value: fmt.Sprintf("k8s.namespace.name=%s,k8s.statefulset.name=%s", instance.Namespace, StatefulSetName(instance)),
},
)
}
// Public URL
if instance.Spec.Deployment.PublicURL != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_PUBLIC_URL", Value: instance.Spec.Deployment.PublicURL})
}
// Allowed hostnames: always include the in-cluster Service DNS names (plus
// loopback) so the operator's own bootstrap Job and other in-cluster clients
// pass the app's authenticated-mode hostname allowlist. User-specified
// hostnames (e.g. the public ingress host) are appended.
vars = append(vars, corev1.EnvVar{
Name: "PAPERCLIP_ALLOWED_HOSTNAMES",
Value: strings.Join(allowedHostnames(instance), ","),
})
// Database URL
switch instance.Spec.Database.Mode {
case ModeExternal:
if instance.Spec.Database.ExternalURLSecretRef != nil {
vars = append(vars, corev1.EnvVar{
Name: "DATABASE_URL",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: instance.Spec.Database.ExternalURLSecretRef,
},
})
} else if instance.Spec.Database.ExternalURL != "" {
vars = append(vars, corev1.EnvVar{Name: "DATABASE_URL", Value: instance.Spec.Database.ExternalURL})
}
case "managed":
// DB_PASSWORD must be defined before DATABASE_URL for $(DB_PASSWORD) substitution to work
vars = append(vars, corev1.EnvVar{
Name: "DB_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: DatabaseSecretName(instance),
},
Key: "password",
},
},
})
vars = append(vars, corev1.EnvVar{
Name: "DATABASE_URL",
Value: fmt.Sprintf("postgresql://paperclip:$(DB_PASSWORD)@%s-db.%s.svc.cluster.local:%d/paperclip",
instance.Name, instance.Namespace, PostgreSQLPort),
})
// "embedded" mode uses PGlite - no DATABASE_URL needed
}
// Auth secret
if instance.Spec.Auth.SecretRef != nil {
vars = append(vars, corev1.EnvVar{
Name: "BETTER_AUTH_SECRET",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: instance.Spec.Auth.SecretRef,
},
})
}
// Disable public self-service sign-up (former single-tenant behavior)
if instance.Spec.Auth.DisableSignUp {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_AUTH_DISABLE_SIGN_UP", Value: "true"})
}
// Secrets management master key
if instance.Spec.Secrets.MasterKeySecretRef != nil {
vars = append(vars, corev1.EnvVar{
Name: "PAPERCLIP_SECRETS_MASTER_KEY",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: instance.Spec.Secrets.MasterKeySecretRef,
},
})
} else {
// Always inject from auto-generated secret to ensure all replicas share the same key
vars = append(vars, corev1.EnvVar{
Name: "PAPERCLIP_SECRETS_MASTER_KEY",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: SecretsMasterKeySecretName(instance),
},
Key: "master-key",
},
},
})
}
if instance.Spec.Secrets.StrictMode {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_SECRETS_STRICT_MODE", Value: "true"})
}
// Secrets provider (e.g. AWS Secrets Manager)
vars = append(vars, buildSecretsProviderEnvVars(instance)...)
// App-native database backups
vars = append(vars, buildAppNativeBackupEnvVars(instance)...)
// Auth: email delivery and OAuth providers
vars = append(vars, buildAuthEmailAndOAuthEnvVars(instance)...)
// Heartbeat scheduler
// When heartbeat is disabled, explicitly disable it on all pods.
// When enabled with multiple replicas, the command wrapper handles per-pod gating
// (only pod-0 runs the scheduler), so we skip the static env var here.
if !instance.Spec.Heartbeat.Enabled {
vars = append(vars, corev1.EnvVar{Name: "HEARTBEAT_SCHEDULER_ENABLED", Value: "false"})
}
if instance.Spec.Heartbeat.IntervalMS > 0 {
vars = append(vars, corev1.EnvVar{
Name: "HEARTBEAT_SCHEDULER_INTERVAL_MS",
Value: fmt.Sprintf("%d", instance.Spec.Heartbeat.IntervalMS),
})
}
// Object storage
if instance.Spec.ObjectStorage != nil {
os := instance.Spec.ObjectStorage
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_STORAGE_PROVIDER", Value: "s3"})
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_STORAGE_S3_BUCKET", Value: os.Bucket})
if os.Region != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_STORAGE_S3_REGION", Value: os.Region})
}
if os.Endpoint != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_STORAGE_S3_ENDPOINT", Value: os.Endpoint})
}
// Path-style addressing: explicit value wins; nil defaults to true for
// MinIO, whose in-cluster deployments lack the wildcard DNS that
// virtual-hosted bucket addressing requires.
forcePathStyle := os.Provider == "minio"
if os.ForcePathStyle != nil {
forcePathStyle = *os.ForcePathStyle
}
if forcePathStyle {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_STORAGE_S3_FORCE_PATH_STYLE", Value: "true"})
}
if os.CredentialsSecretRef != nil {
vars = append(vars,
corev1.EnvVar{
Name: "AWS_ACCESS_KEY_ID",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: *os.CredentialsSecretRef,
Key: "AWS_ACCESS_KEY_ID",
},
},
},
corev1.EnvVar{
Name: "AWS_SECRET_ACCESS_KEY",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: *os.CredentialsSecretRef,
Key: "AWS_SECRET_ACCESS_KEY",
},
},
},
)
}
}
// Brand theming: point the server at the mounted brand directory so it
// serves /branding/brand.css and loads it after the bundled stylesheet.
if brandingConfigMapRef(instance) != nil {
vars = append(vars, corev1.EnvVar{Name: EnvBrandDir, Value: BrandMountPath})
}
// LLM API keys
if instance.Spec.Adapters.APIKeysSecretRef != nil {
vars = append(vars, corev1.EnvVar{
Name: "ANTHROPIC_API_KEY",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: *instance.Spec.Adapters.APIKeysSecretRef,
Key: "ANTHROPIC_API_KEY",
Optional: Ptr(true),
},
},
})
vars = append(vars, corev1.EnvVar{
Name: "OPENAI_API_KEY",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: *instance.Spec.Adapters.APIKeysSecretRef,
Key: "OPENAI_API_KEY",
Optional: Ptr(true),
},
},
})
}
// E2B sandbox provider API key
if instance.Spec.Adapters.E2B != nil {
ref := instance.Spec.Adapters.E2B.APIKeySecretRef
vars = append(vars, corev1.EnvVar{
Name: "E2B_API_KEY",
ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &ref},
})
}
// Cloud sandbox
vars = append(vars, buildCloudSandboxEnvVars(instance)...)
// In-cluster Kubernetes execution policy (@paperclipai/plugin-kubernetes)
vars = append(vars, buildExecutionEnvVars(instance)...)
// Declarative adapter registry (PAPERCLIP_ADAPTERS) consumed by the server's
// adapter-registry bootstrap (picker availability + k8s runtime wiring).
vars = append(vars, buildAdapterRegistryEnvVars(instance)...)
// OAuth connections
if instance.Spec.Connections != nil {
conn := instance.Spec.Connections
key := conn.CredentialsKey
if key == "" {
key = EnvOAuthCredentials
}
vars = append(vars, corev1.EnvVar{
Name: EnvOAuthCredentials,
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: conn.CredentialsSecretRef,
Key: key,
},
},
})
if conn.ProvidersConfigRef != nil {
vars = append(vars, corev1.EnvVar{
Name: EnvOAuthProviders,
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: *conn.ProvidersConfigRef,
Key: EnvOAuthProviders,
Optional: Ptr(true),
},
},
})
}
}
// Logging
if instance.Spec.Observability.Logging.Level != "" {
vars = append(vars, corev1.EnvVar{Name: "LOG_LEVEL", Value: instance.Spec.Observability.Logging.Level})
}
// User-supplied env vars (last, so they can override defaults)
vars = append(vars, instance.Spec.Env...)
return vars
}
func buildAuthEmailAndOAuthEnvVars(instance *paperclipv1alpha1.Instance) []corev1.EnvVar {
var vars []corev1.EnvVar
// Email (Resend)
if instance.Spec.Auth.Email != nil {
email := instance.Spec.Auth.Email
if email.ResendAPIKeySecretRef != nil {
vars = append(vars, corev1.EnvVar{
Name: "RESEND_API_KEY",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: email.ResendAPIKeySecretRef,
},
})
}
if email.From != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_EMAIL_FROM", Value: email.From})
}
if email.VerificationRequired {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_EMAIL_VERIFICATION_REQUIRED", Value: "true"})
}
}
// Google OAuth
if instance.Spec.Auth.Google != nil {
secretRef := instance.Spec.Auth.Google.CredentialsSecretRef
vars = append(vars,
corev1.EnvVar{
Name: "GOOGLE_CLIENT_ID",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: secretRef,
Key: "GOOGLE_CLIENT_ID",
},
},
},
corev1.EnvVar{
Name: "GOOGLE_CLIENT_SECRET",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: secretRef,
Key: "GOOGLE_CLIENT_SECRET",
},
},
},
)
}
// Apple OAuth
if instance.Spec.Auth.Apple != nil {
secretRef := instance.Spec.Auth.Apple.CredentialsSecretRef
vars = append(vars,
corev1.EnvVar{
Name: "APPLE_CLIENT_ID",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: secretRef,
Key: "APPLE_CLIENT_ID",
},
},
},
corev1.EnvVar{
Name: "APPLE_CLIENT_SECRET",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: secretRef,
Key: "APPLE_CLIENT_SECRET",
},
},
},
)
}
return vars
}
// allowedHostnames returns the hostname allowlist for PAPERCLIP_ALLOWED_HOSTNAMES:
// always the in-cluster Service DNS names plus loopback, then any user-specified
// hostnames, deduplicated and order-preserving.
func allowedHostnames(instance *paperclipv1alpha1.Instance) []string {
svc := ServiceName(instance)
ns := instance.Namespace
extra := instance.Spec.Deployment.AllowedHostnames
hosts := make([]string, 0, 6+len(extra))
hosts = append(hosts,
"localhost",
"127.0.0.1",
svc,
svc+"."+ns,
svc+"."+ns+".svc",
svc+"."+ns+".svc.cluster.local",
)
hosts = append(hosts, extra...)
seen := make(map[string]bool, len(hosts))
out := make([]string, 0, len(hosts))
for _, h := range hosts {
if h == "" || seen[h] {
continue
}
seen[h] = true
out = append(out, h)
}
return out
}
// buildSecretsProviderEnvVars emits the env vars selecting an external secrets
// provider. AWS credentials are intentionally not injected - the app uses the
// AWS SDK credential chain (IRSA via serviceAccountAnnotations).
func buildSecretsProviderEnvVars(instance *paperclipv1alpha1.Instance) []corev1.EnvVar {
provider := instance.Spec.Secrets.Provider
if provider == "" || provider == "local_encrypted" {
return nil
}
vars := []corev1.EnvVar{{Name: "PAPERCLIP_SECRETS_PROVIDER", Value: provider}}
if provider == "aws_secrets_manager" && instance.Spec.Secrets.AWS != nil {
aws := instance.Spec.Secrets.AWS
vars = append(vars,
corev1.EnvVar{Name: "PAPERCLIP_SECRETS_AWS_REGION", Value: aws.Region},
corev1.EnvVar{Name: "PAPERCLIP_SECRETS_AWS_KMS_KEY_ID", Value: aws.KMSKeyID},
corev1.EnvVar{Name: "PAPERCLIP_SECRETS_AWS_DEPLOYMENT_ID", Value: aws.DeploymentID},
)
if aws.Prefix != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_SECRETS_AWS_PREFIX", Value: aws.Prefix})
}
if aws.Environment != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_SECRETS_AWS_ENVIRONMENT", Value: aws.Environment})
}
if aws.Endpoint != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_SECRETS_AWS_ENDPOINT", Value: aws.Endpoint})
}
if aws.DeleteRecoveryDays != nil {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_SECRETS_AWS_DELETE_RECOVERY_DAYS", Value: fmt.Sprintf("%d", *aws.DeleteRecoveryDays)})
}
}
return vars
}
// buildAppNativeBackupEnvVars emits Paperclip's built-in DB backup env vars,
// keeping the backup directory on the persistent data volume.
func buildAppNativeBackupEnvVars(instance *paperclipv1alpha1.Instance) []corev1.EnvVar {
if instance.Spec.Backup == nil || instance.Spec.Backup.AppNative == nil {
return nil
}
an := instance.Spec.Backup.AppNative
var vars []corev1.EnvVar
if an.Enabled != nil {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_DB_BACKUP_ENABLED", Value: fmt.Sprintf("%t", *an.Enabled)})
}
if an.IntervalMinutes > 0 {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_DB_BACKUP_INTERVAL_MINUTES", Value: fmt.Sprintf("%d", an.IntervalMinutes)})
}
if an.RetentionDays > 0 {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_DB_BACKUP_RETENTION_DAYS", Value: fmt.Sprintf("%d", an.RetentionDays)})
}
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_DB_BACKUP_DIR", Value: DataMountPath + "/backups"})
return vars
}
func buildCloudSandboxEnvVars(instance *paperclipv1alpha1.Instance) []corev1.EnvVar {
cs := instance.Spec.Adapters.CloudSandbox
if cs == nil || !cs.Enabled {
return nil
}
vars := []corev1.EnvVar{
{Name: "PAPERCLIP_CLOUD_SANDBOX_ENABLED", Value: "true"},
}
ns := cs.Namespace
if ns == "" {
ns = instance.Namespace
}
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_CLOUD_SANDBOX_NAMESPACE", Value: ns})
if cs.DefaultImage != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_CLOUD_SANDBOX_DEFAULT_IMAGE", Value: cs.DefaultImage})
}
if cs.IdleTimeoutMin > 0 {
vars = append(vars, corev1.EnvVar{
Name: "PAPERCLIP_CLOUD_SANDBOX_IDLE_TIMEOUT_MIN",
Value: fmt.Sprintf("%d", cs.IdleTimeoutMin),
})
}
// Phase 4: persistence
if cs.Persistence != nil && cs.Persistence.Enabled {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_CLOUD_SANDBOX_PERSISTENCE_ENABLED", Value: "true"})
if cs.Persistence.StorageClass != "" {
vars = append(vars, corev1.EnvVar{
Name: "PAPERCLIP_CLOUD_SANDBOX_PERSISTENCE_STORAGE_CLASS",
Value: cs.Persistence.StorageClass,
})
}
if cs.Persistence.Size != "" {
vars = append(vars, corev1.EnvVar{
Name: "PAPERCLIP_CLOUD_SANDBOX_PERSISTENCE_SIZE",
Value: cs.Persistence.Size,
})
}
}
// Phase 4: multi-namespace isolation
if cs.MultiNamespace {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_CLOUD_SANDBOX_MULTI_NAMESPACE", Value: "true"})
}
// Node scheduling: pass the instance's scheduling constraints so the
// Paperclip server can apply them to sandbox pods it creates.
if len(instance.Spec.Availability.NodeSelector) > 0 {
if b, err := json.Marshal(instance.Spec.Availability.NodeSelector); err == nil {
vars = append(vars, corev1.EnvVar{
Name: "PAPERCLIP_CLOUD_SANDBOX_NODE_SELECTOR",
Value: string(b),
})
}
}
if len(instance.Spec.Availability.Tolerations) > 0 {
if b, err := json.Marshal(instance.Spec.Availability.Tolerations); err == nil {
vars = append(vars, corev1.EnvVar{
Name: "PAPERCLIP_CLOUD_SANDBOX_TOLERATIONS",
Value: string(b),
})
}
}
return vars
}
// IsKubernetesExecution reports whether the instance is configured to force
// agent execution onto the in-cluster Kubernetes sandbox provider. This gates
// the execution RBAC and the app ServiceAccount-token mount.
func IsKubernetesExecution(instance *paperclipv1alpha1.Instance) bool {
ex := instance.Spec.Adapters.Execution
return ex != nil && ex.Mode == "kubernetes"
}
// buildExecutionEnvVars translates spec.adapters.execution into the
// PAPERCLIP_EXECUTION_MODE / PAPERCLIP_K8S_* env vars consumed by the fork's
// execution-policy bootstrap. Only emitted when execution is configured; when
// Mode is "any" (or the block is nil) the bootstrap stays unrestricted, so we
// emit nothing.
func buildExecutionEnvVars(instance *paperclipv1alpha1.Instance) []corev1.EnvVar {
ex := instance.Spec.Adapters.Execution
if ex == nil || ex.Mode != "kubernetes" {
return nil
}
vars := []corev1.EnvVar{
{Name: "PAPERCLIP_EXECUTION_MODE", Value: "kubernetes"},
// The operator only ever wires the in-cluster path: the app talks to the
// local kube-apiserver via its mounted ServiceAccount token.
{Name: "PAPERCLIP_K8S_IN_CLUSTER", Value: "true"},
}
k := ex.Kubernetes
if k == nil {
return vars
}
if k.Backend != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_K8S_BACKEND", Value: k.Backend})
}
if k.RuntimeClassName != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_K8S_RUNTIME_CLASS_NAME", Value: k.RuntimeClassName})
}
if k.EgressMode != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_K8S_EGRESS_MODE", Value: k.EgressMode})
}
if k.EgressPolicy != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_K8S_EGRESS_POLICY", Value: k.EgressPolicy})
}
if len(k.EgressAllowFQDNs) > 0 {
vars = append(vars, corev1.EnvVar{
Name: "PAPERCLIP_K8S_EGRESS_ALLOW_FQDNS",
Value: strings.Join(k.EgressAllowFQDNs, ","),
})
}
if len(k.EgressAllowCIDRs) > 0 {
vars = append(vars, corev1.EnvVar{
Name: "PAPERCLIP_K8S_EGRESS_ALLOW_CIDRS",
Value: strings.Join(k.EgressAllowCIDRs, ","),
})
}
if k.NamespacePrefix != "" {
vars = append(vars, corev1.EnvVar{Name: "PAPERCLIP_K8S_NAMESPACE_PREFIX", Value: k.NamespacePrefix})
}
if q := k.PerTenantQuota; q != nil {
vars = append(vars,
corev1.EnvVar{Name: "PAPERCLIP_K8S_QUOTA_PODS", Value: q.Pods},
corev1.EnvVar{Name: "PAPERCLIP_K8S_QUOTA_REQUESTS_CPU", Value: q.RequestsCPU},
corev1.EnvVar{Name: "PAPERCLIP_K8S_QUOTA_REQUESTS_MEMORY", Value: q.RequestsMemory},
corev1.EnvVar{Name: "PAPERCLIP_K8S_QUOTA_LIMITS_CPU", Value: q.LimitsCPU},
corev1.EnvVar{Name: "PAPERCLIP_K8S_QUOTA_LIMITS_MEMORY", Value: q.LimitsMemory},
)
}
if lr := k.PerTenantLimitRange; lr != nil {
vars = append(vars,
corev1.EnvVar{Name: "PAPERCLIP_K8S_LIMITRANGE_DEFAULT_CPU", Value: lr.DefaultCPU},
corev1.EnvVar{Name: "PAPERCLIP_K8S_LIMITRANGE_DEFAULT_MEMORY", Value: lr.DefaultMemory},
corev1.EnvVar{Name: "PAPERCLIP_K8S_LIMITRANGE_DEFAULT_REQUEST_CPU", Value: lr.DefaultRequestCPU},
corev1.EnvVar{Name: "PAPERCLIP_K8S_LIMITRANGE_DEFAULT_REQUEST_MEMORY", Value: lr.DefaultRequestMem},
corev1.EnvVar{Name: "PAPERCLIP_K8S_LIMITRANGE_MAX_CPU", Value: lr.MaxCPU},
corev1.EnvVar{Name: "PAPERCLIP_K8S_LIMITRANGE_MAX_MEMORY", Value: lr.MaxMemory},
)
}
return vars
}
// buildAdapterRegistryEnvVars marshals spec.adapters.registry into the
// PAPERCLIP_ADAPTERS env var. Emits nothing when the registry is empty so
// unconfigured instances keep their built-in adapter defaults.
func buildAdapterRegistryEnvVars(instance *paperclipv1alpha1.Instance) []corev1.EnvVar {
registry := instance.Spec.Adapters.Registry
if len(registry) == 0 {
return nil
}
encoded, err := json.Marshal(registry)
if err != nil {
// Marshaling typed structs cannot fail in practice; emit nothing rather
// than produce a malformed env var that would fail the server bootstrap.
return nil
}
return []corev1.EnvVar{{Name: "PAPERCLIP_ADAPTERS", Value: string(encoded)}}
}
func buildVolumes(instance *paperclipv1alpha1.Instance) []corev1.Volume {
var volumes []corev1.Volume
if PersistenceEnabled(instance) {
volumes = append(volumes, corev1.Volume{
Name: DataVolumeName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: PVCName(instance),
},
},
})
} else {
volumes = append(volumes, corev1.Volume{
Name: DataVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
})
}
// Optional brand-assets volume: a ConfigMap of brand files (brand.css, ...)
// mounted read-only and served by the app under /branding.
if ref := brandingConfigMapRef(instance); ref != nil {
volumes = append(volumes, corev1.Volume{
Name: BrandVolumeName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: *ref,
},
},
})
}
return volumes
}
// brandingConfigMapRef returns the brand ConfigMap reference when branding is
// configured, or nil. Centralized so the volume, mount, and env wiring stay
// in sync (mirrors the spec.ObjectStorage != nil gating pattern).
func brandingConfigMapRef(instance *paperclipv1alpha1.Instance) *corev1.LocalObjectReference {
if instance.Spec.Branding == nil {
return nil
}
return instance.Spec.Branding.CSSConfigMapRef
}
func buildVolumeMounts(instance *paperclipv1alpha1.Instance) []corev1.VolumeMount {
mounts := make([]corev1.VolumeMount, 0, 2+len(instance.Spec.ExtraVolumeMounts))
mounts = append(mounts, corev1.VolumeMount{
Name: DataVolumeName,
MountPath: DataMountPath,
})
if brandingConfigMapRef(instance) != nil {
mounts = append(mounts, corev1.VolumeMount{
Name: BrandVolumeName,
MountPath: BrandMountPath,
ReadOnly: true,
})
}
mounts = append(mounts, instance.Spec.ExtraVolumeMounts...)
return mounts
}
func probeHandler(instance *paperclipv1alpha1.Instance, port int32) corev1.ProbeHandler {
if UseTCPProbes(instance) {
return corev1.ProbeHandler{
TCPSocket: &corev1.TCPSocketAction{
Port: intstr.FromInt32(port),
},
}
}
return corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: HealthPath,
Port: intstr.FromInt32(port),
Scheme: corev1.URISchemeHTTP,
},
}
}
func buildLivenessProbe(instance *paperclipv1alpha1.Instance, port int32) *corev1.Probe {
probe := &corev1.Probe{
ProbeHandler: probeHandler(instance, port),
InitialDelaySeconds: 15,
PeriodSeconds: 20,
TimeoutSeconds: 5,
FailureThreshold: 6,
SuccessThreshold: 1,
}
if p := instance.Spec.Probes.Liveness; p != nil {
if p.InitialDelaySeconds != nil {
probe.InitialDelaySeconds = *p.InitialDelaySeconds
}
if p.PeriodSeconds != nil {
probe.PeriodSeconds = *p.PeriodSeconds
}
if p.TimeoutSeconds != nil {
probe.TimeoutSeconds = *p.TimeoutSeconds
}
if p.FailureThreshold != nil {
probe.FailureThreshold = *p.FailureThreshold
}
if p.SuccessThreshold != nil {
probe.SuccessThreshold = *p.SuccessThreshold
}
}
return probe
}
func buildReadinessProbe(instance *paperclipv1alpha1.Instance, port int32) *corev1.Probe {
probe := &corev1.Probe{
ProbeHandler: probeHandler(instance, port),
InitialDelaySeconds: 5,
PeriodSeconds: 10,
TimeoutSeconds: 3,
FailureThreshold: 3,
SuccessThreshold: 1,
}
if p := instance.Spec.Probes.Readiness; p != nil {
if p.InitialDelaySeconds != nil {
probe.InitialDelaySeconds = *p.InitialDelaySeconds
}
if p.PeriodSeconds != nil {
probe.PeriodSeconds = *p.PeriodSeconds
}
if p.TimeoutSeconds != nil {
probe.TimeoutSeconds = *p.TimeoutSeconds
}
if p.FailureThreshold != nil {
probe.FailureThreshold = *p.FailureThreshold
}
if p.SuccessThreshold != nil {
probe.SuccessThreshold = *p.SuccessThreshold
}
}
return probe
}
func buildStartupProbe(instance *paperclipv1alpha1.Instance, port int32) *corev1.Probe {
probe := &corev1.Probe{
ProbeHandler: probeHandler(instance, port),
InitialDelaySeconds: 0,
PeriodSeconds: 5,
TimeoutSeconds: 3,
FailureThreshold: 30,
SuccessThreshold: 1,
}
if p := instance.Spec.Probes.Startup; p != nil {
if p.InitialDelaySeconds != nil {
probe.InitialDelaySeconds = *p.InitialDelaySeconds
}
if p.PeriodSeconds != nil {
probe.PeriodSeconds = *p.PeriodSeconds
}
if p.TimeoutSeconds != nil {
probe.TimeoutSeconds = *p.TimeoutSeconds
}
if p.FailureThreshold != nil {
probe.FailureThreshold = *p.FailureThreshold
}
if p.SuccessThreshold != nil {
probe.SuccessThreshold = *p.SuccessThreshold
}
}
return probe
}
func buildOnboardInitContainer(instance *paperclipv1alpha1.Instance) corev1.Container {
image := containerImage(instance)
// Create the Paperclip config file if it doesn't exist yet.
// Uses `onboard --yes` which accepts quickstart defaults. Unfortunately this also
// starts the server, so we run it in a subshell and kill the entire process group
// once the config file appears.
script := `
CONFIG="/paperclip/instances/default/config.json"
if [ -f "$CONFIG" ]; then
echo "Config already exists, skipping onboard."
exit 0
fi
echo "Running initial onboarding..."
# Run onboard in a separate process group so we can kill the whole tree
sh -c 'exec pnpm paperclipai onboard --yes' &
ONBOARD_PID=$!
# Wait for the config file to appear (onboard creates it before starting the server)
for i in $(seq 1 120); do
if [ -f "$CONFIG" ]; then
echo "Config created successfully."
# Kill the entire process tree (onboard + server + node children)
kill -9 $ONBOARD_PID 2>/dev/null || true
# Also kill any remaining node processes started by onboard
pkill -9 -f "paperclipai" 2>/dev/null || true
pkill -9 -f "server/dist/index" 2>/dev/null || true
exit 0
fi
sleep 1
done
echo "Timed out waiting for config file."
kill -9 $ONBOARD_PID 2>/dev/null || true
exit 1
`
return corev1.Container{
Name: "onboard",
Image: image,
ImagePullPolicy: imagePullPolicy(instance),
Command: []string{"/bin/sh", "-c"},
Args: []string{script},
Env: buildEnvVars(instance),
EnvFrom: instance.Spec.EnvFrom,
VolumeMounts: buildVolumeMounts(instance),
SecurityContext: paperclipContainerSecurityContext(instance),
}
}
// buildSeedInstanceAdminInitContainer runs the product CLI command that
// idempotently seeds a platform-managed instance-admin. It mirrors the onboard
// init container's invocation (pnpm paperclipai ...), securityContext, and
// volume mounts. It must be scheduled AFTER the onboard init container because
// onboard applies the DB migrations this command relies on.
//
// The CLI reads DATABASE_URL (reused from the same external-database secret/uri
// source the main app container uses, via buildBackupDBEnvVars) plus the
// PAPERCLIP_SEED_ADMIN_* env vars sourced from spec.deployment.platformAdmin.
func buildSeedInstanceAdminInitContainer(instance *paperclipv1alpha1.Instance) corev1.Container {
image := containerImage(instance)
admin := instance.Spec.Deployment.PlatformAdmin
// DATABASE_URL (+ DB_PASSWORD for managed mode) from the same source the
// main app container resolves its DB connection from.
env := buildBackupDBEnvVars(instance)
env = append(env, corev1.EnvVar{Name: "PAPERCLIP_SEED_ADMIN_EMAIL", Value: admin.Email})
if admin.Name != "" {
env = append(env, corev1.EnvVar{Name: "PAPERCLIP_SEED_ADMIN_NAME", Value: admin.Name})
}
if admin.UserID != "" {
env = append(env, corev1.EnvVar{Name: "PAPERCLIP_SEED_ADMIN_USER_ID", Value: admin.UserID})
}
return corev1.Container{
Name: "seed-instance-admin",
Image: image,
ImagePullPolicy: imagePullPolicy(instance),
Command: []string{"/bin/sh", "-c"},
Args: []string{"exec pnpm paperclipai auth seed-instance-admin"},
Env: env,
EnvFrom: instance.Spec.EnvFrom,
VolumeMounts: buildVolumeMounts(instance),
SecurityContext: paperclipContainerSecurityContext(instance),
}
}
// shareProcessNamespace returns the effective ShareProcessNamespace value,
// defaulting to true so a pause container reaps zombie processes left by the
// Node.js server (which does not call waitpid()). Users may opt out by setting
// spec.shareProcessNamespace to false.
func shareProcessNamespace(instance *paperclipv1alpha1.Instance) *bool {
if instance.Spec.ShareProcessNamespace != nil {
return instance.Spec.ShareProcessNamespace
}
return Ptr(true)
}
func containerImage(instance *paperclipv1alpha1.Instance) string {
repo := instance.Spec.Image.Repository
if repo == "" {
repo = "ghcr.io/paperclipai/paperclip"
}
if instance.Spec.Image.Digest != "" {
return repo + "@" + instance.Spec.Image.Digest
}
return repo + ":" + instance.Spec.Image.Tag
}
func imagePullPolicy(instance *paperclipv1alpha1.Instance) corev1.PullPolicy {
if instance.Spec.Image.PullPolicy != "" {
return instance.Spec.Image.PullPolicy
}
return corev1.PullIfNotPresent
}
func servicePort(instance *paperclipv1alpha1.Instance) int32 {
return ServerPort(instance)
}