Skip to content

Commit

Permalink
Validate CR/node name from the node names in the cluster
Browse files Browse the repository at this point in the history
We want to validate that CR has been created correctly. Also UT functionality
  • Loading branch information
razo7 committed May 11, 2023
1 parent bc20084 commit 1ac183e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
9 changes: 5 additions & 4 deletions controllers/fenceagentsremediation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ func (r *FenceAgentsRemediationReconciler) Reconcile(ctx context.Context, req ct
r.Log.Error(err, "failed to get FAR CR")
return emptyResult, err
}
// TODO: Validate FAR CR name to nodeName. Run isNodeNameValid
// Validate FAR CR name to match a nodeName from the cluster
if err := farUtils.IsNodeNameValid(r.Client, req.Name); err != nil {
return emptyResult, err
}

// Fetch the FAR's pod
r.Log.Info("Fetch FAR's pod")
pod, err := farUtils.GetFenceAgentsRemediationPod(r.Client)
Expand All @@ -99,7 +103,6 @@ func (r *FenceAgentsRemediationReconciler) Reconcile(ctx context.Context, req ct
//TODO: better seperation between errors from wrong shared parameters values and wrong node parameters values
return emptyResult, err
}

return emptyResult, nil
}

Expand Down Expand Up @@ -131,5 +134,3 @@ func appendParamToSlice(fenceAgentParams []string, paramName v1alpha1.ParameterN
}
return fenceAgentParams
}

// TODO: Add isNodeNameValid function which call listNodeNames to validate the FAR's name with the cluster node names
42 changes: 38 additions & 4 deletions controllers/fenceagentsremediation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ import (
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/medik8s/fence-agents-remediation/api/v1alpha1"
farUtils "github.com/medik8s/fence-agents-remediation/pkg/utils"
)

const (
Expand Down Expand Up @@ -70,8 +72,8 @@ var _ = Describe("FAR Controller", func() {
underTestFAR = newFenceAgentsRemediation(validNodeName, fenceAgentIPMI, testShareParam, testNodeParam)

Context("Functionality", func() {
Context("buildFenceAgentParams", func() {
When("FAR's name isn't a node name", func() {
Context("buildFenceAgentParams - check CR name", func() {
When("FAR's name doesn't match a node name", func() {
It("should fail", func() {
underTestFAR.ObjectMeta.Name = dummyNodeName
_, err := buildFenceAgentParams(underTestFAR)
Expand All @@ -84,6 +86,25 @@ var _ = Describe("FAR Controller", func() {
})
})
})

Context("IsNodeNameValid - check node object", func() {
var node *v1.Node
BeforeEach(func() {
node = getNode(validNodeName)
Expect(k8sClient.Create(context.Background(), node)).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(k8sClient.Delete(context.Background(), node)).NotTo(HaveOccurred())
})
When("FAR's name doesn't match an existing node name", func() {
It("should fail", func() {
Expect(farUtils.IsNodeNameValid(k8sClient, dummyNodeName)).To(HaveOccurred())
})
It("should succeed", func() {
Expect(farUtils.IsNodeNameValid(k8sClient, validNodeName)).NotTo(HaveOccurred())
})
})
})
When("creating a resource", func() {
It("should fail when FAR pod is missing", func() {
//Test getFenceAgentsPod func
Expand All @@ -92,15 +113,18 @@ var _ = Describe("FAR Controller", func() {
})
Context("Reconcile", func() {
//Scenarios

var node *v1.Node
BeforeEach(func() {
fenceAgentsPod = buildFarPod()
// Create fenceAgentsPod and FAR
node = getNode(validNodeName)
// Create node, fenceAgentsPod and FAR
Expect(k8sClient.Create(context.Background(), node)).NotTo(HaveOccurred())
Expect(k8sClient.Create(context.Background(), fenceAgentsPod)).NotTo(HaveOccurred())
Expect(k8sClient.Create(context.Background(), underTestFAR)).NotTo(HaveOccurred())
})

AfterEach(func() {
Expect(k8sClient.Delete(context.Background(), node)).NotTo(HaveOccurred())
Expect(k8sClient.Delete(context.Background(), fenceAgentsPod)).NotTo(HaveOccurred())
Expect(k8sClient.Delete(context.Background(), underTestFAR)).NotTo(HaveOccurred())
})
Expand Down Expand Up @@ -128,6 +152,16 @@ func newFenceAgentsRemediation(nodeName string, agent string, sharedparameters m
}
}

// used for making new node object for test and have a unique resourceVersion
// getNode returns a node object with the name nodeName
func getNode(nodeName string) *corev1.Node {
return &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
},
}
}

// buildFarPod builds a dummy pod with FAR label and namespace
func buildFarPod() *corev1.Pod {
fenceAgentsPod := &corev1.Pod{}
Expand Down
25 changes: 25 additions & 0 deletions pkg/utils/nodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package utils

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// isNodeNameValid returns an error if nodeName doesn't match any node name int the cluster, otherwise a nil
func IsNodeNameValid(r client.Reader, nodeName string) error {
logger := ctrl.Log.WithName("utils-nodes")
node := &corev1.Node{}
objNodeName := types.NamespacedName{Name: nodeName}
err := r.Get(context.TODO(), objNodeName, node)
if err != nil {
errMsg := fmt.Sprintf("node %s is not part of the cluster's node names", nodeName)
logger.Error(err, errMsg)
return err
}
return nil
}

0 comments on commit 1ac183e

Please sign in to comment.