示例#1
0
func (s *SpecSuite) TestNewCircuitBreakerSerializationCycle(c *C) {
	cl, err := NewSpec(
		`LatencyAtQuantileMS(50.0) < 20`,
		`{"Type": "response", "Action": {"StatusCode": 400, "Body": "Come back later"}}`,
		`{"Type": "webhook", "Action": {"URL": "http://localhost", "Method": "POST", "Form": {"Key": ["Val"]}}}`,
		`{"Type": "webhook", "Action": {"URL": "http://localhost", "Method": "POST", "Form": {"Key": ["Val"]}}}`,
		defaultFallbackDuration,
		defaultRecoveryDuration,
		defaultCheckPeriod,
	)

	bytes, err := json.Marshal(cl)
	c.Assert(err, IsNil)

	r := plugin.NewRegistry()
	c.Assert(r.AddSpec(GetSpec()), IsNil)

	spec := r.GetSpec(GetSpec().Type)
	c.Assert(spec, NotNil)

	out, err := spec.FromJSON(bytes)
	c.Assert(err, IsNil)
	c.Assert(out, NotNil)

	spec2 := out.(*Spec)
	c.Assert(spec2.Fallback, NotNil)
	c.Assert(spec2.OnTripped, NotNil)
	c.Assert(spec2.OnStandby, NotNil)
}
示例#2
0
func GetRegistry() (*plugin.Registry, error) {
	r := plugin.NewRegistry()

	specs := []*plugin.MiddlewareSpec{

		connlimit.GetSpec(),

		ratelimit.GetSpec(),

		rewrite.GetSpec(),

		cbreaker.GetSpec(),

		trace.GetSpec(),

		joblogger.GetSpec(),
	}

	for _, spec := range specs {
		if err := r.AddSpec(spec); err != nil {
			return nil, err
		}
	}
	return r, nil
}
示例#3
0
func (s *SpecSuite) TestNewCircuitBreakerFromJSONEmptyStrings(c *C) {
	r := plugin.NewRegistry()
	c.Assert(r.AddSpec(GetSpec()), IsNil)

	bytes := []byte(`{
                 "Condition":"LatencyAtQuantileMS(50.0) < 20",
                 "Fallback":{"Type": "response", "Action": {"StatusCode": 400, "Body": "Come back later"}},
                 "OnTripped": "",
                 "OnStandby": "",
                 "FallbackDuration": 10000000000,
                 "RecoveryDuration": 10000000000,
                 "CheckPeriod": 100000000}`)

	spec := r.GetSpec(GetSpec().Type)
	c.Assert(spec, NotNil)

	out, err := spec.FromJSON(bytes)
	c.Assert(err, IsNil)
	c.Assert(out, NotNil)

	spec2 := out.(*Spec)
	c.Assert(spec2.Fallback, NotNil)
	c.Assert(spec2.OnTripped, Equals, "")
	c.Assert(spec2.OnStandby, Equals, "")
}
示例#4
0
func TestSpecIsOK(t *testing.T) {
	t.Log("Add Locale Middleware spec to Vulcan registry")

	err := plugin.NewRegistry().AddSpec(GetSpec())
	if err != nil {
		t.Errorf("Expected to be able to add spec but got error %+v", err)
	}
}
示例#5
0
func (s *BackendSuite) MiddlewareFromJSON(c *C) {
	cl, err := connlimit.NewConnLimit(10, "client.ip")
	c.Assert(err, IsNil)

	m := &Middleware{Id: "c1", Type: "connlimit", Middleware: cl}

	bytes, err := json.Marshal(m)
	c.Assert(err, IsNil)

	out, err := MiddlewareFromJSON(bytes, plugin.NewRegistry().GetSpec)
	c.Assert(err, IsNil)
	c.Assert(out, NotNil)
	c.Assert(out, DeepEquals, m)
}
示例#6
0
func (s *BackendSuite) TestFrontendsFromJSON(c *C) {
	f, err := NewHTTPFrontend(route.NewMux(), "f1", "b1", `Path("/path")`, HTTPFrontendSettings{})
	c.Assert(err, IsNil)

	bytes, err := json.Marshal(f)

	fs := []Frontend{*f}

	bytes, err = json.Marshal(map[string]interface{}{"Frontends": fs})

	r := plugin.NewRegistry()
	c.Assert(r.AddSpec(connlimit.GetSpec()), IsNil)

	out, err := FrontendsFromJSON(route.NewMux(), bytes)
	c.Assert(err, IsNil)
	c.Assert(out, NotNil)
	c.Assert(out, DeepEquals, fs)
}
示例#7
0
文件: registry.go 项目: huhoo/vulcand
func GetRegistry() *plugin.Registry {
	r := plugin.NewRegistry()

	specs := []*plugin.MiddlewareSpec{
		ratelimit.GetSpec(),
		connlimit.GetSpec(),
		rewrite.GetSpec(),
		cbreaker.GetSpec(),
		trace.GetSpec(),
	}

	for _, spec := range specs {
		if err := r.AddSpec(spec); err != nil {
			panic(err)
		}
	}

	return r
}
示例#8
0
func (s *SpecSuite) TestNewCircuitBreakerFromJSONDefaults(c *C) {
	r := plugin.NewRegistry()
	c.Assert(r.AddSpec(GetSpec()), IsNil)

	bytes := []byte(`{
                 "Condition":"LatencyAtQuantileMS(50.0) < 20",
                 "Fallback":{"Type": "response", "Action": {"StatusCode": 400, "Body": "Come back later"}}}`)

	spec := r.GetSpec(GetSpec().Type)
	c.Assert(spec, NotNil)

	out, err := spec.FromJSON(bytes)
	c.Assert(err, IsNil)
	c.Assert(out, NotNil)

	spec2 := out.(*Spec)
	c.Assert(spec2.Fallback, NotNil)
	c.Assert(spec2.OnTripped, IsNil)
	c.Assert(spec2.OnStandby, IsNil)
}
示例#9
0
// One of the most important tests:
// Make sure the RateLimit spec is compatible and will be accepted by middleware registry
func (s *ConnLimitSuite) TestSpecIsOK(c *C) {
	c.Assert(plugin.NewRegistry().AddSpec(GetSpec()), IsNil)
}
示例#10
0
func main() {

	flag.Parse()
	log.Println("Connecting to kubernetes via url " + kServer)
	log.Println("Connecting to vulcand via url " + vServer)
	log.Println("Provided label query: " + labelQuery)
	log.Println("Observing endpoints within namespace: " + namespace)

	vClient := vClient.NewClient(vServer, vPlugin.NewRegistry())
	kClient, err := kClient.New(&kClient.Config{Host: kServer})

	if err != nil {
		log.Println("Error encountered when connecting to kubernetes api." + err.Error())
		panic(err)
	}

	var labelSelector labels.Selector = nil

	if labelQuery != "" {
		labelSelector, err = labels.Parse(labelQuery)
		if err != nil {
			log.Println("Error parsing the provided label query.")
			panic(err)
		}
	} else {
		labelSelector = labels.Everything()
	}

	socket, err := kClient.Endpoints(namespace).
		Watch(labelSelector, fields.Everything(), api.ListOptions{Watch: true})

	if err != nil {
		log.Println("Error obtaining a watch on the kubernetes endpoints.")
		panic(err)
	}

	// poll the channel indefinitely
	for {

		select {
		case event := <-socket.ResultChan():
			switch event.Type {
			case watch.Added:
				endpoint, _ := deserialize(event.Object)
				ensureEndpointConfiguredForVulcand(vClient, endpoint)
				log.Println("Endpoint was added: \n" + endpoint.Name)
			case watch.Modified:
				endpoint, _ := deserialize(event.Object)
				ensureEndpointConfiguredForVulcand(vClient, endpoint)
				log.Println("Endpoint was modified: \n" + endpoint.Name)
			case watch.Deleted:
				endpoint, _ := deserialize(event.Object)
				removeUnusedEndpointsFromVulcand(vClient, endpoint)
				log.Println("Endpoint was deleted: \n" + endpoint.Name)
			case watch.Error:
				log.Println("Encountered an error from the endpoints socket. Continuing...")
			}
		default:
			time.Sleep(1 * time.Second)
		}

	}

}