Skip to content

Commit

Permalink
Improve Docker Support
Browse files Browse the repository at this point in the history
  • Loading branch information
liberodark committed Nov 12, 2024
1 parent 46dbe85 commit 24f309d
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 63 deletions.
128 changes: 108 additions & 20 deletions cmd/seaweedfs-csi-driver/swarm/build.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,115 @@
#!/bin/bash
USAGE="Usage: ./build.sh [PREFIX] [VERSION] [TAG] [PLUGIN_NAME] [ARCH]"
set -e

if [[ "$1" == "-h" ]]; then
echo "$USAGE"
exit 0
fi
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# Default values
VERSION=${2:-latest}
ARCH=${5:-linux/amd64}
PLUGIN_NAME=${4:-swarm-csi-swaweedfs}
PLUGIN_TAG=${3:-v1.2.0}
PLUGIN_NAME=${4:-swarm-csi-seaweedfs}
PLUGIN_TAG=${3:-v1.2.7}
PREFIX=${1:-gradlon}

docker build --platform ${ARCH} --build-arg BASE_IMAGE=chrislusf/seaweedfs-csi-driver:${VERSION} --build-arg ARCH=$ARCH -t seawadd-csi_tmp_img .
mkdir -p ./plugin/rootfs
cp config.json ./plugin/
docker container create --name seawadd-csi_tmp seawadd-csi_tmp_img
docker container export seawadd-csi_tmp | tar -x -C ./plugin/rootfs
docker container rm -vf seawadd-csi_tmp
docker image rm seawadd-csi_tmp_img

docker plugin disable gradlon/swarm-csi-swaweedfs:v1.2.0
docker plugin rm ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} 2> /dev/null || true
docker plugin create ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} ./plugin
docker plugin push ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG}
rm -rf ./plugin/
# Help function
show_help() {
echo "Build script for SeaweedFS CSI Docker Plugin"
echo
echo "Usage: $0 [PREFIX] [VERSION] [TAG] [PLUGIN_NAME] [ARCH]"
echo
echo "Parameters:"
echo " PREFIX Docker registry prefix (default: gradlon)"
echo " VERSION SeaweedFS CSI driver version (default: latest)"
echo " TAG Plugin tag version (default: v1.2.0)"
echo " PLUGIN_NAME Plugin name (default: swarm-csi-seaweedfs)"
echo " ARCH Target architecture (default: linux/amd64)"
echo
echo "Example:"
echo " $0 myregistry latest v1.2.0 swarm-csi-seaweedfs linux/amd64"
}

# Error handling function
handle_error() {
echo -e "${RED}Error: $1${NC}"
exit 1
}

# Cleanup function
cleanup() {
echo -e "${YELLOW}Cleaning up temporary resources...${NC}"
docker container rm -f seaweed-csi_tmp 2>/dev/null || true
docker image rm -f seaweed-csi_tmp_img 2>/dev/null || true
rm -rf ./plugin 2>/dev/null || true
}

# Parse command line arguments
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
show_help
exit 0
fi

# Trap for cleanup
trap cleanup EXIT

# Log build configuration
echo -e "${GREEN}Building SeaweedFS CSI Plugin with:${NC}"
echo -e "PREFIX: ${YELLOW}$PREFIX${NC}"
echo -e "VERSION: ${YELLOW}$VERSION${NC}"
echo -e "TAG: ${YELLOW}$PLUGIN_TAG${NC}"
echo -e "PLUGIN_NAME: ${YELLOW}$PLUGIN_NAME${NC}"
echo -e "ARCH: ${YELLOW}$ARCH${NC}"

# Check required files
for file in "config.json" "Dockerfile" "entrypoint.sh"; do
if [ ! -f "$file" ]; then
handle_error "Required file $file not found"
fi
done

# Create plugin directory
echo -e "${GREEN}Creating plugin directory structure...${NC}"
mkdir -p ./plugin/rootfs || handle_error "Failed to create plugin directory"

# Copy config file
echo -e "${GREEN}Copying config.json...${NC}"
cp config.json ./plugin/ || handle_error "Failed to copy config.json"

# Build the image
echo -e "${GREEN}Building Docker image...${NC}"
docker build \
--platform ${ARCH} \
--build-arg BASE_IMAGE=chrislusf/seaweedfs-csi-driver:${VERSION} \
--build-arg ARCH=$ARCH \
-t seaweed-csi_tmp_img . || handle_error "Docker build failed"

# Create and export container
echo -e "${GREEN}Creating temporary container and extracting rootfs...${NC}"
docker container create --name seaweed-csi_tmp seaweed-csi_tmp_img || handle_error "Failed to create temporary container"
docker container export seaweed-csi_tmp | tar -x -C ./plugin/rootfs || handle_error "Failed to export container filesystem"

# Remove existing plugin if it exists
echo -e "${GREEN}Removing existing plugin if present...${NC}"
docker plugin disable ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} 2>/dev/null || true
docker plugin rm ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} 2>/dev/null || true

# Create and push plugin
echo -e "${GREEN}Creating plugin...${NC}"
docker plugin create ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} ./plugin || handle_error "Failed to create plugin"

echo -e "${GREEN}Pushing plugin to registry...${NC}"
docker plugin push ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG} || handle_error "Failed to push plugin"

echo -e "${GREEN}Build completed successfully!${NC}"
echo -e "${GREEN}Plugin: ${YELLOW}${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG}${NC}"

# Usage instructions
echo -e "\n${GREEN}To use the plugin:${NC}"
echo -e "1. Install the plugin:"
echo -e "${YELLOW}docker plugin install --grant-all-permissions ${PREFIX}/${PLUGIN_NAME}:${PLUGIN_TAG}${NC}"
echo -e "\n2. Configure the plugin:"
echo -e "${YELLOW}docker plugin set ${PLUGIN_NAME}:${PLUGIN_TAG} FILER=<IP>:<PORT>${NC}"
echo -e "\n3. Enable the plugin:"
echo -e "${YELLOW}docker plugin enable ${PLUGIN_NAME}:${PLUGIN_TAG}${NC}"
87 changes: 47 additions & 40 deletions cmd/seaweedfs-csi-driver/swarm/config.json
Original file line number Diff line number Diff line change
@@ -1,83 +1,80 @@

{
"description": "SeaweedFS csi-driver plugin for Docker",
"documentation": "https://github.com/seaweedfs/seaweedfs-csi-driver",
"entrypoint": ["/entrypoint.sh"],
"env": [
{
"name": "FILER",
"description": "Filer endpoint/s, Fromat <IP1>:<PORT>,<IP2>:<PORT2>",
"settable": [
"value"
],
"description": "Filer endpoint/s, Format <IP1>:<PORT>,<IP2>:<PORT2>",
"settable": ["value"],
"value": ""
},
{
"name": "CACHE_SIZE",
"description": "The size of the cache to use in MB. Default: 256MB",
"settable": [
"value"
],
"settable": ["value"],
"value": "256"
},
{
"name": "CACHE_DIR",
"description": "The cache directory.",
"settable": [
"value"
],
"value": ""
"description": "The cache directory with absolute path",
"settable": ["value"],
"value": "/tmp/seaweedFS/docker-csi"
},
{
"name": "C_WRITER",
"description": "Limit concurrent goroutine writers if not 0. Default 32",
"settable": [
"value"
],
"value": ""
"settable": ["value"],
"value": "32"
},
{
"name": "DATACENTER",
"description": "dataCenter this node is running in (locality-definition)",
"settable": [
"value"
],
"settable": ["value"],
"value": "DefaultDataCenter"
},
{
"name": "UID_MAP",
"description": "map local uid to uid on filer, comma-separated <local_uid>:<filer_uid>",
"settable": [
"value"
],
"settable": ["value"],
"value": ""
},
{
"name": "GID_MAP",
"description": "map local gid to gid on filer, comma-separated <local_gid>:<filer_gid>",
"settable": [
"value"
],
"settable": ["value"],
"value": ""
},
{
"name": "FORCE_STAGING_SUPPORT",
"description": "workaround: force staging support to make Docker 23.0.0 work without https://github.com/moby/swarmkit/pull/3116",
"name": "MAX_RETRIES",
"description": "Maximum number of retry attempts for filer connection",
"settable": ["value"],
"value": "5"
},
{
"name": "RETRY_INTERVAL",
"description": "Interval between retry attempts in seconds",
"settable": ["value"],
"value": "true"
"value": "5"
}
],
"interface": {
"socket": "seaweed.sock",
"types": ["docker.csicontroller/1.0", "docker.csinode/1.0"]
"types": ["docker.csicontroller/2.0", "docker.csinode/2.0"]
},
"linux": {
"allowAllDevices": true,
"capabilities": ["CAP_SYS_ADMIN"],
"capabilities": [
"CAP_SYS_ADMIN",
"CAP_DAC_READ_SEARCH"
],
"devices": [
{
"path": "/dev/fuse"
}
{
"path": "/dev/fuse",
"type": "c",
"major": 10,
"minor": 229
}
]
},
"mounts": [
Expand All @@ -86,18 +83,28 @@
"destination": "/node_hostname",
"source": "/etc/hostname",
"type": "bind",
"options":["bind"]
},
{
"options": ["rbind"]
},
{
"description": "used for caching",
"destination": "/tmp",
"source": "/tmp",
"type": "bind",
"options":["bind"]
}
"options": ["rbind"]
}
],
"network": {
"type": "host"
},
"propagatedmount": "/data/published"
"propagatedmount": "/data/published",
"workdir": "/",
"csi": {
"version": "1.5.0",
"capabilities": [
"STAGE_UNSTAGE_VOLUME",
"GET_VOLUME_STATS",
"EXPAND_VOLUME",
"ONLINE_EXPAND_VOLUME"
]
}
}
100 changes: 97 additions & 3 deletions cmd/seaweedfs-csi-driver/swarm/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,106 @@
#!/bin/sh

MAX_RETRIES=5
RETRY_INTERVAL=5

check_filer_connection() {
local filer=$1
curl -s -f "http://${filer}/dir/status" > /dev/null
return $?
}

wait_for_filer() {
local filer=$1
local retries=0

echo "Checking connection to filer: ${filer}"
while [ $retries -lt $MAX_RETRIES ]; do
if check_filer_connection "${filer}"; then
echo "Successfully connected to filer ${filer}"
return 0
fi
retries=$((retries + 1))
echo "Failed to connect to filer ${filer}, attempt ${retries}/${MAX_RETRIES}"
sleep $RETRY_INTERVAL
done

return 1
}

cleanup_orphaned_mounts() {
echo "Checking for orphaned mounts..."
# Nettoyer les points de montage orphelins dans /data/published
for mount in $(find /data/published -maxdepth 1 -type d); do
if ! mountpoint -q "$mount"; then
echo "Cleaning up orphaned mount: $mount"
rm -rf "$mount"
fi
done
}

if [ -z "$FILER" ]; then
echo "FILER is not set!"
exit 1
echo "FILER is not set!"
exit 1
fi

# Validate and split filer endpoints
IFS=',' read -r -a FILER_ENDPOINTS <<< "$FILER"
CONNECTED=false

for endpoint in "${FILER_ENDPOINTS[@]}"; do
if wait_for_filer "$endpoint"; then
CONNECTED=true
ACTIVE_FILER=$endpoint
break
fi
done

if [ "$CONNECTED" = false ]; then
echo "Failed to connect to any filer endpoint after ${MAX_RETRIES} attempts"
exit 1
fi

NODE_ID=$(cat /node_hostname)
C_WRITER=${C_WRITER:-32}
CMD="/seaweedfs-csi-driver --filer=$FILER --nodeid=${NODE_ID} --endpoint=unix://run/docker/plugins/seaweed.sock --concurrentWriters=${C_WRITER} --dataCenter=${DATACENTER} --dataLocality=none --logtostderr --map.uid=${UID_MAP} --map.gid=${GID_MAP} --cacheCapacityMB=${CACHE_SIZE} --cacheDir=/tmp/seaweedFS/docker-csi"

# Ensure cache directory exists with proper permissions
CACHE_DIR=${CACHE_DIR:-/tmp/seaweedFS/docker-csi}
mkdir -p "$CACHE_DIR"
chmod 755 "$CACHE_DIR"

# Cleanup any orphaned mounts before starting
cleanup_orphaned_mounts

# Set up periodic health check
(
while true; do
if ! check_filer_connection "$ACTIVE_FILER"; then
echo "Lost connection to primary filer, attempting failover..."
for endpoint in "${FILER_ENDPOINTS[@]}"; do
if [ "$endpoint" != "$ACTIVE_FILER" ] && wait_for_filer "$endpoint"; then
echo "Failing over to $endpoint"
ACTIVE_FILER=$endpoint
break
fi
done
fi
sleep 30
done
) &

# Start the CSI driver with improved options
CMD="/seaweedfs-csi-driver \
--filer=$FILER \
--nodeid=${NODE_ID} \
--endpoint=unix://run/docker/plugins/seaweed.sock \
--concurrentWriters=${C_WRITER} \
--dataCenter=${DATACENTER} \
--dataLocality=none \
--logtostderr \
--map.uid=${UID_MAP} \
--map.gid=${GID_MAP} \
--cacheCapacityMB=${CACHE_SIZE:-256} \
--cacheDir=${CACHE_DIR} \
--v=2"

exec $CMD

0 comments on commit 24f309d

Please sign in to comment.