Example #1
0
func main() {
	var pluginVersion bool
	cniApp := &cniAppInfo{}

	if fileStat, err := os.Stat(logfileName); err == nil {
		if fileStat.Size() >= logfileSize {
			os.Rename(logfileName, fmt.Sprintf("%s%s", logfileName, ".old"))
		}
	}

	logFd, err := os.OpenFile(logfileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		logger.Fatalf("%s", err)
	}

	defer logFd.Close()
	logger.SetOutput(logFd)
	cniApp.init()

	logger.Infof("===[%s] starting Contiv CNI plugin %s for Mesos   ===",
		time.Now(), os.Args[0])
	logger.Infof("%s", version.String())
	logger.Infof("CNI version : %s", cniapi.CniDefaultVersion)

	flagSet := flag.NewFlagSet("contivnet", flag.ExitOnError)
	flagSet.BoolVar(&pluginVersion,
		"version",
		false,
		"show version")

	if err := flagSet.Parse(os.Args[1:]); err != nil {
		fmt.Printf("failed to parse command, Error: %s", err)
	}

	if pluginVersion {
		fmt.Printf("Contiv netplugin version : %s", version.String())
		logger.Infof("CNI version : %s", cniapi.CniDefaultVersion)
		os.Exit(0)
	}

	if err := cniApp.parseEnv(); err != nil {
		logger.Errorf("%s", err)
		os.Exit(1)
	}
	cniApp.parseNwInfoLabels()
	retCode := cniApp.processCmd()
	cniLog.Infof("cni return code: %d", retCode)
	os.Exit(retCode)
}
Example #2
0
func main() {
	app := cli.NewApp()
	app.Flags = netctl.NetmasterFlags
	app.Version = "\n" + version.String()
	app.Commands = netctl.Commands
	app.Run(os.Args)
}
Example #3
0
func showVersion(ctx *cli.Context) {
	argCheck(0, ctx)

	ver := version.Info{}
	if err := getObject(ctx, versionURL(ctx), &ver); err != nil {
		fmt.Printf("Unable to fetch version information")
	} else {
		fmt.Printf("Client Version:\n")
		fmt.Printf(version.String())
		fmt.Printf("\n")
		fmt.Printf("Server Version:\n")
		fmt.Printf(version.StringFromInfo(&ver))
	}
}
Example #4
0
func showVersion(ctx *cli.Context) {
	if len(ctx.Args()) != 0 {
		errExit(ctx, exitHelp, "More arguments than required", true)
	}

	ver := version.Info{}
	if err := getObject(ctx, versionURL(ctx), &ver); err != nil {
		fmt.Printf("Unable to fetch version information\n")
	} else {
		fmt.Printf("Client Version:\n")
		fmt.Printf(version.String())
		fmt.Printf("\n")
		fmt.Printf("Server Version:\n")
		fmt.Printf(version.StringFromInfo(&ver))
	}
}
Example #5
0
func execOpts(opts *cliOpts) {

	if opts.help {
		fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
		flagSet.PrintDefaults()
		os.Exit(0)
	}

	if opts.version {
		fmt.Printf(version.String())
		os.Exit(0)
	}

	log.SetFormatter(&log.TextFormatter{FullTimestamp: true, TimestampFormat: time.StampNano})

	if opts.debug {
		log.SetLevel(log.DebugLevel)
	}
}
Example #6
0
func main() {
	var showVersion bool

	// parse rest of the args that require creating state
	flagSet := flag.NewFlagSet("contivk8s", flag.ExitOnError)

	flagSet.BoolVar(&showVersion,
		"version",
		false,
		"Show version")

	if err := flagSet.Parse(os.Args[1:]); err != nil {
		logger.Fatalf("Failed to parse command. Error: %s", err)
	}
	if showVersion {
		fmt.Printf(version.String())
		os.Exit(0)
	}

	mainfunc()
}
Example #7
0
func execOpts(opts *cliOpts) core.StateDriver {

	if opts.help {
		fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
		flagSet.PrintDefaults()
		os.Exit(0)
	}

	if opts.version {
		fmt.Printf(version.String())
		os.Exit(0)
	}

	log.SetFormatter(&log.TextFormatter{FullTimestamp: true, TimestampFormat: time.StampNano})

	if opts.debug {
		log.SetLevel(log.DebugLevel)
	}

	if err := master.SetClusterMode(opts.clusterMode); err != nil {
		log.Fatalf("Failed to set cluster-mode. Error: %s", err)
	}

	if err := master.SetDNSEnabled(opts.dnsEnabled); err != nil {
		log.Fatalf("Failed to set dns-enable. Error: %s", err)
	}

	sd, err := initStateDriver(opts)
	if err != nil {
		log.Fatalf("Failed to init state-store. Error: %s", err)
	}

	if _, err = resources.NewStateResourceManager(sd); err != nil {
		log.Fatalf("Failed to init resource manager. Error: %s", err)
	}

	return sd
}
Example #8
0
func (d *daemon) execOpts() {
	if err := d.parseOpts(); err != nil {
		log.Fatalf("Failed to parse cli options. Error: %s", err)
	}

	if d.opts.help {
		fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
		flagSet.PrintDefaults()
		os.Exit(0)
	}

	if d.opts.version {
		fmt.Printf(version.String())
		os.Exit(0)
	}

	log.SetFormatter(&log.TextFormatter{FullTimestamp: true, TimestampFormat: time.StampNano})

	if d.opts.debug {
		log.SetLevel(log.DebugLevel)
	}

	if err := master.SetClusterMode(d.opts.clusterMode); err != nil {
		log.Fatalf("Failed to set cluster-mode. Error: %s", err)
	}

	sd, err := initStateDriver(&d.opts)
	if err != nil {
		log.Fatalf("Failed to init state-store. Error: %s", err)
	}

	if _, err = resources.NewStateResourceManager(sd); err != nil {
		log.Fatalf("Failed to init resource manager. Error: %s", err)
	}

	d.stateDriver = sd
}
Example #9
0
func main() {
	var opts cliOpts
	var flagSet *flag.FlagSet

	defHostLabel, err := os.Hostname()
	if err != nil {
		log.Fatalf("Failed to fetch hostname. Error: %s", err)
	}

	// parse rest of the args that require creating state
	flagSet = flag.NewFlagSet("netd", flag.ExitOnError)
	flagSet.BoolVar(&opts.debug,
		"debug",
		false,
		"Show debugging information generated by netplugin")
	flagSet.StringVar(&opts.syslog,
		"syslog",
		"",
		"Log to syslog at proto://ip:port -- use 'kernel' to log via kernel syslog")
	flagSet.BoolVar(&opts.jsonLog,
		"json-log",
		false,
		"Format logs as JSON")
	flagSet.StringVar(&opts.hostLabel,
		"host-label",
		defHostLabel,
		"label used to identify endpoints homed for this host, default is host name. If -config flag is used then host-label must be specified in the the configuration passed.")
	flagSet.StringVar(&opts.pluginMode,
		"plugin-mode",
		"docker",
		"plugin mode docker|kubernetes")
	flagSet.StringVar(&opts.cfgFile,
		"config",
		"",
		"plugin configuration. Use '-' to read configuration from stdin")
	flagSet.StringVar(&opts.vtepIP,
		"vtep-ip",
		"",
		"My VTEP ip address")
	flagSet.StringVar(&opts.ctrlIP,
		"ctrl-ip",
		"",
		"Local ip address to be used for control communication")
	flagSet.StringVar(&opts.vlanIntf,
		"vlan-if",
		"",
		"My VTEP ip address")
	flagSet.BoolVar(&opts.version,
		"version",
		false,
		"Show version")
	flagSet.StringVar(&opts.fwdMode,
		"fwd-mode",
		"bridge",
		"Forwarding Mode")
	flagSet.StringVar(&opts.dbURL,
		"cluster-store",
		"etcd://127.0.0.1:2379",
		"state store url")

	err = flagSet.Parse(os.Args[1:])
	if err != nil {
		log.Fatalf("Failed to parse command. Error: %s", err)
	}

	if opts.version {
		fmt.Printf(version.String())
		os.Exit(0)
	}

	// Make sure we are running as root
	usr, err := user.Current()
	if (err != nil) || (usr.Username != "root") {
		log.Fatalf("This process can only be run as root")
	}

	if opts.debug {
		log.SetLevel(log.DebugLevel)
		os.Setenv("CONTIV_TRACE", "1")
	}

	if opts.jsonLog {
		log.SetFormatter(&log.JSONFormatter{})
	} else {
		log.SetFormatter(&log.TextFormatter{FullTimestamp: true, TimestampFormat: time.StampNano})
	}

	if opts.syslog != "" {
		configureSyslog(opts.syslog)
	}

	if opts.fwdMode != "bridge" && opts.fwdMode != "routing" && opts.fwdMode != "mpls" {
		log.Fatalf("Invalid forwarding mode. Allowed modes are bridge,routing ")
	}

	if flagSet.NFlag() < 1 {
		log.Infof("host-label not specified, using default (%s)", opts.hostLabel)
	}

	// default to using local IP addr
	localIP, err := cluster.GetLocalAddr()
	if err != nil {
		log.Fatalf("Error getting local address. Err: %v", err)
	}
	if opts.ctrlIP == "" {
		opts.ctrlIP = localIP
	}
	if opts.vtepIP == "" {
		opts.vtepIP = opts.ctrlIP
	}

	// parse store URL
	parts := strings.Split(opts.dbURL, "://")
	if len(parts) < 2 {
		log.Fatalf("Invalid cluster-store-url %s", opts.dbURL)
	}
	stateStore := parts[0]

	netPlugin := &plugin.NetPlugin{}

	// initialize the config
	pluginConfig := plugin.Config{
		Drivers: plugin.Drivers{
			Network: "ovs",
			State:   stateStore,
		},
		Instance: core.InstanceInfo{
			HostLabel: opts.hostLabel,
			VtepIP:    opts.vtepIP,
			VlanIntf:  opts.vlanIntf,
			RouterIP:  opts.routerIP,
			FwdMode:   opts.fwdMode,
			DbURL:     opts.dbURL,
		},
	}

	// Initialize service registry plugin
	svcPlugin, quitCh, err := svcplugin.NewSvcregPlugin(opts.dbURL, nil)
	if err != nil {
		log.Fatalf("Error initializing service registry plugin")
	}
	defer close(quitCh)

	// Initialize appropriate plugin
	switch opts.pluginMode {
	case "docker":
		dockplugin.InitDockPlugin(netPlugin, svcPlugin)

	case "kubernetes":
		k8splugin.InitCNIServer(netPlugin)

	default:
		log.Fatalf("Unknown plugin mode -- should be docker | kubernetes")
	}

	// Init the driver plugins..
	err = netPlugin.Init(pluginConfig)
	if err != nil {
		log.Fatalf("Failed to initialize the plugin. Error: %s", err)
	}

	// Process all current state
	processCurrentState(netPlugin, opts)

	// Initialize clustering
	cluster.Init(netPlugin, opts.ctrlIP, opts.vtepIP, opts.dbURL)

	if opts.pluginMode == "kubernetes" {
		k8splugin.InitKubServiceWatch(netPlugin)
	}

	if err := handleEvents(netPlugin, opts); err != nil {
		os.Exit(1)
	}
}
Example #10
0
func main() {
	var opts cliOpts
	var flagSet *flag.FlagSet

	defHostLabel, err := os.Hostname()

	// parse rest of the args that require creating state
	flagSet = flag.NewFlagSet("netplugin", flag.ExitOnError)
	flagSet.BoolVar(&opts.debug,
		"debug",
		false,
		"Show debugging information generated by netplugin")
	flagSet.StringVar(&opts.syslog,
		"syslog",
		"",
		"Log to syslog at proto://ip:port -- use 'kernel' to log via kernel syslog")
	flagSet.BoolVar(&opts.jsonLog,
		"json-log",
		false,
		"Format logs as JSON")
	flagSet.StringVar(&opts.hostLabel,
		"host-label",
		defHostLabel,
		"label used to identify endpoints homed for this host, default is host name. If -config flag is used then host-label must be specified in the the configuration passed.")
	flagSet.StringVar(&opts.pluginMode,
		"plugin-mode",
		"docker",
		"plugin mode docker|kubernetes")
	flagSet.StringVar(&opts.cfgFile,
		"config",
		"",
		"plugin configuration. Use '-' to read configuration from stdin")
	flagSet.StringVar(&opts.vtepIP,
		"vtep-ip",
		"",
		"My VTEP ip address")
	flagSet.StringVar(&opts.ctrlIP,
		"ctrl-ip",
		"",
		"Local ip address to be used for control communication")
	flagSet.StringVar(&opts.vlanIntf,
		"vlan-if",
		"",
		"VLAN uplink interface")
	flagSet.BoolVar(&opts.version,
		"version",
		false,
		"Show version")
	flagSet.StringVar(&opts.dbURL,
		"cluster-store",
		"etcd://127.0.0.1:2379",
		"state store url")

	err = flagSet.Parse(os.Args[1:])
	if err != nil {
		log.Fatalf("Failed to parse command. Error: %s", err)
	}

	if opts.version {
		fmt.Printf(version.String())
		os.Exit(0)
	}

	// Make sure we are running as root
	usr, err := user.Current()
	if (err != nil) || (usr.Username != "root") {
		log.Fatalf("This process can only be run as root")
	}

	if opts.debug {
		log.SetLevel(log.DebugLevel)
		os.Setenv("CONTIV_TRACE", "1")
	}

	if opts.jsonLog {
		log.SetFormatter(&log.JSONFormatter{})
	} else {
		log.SetFormatter(&log.TextFormatter{FullTimestamp: true, TimestampFormat: time.StampNano})
	}

	if opts.syslog != "" {
		configureSyslog(opts.syslog)
	}

	if flagSet.NFlag() < 1 {
		log.Infof("host-label not specified, using default (%s)", opts.hostLabel)
	}

	// default to using local IP addr
	localIP, err := cluster.GetLocalAddr()
	if err != nil {
		log.Fatalf("Error getting local address. Err: %v", err)
	}
	if opts.ctrlIP == "" {
		opts.ctrlIP = localIP
	}
	if opts.vtepIP == "" {
		opts.vtepIP = opts.ctrlIP
	}

	// parse store URL
	parts := strings.Split(opts.dbURL, "://")
	if len(parts) < 2 {
		log.Fatalf("Invalid cluster-store-url %s", opts.dbURL)
	}
	stateStore := parts[0]

	// initialize the config
	pluginConfig := plugin.Config{
		Drivers: plugin.Drivers{
			Network: "ovs",
			State:   stateStore,
		},
		Instance: core.InstanceInfo{
			HostLabel:  opts.hostLabel,
			CtrlIP:     opts.ctrlIP,
			VtepIP:     opts.vtepIP,
			VlanIntf:   opts.vlanIntf,
			DbURL:      opts.dbURL,
			PluginMode: opts.pluginMode,
		},
	}

	// Create a new agent
	ag := agent.NewAgent(&pluginConfig)

	// Process all current state
	ag.ProcessCurrentState()

	// post initialization processing
	ag.PostInit()

	// handle events
	if err := ag.HandleEvents(); err != nil {
		log.Infof("Netplugin exiting due to error: %v", err)
		os.Exit(1)
	}
}
Example #11
0
func main() {
	var opts cliOpts
	var flagSet *flag.FlagSet

	defHostLabel, err := os.Hostname()
	if err != nil {
		log.Fatalf("Failed to fetch hostname. Error: %s", err)
	}

	// parse rest of the args that require creating state
	flagSet = flag.NewFlagSet("netd", flag.ExitOnError)
	flagSet.BoolVar(&opts.debug,
		"debug",
		false,
		"Show debugging information generated by netplugin")
	flagSet.StringVar(&opts.syslog,
		"syslog",
		"",
		"Log to syslog at proto://ip:port -- use 'kernel' to log via kernel syslog")
	flagSet.BoolVar(&opts.jsonLog,
		"json-log",
		false,
		"Format logs as JSON")
	flagSet.StringVar(&opts.hostLabel,
		"host-label",
		defHostLabel,
		"label used to identify endpoints homed for this host, default is host name. If -config flag is used then host-label must be specified in the the configuration passed.")
	flagSet.StringVar(&opts.pluginMode,
		"plugin-mode",
		"docker",
		"plugin mode docker|kubernetes")
	flagSet.StringVar(&opts.cfgFile,
		"config",
		"",
		"plugin configuration. Use '-' to read configuration from stdin")
	flagSet.StringVar(&opts.vtepIP,
		"vtep-ip",
		"",
		"My VTEP ip address")
	flagSet.StringVar(&opts.ctrlIP,
		"ctrl-ip",
		"",
		"Local ip address to be used for control communication")
	flagSet.StringVar(&opts.vlanIntf,
		"vlan-if",
		"",
		"My VTEP ip address")
	flagSet.BoolVar(&opts.version,
		"version",
		false,
		"Show version")
	flagSet.StringVar(&opts.routerIP,
		"router-ip",
		"",
		"My Router ip address")
	flagSet.StringVar(&opts.fwdMode,
		"fwd-mode",
		"bridge",
		"Forwarding Mode")

	err = flagSet.Parse(os.Args[1:])
	if err != nil {
		log.Fatalf("Failed to parse command. Error: %s", err)
	}

	if opts.version {
		fmt.Printf(version.String())
		os.Exit(0)
	}

	// Make sure we are running as root
	usr, err := user.Current()
	if (err != nil) || (usr.Username != "root") {
		log.Fatalf("This process can only be run as root")
	}

	if opts.debug {
		log.SetLevel(log.DebugLevel)
		os.Setenv("CONTIV_TRACE", "1")
	}

	if opts.jsonLog {
		log.SetFormatter(&log.JSONFormatter{})
	} else {
		log.SetFormatter(&log.TextFormatter{FullTimestamp: true, TimestampFormat: time.StampNano})
	}

	if opts.syslog != "" {
		configureSyslog(opts.syslog)
	}

	if opts.fwdMode != "bridge" && opts.fwdMode != "routing" {
		log.Infof("Invalid forwarding mode. Setting the mode to bridge ")
		opts.fwdMode = "bridge"
	}

	if flagSet.NFlag() < 1 {
		log.Infof("host-label not specified, using default (%s)", opts.hostLabel)
	}

	// default to using local IP addr
	localIP, err := cluster.GetLocalAddr()
	if err != nil {
		log.Fatalf("Error getting local address. Err: %v", err)
	}
	if opts.vtepIP == "" {
		opts.vtepIP = localIP
	}
	if opts.ctrlIP == "" {
		opts.ctrlIP = localIP
	}

	defConfigStr := fmt.Sprintf(`{
                    "drivers" : {
                       "network": %q,
                       "state": "etcd"
                    },
                    "plugin-instance": {
                       "host-label": %q,
						"vtep-ip": %q,
						"vlan-if": %q,
						"router-ip":%q,
						"fwdMode":%q
                    },
                    %q : {
                       "dbip": "127.0.0.1",
                       "dbport": 6640
                    },
                    "etcd" : {
                        "machines": ["http://127.0.0.1:4001"]
                    },
                    "docker" : {
                        "socket" : "unix:///var/run/docker.sock"
                    }
                  }`, utils.OvsNameStr, opts.hostLabel, opts.vtepIP,
		opts.vlanIntf, opts.routerIP, opts.fwdMode, utils.OvsNameStr)

	netPlugin := &plugin.NetPlugin{}

	config := []byte{}
	if opts.cfgFile == "" {
		log.Infof("config not specified, using default config")
		config = []byte(defConfigStr)
	} else if opts.cfgFile == "-" {
		reader := bufio.NewReader(os.Stdin)
		config, err = ioutil.ReadAll(reader)
		if err != nil {
			log.Fatalf("reading config from stdin failed. Error: %s", err)
		}
	} else {
		config, err = ioutil.ReadFile(opts.cfgFile)
		if err != nil {
			log.Fatalf("reading config from file failed. Error: %s", err)
		}
	}

	// Parse the config
	pluginConfig := plugin.Config{}
	err = json.Unmarshal([]byte(config), &pluginConfig)
	if err != nil {
		log.Fatalf("Error parsing config. Err: %v", err)
	}

	// extract host-label from the configuration
	if pluginConfig.Instance.HostLabel == "" {
		log.Fatalf("Empty host-label passed in configuration")
	}
	opts.hostLabel = pluginConfig.Instance.HostLabel

	// Use default values when config options are not specified
	if pluginConfig.Instance.VtepIP == "" {
		pluginConfig.Instance.VtepIP = opts.vtepIP
	}
	if pluginConfig.Instance.VlanIntf == "" {
		pluginConfig.Instance.VlanIntf = opts.vlanIntf
	}
	if pluginConfig.Instance.RouterIP == "" {
		pluginConfig.Instance.RouterIP = opts.routerIP
	}
	if pluginConfig.Instance.FwdMode == "" {
		pluginConfig.Instance.FwdMode = opts.fwdMode
	}

	svcplugin.QuitCh = make(chan struct{})
	defer close(svcplugin.QuitCh)

	// Initialize appropriate plugin
	switch opts.pluginMode {
	case "docker":
		dockplugin.InitDockPlugin(netPlugin)

	case "kubernetes":
		k8splugin.InitCNIServer(netPlugin)

	default:
		log.Fatalf("Unknown plugin mode -- should be docker | kubernetes")
	}

	// Init the driver plugins..
	err = netPlugin.Init(pluginConfig, string(config))
	if err != nil {
		log.Fatalf("Failed to initialize the plugin. Error: %s", err)
	}

	// Process all current state
	processCurrentState(netPlugin, opts)

	// Initialize clustering
	cluster.Init(netPlugin, opts.ctrlIP)

	//logger := log.New(os.Stdout, "go-etcd: ", log.LstdFlags)
	//etcd.SetLogger(logger)

	if err := handleEvents(netPlugin, opts); err != nil {
		os.Exit(1)
	}
}