From 7ef127b816f2dcdc042c4e700fdd798aa479521c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Mon, 26 Jul 2021 16:13:10 +0200 Subject: [PATCH] ceph: append additional info in the rbd-mirror bootstrap peer token MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the title looks familiar this is normal, this piece of code was removed during https://github.com/rook/rook/pull/7604. Probably due to a rebase. So I'm re-adding the code. We know append additional information to the rbd-mirror bootstrap peer token. It is useful for disaster recovery scenario where the other cluster is reading the peer token and needs to know the pool_id as well as the namespace. Signed-off-by: Sébastien Han --- pkg/operator/ceph/controller/mirror_peer.go | 41 +++++++++++++++++++ .../ceph/controller/mirror_peer_test.go | 27 ++++++++++++ 2 files changed, 68 insertions(+) diff --git a/pkg/operator/ceph/controller/mirror_peer.go b/pkg/operator/ceph/controller/mirror_peer.go index 163bab20b..4b62e0d2b 100644 --- a/pkg/operator/ceph/controller/mirror_peer.go +++ b/pkg/operator/ceph/controller/mirror_peer.go @@ -73,6 +73,13 @@ func CreateBootstrapPeerSecret(ctx *clusterd.Context, clusterInfo *cephclient.Cl if err != nil { return ImmediateRetryResult, errors.Wrapf(err, "failed to create %s-mirror bootstrap peer", daemonType) } + + // Add additional information to the peer token + boostrapToken, err = expandBootstrapPeerToken(ctx, clusterInfo, name, boostrapToken) + if err != nil { + return ImmediateRetryResult, errors.Wrap(err, "failed to add extra information to rbd-mirror bootstrap peer") + } + case *cephv1.CephFilesystem: ns = objectType.Namespace name = objectType.Name @@ -179,3 +186,37 @@ func ValidatePeerToken(object client.Object, data map[string][]byte) error { return nil } + +func expandBootstrapPeerToken(ctx *clusterd.Context, clusterInfo *cephclient.ClusterInfo, poolName string, token []byte) ([]byte, error) { + // First decode the token, it's base64 encoded + decodedToken, err := base64.StdEncoding.DecodeString(string(token)) + if err != nil { + return nil, errors.Wrap(err, "failed to decode bootstrap peer token") + } + + // Unmarshal the decoded value to a Go type + var decodedTokenToGo cephclient.PeerToken + err = json.Unmarshal(decodedToken, &decodedTokenToGo) + if err != nil { + return nil, errors.Wrap(err, "failed to unmarshal decoded token") + } + + // Fetch the pool ID + poolDetails, err := cephclient.GetPoolDetails(ctx, clusterInfo, poolName) + if err != nil { + return nil, errors.Wrapf(err, "failed to get pool %q details", poolName) + } + + // Add extra details to the token + decodedTokenToGo.PoolID = poolDetails.Number + decodedTokenToGo.Namespace = clusterInfo.Namespace + + // Marshal the Go type back to JSON + decodedTokenBackToJSON, err := json.Marshal(decodedTokenToGo) + if err != nil { + return nil, errors.Wrap(err, "failed to encode go type back to json") + } + + // Return the base64 encoded token + return []byte(base64.StdEncoding.EncodeToString(decodedTokenBackToJSON)), nil +} diff --git a/pkg/operator/ceph/controller/mirror_peer_test.go b/pkg/operator/ceph/controller/mirror_peer_test.go index fe4bb47fc..138257502 100644 --- a/pkg/operator/ceph/controller/mirror_peer_test.go +++ b/pkg/operator/ceph/controller/mirror_peer_test.go @@ -18,10 +18,15 @@ limitations under the License. package controller import ( + "encoding/base64" "reflect" "testing" + "github.com/pkg/errors" cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1" + "github.com/rook/rook/pkg/clusterd" + cephclient "github.com/rook/rook/pkg/daemon/ceph/client" + exectest "github.com/rook/rook/pkg/util/exec/test" "github.com/stretchr/testify/assert" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -73,3 +78,25 @@ func TestGenerateStatusInfo(t *testing.T) { }) } } + +func TestExpandBootstrapPeerToken(t *testing.T) { + executor := &exectest.MockExecutor{ + MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) { + if reflect.DeepEqual(args[0:5], []string{"osd", "pool", "get", "pool", "all"}) { + return `{"pool_id":13}`, nil + } + + return "", errors.Errorf("unknown command args: %s", args[0:5]) + }, + } + c := &clusterd.Context{ + Executor: executor, + } + + newToken, err := expandBootstrapPeerToken(c, cephclient.AdminClusterInfo("mu-cluster"), "pool", []byte(`eyJmc2lkIjoiYzZiMDg3ZjItNzgyOS00ZGJiLWJjZmMtNTNkYzM0ZTBiMzVkIiwiY2xpZW50X2lkIjoicmJkLW1pcnJvci1wZWVyIiwia2V5IjoiQVFBV1lsWmZVQ1Q2RGhBQVBtVnAwbGtubDA5YVZWS3lyRVV1NEE9PSIsIm1vbl9ob3N0IjoiW3YyOjE5Mi4xNjguMTExLjEwOjMzMDAsdjE6MTkyLjE2OC4xMTEuMTA6Njc4OV0sW3YyOjE5Mi4xNjguMTExLjEyOjMzMDAsdjE6MTkyLjE2OC4xMTEuMTI6Njc4OV0sW3YyOjE5Mi4xNjguMTExLjExOjMzMDAsdjE6MTkyLjE2OC4xMTEuMTE6Njc4OV0ifQ==`)) + assert.NoError(t, err) + newTokenDecoded, err := base64.StdEncoding.DecodeString(string(newToken)) + assert.NoError(t, err) + assert.Contains(t, string(newTokenDecoded), "pool_id") + assert.Contains(t, string(newTokenDecoded), "namespace") +}