-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
spec.go
1010 lines (897 loc) · 36.5 KB
/
spec.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
/*
Copyright 2018 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package object
import (
"bytes"
_ "embed"
"fmt"
"net/url"
"path"
"strings"
"text/template"
"github.com/hashicorp/vault/api"
"github.com/libopenstorage/secrets/vault"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/daemon/ceph/osd/kms"
cephconfig "github.com/rook/rook/pkg/operator/ceph/config"
"github.com/rook/rook/pkg/operator/ceph/controller"
cephver "github.com/rook/rook/pkg/operator/ceph/version"
"github.com/rook/rook/pkg/operator/k8sutil"
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/validation"
)
const (
serviceAccountName = "rook-ceph-rgw"
sseKMS = "ssekms"
sseS3 = "sses3"
vaultPrefix = "/v1/"
//nolint:gosec // since this is not leaking any hardcoded details
setupVaultTokenFile = `
set -e
VAULT_TOKEN_OLD_PATH=%s
VAULT_TOKEN_NEW_PATH=%s
if [ -d $VAULT_TOKEN_OLD_PATH/ssekms ]
then
cp --recursive --verbose $VAULT_TOKEN_OLD_PATH/ssekms/..data/. $VAULT_TOKEN_NEW_PATH/ssekms
chmod --recursive --verbose 400 $VAULT_TOKEN_NEW_PATH/ssekms/*
chmod --verbose 700 $VAULT_TOKEN_NEW_PATH/ssekms
fi
if [ -d $VAULT_TOKEN_OLD_PATH/sses3 ]
then
cp --recursive --verbose $VAULT_TOKEN_OLD_PATH/sses3/..data/. $VAULT_TOKEN_NEW_PATH/sses3
chmod --recursive --verbose 400 $VAULT_TOKEN_NEW_PATH/sses3/*
chmod --verbose 700 $VAULT_TOKEN_NEW_PATH/sses3
fi
chmod --verbose 700 $VAULT_TOKEN_NEW_PATH
chown --recursive --verbose ceph:ceph $VAULT_TOKEN_NEW_PATH
`
)
var (
cephVersionMinRGWSSES3 = cephver.CephVersion{Major: 17, Minor: 2, Extra: 3}
cephVersionMinRGWSSEKMSTLS = cephver.CephVersion{Major: 16, Minor: 2, Extra: 6}
//go:embed rgw-probe.sh
rgwProbeScriptTemplate string
)
type ProbeType string
type ProtocolType string
const (
StartupProbeType ProbeType = "startup"
ReadinessProbeType ProbeType = "readiness"
HTTPProtocol ProtocolType = "HTTP"
HTTPSProtocol ProtocolType = "HTTPS"
)
type rgwProbeConfig struct {
ProbeType ProbeType
Protocol ProtocolType
Port string
}
func (c *clusterConfig) createDeployment(rgwConfig *rgwConfig) (*apps.Deployment, error) {
pod, err := c.makeRGWPodSpec(rgwConfig)
if err != nil {
return nil, err
}
strategy := apps.DeploymentStrategy{
Type: apps.RecreateDeploymentStrategyType,
}
// Use the same keyring and have dedicated rgw instances reflected in the service map
replicas := c.store.Spec.Gateway.Instances
strategy.Type = apps.RollingUpdateDeploymentStrategyType
strategy.RollingUpdate = &apps.RollingUpdateDeployment{
MaxUnavailable: &intstr.IntOrString{IntVal: int32(1)},
MaxSurge: &intstr.IntOrString{IntVal: int32(0)},
}
d := &apps.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: rgwConfig.ResourceName,
Namespace: c.store.Namespace,
Labels: getLabels(c.store.Name, c.store.Namespace, true),
},
Spec: apps.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: getLabels(c.store.Name, c.store.Namespace, false),
},
Template: pod,
Replicas: &replicas,
Strategy: strategy,
},
}
k8sutil.AddRookVersionLabelToDeployment(d)
c.store.Spec.Gateway.Annotations.ApplyToObjectMeta(&d.ObjectMeta)
c.store.Spec.Gateway.Labels.ApplyToObjectMeta(&d.ObjectMeta)
controller.AddCephVersionLabelToDeployment(c.clusterInfo.CephVersion, d)
return d, nil
}
func (c *clusterConfig) makeRGWPodSpec(rgwConfig *rgwConfig) (v1.PodTemplateSpec, error) {
rgwDaemonContainer, err := c.makeDaemonContainer(rgwConfig)
if err != nil {
return v1.PodTemplateSpec{}, err
}
hostNetwork := c.store.Spec.IsHostNetwork(c.clusterSpec)
podSpec := v1.PodSpec{
InitContainers: []v1.Container{
c.makeChownInitContainer(rgwConfig),
},
Containers: []v1.Container{rgwDaemonContainer},
RestartPolicy: v1.RestartPolicyAlways,
Volumes: append(
controller.DaemonVolumes(c.DataPathMap, rgwConfig.ResourceName, c.clusterSpec.DataDirHostPath),
c.mimeTypesVolume(),
),
HostNetwork: hostNetwork,
PriorityClassName: c.store.Spec.Gateway.PriorityClassName,
ServiceAccountName: serviceAccountName,
}
// If the log collector is enabled we add the side-car container
if c.clusterSpec.LogCollector.Enabled {
shareProcessNamespace := true
podSpec.ShareProcessNamespace = &shareProcessNamespace
podSpec.Containers = append(podSpec.Containers, *controller.LogCollectorContainer(getDaemonName(rgwConfig), c.clusterInfo.Namespace, *c.clusterSpec))
}
// Replace default unreachable node toleration
k8sutil.AddUnreachableNodeToleration(&podSpec)
// Set the ssl cert if specified
if c.store.Spec.Gateway.SecurePort != 0 {
secretVolSrc, err := c.generateVolumeSourceWithTLSSecret()
if err != nil {
return v1.PodTemplateSpec{}, err
}
certVol := v1.Volume{
Name: certVolumeName,
VolumeSource: v1.VolumeSource{
Secret: secretVolSrc,
}}
podSpec.Volumes = append(podSpec.Volumes, certVol)
}
// Check custom caBundle provided
if c.store.Spec.Gateway.CaBundleRef != "" {
customCaBundleVolSrc, err := c.generateVolumeSourceWithCaBundleSecret()
if err != nil {
return v1.PodTemplateSpec{}, err
}
customCaBundleVol := v1.Volume{
Name: caBundleVolumeName,
VolumeSource: v1.VolumeSource{
Secret: customCaBundleVolSrc,
}}
podSpec.Volumes = append(podSpec.Volumes, customCaBundleVol)
updatedCaBundleVol := v1.Volume{
Name: caBundleUpdatedVolumeName,
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
}}
podSpec.Volumes = append(podSpec.Volumes, updatedCaBundleVol)
podSpec.InitContainers = append(podSpec.InitContainers,
c.createCaBundleUpdateInitContainer(rgwConfig))
}
kmsEnabled, err := c.CheckRGWKMS()
if err != nil {
return v1.PodTemplateSpec{}, err
}
s3Enabled, err := c.CheckRGWSSES3Enabled()
if err != nil {
return v1.PodTemplateSpec{}, err
}
if kmsEnabled || s3Enabled {
v := v1.Volume{
Name: rgwVaultVolumeName,
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
}
podSpec.Volumes = append(podSpec.Volumes, v)
if kmsEnabled && c.store.Spec.Security.KeyManagementService.IsTokenAuthEnabled() {
vaultFileVol, _ := kms.VaultVolumeAndMountWithCustomName(c.store.Spec.Security.KeyManagementService.ConnectionDetails,
c.store.Spec.Security.KeyManagementService.TokenSecretName, sseKMS)
podSpec.Volumes = append(podSpec.Volumes, vaultFileVol)
}
if s3Enabled && c.store.Spec.Security.ServerSideEncryptionS3.IsTokenAuthEnabled() {
vaultFileVol, _ := kms.VaultVolumeAndMountWithCustomName(c.store.Spec.Security.ServerSideEncryptionS3.ConnectionDetails,
c.store.Spec.Security.ServerSideEncryptionS3.TokenSecretName, sseS3)
podSpec.Volumes = append(podSpec.Volumes, vaultFileVol)
}
podSpec.InitContainers = append(podSpec.InitContainers,
c.vaultTokenInitContainer(rgwConfig, kmsEnabled, s3Enabled))
}
c.store.Spec.Gateway.Placement.ApplyToPodSpec(&podSpec)
// If host networking is not enabled, preferred pod anti-affinity is added to the rgw daemons
labels := getLabels(c.store.Name, c.store.Namespace, false)
k8sutil.SetNodeAntiAffinityForPod(&podSpec, c.store.Spec.IsHostNetwork(c.clusterSpec), v1.LabelHostname, labels, nil)
podTemplateSpec := v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: rgwConfig.ResourceName,
Labels: getLabels(c.store.Name, c.store.Namespace, true),
},
Spec: podSpec,
}
c.store.Spec.Gateway.Annotations.ApplyToObjectMeta(&podTemplateSpec.ObjectMeta)
c.store.Spec.Gateway.Labels.ApplyToObjectMeta(&podTemplateSpec.ObjectMeta)
if hostNetwork {
podTemplateSpec.Spec.DNSPolicy = v1.DNSClusterFirstWithHostNet
} else if c.clusterSpec.Network.IsMultus() {
if err := k8sutil.ApplyMultus(c.clusterInfo.Namespace, &c.clusterSpec.Network, &podTemplateSpec.ObjectMeta); err != nil {
return podTemplateSpec, err
}
}
return podTemplateSpec, nil
}
func (c *clusterConfig) createCaBundleUpdateInitContainer(rgwConfig *rgwConfig) v1.Container {
caBundleMount := v1.VolumeMount{Name: caBundleVolumeName, MountPath: caBundleSourceCustomDir, ReadOnly: true}
volumeMounts := append(controller.DaemonVolumeMounts(c.DataPathMap, rgwConfig.ResourceName, c.clusterSpec.DataDirHostPath), caBundleMount)
updatedCaBundleDir := "/tmp/new-ca-bundle/"
updatedBundleMount := v1.VolumeMount{Name: caBundleUpdatedVolumeName, MountPath: updatedCaBundleDir, ReadOnly: false}
volumeMounts = append(volumeMounts, updatedBundleMount)
return v1.Container{
Name: "update-ca-bundle-initcontainer",
Command: []string{"/bin/bash", "-c"},
// copy all content of caBundleExtractedDir to avoid directory mount itself
Args: []string{
fmt.Sprintf("/usr/bin/update-ca-trust extract; cp -rf %s/* %s", caBundleExtractedDir, updatedCaBundleDir),
},
Image: c.clusterSpec.CephVersion.Image,
ImagePullPolicy: controller.GetContainerImagePullPolicy(c.clusterSpec.CephVersion.ImagePullPolicy),
VolumeMounts: volumeMounts,
Resources: c.store.Spec.Gateway.Resources,
SecurityContext: controller.PodSecurityContext(),
}
}
// The vault token is passed as Secret for rgw container. So it is mounted as read only.
// RGW has restrictions over vault token file, it should owned by same user (ceph) which
// rgw daemon runs and all other permission should be nil or zero. Here ownership can be
// changed with help of FSGroup but in openshift environments for security reasons it has
// predefined value, so it won't work there. Hence the token file and certs (if present)
// are copied to other volume from mounted secrets then ownership/permissions are changed
// accordingly with help of an init container.
func (c *clusterConfig) vaultTokenInitContainer(rgwConfig *rgwConfig, kmsEnabled, s3Enabled bool) v1.Container {
var vaultVolMounts []v1.VolumeMount
tmpVaultMount := v1.VolumeMount{Name: rgwVaultVolumeName, MountPath: rgwVaultDirName}
vaultVolMounts = append(vaultVolMounts, tmpVaultMount)
if kmsEnabled {
_, ssekmsVaultVolMount := kms.VaultVolumeAndMountWithCustomName(c.store.Spec.Security.KeyManagementService.ConnectionDetails, "", sseKMS)
vaultVolMounts = append(vaultVolMounts, ssekmsVaultVolMount)
}
if s3Enabled {
_, sses3VaultVolMount := kms.VaultVolumeAndMountWithCustomName(c.store.Spec.Security.ServerSideEncryptionS3.ConnectionDetails, "", sseS3)
vaultVolMounts = append(vaultVolMounts, sses3VaultVolMount)
}
return v1.Container{
Name: "vault-initcontainer-token-file-setup",
Command: []string{
"/bin/bash",
"-c",
fmt.Sprintf(setupVaultTokenFile,
kms.EtcVaultDir, rgwVaultDirName),
},
Image: c.clusterSpec.CephVersion.Image,
ImagePullPolicy: controller.GetContainerImagePullPolicy(c.clusterSpec.CephVersion.ImagePullPolicy),
VolumeMounts: append(
controller.DaemonVolumeMounts(c.DataPathMap, rgwConfig.ResourceName, c.clusterSpec.DataDirHostPath), vaultVolMounts...),
Resources: c.store.Spec.Gateway.Resources,
SecurityContext: controller.PodSecurityContext(),
}
}
func (c *clusterConfig) makeChownInitContainer(rgwConfig *rgwConfig) v1.Container {
return controller.ChownCephDataDirsInitContainer(
*c.DataPathMap,
c.clusterSpec.CephVersion.Image,
controller.GetContainerImagePullPolicy(c.clusterSpec.CephVersion.ImagePullPolicy),
controller.DaemonVolumeMounts(c.DataPathMap, rgwConfig.ResourceName, c.clusterSpec.DataDirHostPath),
c.store.Spec.Gateway.Resources,
controller.PodSecurityContext(),
"",
)
}
func (c *clusterConfig) makeDaemonContainer(rgwConfig *rgwConfig) (v1.Container, error) {
// start the rgw daemon in the foreground
startupProbe, err := c.defaultStartupProbe()
if err != nil {
return v1.Container{}, errors.Wrap(err, "failed to generate default startup probe")
}
readinessProbe, err := c.defaultReadinessProbe()
if err != nil {
return v1.Container{}, errors.Wrap(err, "failed to generate default readiness probe")
}
container := v1.Container{
Name: "rgw",
Image: c.clusterSpec.CephVersion.Image,
ImagePullPolicy: controller.GetContainerImagePullPolicy(c.clusterSpec.CephVersion.ImagePullPolicy),
Command: []string{
"radosgw",
},
Args: append(
controller.DaemonFlags(c.clusterInfo, c.clusterSpec,
strings.TrimPrefix(generateCephXUser(rgwConfig.ResourceName), "client.")),
"--foreground",
cephconfig.NewFlag("rgw frontends", fmt.Sprintf("%s %s", rgwFrontendName, c.portString())),
cephconfig.NewFlag("host", controller.ContainerEnvVarReference(k8sutil.PodNameEnvVar)),
cephconfig.NewFlag("rgw-mime-types-file", mimeTypesMountPath()),
cephconfig.NewFlag("rgw realm", rgwConfig.Realm),
cephconfig.NewFlag("rgw zonegroup", rgwConfig.ZoneGroup),
cephconfig.NewFlag("rgw zone", rgwConfig.Zone),
),
VolumeMounts: append(
controller.DaemonVolumeMounts(c.DataPathMap, rgwConfig.ResourceName, c.clusterSpec.DataDirHostPath),
c.mimeTypesVolumeMount(),
),
Env: controller.DaemonEnvVars(c.clusterSpec),
Resources: c.store.Spec.Gateway.Resources,
StartupProbe: startupProbe,
LivenessProbe: noLivenessProbe(),
ReadinessProbe: readinessProbe,
SecurityContext: controller.PodSecurityContext(),
WorkingDir: cephconfig.VarLogCephDir,
}
// If the startup probe is enabled
container = cephconfig.ConfigureStartupProbe(container, c.store.Spec.HealthCheck.StartupProbe)
// If the readiness probe is enabled
configureReadinessProbe(&container, c.store.Spec.HealthCheck)
if c.store.Spec.IsTLSEnabled() {
// Add a volume mount for the ssl certificate
mount := v1.VolumeMount{Name: certVolumeName, MountPath: certDir, ReadOnly: true}
container.VolumeMounts = append(container.VolumeMounts, mount)
}
if c.store.Spec.Gateway.CaBundleRef != "" {
updatedBundleMount := v1.VolumeMount{Name: caBundleUpdatedVolumeName, MountPath: caBundleExtractedDir, ReadOnly: true}
container.VolumeMounts = append(container.VolumeMounts, updatedBundleMount)
}
kmsEnabled, err := c.CheckRGWKMS()
if err != nil {
logger.Errorf("failed to enable SSE-KMS. %v", err)
return v1.Container{}, err
}
if kmsEnabled {
logger.Debugf("enabliing SSE-KMS. %v", c.store.Spec.Security.KeyManagementService)
container.Args = append(container.Args, c.sseKMSDefaultOptions(kmsEnabled)...)
if c.store.Spec.Security.KeyManagementService.IsTokenAuthEnabled() {
container.Args = append(container.Args, c.sseKMSVaultTokenOptions(kmsEnabled)...)
}
if c.store.Spec.Security.KeyManagementService.IsTLSEnabled() &&
c.clusterInfo.CephVersion.IsAtLeast(cephVersionMinRGWSSEKMSTLS) {
container.Args = append(container.Args, c.sseKMSVaultTLSOptions(kmsEnabled)...)
}
}
s3Enabled, err := c.CheckRGWSSES3Enabled()
if err != nil {
return v1.Container{}, err
}
if s3Enabled {
logger.Debugf("enabliing SSE-S3. %v", c.store.Spec.Security.ServerSideEncryptionS3)
container.Args = append(container.Args, c.sseS3DefaultOptions(s3Enabled)...)
if c.store.Spec.Security.ServerSideEncryptionS3.IsTokenAuthEnabled() {
container.Args = append(container.Args, c.sseS3VaultTokenOptions(s3Enabled)...)
}
if c.store.Spec.Security.ServerSideEncryptionS3.IsTLSEnabled() {
container.Args = append(container.Args, c.sseS3VaultTLSOptions(s3Enabled)...)
}
}
if s3Enabled || kmsEnabled {
vaultVolMount := v1.VolumeMount{Name: rgwVaultVolumeName, MountPath: rgwVaultDirName}
container.VolumeMounts = append(container.VolumeMounts, vaultVolMount)
}
hostingOptions, err := c.addDNSNamesToRGWServer()
if err != nil {
return v1.Container{}, err
}
if hostingOptions != "" {
container.Args = append(container.Args, hostingOptions)
}
return container, nil
}
// configureReadinessProbe returns the desired readiness probe for a given daemon
func configureReadinessProbe(container *v1.Container, healthCheck cephv1.ObjectHealthCheckSpec) {
if ok := healthCheck.ReadinessProbe; ok != nil {
if !healthCheck.ReadinessProbe.Disabled {
probe := healthCheck.ReadinessProbe.Probe
// If the spec value is empty, let's use a default
if probe != nil {
// Set the readiness probe on the container to overwrite the default probe created by Rook
container.ReadinessProbe = cephconfig.GetProbeWithDefaults(probe, container.ReadinessProbe)
}
} else {
container.ReadinessProbe = nil
}
}
}
// RGW has internal mechanisms for restarting its processing if it gets stuck. any failures detected
// in a liveness probe are likely to either (1) be resolved by the RGW internally or (2) be a result
// of connection issues to the Ceph cluster. in the first case, restarting is unnecessary. in the
// second case, restarting will only cause more load to the Ceph cluster by causing RGWs to attempt
// to re-connect, potentially causing more issues with the Ceph cluster. forcing a restart of the
// RGW is more likely to cause issues than solve them, so do not implement this probe.
func noLivenessProbe() *v1.Probe {
return nil
}
func (c *clusterConfig) defaultReadinessProbe() (*v1.Probe, error) {
proto, port := c.endpointInfo()
cfg := rgwProbeConfig{
ProbeType: ReadinessProbeType,
Protocol: proto,
Port: port.String(),
}
script, err := renderProbe(cfg)
if err != nil {
return nil, err
}
probe := &v1.Probe{
ProbeHandler: v1.ProbeHandler{
Exec: &v1.ExecAction{
Command: []string{
"bash", "-c", script,
},
},
},
TimeoutSeconds: 5,
InitialDelaySeconds: 10,
// if RGWs aren't responding reliably, remove them from service routing until they are stable
PeriodSeconds: 10,
FailureThreshold: 3,
SuccessThreshold: 3, // don't re-add too soon to "flappy" RGWs from being rout-able
}
return probe, nil
}
func (c *clusterConfig) defaultStartupProbe() (*v1.Probe, error) {
proto, port := c.endpointInfo()
cfg := rgwProbeConfig{
ProbeType: StartupProbeType,
Protocol: proto,
Port: port.String(),
}
script, err := renderProbe(cfg)
if err != nil {
return nil, err
}
probe := &v1.Probe{
ProbeHandler: v1.ProbeHandler{
Exec: &v1.ExecAction{
Command: []string{
"bash", "-c", script,
},
},
},
TimeoutSeconds: 5,
InitialDelaySeconds: 10,
// RGW's default init timeout is 300 seconds; give an extra margin before the pod should be
// restarted by kubernetes
PeriodSeconds: 10,
FailureThreshold: 33,
}
return probe, nil
}
func (c *clusterConfig) endpointInfo() (ProtocolType, *intstr.IntOrString) {
// The port the liveness probe needs to probe
// Assume we run on SDN by default
proto := HTTPProtocol
port := intstr.FromInt(int(rgwPortInternalPort))
// If Host Networking is enabled, the port from the spec must be reflected
if c.store.Spec.IsHostNetwork(c.clusterSpec) {
proto = HTTPProtocol
port = intstr.FromInt(int(c.store.Spec.Gateway.Port))
}
if c.store.Spec.Gateway.Port == 0 && c.store.Spec.IsTLSEnabled() {
proto = HTTPSProtocol
port = intstr.FromInt(int(c.store.Spec.Gateway.SecurePort))
}
logger.Debugf("rgw %q probe port is %v", c.store.Namespace+"/"+c.store.Name, port)
return proto, &port
}
func (c *clusterConfig) generateService(cephObjectStore *cephv1.CephObjectStore) *v1.Service {
svc := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: instanceName(cephObjectStore.Name),
Namespace: cephObjectStore.Namespace,
Labels: getLabels(cephObjectStore.Name, cephObjectStore.Namespace, true),
},
}
if c.store.Spec.Gateway.Service != nil {
c.store.Spec.Gateway.Service.Annotations.ApplyToObjectMeta(&svc.ObjectMeta)
}
if c.store.Spec.IsHostNetwork(c.clusterSpec) {
svc.Spec.ClusterIP = v1.ClusterIPNone
}
_, destPort := c.endpointInfo()
// When the cluster is external we must use the same one as the gateways are listening on
if cephObjectStore.Spec.IsExternal() {
destPort.IntVal = cephObjectStore.Spec.Gateway.Port
} else {
// If the cluster is not external we add the Selector
svc.Spec = v1.ServiceSpec{
Selector: getLabels(cephObjectStore.Name, cephObjectStore.Namespace, false),
}
}
addPort(svc, "http", cephObjectStore.Spec.Gateway.Port, destPort.IntVal)
addPort(svc, "https", cephObjectStore.Spec.Gateway.SecurePort, cephObjectStore.Spec.Gateway.SecurePort)
return svc
}
func (c *clusterConfig) generateEndpoint(cephObjectStore *cephv1.CephObjectStore) *v1.Endpoints {
labels := getLabels(cephObjectStore.Name, cephObjectStore.Namespace, true)
k8sEndpointAddrs := []v1.EndpointAddress{}
for _, rookEndpoint := range cephObjectStore.Spec.Gateway.ExternalRgwEndpoints {
k8sEndpointAddr := v1.EndpointAddress{
IP: rookEndpoint.IP,
Hostname: rookEndpoint.Hostname,
}
k8sEndpointAddrs = append(k8sEndpointAddrs, k8sEndpointAddr)
}
endpoints := &v1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: instanceName(cephObjectStore.Name),
Namespace: cephObjectStore.Namespace,
Labels: labels,
},
Subsets: []v1.EndpointSubset{
{
Addresses: k8sEndpointAddrs,
},
},
}
addPortToEndpoint(endpoints, "http", cephObjectStore.Spec.Gateway.Port)
addPortToEndpoint(endpoints, "https", cephObjectStore.Spec.Gateway.SecurePort)
return endpoints
}
func (c *clusterConfig) reconcileExternalEndpoint(cephObjectStore *cephv1.CephObjectStore) error {
logger.Info("reconciling external object store service")
endpoint := c.generateEndpoint(cephObjectStore)
// Set owner ref to the parent object
err := c.ownerInfo.SetControllerReference(endpoint)
if err != nil {
return errors.Wrapf(err, "failed to set owner reference to ceph object store endpoint %q", endpoint.Name)
}
_, err = k8sutil.CreateOrUpdateEndpoint(c.clusterInfo.Context, c.context.Clientset, cephObjectStore.Namespace, endpoint)
if err != nil {
return errors.Wrapf(err, "failed to create or update object store %q endpoint", cephObjectStore.Name)
}
return nil
}
func (c *clusterConfig) reconcileService(store *cephv1.CephObjectStore) error {
service := c.generateService(store)
// Set owner ref to the parent object
err := c.ownerInfo.SetControllerReference(service)
if err != nil {
return errors.Wrapf(err, "failed to set owner reference to ceph object store service %q", service.Name)
}
svc, err := k8sutil.CreateOrUpdateService(c.clusterInfo.Context, c.context.Clientset, store.Namespace, service)
if err != nil {
return errors.Wrapf(err, "failed to create or update object store %q service", store.Name)
}
logger.Infof("ceph object store gateway service running at %s", svc.Spec.ClusterIP)
return nil
}
func (c *clusterConfig) vaultPrefixRGW() string {
secretEngine := c.store.Spec.Security.KeyManagementService.ConnectionDetails[kms.VaultSecretEngineKey]
var vaultPrefixPath string
switch secretEngine {
case kms.VaultKVSecretEngineKey:
vaultPrefixPath = path.Join(vaultPrefix,
c.store.Spec.Security.KeyManagementService.ConnectionDetails[vault.VaultBackendPathKey], "/data")
case kms.VaultTransitSecretEngineKey:
vaultPrefixPath = path.Join(vaultPrefix, secretEngine)
}
return vaultPrefixPath
}
func (c *clusterConfig) CheckRGWKMS() (bool, error) {
if c.store.Spec.Security != nil && c.store.Spec.Security.KeyManagementService.IsEnabled() {
err := kms.ValidateConnectionDetails(c.clusterInfo.Context, c.context, &c.store.Spec.Security.KeyManagementService, c.store.Namespace)
if err != nil {
return false, err
}
secretEngine := c.store.Spec.Security.KeyManagementService.ConnectionDetails[kms.VaultSecretEngineKey]
// currently RGW supports kv(version 2) and transit secret engines in vault for sse:kms
switch secretEngine {
case kms.VaultKVSecretEngineKey:
kvVers := c.store.Spec.Security.KeyManagementService.ConnectionDetails[vault.VaultBackendKey]
if kvVers != "" {
if kvVers != "v2" {
return false, errors.New("failed to validate vault kv version, only v2 is supported")
}
} else {
// If VAUL_BACKEND is not specified let's assume it's v2
logger.Warningf("%s is not set, assuming the only supported version 2", vault.VaultBackendKey)
c.store.Spec.Security.KeyManagementService.ConnectionDetails[vault.VaultBackendKey] = "v2"
}
return true, nil
case kms.VaultTransitSecretEngineKey:
return true, nil
default:
return false, errors.New("failed to validate vault secret engine")
}
}
return false, nil
}
func (c *clusterConfig) CheckRGWSSES3Enabled() (bool, error) {
if c.store.Spec.Security != nil && c.store.Spec.Security.ServerSideEncryptionS3.IsEnabled() {
if !c.clusterInfo.CephVersion.IsAtLeast(cephVersionMinRGWSSES3) {
return false, errors.New("minimum ceph quincy is required for AWS-SSE:S3")
}
err := kms.ValidateConnectionDetails(c.clusterInfo.Context, c.context, &c.store.Spec.Security.ServerSideEncryptionS3, c.store.Namespace)
if err != nil {
return false, err
}
// currently RGW supports only transit secret engines in vault for sse:s3
if c.store.Spec.Security.ServerSideEncryptionS3.ConnectionDetails[kms.VaultSecretEngineKey] != kms.VaultTransitSecretEngineKey {
return false, errors.New("vault secret engine is not transit")
}
return true, nil
}
return false, nil
}
func addPort(service *v1.Service, name string, port, destPort int32) {
if port == 0 || destPort == 0 {
return
}
service.Spec.Ports = append(service.Spec.Ports, v1.ServicePort{
Name: name,
Port: port,
TargetPort: intstr.FromInt(int(destPort)),
Protocol: v1.ProtocolTCP,
})
}
func addPortToEndpoint(endpoints *v1.Endpoints, name string, port int32) {
if port == 0 {
return
}
endpoints.Subsets[0].Ports = append(endpoints.Subsets[0].Ports, v1.EndpointPort{
Name: name,
Port: port,
Protocol: v1.ProtocolTCP,
},
)
}
func getLabels(name, namespace string, includeNewLabels bool) map[string]string {
labels := controller.CephDaemonAppLabels(AppName, namespace, cephconfig.RgwType, name, name, "cephobjectstores.ceph.rook.io", includeNewLabels)
labels["rook_object_store"] = name
return labels
}
func (c *clusterConfig) generateVolumeSourceWithTLSSecret() (*v1.SecretVolumeSource, error) {
// Keep the TLS secret as secure as possible in the container. Give only user read perms.
// Because the Secret mount is owned by "root" and fsGroup breaks on OCP since we cannot predict it
// Also, we don't want to change the SCC for fsGroup to RunAsAny since it has a major broader impact
// Let's open the permissions a bit more so that everyone can read the cert.
userReadOnly := int32(0444)
var secretVolSrc *v1.SecretVolumeSource
if c.store.Spec.Gateway.SSLCertificateRef != "" {
secretVolSrc = &v1.SecretVolumeSource{
SecretName: c.store.Spec.Gateway.SSLCertificateRef,
}
secretType, err := c.rgwTLSSecretType(c.store.Spec.Gateway.SSLCertificateRef)
if err != nil {
return nil, err
}
switch secretType {
case v1.SecretTypeOpaque:
secretVolSrc.Items = []v1.KeyToPath{
{Key: certKeyName, Path: certFilename, Mode: &userReadOnly},
}
case v1.SecretTypeTLS:
secretVolSrc.Items = []v1.KeyToPath{
{Key: v1.TLSCertKey, Path: certFilename, Mode: &userReadOnly},
{Key: v1.TLSPrivateKeyKey, Path: certKeyFileName, Mode: &userReadOnly},
}
}
} else if c.store.Spec.GetServiceServingCert() != "" {
secretVolSrc = &v1.SecretVolumeSource{
SecretName: c.store.Spec.GetServiceServingCert(),
Items: []v1.KeyToPath{
{Key: v1.TLSCertKey, Path: certFilename, Mode: &userReadOnly},
{Key: v1.TLSPrivateKeyKey, Path: certKeyFileName, Mode: &userReadOnly},
}}
} else {
return nil, errors.New("no TLS certificates found")
}
return secretVolSrc, nil
}
func (c *clusterConfig) generateVolumeSourceWithCaBundleSecret() (*v1.SecretVolumeSource, error) {
// Keep the ca-bundle as secure as possible in the container. Give only user read perms.
// Same as above for generateVolumeSourceWithTLSSecret function.
userReadOnly := int32(0400)
caBundleVolSrc := &v1.SecretVolumeSource{
SecretName: c.store.Spec.Gateway.CaBundleRef,
}
secretType, err := c.rgwTLSSecretType(c.store.Spec.Gateway.CaBundleRef)
if err != nil {
return nil, err
}
if secretType != v1.SecretTypeOpaque {
return nil, errors.New("CaBundle secret should be 'Opaque' type")
}
caBundleVolSrc.Items = []v1.KeyToPath{
{Key: caBundleKeyName, Path: caBundleFileName, Mode: &userReadOnly},
}
return caBundleVolSrc, nil
}
func (c *clusterConfig) rgwTLSSecretType(secretName string) (v1.SecretType, error) {
rgwTlsSecret, err := c.context.Clientset.CoreV1().Secrets(c.clusterInfo.Namespace).Get(c.clusterInfo.Context, secretName, metav1.GetOptions{})
if rgwTlsSecret != nil {
return rgwTlsSecret.Type, nil
}
return "", errors.Wrapf(err, "no Kubernetes secrets referring TLS certificates found")
}
func getDaemonName(rgwConfig *rgwConfig) string {
return fmt.Sprintf("ceph-%s", generateCephXUser(rgwConfig.ResourceName))
}
// Following apis set the RGW options if requested, since they are used in unit tests for validating different scenarios
func (c *clusterConfig) sseKMSDefaultOptions(setOptions bool) []string {
if setOptions {
return []string{
cephconfig.NewFlag("rgw crypt s3 kms backend",
c.store.Spec.Security.KeyManagementService.ConnectionDetails[kms.Provider]),
cephconfig.NewFlag("rgw crypt vault addr",
c.store.Spec.Security.KeyManagementService.ConnectionDetails[api.EnvVaultAddress]),
}
}
return []string{}
}
func (c *clusterConfig) sseS3DefaultOptions(setOptions bool) []string {
if setOptions {
return []string{
cephconfig.NewFlag("rgw crypt sse s3 backend",
c.store.Spec.Security.ServerSideEncryptionS3.ConnectionDetails[kms.Provider]),
cephconfig.NewFlag("rgw crypt sse s3 vault addr",
c.store.Spec.Security.ServerSideEncryptionS3.ConnectionDetails[api.EnvVaultAddress]),
}
}
return []string{}
}
func (c *clusterConfig) sseKMSVaultTokenOptions(setOptions bool) []string {
if setOptions {
return []string{
cephconfig.NewFlag("rgw crypt vault auth", kms.KMSTokenSecretNameKey),
cephconfig.NewFlag("rgw crypt vault token file",
path.Join(rgwVaultDirName, sseKMS, kms.VaultFileName)),
cephconfig.NewFlag("rgw crypt vault prefix", c.vaultPrefixRGW()),
cephconfig.NewFlag("rgw crypt vault secret engine",
c.store.Spec.Security.KeyManagementService.ConnectionDetails[kms.VaultSecretEngineKey]),
}
}
return []string{}
}
func (c *clusterConfig) sseS3VaultTokenOptions(setOptions bool) []string {
if setOptions {
return []string{
cephconfig.NewFlag("rgw crypt sse s3 vault auth", kms.KMSTokenSecretNameKey),
cephconfig.NewFlag("rgw crypt sse s3 vault token file",
path.Join(rgwVaultDirName, sseS3, kms.VaultFileName)),
cephconfig.NewFlag("rgw crypt sse s3 vault prefix",
path.Join(vaultPrefix, c.store.Spec.Security.ServerSideEncryptionS3.ConnectionDetails[kms.VaultSecretEngineKey])),
cephconfig.NewFlag("rgw crypt sse s3 vault secret engine",
c.store.Spec.Security.ServerSideEncryptionS3.ConnectionDetails[kms.VaultSecretEngineKey]),
}
}
return []string{}
}
func (c *clusterConfig) sseKMSVaultTLSOptions(setOptions bool) []string {
var rgwOptions []string
if setOptions {
rgwOptions = append(rgwOptions, cephconfig.NewFlag("rgw crypt vault verify ssl", "true"))
if kms.GetParam(c.store.Spec.Security.KeyManagementService.ConnectionDetails, api.EnvVaultClientCert) != "" {
rgwOptions = append(rgwOptions,
cephconfig.NewFlag("rgw crypt vault ssl clientcert", path.Join(rgwVaultDirName, sseKMS, kms.VaultCertFileName)))
}
if kms.GetParam(c.store.Spec.Security.KeyManagementService.ConnectionDetails, api.EnvVaultClientKey) != "" {
rgwOptions = append(rgwOptions,
cephconfig.NewFlag("rgw crypt vault ssl clientkey", path.Join(rgwVaultDirName, sseKMS, kms.VaultKeyFileName)))
}
if kms.GetParam(c.store.Spec.Security.KeyManagementService.ConnectionDetails, api.EnvVaultCACert) != "" {
rgwOptions = append(rgwOptions,
cephconfig.NewFlag("rgw crypt vault ssl cacert", path.Join(rgwVaultDirName, sseKMS, kms.VaultCAFileName)))
}
}
return rgwOptions
}
func (c *clusterConfig) sseS3VaultTLSOptions(setOptions bool) []string {
var rgwOptions []string
if setOptions {
rgwOptions = append(rgwOptions, cephconfig.NewFlag("rgw crypt sse s3 vault verify ssl", "true"))
if kms.GetParam(c.store.Spec.Security.ServerSideEncryptionS3.ConnectionDetails, api.EnvVaultClientCert) != "" {
rgwOptions = append(rgwOptions,
cephconfig.NewFlag("rgw crypt sse s3 vault ssl clientcert", path.Join(rgwVaultDirName, sseS3, kms.VaultCertFileName)))
}
if kms.GetParam(c.store.Spec.Security.ServerSideEncryptionS3.ConnectionDetails, api.EnvVaultClientKey) != "" {
rgwOptions = append(rgwOptions,
cephconfig.NewFlag("rgw crypt sse s3 vault ssl clientkey", path.Join(rgwVaultDirName, sseS3, kms.VaultKeyFileName)))
}
if kms.GetParam(c.store.Spec.Security.ServerSideEncryptionS3.ConnectionDetails, api.EnvVaultCACert) != "" {
rgwOptions = append(rgwOptions,
cephconfig.NewFlag("rgw crypt sse s3 vault ssl cacert", path.Join(rgwVaultDirName, sseS3, kms.VaultCAFileName)))
}
}
return rgwOptions
}
func renderProbe(cfg rgwProbeConfig) (string, error) {
var writer bytes.Buffer
name := string(cfg.ProbeType) + "-probe"
t := template.New(name)
t, err := t.Parse(rgwProbeScriptTemplate)
if err != nil {
return "", errors.Wrapf(err, "failed to parse template %q", name)
}
if err := t.Execute(&writer, cfg); err != nil {
return "", errors.Wrapf(err, "failed to render template %q", name)
}
return writer.String(), nil
}
func (c *clusterConfig) addDNSNamesToRGWServer() (string, error) {
if (c.store.Spec.Hosting == nil) || len(c.store.Spec.Hosting.DNSNames) <= 0 {
return "", nil
}
if !c.clusterInfo.CephVersion.IsAtLeastReef() {
return "", errors.New("rgw dns names are supported from ceph v18 onwards")
}
// add default RGW service name to dns names
dnsNames := c.store.Spec.Hosting.DNSNames
dnsNames = append(dnsNames, domainNameOfService(c.store))
// add custom endpoints from zone spec if exists
if c.store.Spec.Zone.Name != "" {
zone, err := c.context.RookClientset.CephV1().CephObjectZones(c.store.Namespace).Get(c.clusterInfo.Context, c.store.Spec.Zone.Name, metav1.GetOptions{})
if err != nil {
return "", err
}
dnsNames = append(dnsNames, zone.Spec.CustomEndpoints...)
}
// validate dns names
var hostNames []string
for _, dnsName := range dnsNames {
hostName, err := GetHostnameFromEndpoint(dnsName)
if err != nil {
return "", errors.Wrap(err,
"failed to interpret endpoint from one of the following sources: CephObjectStore.spec.hosting.dnsNames, CephObjectZone.spec.customEndpoints")
}
hostNames = append(hostNames, hostName)
}
// remove duplicate host names
checkDuplicate := make(map[string]bool)
removeDuplicateHostNames := []string{}
for _, hostName := range hostNames {
if _, ok := checkDuplicate[hostName]; !ok {
checkDuplicate[hostName] = true
removeDuplicateHostNames = append(removeDuplicateHostNames, hostName)
}
}
return cephconfig.NewFlag("rgw dns name", strings.Join(removeDuplicateHostNames, ",")), nil
}
func GetHostnameFromEndpoint(endpoint string) (string, error) {
if len(endpoint) == 0 {
return "", fmt.Errorf("endpoint cannot be empty string")
}
// if endpoint doesn't end in '/', Ceph adds it
// Rook can do this also to get more accurate error results from this function
if !strings.HasSuffix(endpoint, "/") {
endpoint = endpoint + "/"
}
// url.Parse() requires a protocol to parse the host name correctly
if !strings.Contains(endpoint, "://") {
endpoint = "http://" + endpoint
}
// "net/url".Parse() assumes that a URL is a "path" with optional things surrounding it.
// For Ceph RGWs, we assume an endpoint is a "hostname" with optional things surrounding it.
// Because of this difference in fundamental assumption, Rook needs to adjust some endpoints
// used as input to url.Parse() to allow the function to extract a hostname reliably. Also,
// Rook needs to look at several parts of the url.Parse() output to identify more failure scenarios
parsedURL, err := url.Parse(endpoint)
if err != nil {
return "", err
}
// error in this case: url.Parse("https://http://hostname") parses without error with `Host = "http:"`
// also catches issue where user adds colon but no port number after
if strings.HasSuffix(parsedURL.Host, ":") {
return "", fmt.Errorf("host %q parsed from endpoint %q has invalid colon (:) placement", parsedURL.Host, endpoint)