Skip to content

Commit

Permalink
Fix bug: networking between longhorn-csi-plugin and longhorn-manager …
Browse files Browse the repository at this point in the history
…is broken after upgrading Longhorn to 1.7.0-rc3

1. Revert to set `HostPID: true` and mounting the host proc for CSI plugin pod
2. Revert to set `HostNetwork: false`
3. When RWX storage network is enabled, we mount NFS in the container namespace.
   When it is not we mount the NFS in host name space by nsenter the host namespace

longhorn-9223

Signed-off-by: Phan Le <phan.le@suse.com>
  • Loading branch information
PhanLe1010 committed Aug 12, 2024
1 parent d4ab049 commit 591c106
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 13 deletions.
16 changes: 14 additions & 2 deletions csi/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ func NewPluginDeployment(namespace, serviceAccount, nodeDriverRegistrarImage, li
Tolerations: tolerations,
NodeSelector: nodeSelector,
PriorityClassName: priorityClass,
HostNetwork: true,
DNSPolicy: corev1.DNSClusterFirstWithHostNet,
HostPID: true,
Containers: []corev1.Container{
{
Name: "node-driver-registrar",
Expand Down Expand Up @@ -457,6 +456,11 @@ func NewPluginDeployment(namespace, serviceAccount, nodeDriverRegistrarImage, li
Name: "host-sys",
MountPath: "/sys",
},
{
Name: "host",
MountPath: "/host",
MountPropagation: &MountPropagationBidirectional,
},
{
Name: "lib-modules",
MountPath: "/lib/modules",
Expand Down Expand Up @@ -526,6 +530,14 @@ func NewPluginDeployment(namespace, serviceAccount, nodeDriverRegistrarImage, li
},
},
},
{
Name: "host",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/",
},
},
},
{
Name: "lib-modules",
VolumeSource: corev1.VolumeSource{
Expand Down
40 changes: 40 additions & 0 deletions csi/node_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,11 +812,51 @@ func getNodeServiceCapabilities(cs []csi.NodeServiceCapability_RPC_Type) []*csi.
return nscs
}

func (ns *NodeServer) requireHostNamespaceMounter(volume *longhornclient.Volume, volumeCapability *csi.VolumeCapability) (bool, error) {
if !requiresSharedAccess(volume, volumeCapability) {
return false, nil
}
if volume.Migratable {
return false, nil
}

storageNetworkSetting, err := ns.apiClient.Setting.ById(string(types.SettingNameStorageNetwork))
if err != nil {
return false, err
}
if storageNetworkSetting.Value == "" {
return false, nil
}

storageNetworkForRWXVolumeEnabledSetting, err := ns.apiClient.Setting.ById(string(types.SettingNameStorageNetworkForRWXVolumeEnabled))
if err != nil {
return false, err
}
storageNetworkForRWXVolumeEnabled, err := strconv.ParseBool(storageNetworkForRWXVolumeEnabledSetting.Value)
if err != nil {
return false, err
}
if !storageNetworkForRWXVolumeEnabled {
return false, nil
}

return true, nil
}

func (ns *NodeServer) getMounter(volume *longhornclient.Volume, volumeCapability *csi.VolumeCapability, volumeContext map[string]string) (mount.Interface, error) {
if volumeCapability.GetBlock() != nil {
return mount.New(""), nil
}

// HACK: to nsenter host namespaces for the nfs mounts to stay available after csi plugin dies
requireHostNamespaceMounter, err := ns.requireHostNamespaceMounter(volume, volumeCapability)
if err != nil {
return nil, err
}
if requireHostNamespaceMounter {
return mount.New("/usr/local/sbin/nsmounter"), nil
}

// mounter that can format and use hard coded filesystem params
if volumeCapability.GetMount() != nil {
fsType := volumeCapability.GetMount().GetFsType()
Expand Down
2 changes: 1 addition & 1 deletion package/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ENV ARCH ${TARGETPLATFORM#linux/}

COPY package/bin/longhorn-manager-${ARCH} /usr/local/sbin/longhorn-manager

COPY package/launch-manager /usr/local/sbin/
COPY package/launch-manager package/nsmounter /usr/local/sbin/

RUN zypper -n ref && \
zypper update -y
Expand Down
46 changes: 46 additions & 0 deletions package/nsmounter
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

PROC_DIR="/proc"

os_distro_talos="talos"
os_distro=""

get_os_distro() {
local version_info=$(< $PROC_DIR/version)

[[ $version_info =~ $os_distro_talos ]] && os_distro=$os_distro_talos
}

target_pid=1

get_pid() {
local process_name=$1
local pid
local status_file
local name

for dir in $PROC_DIR/*/; do
pid=$(basename "$dir")
status_file="$PROC_DIR/$pid/status"

if [ -f "$status_file" ]; then
while IFS= read -r line; do
if [[ $line == "Name:"* ]]; then
name="${line#*:}"
name="${name//[$'\t ']/}" # Remove both spaces and tabs
break # Exit the loop once the Name is found
fi
done < "$status_file"
fi

if [ "$name" = "$process_name" ]; then
target_pid=$pid
fi
done
}

get_os_distro

[[ $os_distro = $os_distro_talos ]] && get_pid "kubelet"

nsenter -t $target_pid -m -n -u -- "$@"
10 changes: 0 additions & 10 deletions types/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,6 @@ func UpdateDaemonSetTemplateBasedOnStorageNetwork(daemonSet *appsv1.DaemonSet, s

isContainerNetworkNamespace := IsStorageNetworkForRWXVolume(storageNetwork, isStorageNetworkForRWXVolumeEnabled)

updateHostNetwork := func() {
newHostNetwork := !isContainerNetworkNamespace
logger.WithFields(logrus.Fields{
"oldValue": daemonSet.Spec.Template.Spec.HostNetwork,
"newValue": newHostNetwork,
}).Debugf("Updating hostNetwork")
daemonSet.Spec.Template.Spec.HostNetwork = newHostNetwork
}

updateAnnotation := func() {
annotKey := string(CNIAnnotationNetworks)
annotValue := ""
Expand All @@ -137,6 +128,5 @@ func UpdateDaemonSetTemplateBasedOnStorageNetwork(daemonSet *appsv1.DaemonSet, s
}
}

updateHostNetwork()
updateAnnotation()
}

0 comments on commit 591c106

Please sign in to comment.