1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-28 02:37:11 +00:00

apis/nfd: migrate to structured logging

This commit is contained in:
Markus Lehtonen 2023-05-03 15:59:39 +03:00
parent 6e3b181ab4
commit 64d5af016e
2 changed files with 11 additions and 29 deletions

View file

@ -210,18 +210,15 @@ func (m *MatchExpression) MatchKeys(name string, keys map[string]Nil) (bool, err
return false, fmt.Errorf("invalid Op %q when matching keys", m.Op)
}
if klog.V(3).Enabled() {
mString := map[bool]string{false: "no match", true: "match found"}[matched]
if klogV := klog.V(3); klogV.Enabled() {
klogV.InfoS("matched keys", "matchResult", "matched", "matchKey", name, "matchOp", m.Op)
} else if klogV := klog.V(4); klogV.Enabled() {
k := make([]string, 0, len(keys))
for n := range keys {
k = append(k, n)
}
sort.Strings(k)
if len(keys) < 10 || klog.V(4).Enabled() {
klog.Infof("%s when matching %q %q against %s", mString, name, m.Op, strings.Join(k, " "))
} else {
klog.Infof("%s when matching %q %q against %s... (list truncated)", mString, name, m.Op, strings.Join(k[0:10], ", "))
}
klogV.InfoS("matched keys", "matchResult", "matched", "matchKey", name, "matchOp", m.Op, "inputKeys", k)
}
return matched, nil
}
@ -234,25 +231,10 @@ func (m *MatchExpression) MatchValues(name string, values map[string]string) (bo
return false, err
}
if klog.V(3).Enabled() {
mString := map[bool]string{false: "no match", true: "match found"}[matched]
keys := make([]string, 0, len(values))
for k := range values {
keys = append(keys, k)
}
sort.Strings(keys)
kv := make([]string, len(keys))
for i, k := range keys {
kv[i] = k + ":" + values[k]
}
if len(values) < 10 || klog.V(4).Enabled() {
klog.Infof("%s when matching %q %q %v against %s", mString, name, m.Op, m.Value, strings.Join(kv, " "))
} else {
klog.Infof("%s when matching %q %q %v against %s... (list truncated)", mString, name, m.Op, m.Value, strings.Join(kv[0:10], " "))
}
if klogV := klog.V(3); klogV.Enabled() {
klogV.InfoS("matched values", "matchResult", "matched", "matchKey", name, "matchOp", m.Op, "matchValue", m.Value)
} else if klogV := klog.V(4); klogV.Enabled() {
klogV.InfoS("matched values", "matchResult", "matched", "matchKey", name, "matchOp", m.Op, "matchValue", m.Value, "inputValues", values)
}
return matched, nil

View file

@ -68,7 +68,7 @@ func (r *Rule) Execute(features *Features) (RuleOutput, error) {
}
}
if !matched {
klog.V(2).Infof("rule %q did not match", r.Name)
klog.V(2).InfoS("rule did not match", "ruleName", r.Name)
return RuleOutput{}, nil
}
}
@ -77,7 +77,7 @@ func (r *Rule) Execute(features *Features) (RuleOutput, error) {
if isMatch, matches, err := r.MatchFeatures.match(features); err != nil {
return RuleOutput{}, err
} else if !isMatch {
klog.V(2).Infof("rule %q did not match", r.Name)
klog.V(2).InfoS("rule did not match", "ruleName", r.Name)
return RuleOutput{}, nil
} else {
utils.KlogDump(4, "matches for matchFeatures "+r.Name, " ", matches)
@ -169,7 +169,7 @@ func (m *FeatureMatcher) match(features *Features) (bool, matchedFeatures, error
nameSplit := strings.SplitN(term.Feature, ".", 2)
if len(nameSplit) != 2 {
klog.Warning("feature %q not of format <domain>.<feature>, cannot be used for templating", term.Feature)
klog.InfoS("invalid feature name (not <domain>.<feature>), cannot be used for templating", "featureName", term.Feature)
nameSplit = []string{featureName, ""}
}