Skip to content

Commit

Permalink
added support for ocs provider server to fetch noobaa resources
Browse files Browse the repository at this point in the history
Signed-off-by: Kaustav Majumder <[email protected]>
  • Loading branch information
Kaustav Majumder committed Jul 18, 2024
1 parent d8d1f6f commit 9b30587
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions controllers/storagecluster/storageclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (s *storageClient) ensureCreated(r *StorageClusterReconciler, storagecluste
// the controller of storageclient is running in same namespace and should be able to resolve the endpoint
// via servicename:serviceport irrespective of clusterip/nodeport/lb
storageClient.Spec.StorageProviderEndpoint = fmt.Sprintf("%s:%d", ocsProviderServerName, ocsProviderServicePort)
util.AddAnnotation(storageClient, "is-local-client", "true")
return nil
})
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions services/provider/server/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"sync"

nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
ifaces "github.com/red-hat-storage/ocs-operator/v4/services/provider/interfaces"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -225,3 +226,21 @@ func (c *ocsConsumerManager) UpdateConsumerStatus(ctx context.Context, id string
klog.Infof("successfully updated Status for StorageConsumer %v", consumerObj.Name)
return nil
}

func (c *ocsConsumerManager) CreateNoobaaAccount(ctx context.Context, id string) error {

consumerObj, err := c.Get(ctx, id)
if err != nil {
return err
}
nbAccountObj := &nbv1.NooBaaAccount{}
nbAccountObj.Name = consumerObj.Name
nbAccountObj.Namespace = consumerObj.Namespace
nbAccountObj.Spec.Role = "operator"

err = c.client.Create(ctx, nbAccountObj)
if err != nil {
return fmt.Errorf("failed to create noobaa account for storageConsumer %v: %v", consumerObj.Name, err)
}
return nil
}
50 changes: 46 additions & 4 deletions services/provider/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import (
"time"

"github.com/blang/semver/v4"
routev1 "github.com/openshift/api/route/v1"
opv1a1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
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"
pb "github.com/red-hat-storage/ocs-operator/v4/services/provider/pb"
ocsVersion "github.com/red-hat-storage/ocs-operator/v4/version"
rookCephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"

opv1a1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/red-hat-storage/ocs-operator/v4/services"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -141,7 +141,10 @@ func (s *OCSProviderServer) AcknowledgeOnboarding(ctx context.Context, req *pb.A
}
return nil, status.Errorf(codes.Internal, "Failed to update the storageConsumer. %v", err)
}

// create noobaa account CR
if err := s.consumerManager.CreateNoobaaAccount(ctx, req.StorageConsumerUUID); err != nil {
return nil, status.Errorf(codes.Internal, "Failed to create noobaa account for storageconsumer. %v", err)
}
return &pb.AcknowledgeOnboardingResponse{}, nil
}

Expand Down Expand Up @@ -183,6 +186,7 @@ func (s *OCSProviderServer) GetStorageConfig(ctx context.Context, req *pb.Storag
func (s *OCSProviderServer) OffboardConsumer(ctx context.Context, req *pb.OffboardConsumerRequest) (*pb.OffboardConsumerResponse, error) {

err := s.consumerManager.Delete(ctx, req.StorageConsumerUUID)
// noobaa client resources here
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to delete storageConsumer resource with the provided UUID. %v", err)
}
Expand Down Expand Up @@ -233,6 +237,10 @@ func newClient() (client.Client, error) {
if err != nil {
return nil, fmt.Errorf("failed to add operatorsv1alpha1 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 {
Expand Down Expand Up @@ -367,6 +375,40 @@ func (s *OCSProviderServer) getExternalResources(ctx context.Context, consumerRe
}),
})

// Fetch noobaa remote secret and management address and append to extResources
noobaaOperatorSecret := &v1.Secret{}
noobaaOperatorSecretName := consumerResource.Name
err = s.client.Get(ctx, types.NamespacedName{Name: noobaaOperatorSecretName, Namespace: s.namespace}, noobaaOperatorSecret)
if err != nil {
return nil, fmt.Errorf("failed to get %s secret. %v", noobaaOperatorSecretName, err)
}

authToken, ok := noobaaOperatorSecret.Data["auth_token"]
if !ok || len(authToken) == 0 {
return nil, fmt.Errorf("auth_token not found in %s secret", noobaaOperatorSecretName)
}

noobaMgmtRoute := &routev1.Route{}
err = s.client.Get(ctx, types.NamespacedName{Name: "noobaa-mgmt", Namespace: s.namespace}, noobaMgmtRoute)
if 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: consumerResource.Name,
Kind: "Secret",
Data: mustMarshal((map[string]string{
"auth_token": string(authToken),
"mgmt_addr": noobaaMgmtAddress,
})),
})
return extR, nil
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9b30587

Please sign in to comment.