From 0462d79ef7933dcfd19b67b1b6056dcf47a4a394 Mon Sep 17 00:00:00 2001 From: Simon Croome Date: Wed, 18 Mar 2020 12:52:18 +0000 Subject: [PATCH] Pass cluster node selector to NFS pods (#236) * Inherit NFS server node selectors from cluster --- README.md | 2 +- pkg/nfs/statefulset.go | 7 ++++++- pkg/util/podspec.go | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4daad761f..d618ba319 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ Parameter | Description | Default `disableTCMU` | Disable TCMU to allow co-existence with other storage systems but degrades performance | `false` `forceTCMU` | Forces TCMU to be enabled or causes StorageOS to abort startup | `false` `disableScheduler` | Disable StorageOS scheduler for data locality | `false` -`nodeSelectorTerms` | Set node selector for storageos pod placement | +`nodeSelectorTerms` | Set node selector for storageos pod placement, including NFS pods | `tolerations` | Set pod tolerations for storageos pod placement | `resources` | Set resource requirements for the containers | `k8sDistro` | The name of the Kubernetes distribution is use, e.g. `rancher` or `eks` | diff --git a/pkg/nfs/statefulset.go b/pkg/nfs/statefulset.go index c379d6436..fd87a2ef9 100644 --- a/pkg/nfs/statefulset.go +++ b/pkg/nfs/statefulset.go @@ -35,9 +35,14 @@ func (d *Deployment) createStatefulSet(pvcVS *corev1.PersistentVolumeClaimVolume } spec.Template.Spec.Volumes = append(spec.Template.Spec.Volumes, vol) - // TODO: Add node affinity support for NFS server pods. util.AddTolerations(&spec.Template.Spec, d.nfsServer.Spec.Tolerations) + // If the cluster was configured with node selectors to only run on certain + // nodes, use the same selectors to selct the nodes that the NFS pods can + // run on. NFSServer does not currently allow setting node selectors or + // affinity directly. + util.AddRequiredNodeAffinity(&spec.Template.Spec, d.cluster.Spec.NodeSelectorTerms) + return d.k8sResourceManager.StatefulSet(d.nfsServer.Name, d.nfsServer.Namespace, nil, spec).Create() } diff --git a/pkg/util/podspec.go b/pkg/util/podspec.go index 12e763e84..f4aa1bf97 100644 --- a/pkg/util/podspec.go +++ b/pkg/util/podspec.go @@ -18,3 +18,17 @@ func AddTolerations(podSpec *corev1.PodSpec, tolerations []corev1.Toleration) er } return nil } + +// AddRequiredNodeAffinity adds required node affinity to the given pod spec. +func AddRequiredNodeAffinity(podSpec *corev1.PodSpec, terms []corev1.NodeSelectorTerm) { + if len(terms) == 0 { + return + } + podSpec.Affinity = &corev1.Affinity{ + NodeAffinity: &corev1.NodeAffinity{ + RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{ + NodeSelectorTerms: terms, + }, + }, + } +}