-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathserver.go
951 lines (829 loc) · 32.9 KB
/
server.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
package server
import (
"context"
"crypto"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"fmt"
"math"
"net"
"slices"
"strconv"
"strings"
"time"
"k8s.io/utils/ptr"
"github.com/blang/semver/v4"
nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
quotav1 "github.com/openshift/api/quota/v1"
routev1 "github.com/openshift/api/route/v1"
opv1a1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
pb "github.com/red-hat-storage/ocs-operator/services/provider/api/v4"
controllers "github.com/red-hat-storage/ocs-operator/v4/controllers/storageconsumer"
"github.com/red-hat-storage/ocs-operator/v4/controllers/util"
"github.com/red-hat-storage/ocs-operator/v4/services"
ocsVersion "github.com/red-hat-storage/ocs-operator/v4/version"
rookCephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
csiopv1a1 "github.com/ceph/ceph-csi-operator/api/v1alpha1"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
klog "k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
const (
TicketAnnotation = "ocs.openshift.io/provider-onboarding-ticket"
ProviderCertsMountPoint = "/mnt/cert"
onboardingTicketKeySecret = "onboarding-ticket-key"
storageRequestNameLabel = "ocs.openshift.io/storagerequest-name"
notAvailable = "N/A"
)
const (
monConfigMap = "rook-ceph-mon-endpoints"
monSecret = "rook-ceph-mon"
)
type OCSProviderServer struct {
pb.UnimplementedOCSProviderServer
client client.Client
consumerManager *ocsConsumerManager
storageRequestManager *storageRequestManager
namespace string
}
func NewOCSProviderServer(ctx context.Context, namespace string) (*OCSProviderServer, error) {
client, err := newClient()
if err != nil {
return nil, fmt.Errorf("failed to create new client. %v", err)
}
consumerManager, err := newConsumerManager(ctx, client, namespace)
if err != nil {
return nil, fmt.Errorf("failed to create new OCSConumer instance. %v", err)
}
storageRequestManager, err := newStorageRequestManager(client, namespace)
if err != nil {
return nil, fmt.Errorf("failed to create new StorageRequest instance. %v", err)
}
return &OCSProviderServer{
client: client,
consumerManager: consumerManager,
storageRequestManager: storageRequestManager,
namespace: namespace,
}, nil
}
// OnboardConsumer RPC call to onboard a new OCS consumer cluster.
func (s *OCSProviderServer) OnboardConsumer(ctx context.Context, req *pb.OnboardConsumerRequest) (*pb.OnboardConsumerResponse, error) {
version, err := semver.FinalizeVersion(req.ClientOperatorVersion)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "malformed ClientOperatorVersion for client %q is provided. %v", req.ConsumerName, err)
}
serverVersion, _ := semver.Make(ocsVersion.Version)
clientVersion, _ := semver.Make(version)
if serverVersion.Major != clientVersion.Major || serverVersion.Minor != clientVersion.Minor {
return nil, status.Errorf(codes.FailedPrecondition, "both server and client %q operators major and minor versions should match for onboarding process", req.ConsumerName)
}
pubKey, err := s.getOnboardingValidationKey(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get public key to validate onboarding ticket for consumer %q. %v", req.ConsumerName, err)
}
onboardingTicket, err := decodeAndValidateTicket(req.OnboardingTicket, pubKey)
if err != nil {
klog.Errorf("failed to validate onboarding ticket for consumer %q. %v", req.ConsumerName, err)
return nil, status.Errorf(codes.InvalidArgument, "onboarding ticket is not valid. %v", err)
}
storageQuotaInGiB := ptr.Deref(onboardingTicket.StorageQuotaInGiB, 0)
if onboardingTicket.SubjectRole != services.ClientRole {
err := fmt.Errorf("unsupported ticket role for consumer %q, found %s, expected %s", req.ConsumerName, onboardingTicket.SubjectRole, services.ClientRole)
klog.Error(err)
return nil, status.Error(codes.InvalidArgument, err.Error())
}
storageConsumerUUID, err := s.consumerManager.Create(ctx, req, int(storageQuotaInGiB))
if err != nil {
if !kerrors.IsAlreadyExists(err) && err != errTicketAlreadyExists {
return nil, status.Errorf(codes.Internal, "failed to create storageConsumer %q. %v", req.ConsumerName, err)
}
storageConsumer, err := s.consumerManager.GetByName(ctx, req.ConsumerName)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to get storageConsumer. %v", err)
}
if storageConsumer.Spec.Enable {
err = fmt.Errorf("storageconsumers.ocs.openshift.io %s already exists", req.ConsumerName)
return nil, status.Errorf(codes.AlreadyExists, "failed to create storageConsumer %q. %v", req.ConsumerName, err)
}
storageConsumerUUID = string(storageConsumer.UID)
}
return &pb.OnboardConsumerResponse{StorageConsumerUUID: storageConsumerUUID}, nil
}
// AcknowledgeOnboarding acknowledge the onboarding is complete
func (s *OCSProviderServer) AcknowledgeOnboarding(ctx context.Context, req *pb.AcknowledgeOnboardingRequest) (*pb.AcknowledgeOnboardingResponse, error) {
if err := s.consumerManager.EnableStorageConsumer(ctx, req.StorageConsumerUUID); err != nil {
if kerrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "storageConsumer not found. %v", err)
}
return nil, status.Errorf(codes.Internal, "Failed to update the storageConsumer. %v", err)
}
return &pb.AcknowledgeOnboardingResponse{}, nil
}
// GetStorageConfig RPC call to onboard a new OCS consumer cluster.
func (s *OCSProviderServer) GetStorageConfig(ctx context.Context, req *pb.StorageConfigRequest) (*pb.StorageConfigResponse, error) {
// Get storage consumer resource using UUID
consumerObj, err := s.consumerManager.Get(ctx, req.StorageConsumerUUID)
if err != nil {
return nil, err
}
klog.Infof("Found storageConsumer for GetStorageConfig")
// Verify Status
switch consumerObj.Status.State {
case ocsv1alpha1.StorageConsumerStateDisabled:
return nil, status.Errorf(codes.FailedPrecondition, "storageConsumer is in disabled state")
case ocsv1alpha1.StorageConsumerStateFailed:
// TODO: get correct error message from the storageConsumer status
return nil, status.Errorf(codes.Internal, "storageConsumer status failed")
case ocsv1alpha1.StorageConsumerStateConfiguring:
return nil, status.Errorf(codes.Unavailable, "waiting for the rook resources to be provisioned")
case ocsv1alpha1.StorageConsumerStateDeleting:
return nil, status.Errorf(codes.NotFound, "storageConsumer is already in deleting phase")
case ocsv1alpha1.StorageConsumerStateReady:
conString, err := s.getExternalResources(ctx, consumerObj)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get external resources. %v", err)
}
channelName, err := s.getOCSSubscriptionChannel(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to construct status response: %v", err)
}
storageCluster, err := s.getStorageCluster(ctx)
if err != nil {
return nil, err
}
desiredClientConfigHash := getDesiredClientConfigHash(
channelName,
consumerObj,
isEncryptionInTransitEnabled(storageCluster.Spec.Network),
)
klog.Infof("successfully returned the config details to the consumer.")
return &pb.StorageConfigResponse{
ExternalResource: conString,
DesiredConfigHash: desiredClientConfigHash,
}, nil
}
return nil, status.Errorf(codes.Unavailable, "storage consumer status is not set")
}
// OffboardConsumer RPC call to delete the StorageConsumer CR
func (s *OCSProviderServer) OffboardConsumer(ctx context.Context, req *pb.OffboardConsumerRequest) (*pb.OffboardConsumerResponse, error) {
err := s.consumerManager.Delete(ctx, req.StorageConsumerUUID)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to delete storageConsumer resource with the provided UUID. %v", err)
}
return &pb.OffboardConsumerResponse{}, nil
}
func (s *OCSProviderServer) Start(port int, opts []grpc.ServerOption) {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
klog.Fatalf("failed to listen: %v", err)
}
certFile := ProviderCertsMountPoint + "/tls.crt"
keyFile := ProviderCertsMountPoint + "/tls.key"
creds, sslErr := credentials.NewServerTLSFromFile(certFile, keyFile)
if sslErr != nil {
klog.Fatalf("Failed loading certificates: %v", sslErr)
return
}
opts = append(opts, grpc.Creds(creds))
grpcServer := grpc.NewServer(opts...)
pb.RegisterOCSProviderServer(grpcServer, s)
// Register reflection service on gRPC server.
reflection.Register(grpcServer)
err = grpcServer.Serve(lis)
if err != nil {
klog.Fatalf("failed to start gRPC server: %v", err)
}
}
func newClient() (client.Client, error) {
scheme := runtime.NewScheme()
err := ocsv1alpha1.AddToScheme(scheme)
if err != nil {
return nil, fmt.Errorf("failed to add ocsv1alpha1 to scheme. %v", err)
}
err = corev1.AddToScheme(scheme)
if err != nil {
return nil, fmt.Errorf("failed to add ocsv1alpha1 to scheme. %v", err)
}
err = rookCephv1.AddToScheme(scheme)
if err != nil {
return nil, fmt.Errorf("failed to add rookCephv1 to scheme. %v", err)
}
err = opv1a1.AddToScheme(scheme)
if err != nil {
return nil, fmt.Errorf("failed to add operatorsv1alpha1 to scheme. %v", err)
}
err = ocsv1.AddToScheme(scheme)
if err != nil {
return nil, fmt.Errorf("failed to add ocsv1 to scheme. %v", err)
}
err = routev1.AddToScheme(scheme)
if err != nil {
return nil, fmt.Errorf("failed to add routev1 to scheme. %v", err)
}
config, err := config.GetConfig()
if err != nil {
klog.Error(err, "failed to get rest.config")
return nil, err
}
client, err := client.New(config, client.Options{Scheme: scheme})
if err != nil {
klog.Error(err, "failed to create controller-runtime client")
return nil, err
}
return client, nil
}
func (s *OCSProviderServer) getExternalResources(ctx context.Context, consumerResource *ocsv1alpha1.StorageConsumer) ([]*pb.ExternalResource, error) {
var extR []*pb.ExternalResource
// Configmap with mon endpoints
configmap := &v1.ConfigMap{}
err := s.client.Get(ctx, types.NamespacedName{Name: monConfigMap, Namespace: s.namespace}, configmap)
if err != nil {
return nil, fmt.Errorf("failed to get %s configMap. %v", monConfigMap, err)
}
if configmap.Data["data"] == "" {
return nil, fmt.Errorf("configmap %s data is empty", monConfigMap)
}
extR = append(extR, &pb.ExternalResource{
Name: monConfigMap,
Kind: "ConfigMap",
Data: mustMarshal(map[string]string{
"data": configmap.Data["data"], // IP Address of all mon's
"maxMonId": "0",
"mapping": "{}",
})})
monIps, err := extractMonitorIps(configmap.Data["data"])
if err != nil {
return nil, fmt.Errorf("failed to extract monitor IPs from configmap %s: %v", monConfigMap, err)
}
extR = append(extR, &pb.ExternalResource{
Kind: "CephConnection",
Name: "monitor-endpoints",
Data: mustMarshal(&csiopv1a1.CephConnectionSpec{Monitors: monIps}),
})
scMon := &v1.Secret{}
// Secret storing cluster mon.admin key, fsid and name
err = s.client.Get(ctx, types.NamespacedName{Name: monSecret, Namespace: s.namespace}, scMon)
if err != nil {
return nil, fmt.Errorf("failed to get %s secret. %v", monSecret, err)
}
fsid := string(scMon.Data["fsid"])
if fsid == "" {
return nil, fmt.Errorf("secret %s data fsid is empty", monSecret)
}
// Get mgr pod hostIP
podList := &corev1.PodList{}
listOpts := []client.ListOption{
client.InNamespace(s.namespace),
client.MatchingLabels(map[string]string{"app": "rook-ceph-mgr"}),
}
err = s.client.List(ctx, podList, listOpts...)
if err != nil {
return nil, fmt.Errorf("failed to list pod with rook-ceph-mgr label. %v", err)
}
if len(podList.Items) == 0 {
return nil, fmt.Errorf("no pods available with rook-ceph-mgr label")
}
mgrPod := &podList.Items[0]
var port int32 = -1
for i := range mgrPod.Spec.Containers {
container := &mgrPod.Spec.Containers[i]
if container.Name == "mgr" {
for j := range container.Ports {
if container.Ports[j].Name == "http-metrics" {
port = container.Ports[j].ContainerPort
}
}
}
}
if port < 0 {
return nil, fmt.Errorf("mgr pod port is empty")
}
extR = append(extR, &pb.ExternalResource{
Name: "monitoring-endpoint",
Kind: "CephCluster",
Data: mustMarshal(map[string]string{
"MonitoringEndpoint": mgrPod.Status.HostIP,
"MonitoringPort": strconv.Itoa(int(port)),
})})
if consumerResource.Spec.StorageQuotaInGiB > 0 {
clusterResourceQuotaSpec := "av1.ClusterResourceQuotaSpec{
Selector: quotav1.ClusterResourceQuotaSelector{
LabelSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: string(consumerResource.UID),
Operator: metav1.LabelSelectorOpDoesNotExist,
},
},
},
},
Quota: corev1.ResourceQuotaSpec{
Hard: corev1.ResourceList{"requests.storage": *resource.NewScaledQuantity(
int64(consumerResource.Spec.StorageQuotaInGiB),
resource.Giga,
)},
},
}
extR = append(extR, &pb.ExternalResource{
Name: "QuotaForConsumer",
Kind: "ClusterResourceQuota",
Data: mustMarshal(clusterResourceQuotaSpec),
})
}
// Fetch noobaa remote secret and management address and append to extResources
consumerName := consumerResource.Name
noobaaOperatorSecret := &v1.Secret{}
noobaaOperatorSecret.Name = fmt.Sprintf("noobaa-account-%s", consumerName)
noobaaOperatorSecret.Namespace = s.namespace
if err := s.client.Get(ctx, client.ObjectKeyFromObject(noobaaOperatorSecret), noobaaOperatorSecret); err != nil {
if kerrors.IsNotFound(err) {
// ignoring because it is a provider cluster and the noobaa secret does not exist
return extR, nil
}
return nil, fmt.Errorf("failed to get %s secret. %v", noobaaOperatorSecret.Name, err)
}
authToken, ok := noobaaOperatorSecret.Data["auth_token"]
if !ok || len(authToken) == 0 {
return nil, fmt.Errorf("auth_token not found in %s secret", noobaaOperatorSecret.Name)
}
noobaMgmtRoute := &routev1.Route{}
noobaMgmtRoute.Name = "noobaa-mgmt"
noobaMgmtRoute.Namespace = s.namespace
if err = s.client.Get(ctx, client.ObjectKeyFromObject(noobaMgmtRoute), noobaMgmtRoute); err != nil {
return nil, fmt.Errorf("failed to get noobaa-mgmt route. %v", err)
}
if noobaMgmtRoute.Status.Ingress == nil || len(noobaMgmtRoute.Status.Ingress) == 0 {
return nil, fmt.Errorf("no Ingress available in noobaa-mgmt route")
}
noobaaMgmtAddress := noobaMgmtRoute.Status.Ingress[0].Host
if noobaaMgmtAddress == "" {
return nil, fmt.Errorf("no Host found in noobaa-mgmt route Ingress")
}
extR = append(extR, &pb.ExternalResource{
Name: "noobaa-remote-join-secret",
Kind: "Secret",
Data: mustMarshal(map[string]string{
"auth_token": string(authToken),
"mgmt_addr": noobaaMgmtAddress,
}),
})
extR = append(extR, &pb.ExternalResource{
Name: "noobaa-remote",
Kind: "Noobaa",
Data: mustMarshal(&nbv1.NooBaaSpec{
JoinSecret: &v1.SecretReference{
Name: "noobaa-remote-join-secret",
},
}),
})
return extR, nil
}
func (s *OCSProviderServer) getCephClientInformation(ctx context.Context, name string) (string, string, error) {
cephClient := &rookCephv1.CephClient{}
err := s.client.Get(ctx, types.NamespacedName{Name: name, Namespace: s.namespace}, cephClient)
if err != nil {
return "", "", fmt.Errorf("failed to get rook ceph client %s secret. %v", name, err)
}
if cephClient.Status == nil {
return "", "", fmt.Errorf("rook ceph client %s status is nil", name)
}
if cephClient.Status.Info == nil {
return "", "", fmt.Errorf("rook ceph client %s Status.Info is empty", name)
}
if len(cephClient.Annotations) == 0 {
return "", "", fmt.Errorf("rook ceph client %s annotation is empty", name)
}
if cephClient.Annotations[controllers.StorageRequestAnnotation] == "" || cephClient.Annotations[controllers.StorageCephUserTypeAnnotation] == "" {
klog.Warningf("rook ceph client %s has missing storage annotations", name)
}
return cephClient.Status.Info["secretName"], cephClient.Annotations[controllers.StorageCephUserTypeAnnotation], nil
}
func (s *OCSProviderServer) getOnboardingValidationKey(ctx context.Context) (*rsa.PublicKey, error) {
pubKeySecret := &corev1.Secret{}
err := s.client.Get(ctx, types.NamespacedName{Name: onboardingTicketKeySecret, Namespace: s.namespace}, pubKeySecret)
if err != nil {
return nil, fmt.Errorf("failed to get public key secret %q", onboardingTicketKeySecret)
}
pubKeyBytes := pubKeySecret.Data["key"]
if len(pubKeyBytes) == 0 {
return nil, fmt.Errorf("public key is not found inside the secret %q", onboardingTicketKeySecret)
}
block, _ := pem.Decode(pubKeyBytes)
if block == nil {
return nil, fmt.Errorf("invalid PEM block")
}
publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse public key. %v", err)
}
return publicKey, nil
}
func mustMarshal[T any](value T) []byte {
newData, err := json.Marshal(value)
if err != nil {
panic("failed to marshal")
}
return newData
}
func getSubVolumeGroupClusterID(subVolumeGroup *rookCephv1.CephFilesystemSubVolumeGroup) string {
str := fmt.Sprintf(
"%s-%s-file-%s",
subVolumeGroup.Namespace,
subVolumeGroup.Spec.FilesystemName,
subVolumeGroup.Name,
)
hash := sha256.Sum256([]byte(str))
return hex.EncodeToString(hash[:16])
}
func decodeAndValidateTicket(ticket string, pubKey *rsa.PublicKey) (*services.OnboardingTicket, error) {
ticketArr := strings.Split(string(ticket), ".")
if len(ticketArr) != 2 {
return nil, fmt.Errorf("invalid ticket")
}
message, err := base64.StdEncoding.DecodeString(ticketArr[0])
if err != nil {
return nil, fmt.Errorf("failed to decode onboarding ticket: %v", err)
}
var ticketData services.OnboardingTicket
err = json.Unmarshal(message, &ticketData)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal onboarding ticket message. %v", err)
}
switch ticketData.SubjectRole {
case services.ClientRole:
if ticketData.StorageQuotaInGiB != nil {
quota := *ticketData.StorageQuotaInGiB
if quota > math.MaxInt {
return nil, fmt.Errorf("invalid value sent in onboarding ticket, storage quota should be greater than 0 and less than %v: %v", math.MaxInt, quota)
}
}
case services.PeerRole:
default:
return nil, fmt.Errorf("invalid onboarding ticket subject role")
}
signature, err := base64.StdEncoding.DecodeString(ticketArr[1])
if err != nil {
return nil, fmt.Errorf("failed to decode onboarding ticket %s signature: %v", ticketData.ID, err)
}
hash := sha256.Sum256(message)
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, hash[:], signature)
if err != nil {
return nil, fmt.Errorf("failed to verify onboarding ticket signature. %v", err)
}
if ticketData.ExpirationDate < time.Now().Unix() {
return nil, fmt.Errorf("onboarding ticket %s is expired", ticketData.ID)
}
klog.Infof("onboarding ticket %s has been verified successfully", ticketData.ID)
return &ticketData, nil
}
// FulfillStorageClaim RPC call to create the StorageClaim CR on
// provider cluster.
func (s *OCSProviderServer) FulfillStorageClaim(ctx context.Context, req *pb.FulfillStorageClaimRequest) (*pb.FulfillStorageClaimResponse, error) {
// Get storage consumer resource using UUID
consumerObj, err := s.consumerManager.Get(ctx, req.StorageConsumerUUID)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
klog.Infof("Found StorageConsumer %q (%q)", consumerObj.Name, req.StorageConsumerUUID)
var storageType string
switch req.StorageType {
case pb.FulfillStorageClaimRequest_BLOCK:
storageType = "block"
case pb.FulfillStorageClaimRequest_SHAREDFILE:
storageType = "sharedfile"
default:
return nil, status.Errorf(codes.InvalidArgument, "encountered an unknown storage type, %s", storageType)
}
err = s.storageRequestManager.Create(ctx, consumerObj, req.StorageClaimName, storageType, req.EncryptionMethod, req.StorageProfile)
if err != nil {
errMsg := fmt.Sprintf("failed to fulfill storage class claim for %q. %v", req.StorageConsumerUUID, err)
klog.Error(errMsg)
if kerrors.IsAlreadyExists(err) {
return nil, status.Error(codes.AlreadyExists, errMsg)
}
return nil, status.Error(codes.Internal, errMsg)
}
return &pb.FulfillStorageClaimResponse{}, nil
}
// RevokeStorageClaim RPC call to delete the StorageClaim CR on
// provider cluster.
func (s *OCSProviderServer) RevokeStorageClaim(ctx context.Context, req *pb.RevokeStorageClaimRequest) (*pb.RevokeStorageClaimResponse, error) {
err := s.storageRequestManager.Delete(ctx, req.StorageConsumerUUID, req.StorageClaimName)
if err != nil {
errMsg := fmt.Sprintf("failed to revoke storage class claim %q for %q. %v", req.StorageClaimName, req.StorageConsumerUUID, err)
klog.Error(errMsg)
return nil, status.Errorf(codes.Internal, errMsg)
}
return &pb.RevokeStorageClaimResponse{}, nil
}
func storageClaimCephCsiSecretName(secretType, suffix string) string {
return fmt.Sprintf("ceph-client-%s-%s", secretType, suffix)
}
// GetStorageClaim RPC call to get the ceph resources for the StorageClaim.
func (s *OCSProviderServer) GetStorageClaimConfig(ctx context.Context, req *pb.StorageClaimConfigRequest) (*pb.StorageClaimConfigResponse, error) {
storageRequest, err := s.storageRequestManager.Get(ctx, req.StorageConsumerUUID, req.StorageClaimName)
if err != nil {
errMsg := fmt.Sprintf("failed to get storage class claim config %q for %q. %v", req.StorageClaimName, req.StorageConsumerUUID, err)
if kerrors.IsNotFound(err) {
return nil, status.Error(codes.NotFound, errMsg)
}
return nil, status.Error(codes.Internal, errMsg)
}
// Verify Status.Phase
msg := fmt.Sprintf("storage claim %q for %q is in %q phase", req.StorageClaimName, req.StorageConsumerUUID, storageRequest.Status.Phase)
klog.Info(msg)
if storageRequest.Status.Phase != ocsv1alpha1.StorageRequestReady {
switch storageRequest.Status.Phase {
case ocsv1alpha1.StorageRequestFailed:
return nil, status.Error(codes.Internal, msg)
case ocsv1alpha1.StorageRequestInitializing:
return nil, status.Error(codes.Unavailable, msg)
case ocsv1alpha1.StorageRequestCreating:
return nil, status.Error(codes.Unavailable, msg)
case "":
return nil, status.Errorf(codes.Unavailable, "status is not set for storage class claim %q for %q", req.StorageClaimName, req.StorageConsumerUUID)
default:
return nil, status.Error(codes.Internal, msg)
}
}
var extR []*pb.ExternalResource
storageRequestHash := getStorageRequestHash(req.StorageConsumerUUID, req.StorageClaimName)
for _, cephRes := range storageRequest.Status.CephResources {
switch cephRes.Kind {
case "CephClient":
clientSecretName, clientUserType, err := s.getCephClientInformation(ctx, cephRes.Name)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
extSecretName := storageClaimCephCsiSecretName(clientUserType, storageRequestHash)
cephUserSecret := &v1.Secret{}
err = s.client.Get(ctx, types.NamespacedName{Name: clientSecretName, Namespace: s.namespace}, cephUserSecret)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get %s secret. %v", clientSecretName, err)
}
idProp := "userID"
keyProp := "userKey"
if storageRequest.Spec.Type == "sharedfile" {
idProp = "adminID"
keyProp = "adminKey"
}
extR = append(extR, &pb.ExternalResource{
Name: extSecretName,
Kind: "Secret",
Data: mustMarshal(map[string]string{
idProp: cephRes.Name,
keyProp: string(cephUserSecret.Data[cephRes.Name]),
}),
})
case "CephBlockPoolRadosNamespace":
rns := &rookCephv1.CephBlockPoolRadosNamespace{}
err = s.client.Get(ctx, types.NamespacedName{Name: cephRes.Name, Namespace: s.namespace}, rns)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get %s CephBlockPoolRadosNamespace. %v", cephRes.Name, err)
}
provisionerSecretName := storageClaimCephCsiSecretName("provisioner", storageRequestHash)
nodeSecretName := storageClaimCephCsiSecretName("node", storageRequestHash)
rbdStorageClassData := map[string]string{
"clusterID": s.namespace,
"radosnamespace": cephRes.Name,
"pool": rns.Spec.BlockPoolName,
"imageFeatures": "layering,deep-flatten,exclusive-lock,object-map,fast-diff",
"csi.storage.k8s.io/fstype": "ext4",
"imageFormat": "2",
"csi.storage.k8s.io/provisioner-secret-name": provisionerSecretName,
"csi.storage.k8s.io/node-stage-secret-name": nodeSecretName,
"csi.storage.k8s.io/controller-expand-secret-name": provisionerSecretName,
}
if storageRequest.Spec.EncryptionMethod != "" {
rbdStorageClassData["encrypted"] = "true"
rbdStorageClassData["encryptionKMSID"] = storageRequest.Spec.EncryptionMethod
}
extR = append(extR,
&pb.ExternalResource{
Name: "ceph-rbd",
Kind: "StorageClass",
Data: mustMarshal(rbdStorageClassData),
},
&pb.ExternalResource{
Name: "ceph-rbd",
Kind: "VolumeSnapshotClass",
Data: mustMarshal(map[string]string{
"csi.storage.k8s.io/snapshotter-secret-name": provisionerSecretName,
})},
&pb.ExternalResource{
Name: "ceph-rbd",
Kind: "VolumeGroupSnapshotClass",
Data: mustMarshal(map[string]string{
"csi.storage.k8s.io/group-snapshotter-secret-name": provisionerSecretName,
})},
&pb.ExternalResource{
Kind: "ClientProfile",
Name: "ceph-rbd",
Data: mustMarshal(&csiopv1a1.ClientProfileSpec{
Rbd: &csiopv1a1.RbdConfigSpec{
RadosNamespace: cephRes.Name,
},
})},
)
case "CephFilesystemSubVolumeGroup":
subVolumeGroup := &rookCephv1.CephFilesystemSubVolumeGroup{}
err := s.client.Get(ctx, types.NamespacedName{Name: cephRes.Name, Namespace: s.namespace}, subVolumeGroup)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get %s cephFilesystemSubVolumeGroup. %v", cephRes.Name, err)
}
provisionerSecretName := storageClaimCephCsiSecretName("provisioner", storageRequestHash)
nodeSecretName := storageClaimCephCsiSecretName("node", storageRequestHash)
cephfsStorageClassData := map[string]string{
"clusterID": getSubVolumeGroupClusterID(subVolumeGroup),
"subvolumegroupname": subVolumeGroup.Name,
"fsName": subVolumeGroup.Spec.FilesystemName,
"pool": subVolumeGroup.GetLabels()[ocsv1alpha1.CephFileSystemDataPoolLabel],
"csi.storage.k8s.io/provisioner-secret-name": provisionerSecretName,
"csi.storage.k8s.io/node-stage-secret-name": nodeSecretName,
"csi.storage.k8s.io/controller-expand-secret-name": provisionerSecretName,
}
storageCluster, err := s.getStorageCluster(ctx)
if err != nil {
return nil, err
}
var kernelMountOptions map[string]string
for _, option := range strings.Split(util.GetCephFSKernelMountOptions(storageCluster), ",") {
if kernelMountOptions == nil {
kernelMountOptions = map[string]string{}
}
parts := strings.Split(option, "=")
kernelMountOptions[parts[0]] = parts[1]
}
extR = append(extR,
&pb.ExternalResource{
Name: "cephfs",
Kind: "StorageClass",
Data: mustMarshal(cephfsStorageClassData),
},
&pb.ExternalResource{
Name: cephRes.Name,
Kind: cephRes.Kind,
Data: mustMarshal(map[string]string{
"filesystemName": subVolumeGroup.Spec.FilesystemName,
})},
&pb.ExternalResource{
Name: "cephfs",
Kind: "VolumeSnapshotClass",
Data: mustMarshal(map[string]string{
"csi.storage.k8s.io/snapshotter-secret-name": provisionerSecretName,
})},
&pb.ExternalResource{
Name: "cephfs",
Kind: "VolumeGroupSnapshotClass",
Data: mustMarshal(map[string]string{
"csi.storage.k8s.io/group-snapshotter-secret-name": provisionerSecretName,
})},
&pb.ExternalResource{
Kind: "ClientProfile",
Name: "cephfs",
Data: mustMarshal(&csiopv1a1.ClientProfileSpec{
CephFs: &csiopv1a1.CephFsConfigSpec{
SubVolumeGroup: cephRes.Name,
KernelMountOptions: kernelMountOptions,
},
})},
)
}
}
klog.Infof("successfully returned the storage class claim %q for %q", req.StorageClaimName, req.StorageConsumerUUID)
return &pb.StorageClaimConfigResponse{ExternalResource: extR}, nil
}
// ReportStatus rpc call to check if a consumer can reach to the provider.
func (s *OCSProviderServer) ReportStatus(ctx context.Context, req *pb.ReportStatusRequest) (*pb.ReportStatusResponse, error) {
// Update the status in storageConsumer CR
klog.Infof("Client status report received: %+v", req)
if req.ClientOperatorVersion == "" {
req.ClientOperatorVersion = notAvailable
} else {
if _, err := semver.Parse(req.ClientOperatorVersion); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Malformed ClientOperatorVersion: %v", err)
}
}
if req.ClientPlatformVersion == "" {
req.ClientPlatformVersion = notAvailable
} else {
if _, err := semver.Parse(req.ClientPlatformVersion); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Malformed ClientPlatformVersion: %v", err)
}
}
if err := s.consumerManager.UpdateConsumerStatus(ctx, req.StorageConsumerUUID, req); err != nil {
if kerrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "Failed to update lastHeartbeat payload in the storageConsumer resource: %v", err)
}
return nil, status.Errorf(codes.Internal, "Failed to update lastHeartbeat payload in the storageConsumer resource: %v", err)
}
storageConsumer, err := s.consumerManager.Get(ctx, req.StorageConsumerUUID)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to get storageConsumer resource: %v", err)
}
channelName, err := s.getOCSSubscriptionChannel(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to construct status response: %v", err)
}
storageCluster, err := s.getStorageCluster(ctx)
if err != nil {
return nil, err
}
desiredClientConfigHash := getDesiredClientConfigHash(
channelName,
storageConsumer,
isEncryptionInTransitEnabled(storageCluster.Spec.Network),
)
return &pb.ReportStatusResponse{
DesiredClientOperatorChannel: channelName,
DesiredConfigHash: desiredClientConfigHash,
}, nil
}
func getDesiredClientConfigHash(channelName string, storageConsumer *ocsv1alpha1.StorageConsumer, encryptionInTransit bool) string {
var arr = []any{
channelName,
storageConsumer.Spec.StorageQuotaInGiB,
encryptionInTransit,
}
return util.CalculateMD5Hash(arr)
}
func (s *OCSProviderServer) getOCSSubscriptionChannel(ctx context.Context) (string, error) {
subscriptionList := &opv1a1.SubscriptionList{}
err := s.client.List(ctx, subscriptionList, client.InNamespace(s.namespace))
if err != nil {
return "", err
}
subscription := util.Find(subscriptionList.Items, func(sub *opv1a1.Subscription) bool {
return sub.Spec.Package == "ocs-operator"
})
if subscription == nil {
return "", fmt.Errorf("unable to find ocs-operator subscription")
}
return subscription.Spec.Channel, nil
}
func (s *OCSProviderServer) getStorageCluster(ctx context.Context) (*ocsv1.StorageCluster, error) {
scList := &ocsv1.StorageClusterList{}
if err := s.client.List(ctx, scList, client.InNamespace(s.namespace)); err != nil {
return nil, status.Errorf(codes.Internal, "failed to list storage clusters: %v", err)
}
var foundSc *ocsv1.StorageCluster
for i := range scList.Items {
sc := &scList.Items[i]
if sc.Status.Phase == util.PhaseIgnored {
continue // Skip Ignored storage cluster
}
if sc.Spec.AllowRemoteStorageConsumers {
if foundSc != nil {
// This means we have already found one storage cluster, so this is a second one
return nil, status.Errorf(codes.FailedPrecondition, "multiple provider storage clusters found")
}
foundSc = sc
}
}
if foundSc == nil {
return nil, status.Errorf(codes.NotFound, "no provider storage cluster found")
}
return foundSc, nil
}
func isEncryptionInTransitEnabled(networkSpec *rookCephv1.NetworkSpec) bool {
return networkSpec != nil &&
networkSpec.Connections != nil &&
networkSpec.Connections.Encryption != nil &&
networkSpec.Connections.Encryption.Enabled
}
func extractMonitorIps(data string) ([]string, error) {
var ips []string
mons := strings.Split(data, ",")
for _, mon := range mons {
parts := strings.Split(mon, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid mon ip format: %s", mon)
}
ips = append(ips, parts[1])
}
// sorting here removes any positional change which reduces spurious reconciles
slices.Sort(ips)
return ips, nil
}
func (s *OCSProviderServer) PeerStorageCluster(_ context.Context, _ *pb.PeerStorageClusterRequest) (*pb.PeerStorageClusterResponse, error) {
return &pb.PeerStorageClusterResponse{}, nil
}