-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement server side rpc call for peering
Signed-off-by: Rewant Soni <[email protected]>
- Loading branch information
1 parent
502f1dd
commit 5385c9e
Showing
3 changed files
with
137 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1" | ||
"github.com/red-hat-storage/ocs-operator/v4/services" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
type storageClusterPeerManager struct { | ||
client client.Client | ||
namespace string | ||
} | ||
|
||
func newStorageClusterPeerManager(cl client.Client, namespace string) (*storageClusterPeerManager, error) { | ||
return &storageClusterPeerManager{ | ||
client: cl, | ||
namespace: namespace, | ||
}, nil | ||
} | ||
|
||
func (s *storageClusterPeerManager) GetByStorageClusterUID(ctx context.Context, storageClusterUID types.UID) (*ocsv1.StorageClusterPeer, error) { | ||
storageClusterPeerList := &ocsv1.StorageClusterPeerList{} | ||
err := s.client.List(ctx, storageClusterPeerList, client.InNamespace(s.namespace)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for i := range storageClusterPeerList.Items { | ||
storageClusterUIDInTicket, err := readStorageClusterUID(storageClusterPeerList.Items[i].Spec.OnboardingToken) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if storageClusterUIDInTicket == storageClusterUID { | ||
return &storageClusterPeerList.Items[i], nil | ||
} | ||
} | ||
return nil, fmt.Errorf("StorageClusterPeer linked to StorageCluster with uid %q not found", storageClusterUID) | ||
} | ||
|
||
func readStorageClusterUID(ticket string) (types.UID, error) { | ||
ticketArr := strings.Split(string(ticket), ".") | ||
if len(ticketArr) != 2 { | ||
return "", fmt.Errorf("invalid ticket") | ||
} | ||
|
||
message, err := base64.StdEncoding.DecodeString(ticketArr[0]) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to decode onboarding ticket message: %v", err) | ||
} | ||
|
||
var ticketData services.OnboardingTicket | ||
err = json.Unmarshal(message, &ticketData) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to unmarshal onboarding ticket message. %v", err) | ||
} | ||
|
||
return ticketData.StorageCluster, nil | ||
} |