Skip to content

Commit

Permalink
simplify logic
Browse files Browse the repository at this point in the history
Signed-off-by: Jesse Nelson <jesse@swirldslabs.com>
  • Loading branch information
jnels124 committed Oct 27, 2023
1 parent d4e59c8 commit 3eeccb2
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions pkg/mgmt/zfsnode/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package zfsnode
import (
"context"
"fmt"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
Expand Down Expand Up @@ -66,39 +65,33 @@ func Start(controllerMtx *sync.RWMutex, stopCh <-chan struct{}) error {
options.FieldSelector = fields.OneTermEqualSelector("metadata.name", zfs.NodeID).String()
}))

var k8sNode *v1.Node
nodeName := os.Getenv("OPENEBS_NODE_NAME")
if nodeName != zfs.NodeID {
topologyRequirement, requirementError := labels.NewRequirement(zfs.ZFSTopologyKey, selection.Equals, []string{zfs.NodeID})
if requirementError != nil {
return errors.Wrapf(requirementError, "Unable to retrieve node by %s for node id %s", zfs.ZFSTopologyKey, zfs.NodeID)
}
topologySelector := labels.NewSelector().Add(*topologyRequirement).String()
klog.Infof("The topology selector is %s", topologySelector)

k8sNodeCandidates, listError := kubeClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{
LabelSelector: topologySelector,
})

if listError != nil {
return errors.Wrapf(err, "fetch k8s node %s", zfs.NodeID)
}

if k8sNodeCandidates == nil || len(k8sNodeCandidates.Items) != 1 {
return fmt.Errorf("unable to retrieve a single node by %s for %s", zfs.ZFSTopologyKey, zfs.NodeID)
}

k8sNode = &k8sNodeCandidates.Items[0]
var searchLabel string
if nodeName == zfs.NodeID {
searchLabel = zfs.ZFSTopoNodenameKey
} else {
byName, byNameErr := kubeClient.CoreV1().Nodes().Get(context.TODO(), zfs.NodeID, metav1.GetOptions{})
searchLabel = zfs.ZFSTopologyKey
}
topologyRequirement, requirementError := labels.NewRequirement(searchLabel, selection.Equals, []string{zfs.NodeID})
if requirementError != nil {
return errors.Wrapf(requirementError, "Unable to retrieve node by %s for node id %s", zfs.ZFSTopologyKey, zfs.NodeID)
}
topologySelector := labels.NewSelector().Add(*topologyRequirement).String()
klog.Infof("The topology selector is %s", topologySelector)

k8sNodeCandidates, err := kubeClient.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{
LabelSelector: topologySelector,
})

if byNameErr != nil {
return errors.Wrapf(err, "fetch k8s node %s", zfs.NodeID)
}
if err != nil {
return errors.Wrapf(err, "fetch k8s node %s", zfs.NodeID)
}

k8sNode = byName
if k8sNodeCandidates == nil || len(k8sNodeCandidates.Items) != 1 {
return fmt.Errorf("unable to retrieve a single node by %s for %s", zfs.ZFSTopologyKey, zfs.NodeID)
}

k8sNode := k8sNodeCandidates.Items[0]
isTrue := true
// as object returned by client go clears all TypeMeta from it.
nodeGVK := &schema.GroupVersionKind{
Expand Down

0 comments on commit 3eeccb2

Please sign in to comment.