diff --git a/pkg/api/daemon.go b/pkg/api/daemon.go index dcad73962..8364ba711 100644 --- a/pkg/api/daemon.go +++ b/pkg/api/daemon.go @@ -22,6 +22,7 @@ import ( "github.com/rook/rook/pkg/ceph/mon" "github.com/rook/rook/pkg/clusterd" monop "github.com/rook/rook/pkg/operator/mon" + "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/watch" @@ -76,6 +77,10 @@ func WatchMonConfig(context *clusterd.Context, c *Config) { for { e := <-w.ResultChan() if e.Type == watch.Modified { + // "unmarshal" object into configmap and set new endpoints + monEndpoints := e.Object.(*v1.ConfigMap) + c.clusterInfo.Monitors = mon.ParseMonEndpoints(monEndpoints.Data[monop.EndpointDataKey]) + // write the latest config to the config dir if err := mon.GenerateAdminConnectionConfig(context, c.clusterInfo); err != nil { logger.Errorf("failed to write connection config. %+v", err) diff --git a/pkg/ceph/client/test/mon.go b/pkg/ceph/client/test/mon.go index 9d3c78fb8..bc547c81a 100644 --- a/pkg/ceph/client/test/mon.go +++ b/pkg/ceph/client/test/mon.go @@ -23,7 +23,7 @@ import ( func MonInQuorumResponse() string { resp := client.MonStatusResponse{Quorum: []int{0}} - resp.MonMap.Mons = []client.MonMapEntry{{Name: "mon0", Rank: 0, Address: "1.2.3.4"}} + resp.MonMap.Mons = []client.MonMapEntry{{Name: "mon1", Rank: 0, Address: "1.2.3.4"}} serialized, _ := json.Marshal(resp) return string(serialized) } diff --git a/pkg/operator/mon/health.go b/pkg/operator/mon/health.go index 523d44276..b5dcc27f8 100644 --- a/pkg/operator/mon/health.go +++ b/pkg/operator/mon/health.go @@ -75,9 +75,36 @@ func (c *Cluster) checkHealth() error { } logger.Debugf("Mon status: %+v", status) - // failover the unhealthy mons + // Source of thruth of which mons should exist is our *clusterInfo* + monsNotFound := map[string]interface{}{} + for _, mon := range c.clusterInfo.Monitors { + monsNotFound[mon.Name] = struct{}{} + } + + // first handle mons that are not in quorum but in the cecph mon map + //failover the unhealthy mons for _, mon := range status.MonMap.Mons { inQuorum := monInQuorum(mon, status.Quorum) + // if the mon is in quorum remove it from our check for "existence" + //else see below condition + if _, ok := monsNotFound[mon.Name]; ok { + delete(monsNotFound, mon.Name) + } else { + // when the mon isn't in the clusterInfo, but is in qorum and there are + //enough mons, remove it else remove it on the next run + if inQuorum && len(status.MonMap.Mons) > c.Size { + logger.Warningf("mon %s not in source of truth but in quorum, removing", mon.Name) + c.removeMon(mon.Name) + } else { + logger.Warningf( + "mon %s not in source of truth and not in quorum, not enough mons to remove now (wanted: %d, current: %d)", + mon.Name, + c.Size, + len(status.MonMap.Mons), + ) + } + } + if inQuorum { logger.Debugf("mon %s found in quorum", mon.Name) // delete the "timeout" for a mon if the pod is in quorum again @@ -100,27 +127,39 @@ func (c *Cluster) checkHealth() error { continue } - if len(status.MonMap.Mons) > c.Size { - // no need to create a new mon since we have an extra - err = c.removeMon(mon.Name) - if err != nil { - logger.Errorf("failed to remove mon %s. %+v", mon.Name, err) - } - } else { - // bring up a new mon to replace the unhealthy mon - err = c.failoverMon(mon.Name) - if err != nil { - logger.Errorf("failed to failover mon %s. %+v", mon.Name, err) - } - } + c.failMon(len(status.MonMap.Mons), mon.Name) // only deal with one unhealthy mon per health check return nil } } + // after all unhealthy mons have been removed/failovered + //handle all mons that haven't been in the Ceph mon map + for mon := range monsNotFound { + logger.Warningf("mon %s NOT found in ceph mon map, failover", mon) + c.failMon(len(c.clusterInfo.Monitors), mon) + // only deal with one "not found in ceph mon map" mon per health check + return nil + } + return nil } +// failMon monCount is compared against c.Size (wanted mon count) +func (c *Cluster) failMon(monCount int, name string) { + if monCount > c.Size { + // no need to create a new mon since we have an extra + if err := c.removeMon(name); err != nil { + logger.Errorf("failed to remove mon %s. %+v", name, err) + } + } else { + // bring up a new mon to replace the unhealthy mon + if err := c.failoverMon(name); err != nil { + logger.Errorf("failed to failover mon %s. %+v", name, err) + } + } +} + func (c *Cluster) failoverMon(name string) error { logger.Infof("Failing over monitor %s", name) @@ -177,19 +216,22 @@ func (c *Cluster) removeMon(name string) error { return fmt.Errorf("failed to remove mon %s from quorum. %+v", name, err) } delete(c.clusterInfo.Monitors, name) - nodeName := c.mapping.Node[name].Name - // if node->port "mapping" has been created, decrease or delete it - if port, ok := c.mapping.Port[nodeName]; ok { - if port == mon.DefaultPort { - delete(c.mapping.Port, nodeName) + // check if a mapping exists for the mon + if _, ok := c.mapping.Node[name]; ok { + nodeName := c.mapping.Node[name].Name + delete(c.mapping.Node, name) + // if node->port "mapping" has been created, decrease or delete it + if port, ok := c.mapping.Port[nodeName]; ok { + if port == mon.DefaultPort { + delete(c.mapping.Port, nodeName) + } + // don't clean up if a node port is higher than the default port, other + //mons could be on the same node with > DefaultPort ports, decreasing could + //cause port collsions + // This can be solved by using a map[nodeName][]int32 for the ports to + //even better check which ports are open for the HostNetwork mode } - // don't clean up if a node port is higher than the default port, other - //mons could be on the same node with > DefaultPort ports, decreasing could - //cause port collsions - // This can be solved by using a map[nodeName][]int32 for the ports to - //even better check which ports are open for the HostNetwork mode } - delete(c.mapping.Node, name) // Remove the service endpoint if err := c.context.Clientset.CoreV1().Services(c.Namespace).Delete(name, options); err != nil { diff --git a/pkg/operator/mon/mon.go b/pkg/operator/mon/mon.go index 5287d59ed..b6838ae6e 100644 --- a/pkg/operator/mon/mon.go +++ b/pkg/operator/mon/mon.go @@ -128,37 +128,7 @@ func (c *Cluster) Start() error { } if len(c.clusterInfo.Monitors) < c.Size { - // init the mons config - mons := c.initMonConfig(c.Size) - - // Assign the pods to nodes - if err := c.assignMons(mons); err != nil { - return fmt.Errorf("failed to assign pods to mons. %+v", err) - } - - // Start one monitor at a time - for i := len(c.clusterInfo.Monitors); i < c.Size; i++ { - // Init the mon IPs - if err := c.initMonIPs(mons[0 : i+1]); err != nil { - return fmt.Errorf("failed to init mon services. %+v", err) - } - - // save the mon config after we have "initiated the IPs" - if err := c.saveMonConfig(); err != nil { - return fmt.Errorf("failed to save mons. %+v", err) - } - - // make sure we have the connection info generated so connections can happen - if err := WriteConnectionConfig(c.context, c.clusterInfo); err != nil { - return err - } - - // Start pods - if err := c.startPods(mons[0 : i+1]); err != nil { - return fmt.Errorf("failed to start mon pods. %+v", err) - } - } - logger.Debugf("mon endpoints used are: %s", c.clusterInfo.MonEndpoints()) + c.startMons() } else { // Check the health of a previously started cluster if err := c.checkHealth(); err != nil { @@ -169,6 +139,42 @@ func (c *Cluster) Start() error { return nil } +func (c *Cluster) startMons() error { + // init the mons config + mons := c.initMonConfig(c.Size) + + // Assign the pods to nodes + if err := c.assignMons(mons); err != nil { + return fmt.Errorf("failed to assign pods to mons. %+v", err) + } + + // Start one monitor at a time + for i := len(c.clusterInfo.Monitors); i < c.Size; i++ { + // Init the mon IPs + if err := c.initMonIPs(mons[0 : i+1]); err != nil { + return fmt.Errorf("failed to init mon services. %+v", err) + } + + // save the mon config after we have "initiated the IPs" + if err := c.saveMonConfig(); err != nil { + return fmt.Errorf("failed to save mons. %+v", err) + } + + // make sure we have the connection info generated so connections can happen + if err := WriteConnectionConfig(c.context, c.clusterInfo); err != nil { + return err + } + + // Start pods + if err := c.startPods(mons[0 : i+1]); err != nil { + return fmt.Errorf("failed to start mon pods. %+v", err) + } + } + + logger.Debugf("mon endpoints used are: %s", c.clusterInfo.MonEndpoints()) + return nil +} + // Retrieve the ceph cluster info if it already exists. // If a new cluster create new keys. func (c *Cluster) initClusterInfo() error { diff --git a/pkg/operator/mon/mon_test.go b/pkg/operator/mon/mon_test.go index 620791c2e..b978b9fce 100644 --- a/pkg/operator/mon/mon_test.go +++ b/pkg/operator/mon/mon_test.go @@ -195,20 +195,21 @@ func TestCheckHealth(t *testing.T) { Executor: executor, } c := New(context, "ns", "", "myversion", k8sutil.Placement{}, false) + c.Size = 1 c.clusterInfo = test.CreateClusterInfo(1) c.waitForStart = false defer os.RemoveAll(c.context.ConfigDir) - err := c.checkHealth() - assert.Nil(t, err) - c.mapping.Node["mon1"] = &nodeInfo{ Name: "node0", Address: "", } c.mapping.Port["node0"] = cephmon.DefaultPort - c.maxMonID = 10 + + err := c.checkHealth() + assert.Nil(t, err) + err = c.failoverMon("mon1") assert.Nil(t, err) @@ -465,3 +466,60 @@ func TestHostNetworkPortIncrease(t *testing.T) { sEndpoint = strings.Split(c.clusterInfo.Monitors["mon2"].Endpoint, ":") assert.Equal(t, strconv.Itoa(cephmon.DefaultPort+1), sEndpoint[1]) } + +func TestCheckHealthNotFound(t *testing.T) { + executor := &exectest.MockExecutor{ + MockExecuteCommandWithOutputFile: func(debug bool, actionName string, command string, outFileArg string, args ...string) (string, error) { + return clienttest.MonInQuorumResponse(), nil + }, + } + clientset := test.New(1) + configDir, _ := ioutil.TempDir("", "") + defer os.RemoveAll(configDir) + context := &clusterd.Context{ + Clientset: clientset, + ConfigDir: configDir, + Executor: executor, + } + c := New(context, "ns", "", "myversion", k8sutil.Placement{}, false) + c.Size = 2 + c.clusterInfo = test.CreateClusterInfo(2) + c.waitForStart = false + defer os.RemoveAll(c.context.ConfigDir) + fmt.Printf("TEST: %+v\n", c.clusterInfo) + + c.mapping.Node["mon1"] = &nodeInfo{ + Name: "node0", + Address: "", + } + c.mapping.Node["mon2"] = &nodeInfo{ + Name: "node0", + Address: "", + } + c.mapping.Port["node0"] = cephmon.DefaultPort + c.maxMonID = 10 + + c.saveMonConfig() + + // Check if the two mons are found in the configmap + cm, err := c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Get(EndpointConfigMapName, metav1.GetOptions{}) + assert.Nil(t, err) + if cm.Data[EndpointDataKey] == "mon1=1.2.3.1:6790,mon2=1.2.3.2:6790" { + assert.Equal(t, "mon1=1.2.3.1:6790,mon2=1.2.3.2:6790", cm.Data[EndpointDataKey]) + } else { + assert.Equal(t, "mon2=1.2.3.2:6790,mon1=1.2.3.1:6790", cm.Data[EndpointDataKey]) + } + + // Because mon2 isn't in the MonInQuorumResponse() this will create a mon11 + err = c.checkHealth() + assert.Nil(t, err) + + // recheck that the "not found" mon has been replaced with a new one + cm, err = c.context.Clientset.CoreV1().ConfigMaps(c.Namespace).Get(EndpointConfigMapName, metav1.GetOptions{}) + assert.Nil(t, err) + if cm.Data[EndpointDataKey] == "mon1=1.2.3.1:6790,rook-ceph-mon11=:6790" { + assert.Equal(t, "mon1=1.2.3.1:6790,rook-ceph-mon11=:6790", cm.Data[EndpointDataKey]) + } else { + assert.Equal(t, "rook-ceph-mon11=:6790,mon1=1.2.3.1:6790", cm.Data[EndpointDataKey]) + } +}