-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding nfd feature rule package and new builder
- Loading branch information
Guy Gordani
committed
Jan 22, 2025
1 parent
272f025
commit 28d8007
Showing
15 changed files
with
4,107 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package nfd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/golang/glog" | ||
"github.com/openshift-kni/eco-goinfra/pkg/clients" | ||
"github.com/openshift-kni/eco-goinfra/pkg/msg" | ||
nfdv1 "github.com/openshift/node-feature-discovery/api/nfd/v1alpha1" | ||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
goclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// NodeFeatureRuleBuilder provides a struct for NodeFeatureRule object | ||
// from the cluster and a NodeFeatureRule definition. | ||
type NodeFeatureRuleBuilder struct { | ||
// Builder definition. Used to create | ||
// Builder object with minimum set of required elements. | ||
Definition *nfdv1.NodeFeatureRule | ||
// Created Builder object on the cluster. | ||
Object *nfdv1.NodeFeatureRule | ||
// api client to interact with the cluster. | ||
apiClient *clients.Settings | ||
// errorMsg is processed before Builder object is created. | ||
errorMsg string | ||
} | ||
|
||
// NewnodeFeatureRuleBuilderFromObjectString creates a Builder object from CSV alm-examples. | ||
func NewnodeFeatureRuleBuilderFromObjectString(apiClient *clients.Settings, almExample string) *NodeFeatureRuleBuilder { | ||
glog.V(100).Infof( | ||
"Initializing new Builder structure from almExample string") | ||
|
||
if apiClient == nil { | ||
glog.V(100).Info("The apiClient of the Policy is nil") | ||
|
||
return nil | ||
} | ||
|
||
err := apiClient.AttachScheme(nfdv1.AddToScheme) | ||
if err != nil { | ||
glog.V(100).Info("Failed to add nfd v1 scheme to client schemes") | ||
|
||
return nil | ||
} | ||
|
||
nodeFeatureRule, err := getNodeFeatureRuleFromAlmExample(almExample) | ||
|
||
glog.V(100).Infof( | ||
"Initializing Builder definition to NodeFeatureRule object") | ||
|
||
nodeFeatureRuleBuilder := NodeFeatureRuleBuilder{ | ||
apiClient: apiClient, | ||
Definition: nodeFeatureRule, | ||
Object: nodeFeatureRule, | ||
} | ||
|
||
if err != nil { | ||
glog.V(100).Infof( | ||
"Error initializing NodeFeatureRule from alm-examples: %s", err.Error()) | ||
|
||
nodeFeatureRuleBuilder.errorMsg = fmt.Sprintf("Error initializing NodeFeatureRule from alm-examples: %s", | ||
err.Error()) | ||
} | ||
|
||
if nodeFeatureRuleBuilder.Definition == nil { | ||
glog.V(100).Infof("The NodeFeatureRule object definition is nil") | ||
|
||
nodeFeatureRuleBuilder.errorMsg = "NodeFeatureRule definition is nil" | ||
} | ||
|
||
return &nodeFeatureRuleBuilder | ||
} | ||
|
||
// getNodeFeatureRuleFromAlmExample extracts the NodeFeatureRule from the alm-examples block. | ||
func getNodeFeatureRuleFromAlmExample(almExample string) (*nfdv1.NodeFeatureRule, error) { | ||
nodeFeatureRuleList := &nfdv1.NodeFeatureRuleList{} | ||
|
||
if almExample == "" { | ||
return nil, fmt.Errorf("almExample is an empty string") | ||
} | ||
|
||
err := json.Unmarshal([]byte(almExample), &nodeFeatureRuleList.Items) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(nodeFeatureRuleList.Items) == 0 { | ||
return nil, fmt.Errorf("failed to get alm examples") | ||
} | ||
|
||
for i, item := range nodeFeatureRuleList.Items { | ||
if item.Kind == "NodeFeatureRule" { | ||
return &nodeFeatureRuleList.Items[i], nil | ||
} | ||
} | ||
return nil, fmt.Errorf("NodeFeatureRule is missing in alm-examples ") | ||
} | ||
|
||
// Create makes a NodeFeatureRule in the cluster and stores the created object in struct. | ||
func (builder *NodeFeatureRuleBuilder) Create() (*NodeFeatureRuleBuilder, error) { | ||
if valid, err := builder.validate(); !valid { | ||
return builder, err | ||
} | ||
|
||
glog.V(100).Infof("Creating the NodeFeatureRule %s in namespace %s", builder.Definition.Name, | ||
builder.Definition.Namespace) | ||
|
||
var err error | ||
if !builder.Exists() { | ||
err = builder.apiClient.Create(context.TODO(), builder.Definition) | ||
|
||
if err == nil { | ||
builder.Object = builder.Definition | ||
} | ||
} | ||
|
||
return builder, err | ||
} | ||
|
||
// Exists checks whether the given NodeFeatureRule exists. | ||
func (builder *NodeFeatureRuleBuilder) Exists() bool { | ||
if valid, _ := builder.validate(); !valid { | ||
return false | ||
} | ||
|
||
glog.V(100).Infof( | ||
"Checking if NodeFeatureRule %s exists in namespace %s", builder.Definition.Name, | ||
builder.Definition.Namespace) | ||
|
||
var err error | ||
builder.Object, err = builder.Get() | ||
|
||
if err != nil { | ||
glog.V(100).Infof("Failed to collect NodeFeatureRule object due to %s", err.Error()) | ||
} | ||
|
||
return err == nil || !k8serrors.IsNotFound(err) | ||
} | ||
|
||
// validate will check that the builder and builder definition are properly initialized before | ||
// accessing any member fields. | ||
func (builder *NodeFeatureRuleBuilder) validate() (bool, error) { | ||
resourceCRD := "NodeFeatureRule" | ||
|
||
if builder == nil { | ||
glog.V(100).Infof("The %s builder is uninitialized", resourceCRD) | ||
|
||
return false, fmt.Errorf("error: received nil %s builder", resourceCRD) | ||
} | ||
|
||
if builder.Definition == nil { | ||
glog.V(100).Infof("The %s is undefined", resourceCRD) | ||
|
||
return false, fmt.Errorf(msg.UndefinedCrdObjectErrString(resourceCRD)) | ||
} | ||
|
||
if builder.apiClient == nil { | ||
glog.V(100).Infof("The %s builder apiclient is nil", resourceCRD) | ||
|
||
return false, fmt.Errorf("%s builder cannot have nil apiClient", resourceCRD) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// Get returns NodeFeatureRule object if found. | ||
func (builder *NodeFeatureRuleBuilder) Get() (*nfdv1.NodeFeatureRule, error) { | ||
if valid, err := builder.validate(); !valid { | ||
return nil, err | ||
} | ||
|
||
glog.V(100).Infof("Collecting NodeFeatureRule object %s in namespace %s", | ||
builder.Definition.Name, builder.Definition.Namespace) | ||
|
||
NodeFeatureRule := &nfdv1.NodeFeatureRule{} | ||
err := builder.apiClient.Get(context.TODO(), goclient.ObjectKey{ | ||
Name: builder.Definition.Name, | ||
Namespace: builder.Definition.Namespace, | ||
}, NodeFeatureRule) | ||
|
||
if err != nil { | ||
glog.V(100).Infof("NodeFeatureRule object %s does not exist in namespace %s", | ||
builder.Definition.Name, builder.Definition.Namespace) | ||
|
||
return nil, err | ||
} | ||
|
||
return NodeFeatureRule, err | ||
} | ||
|
||
func (builder *NodeFeatureRuleBuilder) GetErrorMessage() string { | ||
return builder.errorMsg | ||
} |
Oops, something went wrong.