mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-14 20:56:42 +00:00
apis/nfd: drop the private template caching fields
Drop the private fields – that were supposed to be used for caching parsed templates – from the Rule type. Keep the API typedefs cleaner and simpler. Moreover, the caching was not even used in practice, effectively complicating code without any benefit: the way the types are used in nfd-master creates a local copy of Rule type storing the cached template in the copy, wasting it from any future users. There are also other possible caveats in caching like we tried to do it. For example the objects returned by the api lister are supposed to be treated as read-only - in particular if we would be to modify them there should at least be proper locking in place as nfd-master potentially processes the same rule (the same Go object) in parallel for multiple nodes. If any optimization like this will be pursued it should be done properly, probably with private type(s) at the consumer's end, not contaminating the API types.
This commit is contained in:
parent
443ff80748
commit
b2d9e15a00
4 changed files with 9 additions and 47 deletions
|
@ -112,15 +112,12 @@ func (r *Rule) executeLabelsTemplate(in matchedFeatures, out map[string]string)
|
|||
return nil
|
||||
}
|
||||
|
||||
if r.labelsTemplate == nil {
|
||||
t, err := newTemplateHelper(r.LabelsTemplate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse LabelsTemplate: %w", err)
|
||||
}
|
||||
r.labelsTemplate = t
|
||||
th, err := newTemplateHelper(r.LabelsTemplate)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse LabelsTemplate: %w", err)
|
||||
}
|
||||
|
||||
labels, err := r.labelsTemplate.expandMap(in)
|
||||
labels, err := th.expandMap(in)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to expand LabelsTemplate: %w", err)
|
||||
}
|
||||
|
@ -134,15 +131,13 @@ func (r *Rule) executeVarsTemplate(in matchedFeatures, out map[string]string) er
|
|||
if r.VarsTemplate == "" {
|
||||
return nil
|
||||
}
|
||||
if r.varsTemplate == nil {
|
||||
t, err := newTemplateHelper(r.VarsTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.varsTemplate = t
|
||||
|
||||
th, err := newTemplateHelper(r.VarsTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vars, err := r.varsTemplate.expandMap(in)
|
||||
vars, err := th.expandMap(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -216,22 +211,6 @@ func newTemplateHelper(name string) (*templateHelper, error) {
|
|||
return &templateHelper{template: tmpl}, nil
|
||||
}
|
||||
|
||||
// DeepCopy is a stub to augment the auto-generated code
|
||||
func (h *templateHelper) DeepCopy() *templateHelper {
|
||||
if h == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(templateHelper)
|
||||
h.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is a stub to augment the auto-generated code
|
||||
func (h *templateHelper) DeepCopyInto(out *templateHelper) {
|
||||
// HACK: just re-use the template
|
||||
out.template = h.template
|
||||
}
|
||||
|
||||
func (h *templateHelper) execute(data interface{}) (string, error) {
|
||||
var tmp bytes.Buffer
|
||||
if err := h.template.Execute(&tmp, data); err != nil {
|
||||
|
|
|
@ -369,17 +369,14 @@ var-2=
|
|||
assert.Equal(t, map[string]string{"foo": "bar"}, m.Labels, "instances should have matched")
|
||||
assert.Empty(t, m.Vars)
|
||||
|
||||
r2.labelsTemplate = nil
|
||||
r2.LabelsTemplate = "foo"
|
||||
_, err = r2.Execute(f)
|
||||
assert.Error(t, err)
|
||||
|
||||
r2.labelsTemplate = nil
|
||||
r2.LabelsTemplate = "{{"
|
||||
_, err = r2.Execute(f)
|
||||
assert.Error(t, err)
|
||||
|
||||
r2.labelsTemplate = nil
|
||||
r2.LabelsTemplate = ""
|
||||
r2.VarsTemplate = "bar=baz"
|
||||
m, err = r2.Execute(f)
|
||||
|
@ -387,12 +384,10 @@ var-2=
|
|||
assert.Empty(t, m.Labels)
|
||||
assert.Equal(t, map[string]string{"bar": "baz"}, m.Vars, "instances should have matched")
|
||||
|
||||
r2.varsTemplate = nil
|
||||
r2.VarsTemplate = "bar"
|
||||
_, err = r2.Execute(f)
|
||||
assert.Error(t, err)
|
||||
|
||||
r2.varsTemplate = nil
|
||||
r2.VarsTemplate = "{{"
|
||||
_, err = r2.Execute(f)
|
||||
assert.Error(t, err)
|
||||
|
|
|
@ -178,10 +178,6 @@ type Rule struct {
|
|||
// MatchAny specifies a list of matchers one of which must match.
|
||||
// +optional
|
||||
MatchAny []MatchAnyElem `json:"matchAny"`
|
||||
|
||||
// private helpers/cache for handling golang templates
|
||||
labelsTemplate *templateHelper `json:"-"`
|
||||
varsTemplate *templateHelper `json:"-"`
|
||||
}
|
||||
|
||||
// MatchAnyElem specifies one sub-matcher of MatchAny.
|
||||
|
|
|
@ -503,14 +503,6 @@ func (in *Rule) DeepCopyInto(out *Rule) {
|
|||
(*in)[i].DeepCopyInto(&(*out)[i])
|
||||
}
|
||||
}
|
||||
if in.labelsTemplate != nil {
|
||||
in, out := &in.labelsTemplate, &out.labelsTemplate
|
||||
*out = (*in).DeepCopy()
|
||||
}
|
||||
if in.varsTemplate != nil {
|
||||
in, out := &in.varsTemplate, &out.varsTemplate
|
||||
*out = (*in).DeepCopy()
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Rule.
|
||||
|
|
Loading…
Add table
Reference in a new issue