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 7, 2018
1 parent bf7d348 commit a1c071f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 20 deletions.
26 changes: 21 additions & 5 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,17 @@ func cmdAdd(args *skel.CmdArgs) error {

utils.ConfigureLogging(conf.LogLevel)

// Allow the nodename to be overridden by the network config
nodename = utils.DetermineNodename(conf)
if !conf.NodenameFileOptional {
// Configured to wait for the nodename file - don't start until it exists.
if _, err := os.Stat("/var/lib/calico/nodename"); err != nil {
s := "%s: check that the calico/node container is running and has mounted /var/lib/calico/"
return fmt.Errorf(s, err)
}
logrus.Debug("/var/lib/calico/nodename exists")
}

// Determine which node name to use.
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,7 +381,16 @@ func cmdDel(args *skel.CmdArgs) error {

utils.ConfigureLogging(conf.LogLevel)

// Allow the nodename to be overridden by the network config
if !conf.NodenameFileOptional {
// Configured to wait for the nodename file - don't start until it exists.
if _, err := os.Stat("/var/lib/calico/nodename"); err != nil {
s := "%s: check that the calico/node container is running and has mounted /var/lib/calico/"
return fmt.Errorf(s, err)
}
logrus.Debug("/var/lib/calico/nodename exists")
}

// Determine which node name to use.
nodename := utils.DetermineNodename(conf)

epIDs, err := utils.GetIdentifiers(args, nodename)
Expand Down
11 changes: 11 additions & 0 deletions calico_cni_k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var _ = Describe("CalicoCni", func() {
"k8s_api_root": "http://127.0.0.1:8080"
},
"policy": {"type": "k8s"},
"nodename_file_optional": true,
"log_level":"info"
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"))

Expand Down Expand Up @@ -392,6 +393,7 @@ var _ = Describe("CalicoCni", func() {
"name": "net2",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"nodename_file_optional": true,
"datastore_type": "%s",
"ipam": {
"type": "calico-ipam"
Expand Down Expand Up @@ -460,6 +462,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"nodename_file_optional": true,
"ipam": {},
"kubernetes": {
"k8s_api_root": "http://127.0.0.1:8080"
Expand Down Expand Up @@ -560,6 +563,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"nodename_file_optional": true,
"ipam": {
"type": "calico-ipam",
"assign_ipv4": "true",
Expand Down Expand Up @@ -668,6 +672,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"nodename_file_optional": true,
"ipam": {
"type": "calico-ipam",
"assign_ipv4": "true",
Expand Down Expand Up @@ -774,6 +779,7 @@ var _ = Describe("CalicoCni", func() {
{
"cniVersion": "%s",
"name": "net6",
"nodename_file_optional": true,
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
Expand Down Expand Up @@ -867,6 +873,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"nodename_file_optional": true,
"ipam": {
"type": "calico-ipam"
},
Expand Down Expand Up @@ -1032,6 +1039,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"nodename_file_optional": true,
"ipam": {
"type": "calico-ipam"
},
Expand Down Expand Up @@ -1190,6 +1198,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"nodename_file_optional": true,
"ipam": {
"type": "calico-ipam"
},
Expand Down Expand Up @@ -1357,6 +1366,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"nodename_file_optional": true,
"ipam": {},
"kubernetes": {
"k8s_api_root": "http://127.0.0.1:8080"
Expand Down Expand Up @@ -1414,6 +1424,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"nodename_file_optional": true,
"log_level": "debug",
"ipam": {
"type": "calico-ipam"
Expand Down
8 changes: 8 additions & 0 deletions calico_cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"log_level": "debug",
"nodename_file_optional": true,
"datastore_type": "%s",
"ipam": {
"type": "host-local",
Expand Down Expand Up @@ -262,6 +263,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"hostname": "named-hostname.somewhere",
"nodename_file_optional": true,
"datastore_type": "%s",
"ipam": {
"type": "host-local",
Expand Down Expand Up @@ -315,6 +317,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"hostname": "named-hostname.somewhere",
"nodename_file_optional": true,
"ipam": {
"type": "host-local",
"subnet": "10.0.0.0/8"
Expand Down Expand Up @@ -363,6 +366,7 @@ var _ = Describe("CalicoCni", func() {
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"hostname": "named-hostname.somewhere",
"nodename_file_optional": true,
"ipam": {
"type": "host-local",
"subnet": "10.0.0.0/8"
Expand Down Expand Up @@ -415,6 +419,7 @@ var _ = Describe("CalicoCni", func() {
"etcd_endpoints": "http://%s:2379",
"hostname": "named-hostname",
"nodename": "named-nodename",
"nodename_file_optional": true,
"datastore_type": "%s",
"ipam": {
"type": "host-local",
Expand Down Expand Up @@ -468,6 +473,7 @@ var _ = Describe("CalicoCni", func() {
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"nodename_file_optional": true,
"datastore_type": "%s",
"ipam": {
"type": "host-local",
Expand Down Expand Up @@ -505,6 +511,7 @@ var _ = Describe("CalicoCni", func() {
"etcd_endpoints": "http://%s:2379",
"datastore_type": "%s",
"log_level": "debug",
"nodename_file_optional": true,
"ipam": { "type": "calico-ipam" }
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"))

Expand Down Expand Up @@ -672,6 +679,7 @@ var _ = Describe("CalicoCni", func() {
"type": "host-local",
"subnet": "10.0.0.0/8"
},
"nodename_file_optional": true,
"log_level":"debug"
}`, cniVersion, os.Getenv("ETCD_IP"), os.Getenv("DATASTORE_TYPE"))

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"`
NodenameFileOptional bool `json:"nodename_file_optional"`
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 os.IsNotExist(err) {
// File doesn't exist, return empty string.
logrus.Info("File /var/lib/calico/nodename does not exist")
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 a1c071f

Please sign in to comment.