func()

in pkg/gcptarget/gcptarget.go [51:92]


func (h *GCPTarget) ToMatcher(constraint *unstructured.Unstructured) (constraints.Matcher, error) {
	match, ok, err := unstructured.NestedMap(constraint.Object, "spec", "match")
	if err != nil {
		return nil, fmt.Errorf("unable to get spec.match: %w", err)
	}
	if !ok {
		return &matcher{ancestries: []string{"**"}, excludedAncestries: []string{}}, nil
	}

	include, ok, err := unstructured.NestedStringSlice(match, "ancestries")
	if err != nil {
		return nil, fmt.Errorf("unable to get string slice from spec.match.ancestries: %w", err)
	}
	if !ok {
		include, ok, err = unstructured.NestedStringSlice(match, "target")
		if err != nil {
			return nil, fmt.Errorf("unable to get string slice from spec.match.target: %w", err)
		}
		if !ok {
			include = []string{"**"}
		}
	}

	exclude, ok, err := unstructured.NestedStringSlice(match, "excludedAncestries")
	if err != nil {
		return nil, fmt.Errorf("unable to get string slice from spec.match.excludedAncestries: %w", err)
	}
	if !ok {
		exclude, ok, err = unstructured.NestedStringSlice(match, "exclude")
		if err != nil {
			return nil, fmt.Errorf("unable to get string slice from spec.match.exclude: %w", err)
		}
		if !ok {
			exclude = []string{}
		}
	}

	return &matcher{
		ancestries:         include,
		excludedAncestries: exclude,
	}, nil
}