-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding nfd feature rule package and new builder #882
Open
ggordaniRed
wants to merge
1
commit into
openshift-kni:main
Choose a base branch
from
ggordaniRed:addingnfdrule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,198 @@ | ||
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 | ||
} | ||
|
||
// GetErrorMessage returns builder's error message. | ||
func (builder *NodeFeatureRuleBuilder) GetErrorMessage() string { | ||
return builder.errorMsg | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the k8s go.mod changes have to happen here? We have an automated job to keep them up to date.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add new package by run make and make install maybe in the process this lib version changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sebrandon1 It seems that NFD is not part of the sync job, thus it is causing this problem. What is currently the procedure? Should @ggordaniRed add config for the sync ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely sure how that sync all works. @kononovn?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't need a sync config for this operator unless it's causing issues with dependencies. There doesn't seem to be any actual changes between 0.30.5 and 0.30.7 of these libraries though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay sounds good. 👍