Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all shell command #1011

Merged
merged 23 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f10587e
remove pfs_stat_collector
huww98 Mar 5, 2024
b313bd8
invoke dbfs_get_home_path.sh without shell
huww98 Mar 5, 2024
fa6c441
dbfs: umount with mounter
huww98 Mar 5, 2024
d43f707
invoke get_dbfs_mount_path without shell
huww98 Mar 5, 2024
c5ea9f2
dbfs: cleanup umountGlobalPath
huww98 Mar 5, 2024
a0747a8
read dbfs version using procfs to access root mount namespace
huww98 Mar 5, 2024
5fb3161
ens: do not use shell to write sysfs
huww98 Mar 5, 2024
64af7f8
ens: replace GetDeviceByMntPoint with mount.GetDeviceNameFromMount
huww98 Mar 5, 2024
2293d9b
ens: remove duplicated function GetNvmeDeviceByVolumeID
huww98 Mar 5, 2024
50f926f
ens: remove duplicated function checkDeviceAvailable
huww98 Mar 5, 2024
6555a80
disk/ens: don't check mounted before remove
huww98 Mar 6, 2024
785a823
disk: replace utils.IsMounted with mounter.IsLikelyNotMountPoint
huww98 Mar 6, 2024
ec100e4
ens: replace utils.IsMounted with mounter.IsLikelyNotMountPoint
huww98 Mar 6, 2024
fc12fae
dbfs: use mount.CleanupMountPoint for NodeUnpublishVolume
huww98 Mar 6, 2024
4c29419
dbfs: replace utils.IsMounted with mounter.IsLikelyNotMountPoint
huww98 Mar 6, 2024
739e665
metric: use IsLikelyNotMountPoint to check for mounted
huww98 Mar 6, 2024
a4afaa4
call udevadm without shell
huww98 Mar 6, 2024
1b7caaf
disk: check regular mountpoint at outside of IsMountPointRunv
huww98 Mar 6, 2024
002002b
remove dead code
huww98 Mar 5, 2024
ebbe6dd
build an additional init image
huww98 Mar 5, 2024
973d723
remove freezefs connector server
huww98 Mar 14, 2024
01f3845
remove shell usage in connector
huww98 Mar 14, 2024
f37d2b5
Merge remote-tracking branch 'origin/master' into rm-shell-2
huww98 Mar 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
28 changes: 14 additions & 14 deletions build/build-all-multi.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env bash
set -ex
REPO_NAME=$2

if [ "$REPO_NAME" == "" ]; then
REPO_NAME="kubernetes-sigs"
fi
BUILD_ARGS=( \
--frontend dockerfile.v0 \
--local context=. \
--local dockerfile=build/multi \
--opt filename=Dockerfile.multi \
--opt platform=linux/amd64,linux/arm64 \
--opt build-arg:CSI_VERSION=$(git describe --tags --always --dirty)
)
BUILD_ARGS+=("$@")

cd ${GOPATH}/src/github.com/$REPO_NAME/alibaba-cloud-csi-driver/
GIT_SHA=`git rev-parse --short HEAD || echo "HEAD"`


VERSION="v1.16.9"
# GIT_HASH=`git rev-parse --short HEAD || echo "HEAD"`
# GIT_BRANCH=`git symbolic-ref --short -q HEAD`
# BUILD_TIME=`date +%FT%T%z`

docker buildx build --platform linux/amd64,linux/arm64 . -f ./build/multi/Dockerfile.multi
buildctl build "${BUILD_ARGS[@]}" \
--output type=image,name=alibaba-cloud-csi-driver:latest
buildctl build "${BUILD_ARGS[@]}" \
--opt target=init \
--output type=image,name=alibaba-cloud-csi-driver:latest-init
17 changes: 11 additions & 6 deletions build/lib/amd64-entrypoint.sh → build/lib/amd64-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

# skip all the setup if running in provisioner mode
if [ "$SERVICE_TYPE" = "provisioner" ]; then
echo "Starting provisioner..."
/bin/plugin.csi.alibabacloud.com $@
exit $?
exit 0
fi

run_oss="false"
Expand Down Expand Up @@ -37,6 +35,16 @@ if [[ "$os_release_exist" = "0" ]]; then
fi
echo "detected host os: $host_os"

OLD_STAGING_PATH=/var/lib/kubelet/plugins/kubernetes.io/csi/pv
if [ -d "$OLD_STAGING_PATH" ]; then
echo unmount old volume staging path. # kubelet will mount the new path at startup.
echo $OLD_STAGING_PATH/*/globalmount
umount $OLD_STAGING_PATH/*/globalmount
rmdir $OLD_STAGING_PATH/*/globalmount
rmdir $OLD_STAGING_PATH/*/
rmdir $OLD_STAGING_PATH
fi

## check which plugin is running
for item in $@;
do
Expand Down Expand Up @@ -286,6 +294,3 @@ fi

# place it here to remove leftover from previous version
rm -rf /host/etc/csi-tool/*.rpm

# start daemon
/bin/plugin.csi.alibabacloud.com $@
18 changes: 11 additions & 7 deletions build/lib/arm64-entrypoint.sh → build/lib/arm64-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

# skip all the setup if running in provisioner mode
if [ "$SERVICE_TYPE" = "provisioner" ]; then
echo "Starting provisioner..."
/bin/plugin.csi.alibabacloud.com $@
exit $?
exit 0
fi

run_oss="false"
Expand All @@ -16,6 +14,16 @@ mkdir -p /host/etc/kubernetes/volumes/disk/uuid

HOST_CMD="/nsenter --mount=/proc/1/ns/mnt"

OLD_STAGING_PATH=/var/lib/kubelet/plugins/kubernetes.io/csi/pv
if [ -d "$OLD_STAGING_PATH" ]; then
echo unmount old volume staging path. # kubelet will mount the new path at startup.
echo $OLD_STAGING_PATH/*/globalmount
umount $OLD_STAGING_PATH/*/globalmount
rmdir $OLD_STAGING_PATH/*/globalmount
rmdir $OLD_STAGING_PATH/*/
rmdir $OLD_STAGING_PATH
fi

## check which plugin is running
for item in $@;
do
Expand Down Expand Up @@ -207,7 +215,3 @@ if ([ "$DISK_BDF_ENABLE" = "true" ] && [ "$run_disk" = "true" ]) || [ "$run_pov"
fi
fi
fi


# start daemon
/bin/plugin.csi.alibabacloud.com $@
121 changes: 14 additions & 107 deletions build/lib/csiplugin-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ import (
const (
// OSSSocketPath socket path
OSSSocketPath = "/run/csi-tool/connector/connector.sock"
// DiskSocketPath socket path
DiskSocketPath = "/run/csi-tool/connector/diskconnector.sock"
// GetPathDevice get the device of specific path
GetPathDevice = "df --output=source %s"
)

func main() {
log.Print("OSS Connector Daemon Is Starting...")

var wg sync.WaitGroup
wg.Add(2)
wg.Add(1)
go func() {
defer wg.Done()
EnsureSocketPath(OSSSocketPath)
Expand All @@ -49,28 +45,6 @@ func main() {
go echoServer(fd)
}
}()
go func() {
defer wg.Done()
EnsureSocketPath(DiskSocketPath)
log.Printf("Socket path is ready: %s", DiskSocketPath)
ln, err := net.Listen("unix", DiskSocketPath)
if err != nil {
log.Fatalf("runDiskProxy: server Listen error: %v", err.Error())
}
log.Print("Disk proxy daemon started ....")
defer ln.Close()
go watchDogCheck()

// Handler to process the command
for {
fd, err := ln.Accept()
if err != nil {
log.Printf("Disk Server Accept error: %s", err.Error())
continue
}
go freezeFilesystemServer(fd)
}
}()
wg.Wait()
}

Expand Down Expand Up @@ -105,80 +79,6 @@ func watchDogCheck() {
}
}

func freezeFilesystemServer(c net.Conn) {
buf := make([]byte, 2048)
nr, err := c.Read(buf)
if err != nil {
log.Printf("freezeFilesystemServer:: server read error: %v", err.Error())
return
}
command := string(buf[0:nr])
log.Printf("freezeFilesystemServer:: server receive freeze parms: %v", command)
err = checkFilesystemConsistentCommand(command)
if err != nil {
out := "Fail:" + err.Error()
log.Printf("freezeFilesystemServer:: check disk command error: %v", out)
if _, err := c.Write([]byte(out)); err != nil {
log.Printf("freezeFilesystemServer:: check disk command write error: %v", out)
}
return
}
log.Printf("freezeFilesystemServer:: command: %v", command)
// run command
if out, err := run(command); err != nil {
reply := "Fail: " + command + ", error: " + err.Error()
_, err = c.Write([]byte(reply))
log.Print("diskServer Fail to run cmd:", reply)
} else {
out = "Success:" + out
_, err = c.Write([]byte(out))
log.Printf("Success: %s", out)
}
}

func checkFilesystemConsistentCommand(paramStr string) error {
params := strings.Split(paramStr, " ")
for index, param := range params {
if index == 0 {
if !strings.EqualFold(param, "/etc/csi-tool/freezefs.sh") {
return fmt.Errorf("checkFilesystemConsistentParams:: scripts name: %v invalid", param)
}
} else {
if !strings.HasPrefix(param, "--path") && !strings.HasPrefix(param, "--timeout") && !strings.HasPrefix(param, "--type") && !strings.HasPrefix(param, "&") {
return fmt.Errorf("checkFilesystemConsistentParams:: paramStr: %v invalid", param)
}
}
if index == 2 {
globalPath := strings.Split(param, "=")[1]
log.Printf("checkFilesystemConsistentCommand:: globalPath: %v", globalPath)
if !isIsolateDevice(globalPath) {
return fmt.Errorf("checkFilesystemConsistentParams:: globalPath: %v isn't isolated device mount path", globalPath)
}
}
}
return nil
}

func isIsolateDevice(globalPath string) bool {
globalPathCommand := fmt.Sprintf(GetPathDevice, globalPath)
pathOut, err := run(globalPathCommand)
if err != nil {
reply := "Fail: " + globalPathCommand + ", error: " + err.Error()
log.Print("Server Fail to run cmd:", reply)
return false
}
globalPathDirCommad := fmt.Sprintf(GetPathDevice, filepath.Dir(globalPath))
dirOut, err := run(globalPathDirCommad)
if err != nil {
reply := "Fail: " + globalPathDirCommad + ", error: " + err.Error()
log.Print("Server Fail to run cmd:", reply)
return false
}

log.Printf("isIsolateDevice:: pathOut: %s, dirOut: %s", pathOut, dirOut)
return !strings.EqualFold(pathOut, dirOut)
}

func echoServer(c net.Conn) {
buf := make([]byte, 2048)
nr, err := c.Read(buf)
Expand All @@ -187,8 +87,15 @@ func echoServer(c net.Conn) {
return
}

cmd := string(buf[0:nr])
log.Printf("Server receive mount cmd: %s", cmd)
cmdStr := string(buf[0:nr])
// '\x00' is chosen as the delimiter because it is the only character that is not vaild in the command line arguments.
// The rationale is the same as `xargs -0`.
args := strings.Split(cmdStr, "\x00")
log.Printf("Server receive mount cmd: %q", args)

// Used when removing shell usage while be compatible with old code
// Should be removed eventually
cmd := strings.Join(args, " ")

if strings.Contains(cmd, "/usr/local/bin/ossfs") {
err = checkOssfsCmd(cmd)
Expand All @@ -207,7 +114,7 @@ func echoServer(c net.Conn) {
return
}
// run command
if out, err := run(cmd); err != nil {
if out, err := run(args...); err != nil {
reply := "Fail: " + cmd + ", error: " + err.Error()
_, err = c.Write([]byte(reply))
log.Print("Server Fail to run cmd:", reply)
Expand Down Expand Up @@ -319,10 +226,10 @@ func checkOssfsCmd(cmd string) error {
return errors.New("Oss Options: options with error prefix: " + cmd)
}

func run(cmd string) (string, error) {
out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
func run(args ...string) (string, error) {
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
if err != nil {
return "", fmt.Errorf("Failed to run cmd: " + cmd + ", with out: " + string(out) + ", with error: " + err.Error())
return "", fmt.Errorf("failed to run cmd: %q, with out: %q, with error: %v", args, string(out), err)
}
return string(out), nil
}
Expand Down
Loading