From 4fa3c04e443c54b5bd5c9a4d4c0f9c17406a36ca Mon Sep 17 00:00:00 2001 From: Travis Nielsen Date: Mon, 25 Jan 2021 10:58:36 -0700 Subject: [PATCH] ceph: only commit the maxmonid after starting a mon daemon The maxMonId should only track the mons that have been committed to starting. Currently the maxMonId was tracking all mon scheduling. If the reconcile was aborted or failed, or the operator restarted in a reconcile, the maxMonId was being reset by analyzing the mon deployments that existed. This does not work for scenarios where mon quroum is lost and being reset to a single mon. Now the maxMonId will only be incremented when a mon daemon is started, and will never be decremented after that to ensure that new mons will be incremented to the next id. Signed-off-by: Travis Nielsen --- pkg/operator/ceph/cluster/mon/config.go | 2 + pkg/operator/ceph/cluster/mon/mon.go | 70 ++++++++++++++++- pkg/operator/ceph/cluster/mon/mon_test.go | 92 ++++++++++++++++++----- 3 files changed, 142 insertions(+), 22 deletions(-) diff --git a/pkg/operator/ceph/cluster/mon/config.go b/pkg/operator/ceph/cluster/mon/config.go index 106c38047..a5e39ce0f 100644 --- a/pkg/operator/ceph/cluster/mon/config.go +++ b/pkg/operator/ceph/cluster/mon/config.go @@ -238,6 +238,8 @@ func loadMonConfig(clientset kubernetes.Interface, namespace string) (map[string storedMaxMonID, err = strconv.Atoi(id) if err != nil { logger.Errorf("invalid max mon id %q. %v", id, err) + } else { + maxMonID = storedMaxMonID } } diff --git a/pkg/operator/ceph/cluster/mon/mon.go b/pkg/operator/ceph/cluster/mon/mon.go index 5544531ce..650696ded 100644 --- a/pkg/operator/ceph/cluster/mon/mon.go +++ b/pkg/operator/ceph/cluster/mon/mon.go @@ -1066,11 +1066,21 @@ func (c *Cluster) saveMonConfig() error { return errors.Wrap(err, "failed to format csi config") } + maxMonID, err := c.getStoredMaxMonID() + if err != nil { + return errors.Wrap(err, "failed to save maxMonID") + } + configMap.Data = map[string]string{ EndpointDataKey: FlattenMonEndpoints(c.ClusterInfo.Monitors), - MaxMonIDKey: strconv.Itoa(c.maxMonID), - MappingKey: string(monMapping), - csi.ConfigKey: csiConfigValue, + // persist the maxMonID that was previously stored in the configmap. We are likely saving info + // about scheduling of the mons, but we only want to update the maxMonID once a new mon has + // actually been started. If the operator is restarted or the reconcile is otherwise restarted, + // we want to calculate the mon scheduling next time based on the committed maxMonID, rather + // than only a mon scheduling, which may not have completed. + MaxMonIDKey: maxMonID, + MappingKey: string(monMapping), + csi.ConfigKey: csiConfigValue, } if _, err := c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Create(ctx, configMap, metav1.CreateOptions{}); err != nil { @@ -1104,6 +1114,55 @@ func (c *Cluster) saveMonConfig() error { return nil } +func (c *Cluster) getStoredMaxMonID() (string, error) { + configmap, err := c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Get(context.TODO(), EndpointConfigMapName, metav1.GetOptions{}) + if err != nil && !kerrors.IsNotFound(err) { + return "", errors.Wrap(err, "could not load maxMonId") + } + if err == nil { + if val, ok := configmap.Data[MaxMonIDKey]; ok { + return val, nil + } + } + + // if the configmap cannot be loaded, assume a new cluster. If the mons have previously + // been created, the maxMonID will anyway analyze them to ensure the index is correct + // even if this error occurs. + logger.Infof("existing maxMonID not found or failed to load. %v", err) + return "-1", nil +} + +func (c *Cluster) commitMaxMonID(monName string) error { + committedMonID, err := k8sutil.NameToIndex(monName) + if err != nil { + return errors.Wrapf(err, "invalid mon name %q", monName) + } + + configmap, err := c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Get(context.TODO(), EndpointConfigMapName, metav1.GetOptions{}) + if err != nil { + return errors.Wrap(err, "failed to find existing mon endpoint config map") + } + + // set the new max key if greater + existingMax, err := strconv.Atoi(configmap.Data[MaxMonIDKey]) + if err != nil { + return errors.Wrap(err, "failed to read existing maxMonId") + } + + if existingMax >= committedMonID { + logger.Infof("no need to commit maxMonID %d since it is not greater than existing maxMonID %d", committedMonID, existingMax) + return nil + } + + logger.Infof("updating maxMonID from %d to %d after committing mon %q", existingMax, committedMonID, monName) + configmap.Data[MaxMonIDKey] = strconv.Itoa(committedMonID) + + if _, err = c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Update(context.TODO(), configmap, metav1.UpdateOptions{}); err != nil { + return errors.Wrap(err, "failed to update mon endpoint config map for the maxMonID") + } + return nil +} + var updateDeploymentAndWait = UpdateCephDeploymentAndWait func (c *Cluster) updateMon(m *monConfig, d *apps.Deployment) error { @@ -1250,6 +1309,11 @@ func (c *Cluster) startMon(m *monConfig, schedule *MonScheduleInfo) error { return errors.Wrapf(err, "failed to create mon deployment %s", d.Name) } + // Commit the maxMonID after a mon deployment has been started (and not just scheduled) + if err := c.commitMaxMonID(m.DaemonName); err != nil { + return errors.Wrapf(err, "failed to commit maxMonId after starting mon %q", m.DaemonName) + } + return nil } diff --git a/pkg/operator/ceph/cluster/mon/mon_test.go b/pkg/operator/ceph/cluster/mon/mon_test.go index 75a2aa5ec..e2310a918 100644 --- a/pkg/operator/ceph/cluster/mon/mon_test.go +++ b/pkg/operator/ceph/cluster/mon/mon_test.go @@ -146,19 +146,19 @@ func TestStartMonPods(t *testing.T) { ctx := context.TODO() namespace := "ns" context, err := newTestStartCluster(t, namespace) - assert.Nil(t, err) + assert.NoError(t, err) c := newCluster(context, namespace, true, v1.ResourceRequirements{}) c.ClusterInfo = clienttest.CreateTestClusterInfo(1) // start a basic cluster _, err = c.Start(c.ClusterInfo, c.rookVersion, cephver.Nautilus, c.spec) - assert.Nil(t, err) + assert.NoError(t, err) validateStart(ctx, t, c) // starting again should be a no-op, but still results in an error _, err = c.Start(c.ClusterInfo, c.rookVersion, cephver.Nautilus, c.spec) - assert.Nil(t, err) + assert.NoError(t, err) validateStart(ctx, t, c) } @@ -167,13 +167,13 @@ func TestOperatorRestart(t *testing.T) { ctx := context.TODO() namespace := "ns" context, err := newTestStartCluster(t, namespace) - assert.Nil(t, err) + assert.NoError(t, err) c := newCluster(context, namespace, true, v1.ResourceRequirements{}) c.ClusterInfo = clienttest.CreateTestClusterInfo(1) // start a basic cluster info, err := c.Start(c.ClusterInfo, c.rookVersion, cephver.Nautilus, c.spec) - assert.Nil(t, err) + assert.NoError(t, err) assert.True(t, info.IsInitialized(true)) validateStart(ctx, t, c) @@ -183,7 +183,7 @@ func TestOperatorRestart(t *testing.T) { // starting again should be a no-op, but will not result in an error info, err = c.Start(c.ClusterInfo, c.rookVersion, cephver.Nautilus, c.spec) - assert.Nil(t, err) + assert.NoError(t, err) assert.True(t, info.IsInitialized(true)) validateStart(ctx, t, c) @@ -194,7 +194,7 @@ func TestOperatorRestartHostNetwork(t *testing.T) { ctx := context.TODO() namespace := "ns" context, err := newTestStartCluster(t, namespace) - assert.Nil(t, err) + assert.NoError(t, err) // cluster without host networking c := newCluster(context, namespace, false, v1.ResourceRequirements{}) @@ -202,7 +202,7 @@ func TestOperatorRestartHostNetwork(t *testing.T) { // start a basic cluster info, err := c.Start(c.ClusterInfo, c.rookVersion, cephver.Nautilus, c.spec) - assert.Nil(t, err) + assert.NoError(t, err) assert.True(t, info.IsInitialized(true)) validateStart(ctx, t, c) @@ -214,7 +214,7 @@ func TestOperatorRestartHostNetwork(t *testing.T) { // starting again should be a no-op, but still results in an error info, err = c.Start(c.ClusterInfo, c.rookVersion, cephver.Nautilus, c.spec) - assert.Nil(t, err) + assert.NoError(t, err) assert.True(t, info.IsInitialized(true), info) validateStart(ctx, t, c) @@ -227,7 +227,7 @@ func validateStart(ctx context.Context, t *testing.T, c *Cluster) { // there is only one pod created. the other two won't be created since the first one doesn't start _, err = c.context.Clientset.AppsV1().Deployments(c.Namespace).Get(ctx, "rook-ceph-mon-a", metav1.GetOptions{}) - assert.Nil(t, err) + assert.NoError(t, err) } func TestSaveMonEndpoints(t *testing.T) { @@ -240,10 +240,10 @@ func TestSaveMonEndpoints(t *testing.T) { // create the initial config map err := c.saveMonConfig() - assert.Nil(t, err) + assert.NoError(t, err) cm, err := c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Get(ctx, EndpointConfigMapName, metav1.GetOptions{}) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, "a=1.2.3.1:6789", cm.Data[EndpointDataKey]) assert.Equal(t, `{"node":{}}`, cm.Data[MappingKey]) assert.Equal(t, "-1", cm.Data[MaxMonIDKey]) @@ -257,13 +257,67 @@ func TestSaveMonEndpoints(t *testing.T) { Hostname: "myhost", } err = c.saveMonConfig() - assert.Nil(t, err) + assert.NoError(t, err) cm, err = c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Get(ctx, EndpointConfigMapName, metav1.GetOptions{}) - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, "a=2.3.4.5:6789", cm.Data[EndpointDataKey]) assert.Equal(t, `{"node":{"a":{"Name":"node0","Hostname":"myhost","Address":"1.1.1.1"}}}`, cm.Data[MappingKey]) - assert.Equal(t, "2", cm.Data[MaxMonIDKey]) + assert.Equal(t, "-1", cm.Data[MaxMonIDKey]) + + // Update the maxMonID to some random value + cm.Data[MaxMonIDKey] = "23" + _, err = c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Update(ctx, cm, metav1.UpdateOptions{}) + assert.NoError(t, err) + // Confirm the maxMonId will be persisted and not updated to anything else. + // The value is only expected to be set directly to the configmap when a mon deployment is started. + err = c.saveMonConfig() + assert.NoError(t, err) + cm, err = c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Get(ctx, EndpointConfigMapName, metav1.GetOptions{}) + assert.NoError(t, err) + assert.Equal(t, "23", cm.Data[MaxMonIDKey]) +} + +func TestMaxMonID(t *testing.T) { + clientset := test.New(t, 1) + configDir, _ := ioutil.TempDir("", "") + defer os.RemoveAll(configDir) + c := New(&clusterd.Context{Clientset: clientset, ConfigDir: configDir}, "ns", cephv1.ClusterSpec{}, metav1.OwnerReference{}, &sync.Mutex{}) + + // when the configmap is not found, the maxMonID is -1 + maxMonID, err := c.getStoredMaxMonID() + assert.NoError(t, err) + assert.Equal(t, "-1", maxMonID) + + // initialize the configmap + setCommonMonProperties(c, 1, cephv1.MonSpec{Count: 3, AllowMultiplePerNode: true}, "myversion") + err = c.saveMonConfig() + assert.NoError(t, err) + + // invalid mon names won't update the maxMonID + err = c.commitMaxMonID("bad-id") + assert.Error(t, err) + + // starting a mon deployment will set the maxMonID + err = c.commitMaxMonID("a") + assert.NoError(t, err) + maxMonID, err = c.getStoredMaxMonID() + assert.NoError(t, err) + assert.Equal(t, "0", maxMonID) + + // set to a higher id + err = c.commitMaxMonID("d") + assert.NoError(t, err) + maxMonID, err = c.getStoredMaxMonID() + assert.NoError(t, err) + assert.Equal(t, "3", maxMonID) + + // setting to an id lower than the max will not update it + err = c.commitMaxMonID("c") + assert.NoError(t, err) + maxMonID, err = c.getStoredMaxMonID() + assert.NoError(t, err) + assert.Equal(t, "3", maxMonID) } func TestMonInQuorum(t *testing.T) { @@ -294,13 +348,13 @@ func TestNameToIndex(t *testing.T) { // valid id, err = fullNameToIndex("b") - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, 1, id) id, err = fullNameToIndex("m") - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, 12, id) id, err = fullNameToIndex("rook-ceph-mon-a") - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, 0, id) } @@ -325,7 +379,7 @@ func TestWaitForQuorum(t *testing.T) { expectedMons := []string{"a"} clusterInfo := &client.ClusterInfo{Namespace: namespace} err = waitForQuorumWithMons(context, clusterInfo, expectedMons, 0, requireAllInQuorum) - assert.Nil(t, err) + assert.NoError(t, err) } func TestMonFoundInQuorum(t *testing.T) {