Пример #1
0
func TestServicePluginInitWithDefaultConfig(t *testing.T) {
	bConfig := bridge.DefaultBridgeConfig()
	reg, err := InitServicePlugin("skydns2:", bConfig)
	if err != nil || reg == nil {
		t.Fatalf("Service plugin init failed. Error: %s", err)
	}
}
Пример #2
0
func TestServicePluginInitWithInvalidRefreshDetails(t *testing.T) {
	bConfig := bridge.DefaultBridgeConfig()

	bConfig.RefreshTTL = 0
	bConfig.RefreshInterval = 30
	reg, err := InitServicePlugin("skydns2:", bConfig)
	if err != nil || reg == nil {
		t.Fatalf("Service plugin init failed for values {%v}. Error: %s", bConfig, err)
	}

	bConfig.RefreshTTL = 30
	bConfig.RefreshInterval = 0
	reg, err = InitServicePlugin("skydns2:", bConfig)
	if err != nil || reg == nil {
		t.Fatalf("Service plugin init failed for values {%v}. Error: %s", bConfig, err)
	}

	bConfig.RefreshTTL = 30
	bConfig.RefreshInterval = 20
	reg, err = InitServicePlugin("skydns2:", bConfig)
	if err != nil || reg == nil {
		t.Fatalf("Service plugin init failed for values {%v}. Error: %s", bConfig, err)
	}

	bConfig.RefreshTTL = -30
	bConfig.RefreshInterval = -20
	reg, err = InitServicePlugin("skydns2:", bConfig)
	if err != nil || reg == nil {
		t.Fatalf("Service plugin init failed for values {%v}. Error: %s", bConfig, err)
	}
}
Пример #3
0
func TestServicePluginInitWithInvalidDeregisterDetails(t *testing.T) {
	bConfig := bridge.DefaultBridgeConfig()

	bConfig.DeregisterCheck = "random"
	reg, err := InitServicePlugin("skydns2:", bConfig)
	if err != nil || reg == nil {
		t.Fatalf("Service plugin init failed for values {%v}. Error: %s", bConfig, err)
	}
}
Пример #4
0
// InitDockPlugin initializes the docker plugin
func InitDockPlugin(netplugin *plugin.NetPlugin) error {
	netPlugin = netplugin
	hostname, err := os.Hostname()
	if err != nil {
		log.Fatalf("Could not retrieve hostname: %v", err)
	}

	log.Debugf("Configuring router")

	router := mux.NewRouter()
	s := router.Headers("Accept", "application/vnd.docker.plugins.v1.1+json").
		Methods("POST").Subrouter()

	dispatchMap := map[string]func(http.ResponseWriter, *http.Request){
		"/Plugin.Activate":                    activate(hostname),
		"/Plugin.Deactivate":                  deactivate(hostname),
		"/NetworkDriver.GetCapabilities":      getCapability(),
		"/NetworkDriver.CreateNetwork":        createNetwork(),
		"/NetworkDriver.DeleteNetwork":        deleteNetwork(),
		"/NetworkDriver.CreateEndpoint":       createEndpoint(hostname),
		"/NetworkDriver.DeleteEndpoint":       deleteEndpoint(hostname),
		"/NetworkDriver.EndpointOperInfo":     endpointInfo,
		"/NetworkDriver.Join":                 join(),
		"/NetworkDriver.Leave":                leave(),
		"/IpamDriver.GetDefaultAddressSpaces": getDefaultAddressSpaces(),
		"/IpamDriver.RequestPool":             requestPool(),
		"/IpamDriver.ReleasePool":             releasePool(),
		"/IpamDriver.RequestAddress":          requestAddress(),
		"/IpamDriver.ReleaseAddress":          releaseAddress(),
	}

	for dispatchPath, dispatchFunc := range dispatchMap {
		s.HandleFunc(dispatchPath, logHandler(dispatchPath, dispatchFunc))
	}

	s.HandleFunc("/NetworkDriver.{*}", unknownAction)
	s.HandleFunc("/IpamDriver.{*}", unknownAction)

	driverPath := path.Join(pluginPath, driverName) + ".sock"
	os.Remove(driverPath)
	os.MkdirAll(pluginPath, 0700)

	go func() {
		l, err := net.ListenUnix("unix", &net.UnixAddr{Name: driverPath, Net: "unix"})
		if err != nil {
			panic(err)
		}

		log.Infof("docker plugin listening on %s", driverPath)
		http.Serve(l, router)
		l.Close()
		log.Infof("docker plugin closing %s", driverPath)
	}()

	// Adding bridge Config temporarily to check service refresh
	bConfig := bridge.DefaultBridgeConfig()
	bConfig.RefreshTTL = 15
	bConfig.RefreshInterval = 10
	dnsBridge, err = svcplugin.InitServicePlugin("skydns2:", bConfig)

	return nil
}
Пример #5
0
// InitServicePlugin creates a new bridge to Service plugin
func InitServicePlugin(bridgeType string, bridgeCfg ...bridge.Config) (*bridge.Bridge, error) {

	log.Printf("Initing service plugin")
	var bConfig bridge.Config

	defaultBridgeConfig := bridge.DefaultBridgeConfig()
	if len(bridgeCfg) == 0 {
		bConfig = defaultBridgeConfig
	} else {
		bConfig = bridgeCfg[0]
		if bConfig.HostIP != "" {
			log.Println("Forcing host IP to", bConfig.HostIP)
		}

		if bConfig.RefreshTTL < 0 {
			log.WithFields(log.Fields{
				"RefreshTTL": bConfig.RefreshTTL,
			}).Warn("RefreshTTL must be greater than 0.",
				"Setting to default value of ",
				defaultBridgeConfig.RefreshTTL,
				" and continuing")
			bConfig.RefreshTTL = 0
		}
		if bConfig.RefreshInterval < 0 {
			log.WithFields(log.Fields{
				"RefreshInterval": bConfig.RefreshInterval,
			}).Warn("RefreshInterval must be greater than 0.",
				"Setting to default value of ",
				defaultBridgeConfig.RefreshInterval,
				" and continuing")
			bConfig.RefreshInterval = 0
		}
		if (bConfig.RefreshTTL == 0 && bConfig.RefreshInterval > 0) ||
			(bConfig.RefreshTTL > 0 && bConfig.RefreshInterval == 0) {
			log.WithFields(log.Fields{
				"RefreshTTL":      bConfig.RefreshTTL,
				"RefreshInterval": bConfig.RefreshInterval,
			}).Warn("RefreshTTL and RefreshInterval ",
				"must be specified together or not at all. ",
				"Setting to default values (",
				defaultBridgeConfig.RefreshTTL,
				",", defaultBridgeConfig.RefreshInterval,
				") and continuing")
			bConfig.RefreshTTL = defaultBridgeConfig.RefreshTTL
			bConfig.RefreshInterval = defaultBridgeConfig.RefreshInterval
		} else if bConfig.RefreshTTL > 0 &&
			bConfig.RefreshTTL <= bConfig.RefreshInterval {
			log.WithFields(log.Fields{
				"RefreshTTL":      bConfig.RefreshTTL,
				"RefreshInterval": bConfig.RefreshInterval,
			}).Warn("RefreshTTL must be greater than RefreshInterval",
				". Setting to default values (",
				defaultBridgeConfig.RefreshTTL,
				",", defaultBridgeConfig.RefreshInterval,
				") and continuing")
			bConfig.RefreshTTL = defaultBridgeConfig.RefreshTTL
			bConfig.RefreshInterval = defaultBridgeConfig.RefreshInterval
		}

		if bConfig.DeregisterCheck != "always" &&
			bConfig.DeregisterCheck != "on-success" {
			log.WithFields(log.Fields{
				"DegisterCheck": bConfig.DeregisterCheck,
			}).Warn("Deregister must be \"always\" or ",
				"\"on-success\". Setting to default value of ",
				defaultBridgeConfig.DeregisterCheck,
				" and continuing")
			bConfig.DeregisterCheck = defaultBridgeConfig.DeregisterCheck
		}
	}

	log.Info("Creating a new bridge: ", bridgeType)

	b, err := bridge.New(bridgeType, bConfig)

	if err != nil {
		log.WithFields(log.Fields{
			"Error": err,
		}).Error("Bridge creation errored out. ",
			"Service registration will not work.")
		return nil, err
	}

	// TODO: Add proper checks for retryInterval, retryAttempts
	attempt := 0
	for bConfig.RetryAttempts == -1 || attempt <= bConfig.RetryAttempts {
		log.Infof("Connecting to backend (%v/%v)",
			attempt, bConfig.RetryAttempts)

		err = b.Ping()
		if err == nil {
			break
		}

		if err != nil && attempt == bConfig.RetryAttempts {
			log.Errorf("Service plugin not connecting to backend %v", err)
		}

		time.Sleep(time.Duration(bConfig.RetryInterval) * time.Millisecond)
		attempt++
	}

	// Start the TTL refresh timer
	if bConfig.RefreshInterval > 0 {
		ticker := time.NewTicker(time.Duration(bConfig.RefreshInterval) * time.Second)
		go func() {
			for {
				select {
				case <-ticker.C:
					b.Refresh()
				case <-QuitCh:
					log.Infof("Quit service plugin")
					ticker.Stop()
					return
				}
			}
		}()
	}

	return b, nil
}