func matches()

in pkg/filter/filter.go [225:292]


func matches(d *objectData, o *Options) bool {
	if o == nil {
		return true
	}

	matchesKinds := true
	if len(o.Kinds) > 0 {
		matchesKinds = false
		for _, optk := range o.Kinds {
			objKind := d.kind
			if strings.ContainsRune(optk, ',') {
				// Assume this is a Qualified Kind match of the form
				// "apps/v1beta1,Deployment". Commas shouldn't be normally in a kind.
				objKind = d.apiVersion + "," + d.kind
			}
			if optk == objKind {
				matchesKinds = true
				break
			}
		}
	}
	matchesNS := true
	if len(o.Namespaces) > 0 {
		matchesNS = false
		for _, optn := range o.Namespaces {
			if optn == d.meta.Namespace {
				matchesNS = true
				break
			}
		}
	}
	matchesNames := true
	if len(o.Names) > 0 {
		matchesNames = false
		for _, optn := range o.Names {
			if optn == d.meta.GetName() {
				matchesNames = true
				break
			}
		}
	}
	matchesAnnot := true
	if len(o.Annotations) > 0 {
		matchesAnnot = false
		for key, v := range o.Annotations {
			if val, ok := d.meta.Annotations[key]; ok && val == v {
				matchesAnnot = true
				break
			}
		}
	}
	matchesLabels := true
	if len(o.Labels) > 0 {
		matchesLabels = false
		for key, v := range o.Labels {
			if val, ok := d.meta.Labels[key]; ok && val == v {
				matchesLabels = true
				break
			}
		}
	}

	matches := matchesKinds && matchesNS && matchesNames && matchesAnnot && matchesLabels
	if o.InvertMatch {
		return !matches
	}
	return matches
}