Skip to content

Commit

Permalink
feat(rwx): share manager pod respects the newly introduced storageCla…
Browse files Browse the repository at this point in the history
…ss.Parameters["shareManagerNodeSelector"]

The commit introduces an enhancement for specifying the locality of a Read-Write-Many (RWX)
volume and its associated Share Manager Pod within the Longhorn storage system. Previously,
an RWX volume and its Share Manager Pod were created randomly on any node within the Longhorn
cluster, without the ability for users to specify a preferred locality.

Longhorn 7872

Signed-off-by: Derek Su <derek.su@suse.com>
  • Loading branch information
derekbit committed Apr 10, 2024
1 parent 89b4555 commit b74a1c7
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions controller/share_manager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"k8s.io/kubernetes/pkg/controller"

corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -743,12 +744,28 @@ func (c *ShareManagerController) syncShareManagerPod(sm *longhorn.ShareManager)
return nil
}

func (c *ShareManagerController) getShareManagerNodeSelectorFromStorageClass(sc *storagev1.StorageClass) map[string]string {
value, ok := sc.Parameters["shareManagerNodeSelector"]
if !ok {
return map[string]string{}
}

nodeSelector, err := types.UnmarshalNodeSelector(value)
if err != nil {
c.logger.WithError(err).Warnf("Failed to unmarshal node selector %v", value)
return map[string]string{}
}

return nodeSelector
}

// createShareManagerPod ensures existence of service, it's assumed that the pvc for this share manager already exists
func (c *ShareManagerController) createShareManagerPod(sm *longhorn.ShareManager) (*corev1.Pod, error) {
tolerations, err := c.ds.GetSettingTaintToleration()
if err != nil {
return nil, errors.Wrap(err, "failed to get taint toleration setting before creating share manager pod")
}

nodeSelector, err := c.ds.GetSettingSystemManagedComponentsNodeSelector()
if err != nil {
return nil, errors.Wrap(err, "failed to get node selector setting before creating share manager pod")
Expand Down Expand Up @@ -798,6 +815,22 @@ func (c *ShareManagerController) createShareManagerPod(sm *longhorn.ShareManager
return nil, err
}

if pv.Spec.StorageClassName != "" {
sc, err := c.ds.GetStorageClass(pv.Spec.StorageClassName)
if err != nil {
c.logger.WithError(err).Warnf("Failed to get storage class %v, will continue the share manager pod creation", pv.Spec.StorageClassName)
} else {
if nodeSelector == nil {
nodeSelector = map[string]string{}
}
// Find the node selector from the storage class and merge it with the system managed components node selector
nodeSelectorFromStorageClass := c.getShareManagerNodeSelectorFromStorageClass(sc)
for k, v := range nodeSelectorFromStorageClass {
nodeSelector[k] = v
}
}
}

fsType := pv.Spec.CSI.FSType
mountOptions := pv.Spec.MountOptions

Expand All @@ -822,8 +855,8 @@ func (c *ShareManagerController) createShareManagerPod(sm *longhorn.ShareManager
string(secret.Data[csi.CryptoPBKDF]))
}

manifest := c.createPodManifest(sm, annotations, tolerations, imagePullPolicy, nil, registrySecret, priorityClass, nodeSelector,
fsType, mountOptions, cryptoKey, cryptoParams)
manifest := c.createPodManifest(sm, annotations, tolerations, imagePullPolicy, nil, registrySecret,
priorityClass, nodeSelector, fsType, mountOptions, cryptoKey, cryptoParams)
pod, err := c.ds.CreatePod(manifest)
if err != nil {
return nil, errors.Wrapf(err, "failed to create pod for share manager %v", sm.Name)
Expand Down Expand Up @@ -858,8 +891,8 @@ func (c *ShareManagerController) createServiceManifest(sm *longhorn.ShareManager
}

func (c *ShareManagerController) createPodManifest(sm *longhorn.ShareManager, annotations map[string]string, tolerations []corev1.Toleration,
pullPolicy corev1.PullPolicy, resourceReq *corev1.ResourceRequirements, registrySecret, priorityClass string, nodeSelector map[string]string,
fsType string, mountOptions []string, cryptoKey string, cryptoParams *crypto.EncryptParams) *corev1.Pod {
pullPolicy corev1.PullPolicy, resourceReq *corev1.ResourceRequirements, registrySecret, priorityClass string,
nodeSelector map[string]string, fsType string, mountOptions []string, cryptoKey string, cryptoParams *crypto.EncryptParams) *corev1.Pod {

// command args for the share-manager
args := []string{"--debug", "daemon", "--volume", sm.Name}
Expand Down

0 comments on commit b74a1c7

Please sign in to comment.