-
Notifications
You must be signed in to change notification settings - Fork 566
/
worker_rc.go
1152 lines (1093 loc) · 38.7 KB
/
worker_rc.go
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
995
996
997
998
999
1000
package server
import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
jsonpatch "github.com/evanphx/json-patch"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
client "github.com/pachyderm/pachyderm/v2/src/internal/client"
"github.com/pachyderm/pachyderm/v2/src/internal/config"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/errutil"
"github.com/pachyderm/pachyderm/v2/src/internal/log"
"github.com/pachyderm/pachyderm/v2/src/internal/ppsutil"
"github.com/pachyderm/pachyderm/v2/src/internal/tracing"
"github.com/pachyderm/pachyderm/v2/src/pps"
workerstats "github.com/pachyderm/pachyderm/v2/src/server/worker/stats"
"github.com/pachyderm/pachyderm/v2/src/version"
)
const (
appLabel = "app"
pipelineProjectLabel = "pipelineProject"
pipelineNameLabel = "pipelineName"
pipelineVersionLabel = "pipelineVersion"
pipelineProjectAnnotation = "pipelineProject"
pipelineNameAnnotation = "pipelineName"
pachVersionAnnotation = "pachVersion"
pipelineVersionAnnotation = "pipelineVersion"
pipelineSpecCommitAnnotation = "specCommit"
hashedAuthTokenAnnotation = "authTokenHash"
// WorkerServiceAccountEnvVar is the name of the environment variable used to tell pachd
// what service account to assign to new worker RCs, for the purpose of
// creating S3 gateway services.
WorkerServiceAccountEnvVar = "WORKER_SERVICE_ACCOUNT"
// DefaultWorkerServiceAccountName is the default value to use if WorkerServiceAccountEnvVar is
// undefined (for compatibility purposes)
DefaultWorkerServiceAccountName = "pachyderm-worker"
// UploadConcurrencyLimitEnvVar is the environment variable for the upload concurrency limit.
// EnvVar defined in src/internal/serviceenv/config.go
UploadConcurrencyLimitEnvVar = "STORAGE_UPLOAD_CONCURRENCY_LIMIT"
StorageCompactionShardSizeThresholdEnvVar = "STORAGE_COMPACTION_SHARD_SIZE_THRESHOLD"
StorageCompactionShardCountThresholdEnvVar = "STORAGE_COMPACTION_SHARD_COUNT_THRESHOLD"
StorageMemoryThresholdEnvVar = "STORAGE_MEMORY_THRESHOLD"
StorageLevelFactorEnvVar = "STORAGE_LEVEL_FACTOR"
StorageMaxFanInEnvVar = "STORAGE_COMPACTION_MAX_FANIN"
StorageMaxOpenFileSetsEnvVar = "STORAGE_FILESETS_MAX_OPEN"
StorageDiskCacheSizeEnvVar = "STORAGE_DISK_CACHE_SIZE"
StorageMemoryCacheSizeEnvVar = "STORAGE_MEMORY_CACHE_SIZE"
)
// Parameters used when creating the kubernetes replication controller in charge
// of a job or pipeline's workers
type workerOptions struct {
rcName string // Name of the replication controller managing workers
specCommit string // Pipeline spec commit ID (needed for s3 inputs)
s3GatewayPort int32 // s3 gateway port (if any s3 pipeline inputs)
userImage string // The user's pipeline/job image
labels map[string]string // k8s labels attached to the RC and workers
annotations map[string]string // k8s annotations attached to the RC and workers
parallelism int32 // Number of replicas the RC maintains
resourceRequests *v1.ResourceList // Resources requested by pipeline/job pods
resourceLimits *v1.ResourceList // Resources requested by pipeline/job pods, applied to the user and init containers
sidecarResourceLimits *v1.ResourceList // Resources requested by pipeline/job pods, applied to the sidecar container
sidecarResourceRequests *v1.ResourceList // Resources requested by pipeline/job pods, applied to the sidecar container
workerEnv []v1.EnvVar // Environment vars set in the user container
volumes []v1.Volume // Volumes that we expose to the user container
volumeMounts []v1.VolumeMount // Paths where we mount each volume in 'volumes'
postgresSecret *v1.SecretKeySelector // the reference to the postgres password
schedulingSpec *pps.SchedulingSpec // the SchedulingSpec for the pipeline
podSpec string
podPatch string
// Secrets that we mount in the worker container (e.g. for reading/writing to
// s3)
imagePullSecrets []v1.LocalObjectReference
service *pps.Service
tolerations []v1.Toleration
}
// getPachctlSecretVolumeAndMount returns a Volume and
// VolumeMount object configured for the pachctl secret (currently used in spout pipelines).
func getPachctlSecretVolumeAndMount(secret string) (v1.Volume, v1.VolumeMount) {
return v1.Volume{
Name: client.PachctlSecretName,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: secret,
},
},
}, v1.VolumeMount{
Name: client.PachctlSecretName,
MountPath: "/pachctl",
}
}
// getTLSCertSecretVolumeAndMount returns a Volume and VolumeMount object
// configured for the pach-tls secret to be stored in pipeline side-cars.
func getTLSCertSecretVolumeAndMount(secret, mountPath string) (v1.Volume, v1.VolumeMount) {
return v1.Volume{
Name: secret,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: secret,
},
},
}, v1.VolumeMount{
Name: secret,
MountPath: mountPath,
}
}
func (kd *kubeDriver) workerPodSpec(ctx context.Context, options *workerOptions, pipelineInfo *pps.PipelineInfo) (v1.PodSpec, error) {
pullPolicy := kd.config.WorkerImagePullPolicy
if pullPolicy == "" {
pullPolicy = "IfNotPresent"
}
// Environment variables that are shared between both containers
commonEnv := []v1.EnvVar{{
Name: "PACH_ROOT",
Value: kd.config.StorageRoot,
}, {
Name: "PACH_NAMESPACE",
Value: kd.namespace,
}, {
Name: "STORAGE_BACKEND",
Value: kd.config.StorageBackend,
}, {
Name: "POSTGRES_USER",
Value: kd.config.PostgresUser,
}, {
Name: "POSTGRES_PASSWORD",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: options.postgresSecret,
},
}, {
Name: "POSTGRES_DATABASE",
Value: kd.config.PostgresDBName,
}, {
Name: "PG_BOUNCER_HOST",
Value: kd.config.PGBouncerHost,
}, {
Name: "PG_BOUNCER_PORT",
Value: strconv.FormatInt(int64(kd.config.PGBouncerPort), 10),
}, {
Name: client.PeerPortEnv,
Value: strconv.FormatUint(uint64(kd.config.PeerPort), 10),
}, {
Name: client.PPSSpecCommitEnv,
Value: options.specCommit,
}, {
Name: client.PPSProjectNameEnv,
Value: pipelineInfo.Pipeline.Project.GetName(),
}, {
Name: client.PPSPipelineNameEnv,
Value: pipelineInfo.Pipeline.Name,
}, {
Name: "LOKI_SERVICE_HOST",
Value: kd.config.LokiHost,
}, {
Name: "LOKI_SERVICE_PORT",
Value: kd.config.LokiPort,
}, {
Name: "GOCOVERDIR",
Value: "/tmp",
},
// These are set explicitly below to prevent kubernetes from setting them to the service host and port.
{
Name: "POSTGRES_PORT",
Value: "",
}, {
Name: "POSTGRES_HOST",
Value: "",
},
}
commonEnv = append(commonEnv, log.WorkerLogConfig.AsKubernetesEnvironment()...)
// Set up sidecar env vars
sidecarEnv := []v1.EnvVar{{
Name: "PORT",
Value: strconv.FormatUint(uint64(kd.config.Port), 10),
}, {
Name: "PACHD_POD_NAME",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "metadata.name",
},
},
}, {
Name: "PACHW_IN_SIDECARS",
Value: strconv.FormatBool(kd.config.PachwInSidecars),
}, {
Name: "GC_PERCENT",
Value: strconv.FormatInt(int64(kd.config.GCPercent), 10),
}}
sidecarEnv = append(sidecarEnv, kd.getStorageEnvVars(pipelineInfo)...)
sidecarEnv = append(sidecarEnv, commonEnv...)
sidecarEnv = append(sidecarEnv, kd.getEgressSecretEnvVars(pipelineInfo)...)
// Set up worker env vars
workerEnv := append(options.workerEnv, []v1.EnvVar{
// Set core pach env vars
{
Name: "PACH_IN_WORKER",
Value: "true",
},
// We use Kubernetes' "Downward API" so the workers know their IP
// addresses, which they will then post on etcd so the job managers
// can discover the workers.
{
Name: client.PPSWorkerIPEnv,
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "status.podIP",
},
},
},
// Set the PPS env vars
{
Name: client.PPSEtcdPrefixEnv,
Value: kd.etcdPrefix,
},
{
Name: client.PPSPodNameEnv,
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "metadata.name",
},
},
},
{
Name: client.PPSWorkerPortEnv,
Value: strconv.FormatUint(uint64(kd.config.PPSWorkerPort), 10),
},
}...)
workerEnv = append(workerEnv, commonEnv...)
// Set S3GatewayPort in the worker (for user code) and sidecar (for serving)
if options.s3GatewayPort != 0 {
workerEnv = append(workerEnv, v1.EnvVar{
Name: "S3GATEWAY_PORT",
Value: strconv.FormatUint(uint64(options.s3GatewayPort), 10),
})
sidecarEnv = append(sidecarEnv, v1.EnvVar{
Name: "S3GATEWAY_PORT",
Value: strconv.FormatUint(uint64(options.s3GatewayPort), 10),
})
}
// Propagate feature flags to worker and sidecar
if kd.config.DisableCommitProgressCounter {
sidecarEnv = append(sidecarEnv, v1.EnvVar{Name: "DISABLE_COMMIT_PROGRESS_COUNTER", Value: "true"})
workerEnv = append(workerEnv, v1.EnvVar{Name: "DISABLE_COMMIT_PROGRESS_COUNTER", Value: "true"})
}
if kd.config.LokiLogging {
sidecarEnv = append(sidecarEnv, v1.EnvVar{Name: "LOKI_LOGGING", Value: "true"})
workerEnv = append(workerEnv, v1.EnvVar{Name: "LOKI_LOGGING", Value: "true"})
}
if p := kd.config.GoogleCloudProfilerProject; p != "" {
sidecarEnv = append(sidecarEnv, v1.EnvVar{Name: "GOOGLE_CLOUD_PROFILER_PROJECT", Value: p})
workerEnv = append(workerEnv, v1.EnvVar{Name: "GOOGLE_CLOUD_PROFILER_PROJECT", Value: p})
}
if pipelineInfo.Details.Determined != nil {
workerEnv = append(workerEnv, kd.getDeterminedEnvVars(pipelineInfo)...)
}
// This only happens in local deployment. We want the workers to be
// able to read from/write to the hostpath volume as well.
storageVolumeName := "pach-disk"
var sidecarVolumeMounts []v1.VolumeMount
userVolumeMounts := make([]v1.VolumeMount, len(options.volumeMounts))
copy(userVolumeMounts, options.volumeMounts)
if kd.config.StorageHostPath != "" {
options.volumes = append(options.volumes, v1.Volume{
Name: storageVolumeName,
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: kd.config.StorageHostPath,
},
},
})
storageMount := v1.VolumeMount{
Name: storageVolumeName,
MountPath: kd.config.StorageRoot,
}
sidecarVolumeMounts = append(sidecarVolumeMounts, storageMount)
userVolumeMounts = append(userVolumeMounts, storageMount)
} else {
// `pach-dir-volume` is needed for openshift, see:
// https://github.com/pachyderm/pachyderm/v2/issues/3404
options.volumes = append(options.volumes, v1.Volume{
Name: "pach-dir-volume",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
}, v1.Volume{
Name: "user-tmp",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
})
emptyDirVolumeMount := v1.VolumeMount{
Name: "pach-dir-volume",
MountPath: kd.config.StorageRoot,
}
userTmpVolumeMount := v1.VolumeMount{
Name: "user-tmp",
MountPath: "/tmp",
}
sidecarVolumeMounts = append(sidecarVolumeMounts, emptyDirVolumeMount)
userVolumeMounts = append(userVolumeMounts, emptyDirVolumeMount, userTmpVolumeMount)
}
// add emptydir for /tmp to allow for read only rootfs
options.volumes = append(options.volumes, v1.Volume{
Name: "tmp",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
}})
tmpDirVolumeMount := v1.VolumeMount{
Name: "tmp",
MountPath: "/tmp",
}
secretVolume, secretMount := GetBackendSecretVolumeAndMount()
options.volumes = append(options.volumes, secretVolume)
sidecarVolumeMounts = append(sidecarVolumeMounts, secretMount, tmpDirVolumeMount)
userVolumeMounts = append(userVolumeMounts, secretMount)
// in the case the pachd is deployed with custom root certs, propagate them to the side-cars
if path, ok := os.LookupEnv("SSL_CERT_DIR"); ok {
sidecarEnv = append(sidecarEnv, v1.EnvVar{Name: "SSL_CERT_DIR", Value: path})
certSecretVolume, certSecretMount := getTLSCertSecretVolumeAndMount(kd.config.TLSCertSecretName, path)
options.volumes = append(options.volumes, certSecretVolume)
sidecarVolumeMounts = append(sidecarVolumeMounts, certSecretMount)
}
// mount secret for spouts using pachctl
if pipelineInfo.Details.Spout != nil {
pachctlSecretVolume, pachctlSecretMount := getPachctlSecretVolumeAndMount(spoutSecretName(pipelineInfo.Pipeline))
options.volumes = append(options.volumes, pachctlSecretVolume)
sidecarVolumeMounts = append(sidecarVolumeMounts, pachctlSecretMount)
userVolumeMounts = append(userVolumeMounts, pachctlSecretMount)
}
// Explicitly set CPU requests to zero because some cloud providers set their
// own defaults which are usually not what we want. Mem request defaults to
// 64M, but is overridden by the CacheSize setting for the sidecar.
cpuZeroQuantity := resource.MustParse("0")
memDefaultQuantity := resource.MustParse("64M")
memSidecarQuantity := resource.MustParse("64M")
// Get service account name for worker from env or use default
workerServiceAccountName, ok := os.LookupEnv(WorkerServiceAccountEnvVar)
if !ok {
workerServiceAccountName = DefaultWorkerServiceAccountName
}
// possibly expose s3 gateway port in the sidecar container
var sidecarPorts []v1.ContainerPort
if options.s3GatewayPort != 0 {
sidecarPorts = append(sidecarPorts, v1.ContainerPort{
ContainerPort: options.s3GatewayPort,
})
}
workerImage := kd.config.WorkerImage
pachSecurityCtx := &v1.SecurityContext{
RunAsUser: int64Ptr(1000),
RunAsGroup: int64Ptr(1000),
AllowPrivilegeEscalation: pointer.Bool(false),
ReadOnlyRootFilesystem: pointer.Bool(true),
Capabilities: &v1.Capabilities{Drop: []v1.Capability{"all"}},
}
var userSecurityCtx *v1.SecurityContext
var podSecurityContext *v1.PodSecurityContext
userStr := pipelineInfo.Details.Transform.User
if !kd.config.EnableWorkerSecurityContexts {
pachSecurityCtx = nil
podSecurityContext = nil
} else if kd.config.WorkerUsesRoot {
pachSecurityCtx = &v1.SecurityContext{RunAsUser: int64Ptr(0)}
userSecurityCtx = &v1.SecurityContext{RunAsUser: int64Ptr(0)}
podSecurityContext = nil
} else if userStr != "" {
// This is to allow the user to be set in the pipeline spec.
if i, err := strconv.ParseInt(userStr, 10, 64); err != nil {
log.Error(ctx, "could not parse user into int", zap.String("user", userStr), zap.Error(err))
} else {
// hard coded security settings besides uid/gid.
podSecurityContext = &v1.PodSecurityContext{
RunAsUser: int64Ptr(i),
RunAsGroup: int64Ptr(i),
FSGroup: int64Ptr(i),
RunAsNonRoot: pointer.Bool(true),
SeccompProfile: &v1.SeccompProfile{
Type: v1.SeccompProfileType("RuntimeDefault"),
}}
userSecurityCtx = &v1.SecurityContext{
RunAsUser: int64Ptr(i),
RunAsGroup: int64Ptr(i),
AllowPrivilegeEscalation: pointer.Bool(false),
ReadOnlyRootFilesystem: pointer.Bool(true),
Capabilities: &v1.Capabilities{Drop: []v1.Capability{"all"}},
}
}
}
envFrom := []v1.EnvFromSource{
{
SecretRef: &v1.SecretEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: client.StorageSecretName,
},
},
},
}
podSpec := v1.PodSpec{
InitContainers: []v1.Container{
{
Name: "init",
Image: workerImage,
Command: []string{"/app/init"},
ImagePullPolicy: v1.PullPolicy(pullPolicy),
VolumeMounts: options.volumeMounts,
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: cpuZeroQuantity,
v1.ResourceMemory: memDefaultQuantity,
},
},
SecurityContext: pachSecurityCtx,
},
},
Containers: []v1.Container{
{
Name: client.PPSWorkerUserContainerName,
Image: options.userImage,
Command: []string{"/pach-bin/dumb-init", "--", "/pach-bin/worker"},
ImagePullPolicy: v1.PullPolicy(pullPolicy),
Env: workerEnv,
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{},
},
VolumeMounts: userVolumeMounts,
SecurityContext: userSecurityCtx,
},
{
Name: client.PPSWorkerSidecarContainerName,
Image: kd.config.WorkerSidecarImage,
Command: []string{"/pachd", "--mode", "sidecar"},
ImagePullPolicy: v1.PullPolicy(pullPolicy),
Env: sidecarEnv,
EnvFrom: envFrom,
VolumeMounts: sidecarVolumeMounts,
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: cpuZeroQuantity,
v1.ResourceMemory: memSidecarQuantity,
},
},
Ports: sidecarPorts,
SecurityContext: pachSecurityCtx,
},
},
ServiceAccountName: workerServiceAccountName,
AutomountServiceAccountToken: pointer.Bool(true),
RestartPolicy: "Always",
Volumes: options.volumes,
ImagePullSecrets: options.imagePullSecrets,
TerminationGracePeriodSeconds: int64Ptr(0),
SecurityContext: podSecurityContext,
Tolerations: options.tolerations,
}
if options.schedulingSpec != nil {
podSpec.NodeSelector = options.schedulingSpec.NodeSelector
podSpec.PriorityClassName = options.schedulingSpec.PriorityClassName
}
if options.resourceRequests != nil {
for k, v := range *options.resourceRequests {
podSpec.Containers[0].Resources.Requests[k] = v
}
}
// Copy over some settings from the user to init container. We don't apply GPU
// requests because of FUD. The init container shouldn't run concurrently with
// the user container, so there should be no contention here.
for _, k := range []v1.ResourceName{v1.ResourceCPU, v1.ResourceMemory, v1.ResourceEphemeralStorage} {
if val, ok := podSpec.Containers[0].Resources.Requests[k]; ok {
podSpec.InitContainers[0].Resources.Requests[k] = val
}
}
if options.resourceLimits != nil {
podSpec.InitContainers[0].Resources.Limits = make(v1.ResourceList)
podSpec.Containers[0].Resources.Limits = make(v1.ResourceList)
for k, v := range *options.resourceLimits {
podSpec.InitContainers[0].Resources.Limits[k] = v
podSpec.Containers[0].Resources.Limits[k] = v
}
}
if options.sidecarResourceLimits != nil {
podSpec.Containers[1].Resources.Limits = make(v1.ResourceList)
for k, v := range *options.sidecarResourceLimits {
podSpec.Containers[1].Resources.Limits[k] = v
}
}
if options.sidecarResourceRequests != nil {
podSpec.Containers[1].Resources.Requests = make(v1.ResourceList)
for k, v := range *options.sidecarResourceRequests {
podSpec.Containers[1].Resources.Requests[k] = v
}
}
if options.podSpec != "" || options.podPatch != "" {
jsonPodSpec, err := json.Marshal(&podSpec)
if err != nil {
return v1.PodSpec{}, errors.EnsureStack(err)
}
// the json now contained in jsonPodSpec is the authoritative copy
// so we should deserialize in into a fresh structure
podSpec = v1.PodSpec{}
if options.podSpec != "" {
jsonPodSpec, err = jsonpatch.MergePatch(jsonPodSpec, []byte(options.podSpec))
if err != nil {
return v1.PodSpec{}, errors.EnsureStack(err)
}
}
if options.podPatch != "" {
patch, err := jsonpatch.DecodePatch([]byte(options.podPatch))
if err != nil {
return v1.PodSpec{}, errors.EnsureStack(err)
}
jsonPodSpec, err = patch.Apply(jsonPodSpec)
if err != nil {
return v1.PodSpec{}, errors.EnsureStack(err)
}
}
if err := json.Unmarshal(jsonPodSpec, &podSpec); err != nil {
return v1.PodSpec{}, errors.EnsureStack(err)
}
}
return podSpec, nil
}
func (kd *kubeDriver) getStorageEnvVars(pipelineInfo *pps.PipelineInfo) []v1.EnvVar {
vars := []v1.EnvVar{
{Name: UploadConcurrencyLimitEnvVar, Value: strconv.Itoa(kd.config.StorageUploadConcurrencyLimit)},
}
if kd.config.StorageCompactionShardSizeThreshold > 0 {
vars = append(vars, v1.EnvVar{
Name: StorageCompactionShardSizeThresholdEnvVar,
Value: strconv.FormatInt(kd.config.StorageCompactionShardSizeThreshold, 10),
})
}
if kd.config.StorageCompactionShardCountThreshold > 0 {
vars = append(vars, v1.EnvVar{
Name: StorageCompactionShardCountThresholdEnvVar,
Value: strconv.FormatInt(kd.config.StorageCompactionShardCountThreshold, 10),
})
}
if kd.config.StorageMemoryThreshold > 0 {
vars = append(vars, v1.EnvVar{
Name: StorageMemoryThresholdEnvVar,
Value: strconv.FormatInt(kd.config.StorageMemoryThreshold, 10),
})
}
if kd.config.StorageLevelFactor > 0 {
vars = append(vars, v1.EnvVar{
Name: StorageLevelFactorEnvVar,
Value: strconv.FormatInt(kd.config.StorageLevelFactor, 10),
})
}
if kd.config.StorageCompactionMaxFanIn != 10 {
vars = append(vars, v1.EnvVar{
Name: StorageMaxFanInEnvVar,
Value: strconv.FormatInt(int64(kd.config.StorageCompactionMaxFanIn), 10),
})
}
if kd.config.StorageFileSetsMaxOpen != 50 {
vars = append(vars, v1.EnvVar{
Name: StorageMaxOpenFileSetsEnvVar,
Value: strconv.FormatInt(int64(kd.config.StorageFileSetsMaxOpen), 10),
})
}
if kd.config.StorageDiskCacheSize != 100 {
vars = append(vars, v1.EnvVar{
Name: StorageDiskCacheSizeEnvVar,
Value: strconv.FormatInt(int64(kd.config.StorageDiskCacheSize), 10),
})
}
if kd.config.StorageMemoryCacheSize != 100 {
vars = append(vars, v1.EnvVar{
Name: StorageMemoryCacheSizeEnvVar,
Value: strconv.FormatInt(int64(kd.config.StorageMemoryCacheSize), 10),
})
}
return vars
}
func (kd *kubeDriver) getEgressSecretEnvVars(pipelineInfo *pps.PipelineInfo) []v1.EnvVar {
result := []v1.EnvVar{}
egress := pipelineInfo.Details.Egress
if egress != nil && egress.GetSqlDatabase() != nil && egress.GetSqlDatabase().GetSecret() != nil {
secret := egress.GetSqlDatabase().GetSecret()
result = append(result, v1.EnvVar{
Name: "PACHYDERM_SQL_PASSWORD", // TODO avoid hardcoding this
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{Name: secret.Name},
Key: secret.Key,
},
},
})
}
return result
}
func (kd *kubeDriver) getDeterminedEnvVars(pipelineInfo *pps.PipelineInfo) []v1.EnvVar {
return []v1.EnvVar{
{
Name: "DET_MASTER_CERT_FILE",
Value: "noverify",
},
{
Name: "DET_MASTER",
Value: kd.config.DeterminedURL,
},
{
Name: "DET_USER",
Value: strings.ReplaceAll(pipelineInfo.Pipeline.String(), "/", "_"), // TODO: call common util for determined pipeline user name
},
{
Name: "DET_PASS",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: pipelineInfo.Pipeline.Project.Name + "-" + pipelineInfo.Pipeline.Name + "-det",
},
Key: "password",
},
},
},
}
}
// We don't want to expose pipeline auth tokens, so we hash it. This will be
// visible to any user with k8s cluster access
// Note: This hash shouldn't be used for authentication in any way. We just use
// this to detect if an auth token has been added/changed
// Note: ptr.AuthToken is a pachyderm-generated UUID, and wouldn't appear in any
// rainbow tables
func hashAuthToken(token string) string {
h := sha256.Sum256([]byte(token))
return base64.RawURLEncoding.EncodeToString(h[:])
}
func (kd *kubeDriver) getWorkerOptions(ctx context.Context, pipelineInfo *pps.PipelineInfo) (*workerOptions, error) {
projectName := pipelineInfo.Pipeline.Project.GetName()
pipelineName := pipelineInfo.Pipeline.Name
pipelineVersion := pipelineInfo.Version
var resourceRequests *v1.ResourceList
var resourceLimits *v1.ResourceList
var sidecarResourceLimits *v1.ResourceList
var sidecarResourceRequests *v1.ResourceList
if pipelineInfo.Details.ResourceRequests != nil {
var err error
resourceRequests, err = ppsutil.GetRequestsResourceListFromPipeline(ctx, pipelineInfo)
if err != nil {
return nil, errors.Wrapf(err, "could not determine resource request")
}
}
if pipelineInfo.Details.ResourceLimits != nil {
var err error
resourceLimits, err = ppsutil.GetLimitsResourceList(ctx, pipelineInfo.Details.ResourceLimits)
if err != nil {
return nil, errors.Wrapf(err, "could not determine resource limit")
}
}
// Users are often surprised when their jobs that claim to require 0 CPU are scheduled to a
// node with no free CPU resources. This avoids that; if resources are completely omitted
// in the pipeline spec, then we supply some reasonable defaults. You can supply an empty
// ResrouceRequests in your pipeline to avoid these defaults; this will explicitly request
// the old behavior of not making any requests.
if pipelineInfo.Details.ResourceRequests == nil && pipelineInfo.Details.ResourceLimits == nil {
resourceRequests = &v1.ResourceList{
v1.ResourceCPU: kd.config.PipelineDefaultCPURequest,
v1.ResourceMemory: kd.config.PipelineDefaultMemoryRequest,
v1.ResourceEphemeralStorage: kd.config.PipelineDefaultStorageRequest,
}
log.Info(ctx, "setting default resource requests on pipeline; supply an empty resource request ('resource_requests: {}') to opt out",
zap.String("pipeline", pipelineInfo.GetPipeline().GetName()),
zap.Reflect("requests", resourceRequests))
}
if pipelineInfo.Details.SidecarResourceLimits != nil {
var err error
sidecarResourceLimits, err = ppsutil.GetLimitsResourceList(ctx, pipelineInfo.Details.SidecarResourceLimits)
if err != nil {
return nil, errors.Wrapf(err, "could not determine sidecar resource limit")
}
}
if pipelineInfo.Details.SidecarResourceRequests != nil {
var err error
sidecarResourceRequests, err = ppsutil.GetLimitsResourceList(ctx, pipelineInfo.Details.SidecarResourceRequests)
if err != nil {
return nil, errors.Wrapf(err, "could not determine sidecar resource request")
}
}
transform := pipelineInfo.Details.Transform
labels := pipelineLabels(projectName, pipelineName, pipelineVersion)
userImage := transform.Image
if userImage == "" {
userImage = DefaultUserImage
}
workerEnv := []v1.EnvVar{}
for name, value := range transform.Env {
workerEnv = append(
workerEnv,
v1.EnvVar{
Name: name,
Value: value,
},
)
}
var volumes []v1.Volume
var volumeMounts []v1.VolumeMount
for _, secret := range transform.Secrets {
if secret.MountPath != "" {
volumes = append(volumes, v1.Volume{
Name: secret.Name,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: secret.Name,
},
},
})
volumeMounts = append(volumeMounts, v1.VolumeMount{
Name: secret.Name,
MountPath: secret.MountPath,
})
}
if secret.EnvVar != "" {
workerEnv = append(workerEnv, v1.EnvVar{
Name: secret.EnvVar,
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: secret.Name,
},
Key: secret.Key,
},
},
})
}
}
volumes = append(volumes, v1.Volume{
Name: "pach-bin",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
})
volumeMounts = append(volumeMounts, v1.VolumeMount{
Name: "pach-bin",
MountPath: "/pach-bin",
})
workerVolume := v1.Volume{
Name: client.PPSWorkerVolume,
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
}
if transform.MemoryVolume {
workerVolume.VolumeSource.EmptyDir.Medium = v1.StorageMediumMemory
}
volumes = append(volumes, workerVolume)
volumeMounts = append(volumeMounts, v1.VolumeMount{
Name: client.PPSWorkerVolume,
MountPath: client.PPSInputPrefix,
})
var imagePullSecrets []v1.LocalObjectReference
for _, secret := range transform.ImagePullSecrets {
imagePullSecrets = append(imagePullSecrets, v1.LocalObjectReference{Name: secret})
}
if kd.config.ImagePullSecrets != "" {
secrets := strings.Split(kd.config.ImagePullSecrets, ",")
for _, secret := range secrets {
imagePullSecrets = append(imagePullSecrets, v1.LocalObjectReference{Name: secret})
}
}
annotations := map[string]string{
pipelineNameAnnotation: pipelineName,
pachVersionAnnotation: version.PrettyVersion(),
pipelineVersionAnnotation: strconv.FormatUint(pipelineInfo.Version, 10),
pipelineSpecCommitAnnotation: pipelineInfo.SpecCommit.Id,
hashedAuthTokenAnnotation: hashAuthToken(pipelineInfo.AuthToken),
}
if projectName != "" {
annotations[pipelineProjectAnnotation] = projectName
}
// add the user's custom metadata (annotations and labels).
metadata := pipelineInfo.Details.GetMetadata()
if metadata != nil {
for k, v := range metadata.Annotations {
if annotations[k] == "" {
annotations[k] = v
}
}
for k, v := range metadata.Labels {
if labels[k] == "" {
labels[k] = v
}
}
}
// A service can be present either directly on the pipeline spec
// or on the spout field of the spec.
var service *pps.Service
if pipelineInfo.Details.Spout != nil && pipelineInfo.Details.Service != nil {
return nil, errors.New("only one of pipeline.service or pipeline.spout can be set")
} else if pipelineInfo.Details.Spout != nil && pipelineInfo.Details.Spout.Service != nil {
service = pipelineInfo.Details.Spout.Service
} else {
service = pipelineInfo.Details.Service
}
var s3GatewayPort int32
if ppsutil.ContainsS3Inputs(pipelineInfo.Details.Input) || pipelineInfo.Details.S3Out {
s3GatewayPort = int32(kd.config.S3GatewayPort)
}
// Get the reference to the postgres secret used by the current pod
podName := kd.config.PachdPodName
selfPodInfo, err := kd.kubeClient.CoreV1().Pods(kd.namespace).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return nil, errors.EnsureStack(err)
}
var postgresSecretRef *v1.SecretKeySelector
for _, container := range selfPodInfo.Spec.Containers {
for _, envVar := range container.Env {
if envVar.Name == "POSTGRES_PASSWORD" && envVar.ValueFrom != nil && envVar.ValueFrom.SecretKeyRef != nil {
postgresSecretRef = envVar.ValueFrom.SecretKeyRef
}
}
}
if postgresSecretRef == nil {
return nil, errors.New("could not load the existing postgres secret reference from kubernetes")
}
// Setup tolerations
var tolerations []v1.Toleration
var tolErr error
for i, in := range pipelineInfo.GetDetails().GetTolerations() {
out, err := transformToleration(in)
if err != nil {
errors.JoinInto(&tolErr, errors.Errorf("toleration %d/%d: %v", i+1, len(pipelineInfo.GetDetails().GetTolerations()), err))
continue
}
tolerations = append(tolerations, out)
}
if tolErr != nil {
return nil, tolErr
}
// Generate options for new RC
return &workerOptions{
rcName: ppsutil.PipelineRcName(pipelineInfo),
s3GatewayPort: s3GatewayPort,
specCommit: pipelineInfo.SpecCommit.Id,
labels: labels,
annotations: annotations,
parallelism: int32(0), // pipelines start w/ 0 workers & are scaled up
resourceRequests: resourceRequests,
resourceLimits: resourceLimits,
sidecarResourceLimits: sidecarResourceLimits,
sidecarResourceRequests: sidecarResourceRequests,
userImage: userImage,
workerEnv: workerEnv,
volumes: volumes,
volumeMounts: volumeMounts,
postgresSecret: postgresSecretRef,
imagePullSecrets: imagePullSecrets,
service: service,
schedulingSpec: pipelineInfo.Details.SchedulingSpec,
podSpec: pipelineInfo.Details.PodSpec,
podPatch: pipelineInfo.Details.PodPatch,
tolerations: tolerations,
}, nil
}
// transformToleration transforms a pps.Toleration into a k8s Toleration. It's used while creating
// the RC, and at pipeline submission to validate the provided tolerations before saving the
// pipeline. It's in here and not in src/pps/pps.go so that the protobuf library doesn't depend on
// k8s.
func transformToleration(in *pps.Toleration) (v1.Toleration, error) {
var out v1.Toleration
out.Key = in.GetKey()
out.Value = in.GetValue()
if ts := in.GetTolerationSeconds(); ts != nil {
out.TolerationSeconds = &ts.Value
}
switch in.GetEffect() { //exhaustive:enforce
case pps.TaintEffect_ALL_EFFECTS:
out.Effect = ""
case pps.TaintEffect_NO_EXECUTE:
out.Effect = v1.TaintEffectNoExecute
case pps.TaintEffect_NO_SCHEDULE:
out.Effect = v1.TaintEffectNoSchedule
case pps.TaintEffect_PREFER_NO_SCHEDULE:
out.Effect = v1.TaintEffectPreferNoSchedule
}
switch in.GetOperator() { //exhaustive:enforce
case pps.TolerationOperator_EMPTY:
return out, errors.New("cannot omit key/value comparison operator; specify EQUAL or EXISTS")
case pps.TolerationOperator_EXISTS:
out.Operator = v1.TolerationOpExists
case pps.TolerationOperator_EQUAL:
out.Operator = v1.TolerationOpEqual
}
return out, nil
}
func (kd *kubeDriver) createWorkerPachctlSecret(ctx context.Context, pipelineInfo *pps.PipelineInfo) error {
var cfg config.Config
err := cfg.InitV2()
if err != nil {
return errors.Wrapf(err, "error initializing V2 for config")
}
_, context, err := cfg.ActiveContext(true)
if err != nil {
return errors.Wrapf(err, "error getting the active context")
}
context.SessionToken = pipelineInfo.AuthToken
context.PachdAddress = "localhost:1653"
rawConfig, err := json.MarshalIndent(&cfg, "", " ")
if err != nil {
return errors.Wrapf(err, "error marshaling the config")
}
s := v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: spoutSecretName(pipelineInfo.Pipeline),
Labels: spoutLabels(pipelineInfo.Pipeline),
},
Data: map[string][]byte{
"config.json": rawConfig,
},
}
labels := s.GetLabels()
if projectName := pipelineInfo.Pipeline.Project.GetName(); projectName != "" {
labels[pipelineProjectLabel] = projectName
}
labels[pipelineNameLabel] = pipelineInfo.Pipeline.Name
s.SetLabels(labels)
// send RPC to k8s to create the secret there
if _, err := kd.kubeClient.CoreV1().Secrets(kd.namespace).Create(ctx, &s, metav1.CreateOptions{}); err != nil {
if !errutil.IsAlreadyExistError(err) {
return errors.EnsureStack(err)
}
}
return nil
}
func spoutSecretName(p *pps.Pipeline) string {
if projectName := p.Project.GetName(); projectName != "" {
return fmt.Sprintf("spout-pachctl-secret-%s-%s", projectName, p.Name)
}
return fmt.Sprintf("spout-pachctl-secret-%s", p.Name)
}
// noValidOptions error may be returned by createWorkerSvcAndRc to indicate that
// getWorkerOptions returned an error to it (getWorkerOptions does not return