add node roles to pkg/cluster/constants

This commit is contained in:
Benjamin Elder
2019-02-28 19:22:32 -08:00
parent 91356d6ac4
commit 2ed8b05b0c
4 changed files with 45 additions and 14 deletions

View File

@@ -23,5 +23,34 @@ const DefaultClusterName = "kind"
// ClusterLabelKey is applied to each "node" docker container for identification
const ClusterLabelKey = "io.k8s.sigs.kind.cluster"
// ClusterRoleKey is applied to each "node" docker container for categorization of nodes by role
const ClusterRoleKey = "io.k8s.sigs.kind.role"
// NodeRoleKey is applied to each "node" docker container for categorization
// of nodes by role
const NodeRoleKey = "io.k8s.sigs.kind.role"
/* node role value constants */
const (
// ControlPlaneNodeRoleValue identifies a node that hosts a Kubernetes
// control-plane.
//
// NOTE: in single node clusters, control-plane nodes act as worker nodes
ControlPlaneNodeRoleValue string = "control-plane"
// WorkerNodeRoleValue identifies a node that hosts a Kubernetes worker
WorkerNodeRoleValue string = "worker"
// ExternalLoadBalancerNodeRoleValue identifies a node that hosts an
// external load balancer for the API server in HA configurations.
//
// Please note that `kind` nodes hosting external load balancer are not
// kubernetes nodes
ExternalLoadBalancerNodeRoleValue string = "external-load-balancer"
// ExternalEtcdNodeRoleValue identifies a node that hosts an external-etcd
// instance.
//
// WARNING: this node type is not yet implemented!
//
// Please note that `kind` nodes hosting external etcd are not
// kubernetes nodes
ExternalEtcdNodeRoleValue string = "external-etcd"
)

View File

@@ -22,6 +22,8 @@ import (
"strings"
"time"
"sigs.k8s.io/kind/pkg/cluster/constants"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
@@ -140,12 +142,13 @@ func (cc *Context) ProvisionNodes() (nodeList map[string]*nodes.Node, err error)
var name = fmt.Sprintf("%s-%s", cc.Name(), configNode.Name)
var node *nodes.Node
switch configNode.Role {
case config.ExternalLoadBalancerRole:
// TODO(bentheelder): decouple from config objects further
switch string(configNode.Role) {
case constants.ExternalLoadBalancerNodeRoleValue:
node, err = nodes.CreateExternalLoadBalancerNode(name, configNode.Image, cc.ClusterLabel())
case config.ControlPlaneRole:
case constants.ControlPlaneNodeRoleValue:
node, err = nodes.CreateControlPlaneNode(name, configNode.Image, cc.ClusterLabel(), configNode.ExtraMounts)
case config.WorkerRole:
case constants.WorkerNodeRoleValue:
node, err = nodes.CreateWorkerNode(name, configNode.Image, cc.ClusterLabel(), configNode.ExtraMounts)
}
if err != nil {

View File

@@ -130,7 +130,7 @@ func createNode(name, image, clusterLabel string, role config.NodeRole, mounts [
// label the node with the cluster ID
"--label", clusterLabel,
// label the node with the role ID
"--label", fmt.Sprintf("%s=%s", constants.ClusterRoleKey, role),
"--label", fmt.Sprintf("%s=%s", constants.NodeRoleKey, role),
// explicitly set the entrypoint
"--entrypoint=/usr/local/bin/entrypoint",
}

View File

@@ -31,7 +31,6 @@ import (
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/version"
"sigs.k8s.io/kind/pkg/cluster/config"
"sigs.k8s.io/kind/pkg/cluster/constants"
"sigs.k8s.io/kind/pkg/container/docker"
@@ -72,7 +71,7 @@ type nodeCache struct {
kubernetesVersion string
ip string
ports map[int]int
role config.NodeRole
role string
containerCmder exec.Cmder
}
@@ -264,20 +263,20 @@ func (n *Node) Ports(containerPort int) (hostPort int, err error) {
}
// Role returns the role of the node
func (n *Node) Role() (role config.NodeRole, err error) {
func (n *Node) Role() (role string, err error) {
// use the cached version first
if n.nodeCache.role != "" {
return n.nodeCache.role, nil
}
// retrive the role the node using docker inspect
lines, err := docker.Inspect(n.nameOrID, fmt.Sprintf("{{index .Config.Labels %q}}", constants.ClusterRoleKey))
lines, err := docker.Inspect(n.nameOrID, fmt.Sprintf("{{index .Config.Labels %q}}", constants.NodeRoleKey))
if err != nil {
return "", errors.Wrapf(err, "failed to get %q label", constants.ClusterRoleKey)
return "", errors.Wrapf(err, "failed to get %q label", constants.NodeRoleKey)
}
if len(lines) != 1 {
return "", errors.Errorf("%q label should only be one line, got %d lines", constants.ClusterRoleKey, len(lines))
return "", errors.Errorf("%q label should only be one line, got %d lines", constants.NodeRoleKey, len(lines))
}
n.nodeCache.role = config.NodeRole(strings.Trim(lines[0], "'"))
n.nodeCache.role = strings.Trim(lines[0], "'")
return n.nodeCache.role, nil
}