Files
my-rook-config/pkg/operator/ceph/cluster/mon/node.go
T
travisn c1628141aa minimize args to mon creation in the operator
Only the args necessary have traditionally been passed to the mon package.
This has led to many properties in the mon struct, whereas we will
now simply pass the cluster crd spec to simplify.

Signed-off-by: travisn <tnielsen@redhat.com>
2019-03-14 13:10:52 -06:00

137 lines
4.2 KiB
Go

/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mon
import (
"fmt"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/operator/k8sutil"
"github.com/rook/rook/pkg/util"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubelet/apis"
)
// getMonNodes detects the nodes that are available for new mons to start.
func (c *Cluster) getMonNodes() ([]v1.Node, error) {
availableNodes, nodes, err := c.getAvailableMonNodes()
if err != nil {
return nil, err
}
logger.Infof("Found %d running nodes without mons", len(availableNodes))
// if all nodes already have mons and the user has given the mon.count, add all nodes to be available
if c.spec.Mon.AllowMultiplePerNode && len(availableNodes) == 0 {
logger.Infof("All nodes are running mons. Adding all %d nodes to the availability.", len(nodes.Items))
for _, node := range nodes.Items {
valid, err := k8sutil.ValidNode(node, cephv1.GetMonPlacement(c.spec.Placement))
if err != nil {
logger.Warning("failed to validate node %s %v", node.Name, err)
} else if valid {
availableNodes = append(availableNodes, node)
}
}
}
return availableNodes, nil
}
func (c *Cluster) getAvailableMonNodes() ([]v1.Node, *v1.NodeList, error) {
nodeOptions := metav1.ListOptions{}
nodeOptions.TypeMeta.Kind = "Node"
nodes, err := c.context.Clientset.CoreV1().Nodes().List(nodeOptions)
if err != nil {
return nil, nil, err
}
logger.Debugf("there are %d nodes available for %d mons", len(nodes.Items), len(c.clusterInfo.Monitors))
// get the nodes that have mons assigned
nodesInUse, err := c.getNodesWithMons(nodes)
if err != nil {
logger.Warningf("could not get nodes with mons. %+v", err)
nodesInUse = util.NewSet()
}
// choose nodes for the new mons that don't have mons currently
availableNodes := []v1.Node{}
for _, node := range nodes.Items {
if !nodesInUse.Contains(node.Name) {
valid, err := k8sutil.ValidNode(node, cephv1.GetMonPlacement(c.spec.Placement))
if err != nil {
logger.Warning("failed to validate node %s %v", node.Name, err)
} else if valid {
availableNodes = append(availableNodes, node)
}
}
}
return availableNodes, nodes, nil
}
func (c *Cluster) getNodesWithMons(nodes *v1.NodeList) (*util.Set, error) {
// get the mon pods and their node affinity
options := metav1.ListOptions{LabelSelector: fmt.Sprintf("app=%s", appName)}
pods, err := c.context.Clientset.CoreV1().Pods(c.Namespace).List(options)
if err != nil {
return nil, err
}
nodesInUse := util.NewSet()
for _, pod := range pods.Items {
hostname := pod.Spec.NodeSelector[apis.LabelHostname]
logger.Debugf("mon pod on node %s", hostname)
name, ok := getNodeNameFromHostname(nodes, hostname)
if !ok {
logger.Errorf("mon %s on hostname %s not found in node list", pod.Name, hostname)
}
nodesInUse.Add(name)
}
return nodesInUse, nil
}
// Look up the immutable node name from the hostname label
func getNodeNameFromHostname(nodes *v1.NodeList, hostname string) (string, bool) {
for _, node := range nodes.Items {
if node.Labels[apis.LabelHostname] == hostname {
return node.Name, true
}
if node.Name == hostname {
return node.Name, true
}
}
return "", false
}
func getNodeInfoFromNode(n v1.Node) (*NodeInfo, error) {
nr := &NodeInfo{
Name: n.Name,
Hostname: n.Labels[apis.LabelHostname],
}
for _, ip := range n.Status.Addresses {
if ip.Type == v1.NodeExternalIP || ip.Type == v1.NodeInternalIP {
logger.Debugf("using IP %s for node %s", ip.Address, n.Name)
nr.Address = ip.Address
break
}
}
if nr.Address == "" {
return nil, fmt.Errorf("couldn't get IP of node %s", nr.Name)
}
return nr, nil
}