Skip to content

Commit

Permalink
controller: add nodeOnChange mechanism
Browse files Browse the repository at this point in the history
    That will help us to sync to the Blockdevice CR if we
    directly change the disk tag on the Longhorn node.

Signed-off-by: Vicente Cheng <[email protected]>
  • Loading branch information
Vicente-Cheng committed Dec 6, 2023
1 parent e19b8f9 commit 79dab1a
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pkg/controller/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package node

import (
"context"
"reflect"
"strings"

longhornv1 "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand All @@ -15,6 +19,7 @@ import (

type Controller struct {
namespace string
nodeName string

BlockDevices ctldiskv1.BlockDeviceController
BlockDeviceCache ctldiskv1.BlockDeviceCache
Expand All @@ -30,15 +35,67 @@ func Register(ctx context.Context, nodes ctllonghornv1.NodeController, bds ctldi

c := &Controller{
namespace: opt.Namespace,
nodeName: opt.NodeName,
Nodes: nodes,
BlockDevices: bds,
BlockDeviceCache: bds.Cache(),
}

nodes.OnChange(ctx, blockDeviceNodeHandlerName, c.OnNodeChange)
nodes.OnRemove(ctx, blockDeviceNodeHandlerName, c.OnNodeDelete)
return nil
}

// OnChange watch the node CR on change and sync up to block device CR
func (c *Controller) OnNodeChange(_ string, node *longhornv1.Node) (*longhornv1.Node, error) {
if node == nil || node.DeletionTimestamp != nil {
logrus.Debugf("Skip this round because the node will be deleted or not created")
return nil, nil
}
if c.nodeName != node.Name {
logrus.Debugf("Skip this round because the CRD node name %s is not belong to this node %s", node.Name, c.nodeName)
return nil, nil
}

for name, disk := range node.Spec.Disks {
// default disk does not included in block device CR
if strings.HasPrefix(name, "default-disk") {
continue
}

logrus.Debugf("Prepare to checking block device %s", name)
bd, err := c.BlockDevices.Get(c.namespace, name, metav1.GetOptions{})
if err != nil {
logrus.Warnf("Get block device %s failed: %v", name, err)
return node, err
}

if bd.Spec.Tags == nil || len(bd.Spec.Tags) == 0 {
continue
}

bdCpy := bd.DeepCopy()
newTags := make([]string, 0)

for _, tag := range bd.Spec.Tags {
if slices.Contains(disk.Tags, tag) {
newTags = append(newTags, tag)
}
}
bdCpy.Spec.Tags = newTags
bdCpy.Status.Tags = newTags

if !reflect.DeepEqual(bd, bdCpy) {
logrus.Debugf("Update block device %s tags (Status) from %v to %v", bd.Name, bd.Status.Tags, newTags)
if _, err := c.BlockDevices.Update(bdCpy); err != nil {
logrus.Warnf("Update block device %s failed: %v", bd.Name, err)
return node, err
}
}
}
return nil, nil
}

// OnNodeDelete watch the node CR on remove and delete node related block devices
func (c *Controller) OnNodeDelete(_ string, node *longhornv1.Node) (*longhornv1.Node, error) {
if node == nil || node.DeletionTimestamp == nil {
Expand Down

0 comments on commit 79dab1a

Please sign in to comment.