Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operator: load cluster owner info in LoadClusterInfo #14079

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems safe to assume there is always an ownerref on this secret, like you said we have had it there for a long time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I think it should be safe because of this code above:

	secrets, err := clusterdContext.Clientset.CoreV1().Secrets(namespace).Get(context, AppName, metav1.GetOptions{})
	if err != nil {
		if !kerrors.IsNotFound(err) {
			return nil, maxMonID, monMapping, errors.Wrap(err, "failed to get mon secrets")
		}
		if ownerInfo == nil {
			return nil, maxMonID, monMapping, ClusterInfoNoClusterNoSecret
		}

When a CephCluster is first created and the Secret is initially created and populated (by initClusterInfo()/PopulateExternalClusterInfo()), the secret shouldn't get created unless there is a CephCluster given to act as the owner.

}
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