Ejemplo n.º 1
0
// NewAgent creates a new netplugin agent
func NewAgent(pluginConfig *plugin.Config) *Agent {
	opts := pluginConfig.Instance
	netPlugin := &plugin.NetPlugin{}

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

	// init cluster state
	err = cluster.Init(opts.DbURL)
	if err != nil {
		log.Fatalf("Error initializing cluster. Err: %v", err)
	}

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

	case "kubernetes":
		k8splugin.InitCNIServer(netPlugin)

	case "test":
		// nothing to do. internal mode for testing
	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)
	}

	// init mesos plugin
	mesosplugin.InitPlugin(netPlugin)

	// create a new agent
	agent := &Agent{
		netPlugin:    netPlugin,
		pluginConfig: pluginConfig,
		svcPlugin:    svcPlugin,
		svcQuitCh:    quitCh,
	}

	return agent
}
Ejemplo n.º 2
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)
	}
}
Ejemplo n.º 3
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)
	}
}