controller: Fix NPE issue after the node update failure
If node update fails, the syncDefaultDisk() function will return nil,
then the node pointer in the main sync function will become empty
pointer.
@@ -462,9 +462,8 @@ func (nc *NodeController) syncNode(key string) (err error) {
return nil
}
- // sync default Disk on labeled Nodes
- node, err = nc.syncDefaultDisk(node)
- if err != nil {
+ // sync default disks on labeled Nodes
+ if err = nc.syncDefaultDisk(node); err != nil {
return err
}
@@ -551,15 +550,15 @@ func (nc *NodeController) enqueueKubernetesNode(n *v1.Node) {
// syncDefaultDisk handles creation of the default Disk if Create Default Disk on Labeled Nodes is enabled. This allows
// for the default Disk to be created even if the Node has been labeled after initial registration with Longhorn,
// provided that there are no existing Disks remaining on the Node.
-func (nc *NodeController) syncDefaultDisk(node *longhorn.Node) (*longhorn.Node, error) {
+func (nc *NodeController) syncDefaultDisk(node *longhorn.Node) error {
requireLabel, err := nc.ds.GetSettingAsBool(types.SettingNameCreateDefaultDiskLabeledNodes)
if err != nil {
- return nil, err
+ return err
}
if requireLabel && len(node.Spec.Disks) == 0 {
kubeNode, err := nc.ds.GetKubernetesNode(node.Name)
if err != nil {
- return nil, err
+ return err
}
if val, ok := kubeNode.Labels[types.NodeCreateDefaultDiskLabel]; ok {
createDisk, err := strconv.ParseBool(val)
@@ -568,14 +567,19 @@ func (nc *NodeController) syncDefaultDisk(node *longhorn.Node) (*longhorn.Node,
types.NodeCreateDefaultDiskLabel, val, err)
} else if createDisk {
if err := nc.ds.CreateDefaultDisk(node); err != nil {
- return nil, err
+ return err
}
//TODO Find a way to move this outside the controller since it changes the node's spec
- return nc.ds.UpdateNode(node)
+ updatedNode, err := nc.ds.UpdateNode(node)
+ if err != nil {
+ return err
+ }
+ node = updatedNode
+ return nil
}
}
}
- return node, nil
+ return nil
}
func (nc *NodeController) syncDiskStatus(node *longhorn.Node) error {
