Skip to content

Commit

Permalink
Use the /var/lib/calico/nodename file if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
caseydavenport committed Mar 2, 2018
1 parent bf7d348 commit f82f8dc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
20 changes: 17 additions & 3 deletions calico.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var nodename string

func init() {
// This ensures that main runs only on main thread (thread group leader).
// since namespace ops (unshare, setns) are done for a single thread, we
Expand All @@ -56,8 +54,16 @@ func cmdAdd(args *skel.CmdArgs) error {

utils.ConfigureLogging(conf.LogLevel)

if conf.RequireNodenameFile {
// Configured to use the nodename file - don't start until it exists.
if _, err := os.Stat("/var/lib/calico/nodename"); err != nil {
return fmt.Errorf("error reading /var/lib/calico/nodename: %s: is calico/node running?", err)
}
logrus.Debug("/var/lib/calico/nodename exists")
}

// Allow the nodename to be overridden by the network config
nodename = utils.DetermineNodename(conf)
nodename := utils.DetermineNodename(conf)

// Extract WEP identifiers such as pod name, pod namespace (for k8s), containerID, IfName.
wepIDs, err := utils.GetIdentifiers(args, nodename)
Expand Down Expand Up @@ -374,6 +380,14 @@ func cmdDel(args *skel.CmdArgs) error {

utils.ConfigureLogging(conf.LogLevel)

if conf.RequireNodenameFile {
// Configured to use the nodename file - don't start until it exists.
if _, err := os.Stat("/var/lib/calico/nodename"); err != nil {
return fmt.Errorf("error reading /var/lib/calico/nodename: %s: is calico/node running?", err)
}
logrus.Debug("/var/lib/calico/nodename exists")
}

// Allow the nodename to be overridden by the network config
nodename := utils.DetermineNodename(conf)

Expand Down
31 changes: 16 additions & 15 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,24 @@ type NetConf struct {
IPv4Pools []string `json:"ipv4_pools,omitempty"`
IPv6Pools []string `json:"ipv6_pools,omitempty"`
} `json:"ipam,omitempty"`
MTU int `json:"mtu"`
Hostname string `json:"hostname"`
Nodename string `json:"nodename"`
DatastoreType string `json:"datastore_type"`
EtcdAuthority string `json:"etcd_authority"`
EtcdEndpoints string `json:"etcd_endpoints"`
LogLevel string `json:"log_level"`
Policy Policy `json:"policy"`
Kubernetes Kubernetes `json:"kubernetes"`
Args Args `json:"args"`
EtcdScheme string `json:"etcd_scheme"`
EtcdKeyFile string `json:"etcd_key_file"`
EtcdCertFile string `json:"etcd_cert_file"`
EtcdCaCertFile string `json:"etcd_ca_cert_file"`
MTU int `json:"mtu"`
Hostname string `json:"hostname"`
Nodename string `json:"nodename"`
RequireNodenameFile bool `json:"require_nodename_file"`
DatastoreType string `json:"datastore_type"`
EtcdAuthority string `json:"etcd_authority"`
EtcdEndpoints string `json:"etcd_endpoints"`
LogLevel string `json:"log_level"`
Policy Policy `json:"policy"`
Kubernetes Kubernetes `json:"kubernetes"`
Args Args `json:"args"`
EtcdScheme string `json:"etcd_scheme"`
EtcdKeyFile string `json:"etcd_key_file"`
EtcdCertFile string `json:"etcd_cert_file"`
EtcdCaCertFile string `json:"etcd_ca_cert_file"`
}

// CNITestArgs is the CNI_ARGS used for test purpose.
// CNITestArgs is the CNI_ARGS used for test purposes.
type CNITestArgs struct {
types.CommonArgs
CNI_TEST_NAMESPACE types.UnmarshallableString
Expand Down
23 changes: 23 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"regexp"
Expand Down Expand Up @@ -57,12 +58,34 @@ func DetermineNodename(conf types.NetConf) string {
nodename = conf.Hostname
logrus.Warn("Configuration option 'hostname' is deprecated, use 'nodename' instead.")
}
if nff := nodenameFromFile(); nff != "" {
logrus.Debugf("Read node name from file: %s", nff)
nodename = nff
}
if conf.Nodename != "" {
logrus.Debugf("Read node name from CNI conf: %s", conf.Nodename)
nodename = conf.Nodename
}
logrus.Debugf("Using node name %s", nodename)
return nodename
}

// nodenameFromFile reads the /var/lib/calico/nodename file if it exists and
// returns the nodename within.
func nodenameFromFile() string {
data, err := ioutil.ReadFile("/var/lib/calico/nodename")
if err != nil {
if strings.Contains(fmt.Sprintf("%s", err), "no such file") {
// File doesn't exist, return empty string.
logrus.Debug("File does not exist: /var/lib/calico/nodename")
return ""
}
logrus.WithError(err).Error("Failed to read /var/lib/calico/nodename")
return ""
}
return string(data)
}

// CreateOrUpdate creates the WorkloadEndpoint if ResourceVersion is not specified,
// or Update if it's specified.
func CreateOrUpdate(ctx context.Context, client client.Interface, wep *api.WorkloadEndpoint) (*api.WorkloadEndpoint, error) {
Expand Down

0 comments on commit f82f8dc

Please sign in to comment.