Skip to content

Commit

Permalink
controller: should respect the StorageReserved setting of Longhorn
Browse files Browse the repository at this point in the history
    - also update the rbac to get the longhorn settings

Signed-off-by: Vicente Cheng <[email protected]>
  • Loading branch information
Vicente-Cheng committed Jan 29, 2024
1 parent 7f597b0 commit bc0e823
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rules:
resources: [ "blockdevices" ]
verbs: [ "*" ]
- apiGroups: [ "longhorn.io" ]
resources: [ "nodes" ]
resources: [ "nodes", "settings" ]
verbs: [ "get", "list", "watch", "update", "patch" ]
- apiGroups: [ "" ]
resources: [ "configmaps", "events" ]
Expand Down
34 changes: 32 additions & 2 deletions pkg/controller/blockdevice/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import (
"os"
"path/filepath"
"reflect"
"strconv"
"sync"
"time"

gocommon "github.com/harvester/go-common"
ghwutil "github.com/jaypipes/ghw/pkg/util"
longhornv1 "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
lhclientset "github.com/longhorn/longhorn-manager/k8s/pkg/client/clientset/versioned"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/rest"

diskv1 "github.com/harvester/node-disk-manager/pkg/apis/harvesterhci.io/v1beta1"
"github.com/harvester/node-disk-manager/pkg/block"
Expand All @@ -29,7 +32,9 @@ import (
)

const (
blockDeviceHandlerName = "harvester-block-device-handler"
blockDeviceHandlerName = "harvester-block-device-handler"
longhornNS = "longhorn-system"
longhornStorageReservedSettings = "storage-reserved-percentage-for-default-disk"
)

// semaphore is a simple semaphore implementation in channel
Expand Down Expand Up @@ -460,12 +465,28 @@ func (c *Controller) provisionDeviceToNode(device *diskv1.BlockDevice) error {
return err
}

lhclient, err := getLonghornClientset()
if err != nil {
return err
}
storageReservedRatio := 30
storageReservedRatioObj, err := lhclient.LonghornV1beta2().Settings(longhornNS).Get(context.TODO(), string(longhornStorageReservedSettings), metav1.GetOptions{})
if err != nil {
logrus.Warnf("Failed to get longhorn setting %s: %v", longhornStorageReservedSettings, err)
} else {
logrus.Debugf("Get longhorn setting %s: %v", longhornStorageReservedSettings, storageReservedRatio)
value, err := strconv.Atoi(storageReservedRatioObj.Value)
if err == nil {
storageReservedRatio = value
}
}

nodeCpy := node.DeepCopy()
diskSpec := longhornv1.DiskSpec{
Path: extraDiskMountPoint(device),
AllowScheduling: true,
EvictionRequested: false,
StorageReserved: 0,
StorageReserved: int64(device.Status.DeviceStatus.Capacity.SizeBytes * uint64(storageReservedRatio) / 100),
Tags: device.Spec.Tags,
}

Expand Down Expand Up @@ -494,6 +515,7 @@ func (c *Controller) provisionDeviceToNode(device *diskv1.BlockDevice) error {
if !updated || !diskv1.DiskAddedToNode.IsTrue(device) {
// not updated means empty or different, we should update it.
if !updated {
logrus.Debugf("Update disk %v to longhorn node `%s` as an additional disk", diskSpec, node.Name)
nodeCpy.Spec.Disks[device.Name] = diskSpec
if _, err = c.Nodes.Update(nodeCpy); err != nil {
return err
Expand Down Expand Up @@ -812,3 +834,11 @@ func removeUnNeeded[T string | int](x []T, y []T) []T {
}
return result
}

func getLonghornClientset() (*lhclientset.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("unable to get client config, %v", err)
}
return lhclientset.NewForConfig(config)
}

0 comments on commit bc0e823

Please sign in to comment.