Ejemplo n.º 1
0
func (h *httpApplicator) GetMatches(selector labels.Selector, labelType Type) ([]Labeled, error) {
	params := url.Values{}
	params.Add("selector", selector.String())
	params.Add("type", labelType.String())

	// Make value copy of URL; don't want to mutate the URL in the struct.
	urlToGet := *h.matchesEndpoint
	urlToGet.RawQuery = params.Encode()

	resp, err := h.client.Get(urlToGet.String())
	if err != nil {
		return []Labeled{}, err
	}

	defer resp.Body.Close()

	matches := []string{}
	decoder := json.NewDecoder(resp.Body)
	err = decoder.Decode(&matches)
	if err != nil {
		return []Labeled{}, err
	}

	labeled := make([]Labeled, len(matches))

	for i, s := range matches {
		labeled[i] = Labeled{
			ID:        s,
			LabelType: labelType,
			Labels:    labels.Set{},
		}
	}

	return labeled, nil
}
Ejemplo n.º 2
0
func (c *consulApplicator) GetMatches(selector labels.Selector, labelType Type) ([]Labeled, error) {
	// TODO: Label selector result caching
	allMatches, _, err := c.kv.List(path.Join(labelRoot, labelType.String()), nil)
	if err != nil {
		return nil, err
	}

	res := []Labeled{}
	for _, kvp := range allMatches {
		l, err := convertKVPToLabeled(kvp)
		if err != nil {
			return res, err
		}
		if selector.Matches(l.Labels) {
			res = append(res, l)
		}
	}
	return res, nil
}
Ejemplo n.º 3
0
func (app *fakeApplicator) GetMatches(selector labels.Selector, labelType Type) ([]Labeled, error) {
	forType, ok := app.data[labelType]
	if !ok {
		return []Labeled{}, nil
	}

	results := []Labeled{}

	for id, set := range forType {
		if selector.Matches(set) {
			results = append(results, Labeled{
				ID:        id,
				LabelType: labelType,
				Labels:    set,
			})
		}
	}

	return results, nil
}