Skip to content

Commit

Permalink
Merge pull request #14079 from BlaineEXE/load-cluster-owner-info-in-L…
Browse files Browse the repository at this point in the history
…oadClusterInfo

operator: load cluster owner info in LoadClusterInfo
  • Loading branch information
travisn committed Apr 16, 2024
2 parents 61332ca + bd9447e commit a9fded2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/operator/ceph/controller/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ func CreateOrLoadClusterInfo(clusterdContext *clusterd.Context, context context.
} else {
return nil, maxMonID, monMapping, errors.New("failed to find either the cluster admin key or the username")
}
if len(secrets.OwnerReferences) > 0 {
clusterInfo.OwnerInfo = k8sutil.NewOwnerInfoWithOwnerRef(&secrets.GetOwnerReferences()[0], namespace)
}
logger.Debugf("found existing monitor secrets for cluster %s", clusterInfo.Namespace)
}

Expand Down
31 changes: 31 additions & 0 deletions pkg/operator/ceph/controller/cluster_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
exectest "github.com/rook/rook/pkg/util/exec/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -74,6 +75,36 @@ func TestCreateClusterSecrets(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, adminSecret, string(secret.Data["ceph-secret"]))

// ensure secret owner info can be loaded and is useful
// this is what the owner info looks like in a live cluster
ownerController := true
blockOwnerDel := true
secret.OwnerReferences[0] = metav1.OwnerReference{
APIVersion: "ceph.rook.io/v1",
Kind: "CephCluster",
Name: "my-cluster",
UID: "e55604f2-710c-4353-9a3e-9d23ea2d6eb9", // random uuid
Controller: &ownerController,
BlockOwnerDeletion: &blockOwnerDel,
}
_, err = clientset.CoreV1().Secrets(namespace).Update(ctx, secret, metav1.UpdateOptions{})
assert.NoError(t, err)
info, _, _, err = CreateOrLoadClusterInfo(context, ctx, namespace, ownerInfo, cephClusterSpec)
assert.NoError(t, err)
// use the SetOwnerReference() method to ensure that the loaded OwnerInfo is usable and correct
cm := corev1.ConfigMap{}
cm.Name = "bob"
cm.Namespace = namespace
err = info.OwnerInfo.SetOwnerReference(&cm)
assert.NoError(t, err)
cmOwner := cm.OwnerReferences[0]
assert.Equal(t, "ceph.rook.io/v1", cmOwner.APIVersion)
assert.Equal(t, "CephCluster", cmOwner.Kind)
assert.Equal(t, "my-cluster", cmOwner.Name)
assert.Equal(t, "e55604f2-710c-4353-9a3e-9d23ea2d6eb9", string(cmOwner.UID))
assert.True(t, *cmOwner.Controller)
assert.True(t, *cmOwner.BlockOwnerDeletion)

// For backward compatibility check that the admin secret can be loaded as previously specified
// Update the secret as if created in an old cluster
delete(secret.Data, CephUserSecretKey)
Expand Down

0 comments on commit a9fded2

Please sign in to comment.