mgr: update services when active mgr changes

When the active mgr changes, there may be user-created services
that also need to have their selector updated to point to the
new active mgr. Now the operator will update any service in the
rook operator namespace that has the label app=rook-ceph-mgr
to point to the active mgr.

Signed-off-by: Travis Nielsen <tnielsen@redhat.com>
(cherry picked from commit 11127f54ca)
This commit is contained in:
Travis Nielsen authored and mergify-bot committed 2022-01-13 19:21:43 +00:00
1 parent da8699f789
commit be72954b70
3 files changed
+97 -10

No files matched your search

+4 -1
View File
@@ -196,7 +196,10 @@ If this value is empty, each pod will get an ephemeral directory to store their
* `mon`: contains mon related options [mon settings](#mon-settings)
For more details on the mons and when to choose a number other than `3`, see the [mon health doc](ceph-mon-health.md).
* `mgr`: manager top level section
* `count`: set number of ceph managers between `1` to `2`. The default value is 1. This is only needed if plural ceph managers are needed.
* `count`: set number of ceph managers between `1` to `2`. The default value is 1. This is only needed if two ceph managers are needed.
If there are two managers, it is important for all mgr services point to the active mgr and not the passive mgr. Therefore, Rook will
automatically update all services (in the cluster namespace) that have a label `app=rook-ceph-mgr` with a selector pointing to the
active mgr. This commonly applies to services for the dashboard or the prometheus metrics collector.
* `modules`: is the list of Ceph manager modules to enable
* `crashCollector`: The settings for crash collector daemon(s).
* `disable`: is set to `true`, the crash collector will not run on any node where a Ceph daemon runs
+26
View File
@@ -312,6 +312,32 @@ func (c *Cluster) reconcileServices(activeDaemon string) error {
}
}
return c.updateServiceSelectors(activeDaemon)
}
// Make a best effort to update the services that have been labeled for being updated
// when the mgr has changed. They might be services for node ports, ingress, etc
func (c *Cluster) updateServiceSelectors(activeDaemon string) error {
selector := metav1.ListOptions{LabelSelector: "app=rook-ceph-mgr"}
services, err := c.context.Clientset.CoreV1().Services(c.clusterInfo.Namespace).List(c.clusterInfo.Context, selector)
if err != nil {
return errors.Wrap(err, "failed to query mgr services to update")
}
for i, service := range services.Items {
// Update the selector on the service to point to the active mgr
if service.Spec.Selector[controller.DaemonIDLabel] == activeDaemon {
logger.Infof("no need to update service %q", service.Name)
continue
}
// Update the service to point to the new active mgr
service.Spec.Selector[controller.DaemonIDLabel] = activeDaemon
logger.Infof("updating selector on mgr service %q to active mgr %q", service.Name, activeDaemon)
if _, err := c.context.Clientset.CoreV1().Services(c.clusterInfo.Namespace).Update(c.clusterInfo.Context, &services.Items[i], metav1.UpdateOptions{}); err != nil {
logger.Errorf("failed to update service %q. %v", service.Name, err)
} else {
logger.Infof("service %q successfully updated to active mgr %q", service.Name, activeDaemon)
}
}
return nil
}
+67 -9
View File
@@ -30,6 +30,7 @@ import (
"github.com/rook/rook/pkg/client/clientset/versioned/scheme"
"github.com/rook/rook/pkg/clusterd"
cephclient "github.com/rook/rook/pkg/daemon/ceph/client"
"github.com/rook/rook/pkg/operator/ceph/controller"
cephver "github.com/rook/rook/pkg/operator/ceph/version"
testopk8s "github.com/rook/rook/pkg/operator/k8sutil/test"
testop "github.com/rook/rook/pkg/operator/test"
@@ -37,6 +38,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
apps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
@@ -94,7 +96,7 @@ func TestStartMgr(t *testing.T) {
// start a basic service
err = c.Start()
assert.Nil(t, err)
assert.NoError(t, err)
validateStart(t, c)
assert.ElementsMatch(t, []string{}, testopk8s.DeploymentNamesUpdated(deploymentsUpdated))
testopk8s.ClearDeploymentsUpdated(deploymentsUpdated)
@@ -102,7 +104,7 @@ func TestStartMgr(t *testing.T) {
c.spec.Dashboard.URLPrefix = "/test"
c.spec.Dashboard.Port = 12345
err = c.Start()
assert.Nil(t, err)
assert.NoError(t, err)
validateStart(t, c)
assert.ElementsMatch(t, []string{"rook-ceph-mgr-a"}, testopk8s.DeploymentNamesUpdated(deploymentsUpdated))
testopk8s.ClearDeploymentsUpdated(deploymentsUpdated)
@@ -112,19 +114,19 @@ func TestStartMgr(t *testing.T) {
c.spec.Dashboard.Enabled = false
// delete the previous mgr since the mocked test won't update the existing one
err = c.context.Clientset.AppsV1().Deployments(c.clusterInfo.Namespace).Delete(context.TODO(), "rook-ceph-mgr-a", metav1.DeleteOptions{})
assert.Nil(t, err)
assert.NoError(t, err)
err = c.Start()
assert.Nil(t, err)
assert.NoError(t, err)
validateStart(t, c)
c.spec.Mgr.Count = 1
c.spec.Dashboard.Enabled = false
// clean the previous deployments
err = c.context.Clientset.AppsV1().Deployments(c.clusterInfo.Namespace).Delete(context.TODO(), "rook-ceph-mgr-a", metav1.DeleteOptions{})
assert.Nil(t, err)
assert.Nil(t, err)
assert.NoError(t, err)
assert.NoError(t, err)
err = c.Start()
assert.Nil(t, err)
assert.NoError(t, err)
validateStart(t, c)
}
@@ -134,7 +136,7 @@ func validateStart(t *testing.T, c *Cluster) {
logger.Infof("Looking for cephmgr replica %d", i)
daemonName := mgrNames[i]
d, err := c.context.Clientset.AppsV1().Deployments(c.clusterInfo.Namespace).Get(context.TODO(), fmt.Sprintf("rook-ceph-mgr-%s", daemonName), metav1.GetOptions{})
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, map[string]string{"my": "annotation"}, d.Spec.Template.Annotations)
assert.Contains(t, d.Spec.Template.Labels, "my-label-key")
assert.Equal(t, "my-priority-class", d.Spec.Template.Spec.PriorityClassName)
@@ -159,7 +161,7 @@ func validateStart(t *testing.T, c *Cluster) {
func validateServices(t *testing.T, c *Cluster) {
_, err := c.context.Clientset.CoreV1().Services(c.clusterInfo.Namespace).Get(context.TODO(), "rook-ceph-mgr", metav1.GetOptions{})
assert.Nil(t, err)
assert.NoError(t, err)
ds, err := c.context.Clientset.CoreV1().Services(c.clusterInfo.Namespace).Get(context.TODO(), "rook-ceph-mgr-dashboard", metav1.GetOptions{})
if c.spec.Dashboard.Enabled {
@@ -176,6 +178,62 @@ func validateServices(t *testing.T, c *Cluster) {
}
}
func TestUpdateServiceSelectors(t *testing.T) {
clientset := testop.New(t, 3)
ctx := &clusterd.Context{Clientset: clientset}
clusterInfo := cephclient.AdminTestClusterInfo("mycluster")
spec := cephv1.ClusterSpec{
Dashboard: cephv1.DashboardSpec{
Enabled: true,
Port: 7000,
},
}
c := &Cluster{spec: spec, context: ctx, clusterInfo: clusterInfo}
t.Run("initial active daemon", func(t *testing.T) {
activeDaemon := "a"
err := c.reconcileServices(activeDaemon)
assert.NoError(t, err)
validateServiceActiveDaemon(t, c, activeDaemon, 2, 0)
})
t.Run("update active daemon", func(t *testing.T) {
activeDaemon := "b"
err := c.updateServiceSelectors(activeDaemon)
assert.NoError(t, err)
validateServiceActiveDaemon(t, c, activeDaemon, 2, 0)
})
t.Run("skip non-mgr services", func(t *testing.T) {
svc := corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "mysvc"}}
_, err := c.context.Clientset.CoreV1().Services(c.clusterInfo.Namespace).Create(clusterInfo.Context, &svc, metav1.CreateOptions{})
assert.NoError(t, err)
activeDaemon := "c"
err = c.updateServiceSelectors(activeDaemon)
assert.NoError(t, err)
validateServiceActiveDaemon(t, c, activeDaemon, 2, 1)
})
}
func validateServiceActiveDaemon(t *testing.T, c *Cluster, activeDaemon string, expectedUpdated, expectedSkipped int) {
services, err := c.context.Clientset.CoreV1().Services(c.clusterInfo.Namespace).List(c.clusterInfo.Context, metav1.ListOptions{})
assert.NoError(t, err)
skipped := 0
updated := 0
for _, service := range services.Items {
if service.Labels["app"] == "rook-ceph-mgr" {
updated++
assert.Equal(t, activeDaemon, service.Spec.Selector[controller.DaemonIDLabel])
} else {
skipped++
assert.Equal(t, "", service.Spec.Selector[controller.DaemonIDLabel])
}
}
assert.Equal(t, expectedUpdated, updated)
assert.Equal(t, expectedSkipped, skipped)
}
func TestMgrSidecarReconcile(t *testing.T) {
activeMgr := "a"
calledMgrStat := false