示例#1
0
func (s *ListenerTestSuite) TestSyncHandler(c *check.C) {
	pod, containers, err := s.createPod(c)
	if err != nil {
		c.Fatal(err)
	}

	log.Info(containers)

	container := containers[0]
	event := &revents.Event{
		ReplyTo: "event-1",
		ID:      "event-id-1",
		Data: map[string]interface{}{
			"instanceHostMap": map[string]interface{}{
				"instance": map[string]interface{}{
					"externalId": container.ID,
					"data": map[string]interface{}{
						"fields": map[string]interface{}{
							"labels": map[string]interface{}{
								"io.kubernetes.pod.namespace":  "default",
								"io.kubernetes.pod.name":       "pod-test-1",
								"io.kubernetes.container.name": "POD",
								"io.kubernetes.pod.uid":        pod.Metadata.Uid,
							},
						},
					},
				},
			},
		},
	}
	sh := eventhandlers.NewProvideLablesHandler(s.kClient)

	err = sh.Handler(event, s.mockRClient)
	if err != nil {
		c.Fatal(err)
	}

	pub := <-s.publishChan
	c.Assert(pub.Name, check.Equals, "event-1")
	c.Assert(pub.PreviousIds, check.DeepEquals, []string{"event-id-1"})
	instance := get(pub.Data, "instance")
	data := get(instance, "+data")
	fields := get(data, "+fields")
	newLabels := get(fields, "+labels")
	c.Assert(newLabels, check.DeepEquals,
		map[string]string{
			"env": "dev",
			"io.rancher.service.deployment.unit": pod.Metadata.Uid,
			"io.rancher.stack.name":              "default",
			"io.rancher.container.display_name":  "pod-test-1",
			"io.rancher.container.network":       "true",
			"io.rancher.service.launch.config":   "io.rancher.service.primary.launch.config",
		})
}
示例#2
0
func ConnectToEventStream(conf config.Config) error {

	kClient := kubernetesclient.NewClient(conf.KubernetesURL, false)

	eventHandlers := map[string]revents.EventHandler{
		"compute.instance.providelabels": eventhandlers.NewProvideLablesHandler(kClient).Handler,
		"config.update":                  eventhandlers.NewPingHandler().Handler,
		"ping":                           eventhandlers.NewPingHandler().Handler,
	}

	router, err := revents.NewEventRouter("", 0, conf.CattleURL, conf.CattleAccessKey, conf.CattleSecretKey, nil, eventHandlers, "", conf.WorkerCount, revents.DefaultPingConfig)
	if err != nil {
		return err
	}
	err = router.StartWithoutCreate(nil)
	return err
}