From 36aad9b0bb20bf3537a4a511fe1fcdd0cfaec45b Mon Sep 17 00:00:00 2001 From: Vicente Cheng Date: Mon, 4 Dec 2023 19:45:48 +0800 Subject: [PATCH] controller: add nodeOnChange mechanism 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 --- pkg/controller/node/node.go | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/pkg/controller/node/node.go b/pkg/controller/node/node.go index abd239ca..791a65bd 100644 --- a/pkg/controller/node/node.go +++ b/pkg/controller/node/node.go @@ -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" @@ -15,6 +19,7 @@ import ( type Controller struct { namespace string + nodeName string BlockDevices ctldiskv1.BlockDeviceController BlockDeviceCache ctldiskv1.BlockDeviceCache @@ -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 {