mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-09 18:27:01 +00:00
Add internal API for the nfd-worker custom feature source rule configuration. This API has already diverged from the NFD NodeFeatureRule API in that annotations, extended resources or taints are not supported. This patch basically copies the Rule type (and it's sub-types) from the nfdv1alpha1 package. It also adds conversion functions from the internal rule API to the "external" nfdv1alpha1 API. This is done to use the same rule matching functionality (from the nfdv1alpha1 package). One notable remark is that the feature source rule config supports some custom formatting (short forms, multi-type fields) that relies on special json/yaml unmarshalling functions that are better to nuke from the nfdv1alpha1 package (in another patch). These (legacy) syntax specialities are most probably used by nobody but let's keep them as they're already there. Unit tests to cover the custom json unmarshalling are now added.
111 lines
3.2 KiB
Go
111 lines
3.2 KiB
Go
/*
|
|
Copyright 2023 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package api
|
|
|
|
import (
|
|
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
|
|
)
|
|
|
|
// convertFeaturematchertermToV1alpha1 converts the internal api type to nfdv1alpha1.
|
|
func convertFeaturematchertermToV1alpha1(in *FeatureMatcherTerm, out *nfdv1alpha1.FeatureMatcherTerm) error {
|
|
out.Feature = in.Feature
|
|
if in.MatchExpressions != nil {
|
|
inME := in.MatchExpressions
|
|
outME := make(nfdv1alpha1.MatchExpressionSet, len(*inME))
|
|
for key := range *inME {
|
|
me := &nfdv1alpha1.MatchExpression{}
|
|
if err := convertMatchexpressionToV1alpha1((*inME)[key], me); err != nil {
|
|
return err
|
|
}
|
|
outME[key] = me
|
|
}
|
|
out.MatchExpressions = &outME
|
|
} else {
|
|
out.MatchExpressions = nil
|
|
}
|
|
|
|
if in.MatchName != nil {
|
|
out.MatchName = &nfdv1alpha1.MatchExpression{}
|
|
if err := convertMatchexpressionToV1alpha1(in.MatchName, out.MatchName); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
out.MatchName = nil
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// convertMatchanyelemToV1alpha1 converts the internal api type to nfdv1alpha1.
|
|
func convertMatchanyelemToV1alpha1(in *MatchAnyElem, out *nfdv1alpha1.MatchAnyElem) error {
|
|
if in.MatchFeatures != nil {
|
|
inMF, outMF := &in.MatchFeatures, &out.MatchFeatures
|
|
*outMF = make(nfdv1alpha1.FeatureMatcher, len(*inMF))
|
|
for i := range *inMF {
|
|
if err := convertFeaturematchertermToV1alpha1(&(*inMF)[i], &(*outMF)[i]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
} else {
|
|
out.MatchFeatures = nil
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// convertMatchexpressionToV1alpha1 converts the internal api type to nfdv1alpha1.
|
|
func convertMatchexpressionToV1alpha1(in *MatchExpression, out *nfdv1alpha1.MatchExpression) error {
|
|
out.Op = nfdv1alpha1.MatchOp(in.Op)
|
|
if in.Value != nil {
|
|
in, out := &in.Value, &out.Value
|
|
*out = make(nfdv1alpha1.MatchValue, len(*in))
|
|
copy(*out, *in)
|
|
} else {
|
|
out.Value = nil
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ConvertRuleToV1alpha1 converts the internal api type to nfdv1alpha1.
|
|
func ConvertRuleToV1alpha1(in *Rule, out *nfdv1alpha1.Rule) error {
|
|
out.Name = in.Name
|
|
out.Labels = in.Labels
|
|
out.LabelsTemplate = in.LabelsTemplate
|
|
out.Vars = in.Vars
|
|
out.VarsTemplate = in.VarsTemplate
|
|
if in.MatchFeatures != nil {
|
|
in, out := &in.MatchFeatures, &out.MatchFeatures
|
|
*out = make(nfdv1alpha1.FeatureMatcher, len(*in))
|
|
for i := range *in {
|
|
if err := convertFeaturematchertermToV1alpha1(&(*in)[i], &(*out)[i]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
} else {
|
|
out.MatchFeatures = nil
|
|
}
|
|
if in.MatchAny != nil {
|
|
in, out := &in.MatchAny, &out.MatchAny
|
|
*out = make([]nfdv1alpha1.MatchAnyElem, len(*in))
|
|
for i := range *in {
|
|
if err := convertMatchanyelemToV1alpha1(&(*in)[i], &(*out)[i]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
} else {
|
|
out.MatchAny = nil
|
|
}
|
|
return nil
|
|
}
|