コード例 #1
0
func (h *syncHandler) replyWithLabels(event *events.Event, cli *client.RancherClient, labels map[string]string) error {
	reply := util.NewReply(event)
	reply.ResourceType = event.ResourceType
	reply.ResourceId = event.ResourceID
	reply.Data = map[string]interface{}{
		"instance": map[string]interface{}{
			"+data": map[string]interface{}{
				"+fields": map[string]interface{}{
					"+labels": labels,
				},
			},
		},
	}
	logrus.WithField("eventID", event.ID).Infof("Reply: %+v", reply)
	return util.PublishReply(reply, cli)
}
コード例 #2
0
func (h *syncHandler) Handler(event *revents.Event, cli *client.RancherClient) error {
	log.Infof("Received event: Name: %s, Event Id: %s, Resource Id: %s", event.Name, event.Id, event.ResourceId)

	namespace, name := h.getPod(event)
	if namespace == "" || name == "" {
		if err := util.CreateAndPublishReply(event, cli); err != nil {
			return err
		}
		return nil
	}

	pod, err := h.kClient.Pod.ByName(namespace, name)
	if err != nil {
		log.Warnf("Error looking up pod: %#v", err)
		if apiErr, ok := err.(*client.ApiError); ok && apiErr.StatusCode == 404 {
			if err := util.CreateAndPublishReply(event, cli); err != nil {
				return err
			}
			return nil
		}
		return err
	}

	/*
	   Building this:
	   {instancehostmap: {
	       instance: {
	           +data: {
	               +fields: {
	                 labels: {
	*/
	replyData := make(map[string]interface{})
	ihm := make(map[string]interface{})
	i := make(map[string]interface{})
	data := make(map[string]interface{})
	fields := make(map[string]interface{})
	labels := make(map[string]string)

	replyData["instanceHostMap"] = ihm
	ihm["instance"] = i
	i["+data"] = data
	data["+fields"] = fields
	fields["+labels"] = labels

	for key, v := range pod.Metadata.Labels {
		if val, ok := v.(string); ok {
			labels[key] = val
		}
	}

	labels["io.rancher.service.deployment.unit"] = pod.Metadata.Uid
	labels["io.rancher.stack.name"] = pod.Metadata.Namespace

	reply := util.NewReply(event)
	reply.ResourceType = "instanceHostMap"
	reply.ResourceId = event.ResourceId
	reply.Data = replyData
	log.Infof("Reply: %+v", reply)
	err = util.PublishReply(reply, cli)
	if err != nil {
		return err
	}
	return nil
}