Skip to content

Commit

Permalink
Merge pull request #1 from avanier/reconcile-ressu-merge-conflicts
Browse files Browse the repository at this point in the history
Reconcile ressu merge conflicts
  • Loading branch information
buu-nguyen authored Nov 27, 2024
2 parents e2cc8de + a22db97 commit 2c13a71
Show file tree
Hide file tree
Showing 15 changed files with 620 additions and 97 deletions.
8 changes: 0 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ RUN apk add --no-cache e2fsprogs e2fsprogs-extra xfsprogs xfsprogs-extra blkid u

# Create symbolic link for chroot.sh
WORKDIR /
RUN mkdir /csibin
COPY chroot/chroot.sh /csibin
RUN chmod 777 /csibin/chroot.sh \
&& ln -s /csibin/chroot.sh /csibin/iscsiadm \
&& ln -s /csibin/chroot.sh /csibin/multipath \
&& ln -s /csibin/chroot.sh /csibin/multipathd

ENV PATH="/csibin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Copy and run CSI driver
COPY --from=builder /go/src/synok8scsiplugin/bin/synology-csi-driver synology-csi-driver
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ REGISTRY_NAME=synology
IMAGE_NAME=synology-csi
IMAGE_VERSION=v1.2.0
IMAGE_TAG=$(REGISTRY_NAME)/$(IMAGE_NAME):$(IMAGE_VERSION)
CONTAINER_BIN=$(shell which podman || which docker)

# For now, only build linux/amd64 platform
ifeq ($(GOARCH),)
Expand All @@ -22,10 +23,10 @@ synology-csi-driver:
$(BUILD_ENV) go build -v -ldflags $(BUILD_FLAGS) -o ./bin/synology-csi-driver ./

docker-build:
docker build -f Dockerfile -t $(IMAGE_TAG) .
$(CONTAINER_BIN) build -f Dockerfile -t $(IMAGE_TAG) .

docker-build-multiarch:
docker buildx build -t $(IMAGE_TAG) --platform linux/amd64,linux/arm/v7,linux/arm64 . --push
$(CONTAINER_BIN) buildx build -t $(IMAGE_TAG) --platform linux/amd64,linux/arm/v7,linux/arm64 . --push

synocli:
@mkdir -p bin
Expand Down
12 changes: 0 additions & 12 deletions chroot/chroot.sh

This file was deleted.

1 change: 1 addition & 0 deletions deploy/helm/templates/node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ spec:
- --endpoint=$(CSI_ENDPOINT)
- --log-level=info
- --nodeid=$(KUBE_NODE_NAME)
- --chroot-dir=/host
env:
- name: CSI_ENDPOINT
value: unix://csi/csi.sock
Expand Down
1 change: 1 addition & 0 deletions deploy/kubernetes/v1.19/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ spec:
- --client-info
- /etc/synology/client-info.yml
- --log-level=info
- --chroot-dir=/host
env:
- name: CSI_ENDPOINT
value: unix://csi/csi.sock
Expand Down
1 change: 1 addition & 0 deletions deploy/kubernetes/v1.20/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ spec:
- --client-info
- /etc/synology/client-info.yml
- --log-level=info
- --chroot-dir=/host
env:
- name: CSI_ENDPOINT
value: unix://csi/csi.sock
Expand Down
40 changes: 32 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/SynologyOpenSource/synology-csi/pkg/driver"
"github.com/SynologyOpenSource/synology-csi/pkg/dsm/common"
"github.com/SynologyOpenSource/synology-csi/pkg/dsm/service"
"github.com/SynologyOpenSource/synology-csi/pkg/logger"
"github.com/SynologyOpenSource/synology-csi/pkg/utils/hostexec"
)

var (
Expand All @@ -23,16 +25,21 @@ var (
csiEndpoint = "unix:///var/lib/kubelet/plugins/" + driver.DriverName + "/csi.sock"
csiClientInfoPath = "/etc/synology/client-info.yml"
// Logging
logLevel = "info"
webapiDebug = false
logLevel = "info"
webapiDebug = false
multipathForUC = true
// Locations is tools and directories
chrootDir = ""
iscsiadmPath = ""
multipathPath = ""
multipathdPath = ""
)

var rootCmd = &cobra.Command{
Use: "synology-csi-driver",
Short: "Synology CSI Driver",
Use: "synology-csi-driver",
Short: "Synology CSI Driver",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
if webapiDebug {
logger.WebapiDebug = true
logLevel = "debug"
Expand Down Expand Up @@ -72,8 +79,21 @@ func driverStart() error {
}
defer dsmService.RemoveAllDsms()

// 2. Create and Run the Driver
drv, err := driver.NewControllerAndNodeDriver(csiNodeID, csiEndpoint, dsmService)
// 2. Create command executor
cmdMap := map[string]string{
"iscsiadm": iscsiadmPath,
"multipath": multipathPath,
"multipathd": multipathdPath,
}
cmdExecutor, err := hostexec.New(cmdMap, chrootDir)
if err != nil {
log.Errorf("Failed to create command executor: %v", err)
return err
}
tools := driver.NewTools(cmdExecutor)

// 3. Create and Run the Driver
drv, err := driver.NewControllerAndNodeDriver(csiNodeID, csiEndpoint, dsmService, tools)
if err != nil {
log.Errorf("Failed to create driver: %v", err)
return err
Expand Down Expand Up @@ -105,6 +125,10 @@ func addFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&logLevel, "log-level", logLevel, "Log level (debug, info, warn, error, fatal)")
cmd.PersistentFlags().BoolVarP(&webapiDebug, "debug", "d", webapiDebug, "Enable webapi debugging logs")
cmd.PersistentFlags().BoolVar(&multipathForUC, "multipath", multipathForUC, "Set to 'false' to disable multipath for UC")
cmd.PersistentFlags().StringVar(&chrootDir, "chroot-dir", chrootDir, "Host directory to chroot into (empty disables chroot)")
cmd.PersistentFlags().StringVar(&iscsiadmPath, "iscsiadm-path", iscsiadmPath, "Full path of iscsiadm executable")
cmd.PersistentFlags().StringVar(&multipathPath, "multipath-path", multipathPath, "Full path of multipath executable")
cmd.PersistentFlags().StringVar(&multipathdPath, "multipathd-path", multipathdPath, "Full path of multipathd executable")

cmd.MarkFlagRequired("endpoint")
cmd.MarkFlagRequired("client-info")
Expand Down
12 changes: 7 additions & 5 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ limitations under the License.
package driver

import (
"github.com/container-storage-interface/spec/lib/go/csi"
log "github.com/sirupsen/logrus"
"github.com/SynologyOpenSource/synology-csi/pkg/interfaces"
"github.com/SynologyOpenSource/synology-csi/pkg/utils"
"github.com/container-storage-interface/spec/lib/go/csi"
log "github.com/sirupsen/logrus"
)

const (
DriverName = "csi.san.synology.com" // CSI dirver name
DriverName = "csi.san.synology.com" // CSI dirver name
DriverVersion = "1.2.0"
)

var (
MultipathEnabled = true
MultipathEnabled = true
supportedProtocolList = []string{utils.ProtocolIscsi, utils.ProtocolSmb, utils.ProtocolNfs}
allowedNfsVersionList = []string{"3", "4", "4.0", "4.1"}
)
Expand All @@ -44,13 +44,14 @@ type Driver struct {
nodeID string
version string
endpoint string
tools tools
csCap []*csi.ControllerServiceCapability
vCap []*csi.VolumeCapability_AccessMode
nsCap []*csi.NodeServiceCapability
DsmService interfaces.IDsmService
}

func NewControllerAndNodeDriver(nodeID string, endpoint string, dsmService interfaces.IDsmService) (*Driver, error) {
func NewControllerAndNodeDriver(nodeID string, endpoint string, dsmService interfaces.IDsmService, tools tools) (*Driver, error) {
log.Debugf("NewControllerAndNodeDriver: DriverName: %v, DriverVersion: %v", DriverName, DriverVersion)

// TODO version format and validation
Expand All @@ -60,6 +61,7 @@ func NewControllerAndNodeDriver(nodeID string, endpoint string, dsmService inter
nodeID: nodeID,
endpoint: endpoint,
DsmService: dsmService,
tools: tools,
}

d.addControllerServiceCapabilities([]csi.ControllerServiceCapability_RPC_Type{
Expand Down
65 changes: 38 additions & 27 deletions pkg/driver/initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import (
"strconv"
"strings"

"github.com/SynologyOpenSource/synology-csi/pkg/utils/hostexec"
log "github.com/sirupsen/logrus"
utilexec "k8s.io/utils/exec"
)

type initiatorDriver struct {
chapUser string
chapPassword string
tools tools
}

type iscsiSession struct {
Expand All @@ -45,10 +47,19 @@ const (
ISCSIPort = 3260
)

func iscsiadm(cmdArgs ...string) utilexec.Cmd {
executor := utilexec.New()
type tools struct {
executor hostexec.Executor
}

// NewTools creates a new tools wrapper for calling utilities with given executor
func NewTools(executor hostexec.Executor) tools {
return tools{
executor: executor,
}
}

return executor.Command("iscsiadm", cmdArgs...)
func (t *tools) iscsiadm(cmdArgs ...string) utilexec.Cmd {
return t.executor.Command("iscsiadm", cmdArgs...)
}

// parseSession takes the raw stdout from the `iscsiadm -m session` command and encodes it into an iSCSI session type
Expand Down Expand Up @@ -81,8 +92,8 @@ func parseSessions(lines string) []iscsiSession {
return sessions
}

func iscsiadm_session() []iscsiSession {
cmd := iscsiadm("-m", "session")
func (t *tools) iscsiadm_session() []iscsiSession {
cmd := t.iscsiadm("-m", "session")
out, err := cmd.CombinedOutput()
if err != nil {
exitErr, ok := err.(utilexec.ExitError)
Expand All @@ -97,8 +108,8 @@ func iscsiadm_session() []iscsiSession {
return parseSessions(string(out))
}

func iscsiadm_discovery(portal string) error {
cmd := iscsiadm(
func (t *tools) iscsiadm_discovery(portal string) error {
cmd := t.iscsiadm(
"-m", "discoverydb",
"--type", "sendtargets",
"--portal", portal,
Expand All @@ -110,8 +121,8 @@ func iscsiadm_discovery(portal string) error {
return nil
}

func iscsiadm_login(iqn, portal string) error {
cmd := iscsiadm(
func (t *tools) iscsiadm_login(iqn, portal string) error {
cmd := t.iscsiadm(
"-m", "node",
"--targetname", iqn,
"--portal", portal,
Expand All @@ -123,8 +134,8 @@ func iscsiadm_login(iqn, portal string) error {
return nil
}

func iscsiadm_update_node_startup(iqn, portal string) error {
cmd := iscsiadm(
func (t *tools) iscsiadm_update_node_startup(iqn, portal string) error {
cmd := t.iscsiadm(
"-m", "node",
"--targetname", iqn,
"--portal", portal,
Expand All @@ -138,8 +149,8 @@ func iscsiadm_update_node_startup(iqn, portal string) error {
return nil
}

func iscsiadm_logout(iqn string) error {
cmd := iscsiadm(
func (t *tools) iscsiadm_logout(iqn string) error {
cmd := t.iscsiadm(
"-m", "node",
"--targetname", iqn,
"--logout")
Expand All @@ -149,8 +160,8 @@ func iscsiadm_logout(iqn string) error {
return nil
}

func iscsiadm_rescan(iqn string) error {
cmd := iscsiadm(
func (t *tools) iscsiadm_rescan(iqn string) error {
cmd := t.iscsiadm(
"-m", "node",
"--targetname", iqn,
"-R")
Expand All @@ -160,8 +171,8 @@ func iscsiadm_rescan(iqn string) error {
return nil
}

func hasSession(targetIqn string, portal string) bool {
sessions := iscsiadm_session()
func (t *tools) hasSession(targetIqn string, portal string) bool {
sessions := t.iscsiadm_session()

for _, s := range sessions {
if targetIqn == s.Iqn && (portal == s.Portal || portal == "") {
Expand All @@ -172,8 +183,8 @@ func hasSession(targetIqn string, portal string) bool {
return false
}

func listSessionsByIqn(targetIqn string) (matchedSessions []iscsiSession) {
sessions := iscsiadm_session()
func (t *tools) listSessionsByIqn(targetIqn string) (matchedSessions []iscsiSession) {
sessions := t.iscsiadm_session()

for _, s := range sessions {
if targetIqn == s.Iqn {
Expand All @@ -185,22 +196,22 @@ func listSessionsByIqn(targetIqn string) (matchedSessions []iscsiSession) {
}

func (d *initiatorDriver) login(targetIqn string, portal string) error {
if hasSession(targetIqn, portal) {
if d.tools.hasSession(targetIqn, portal) {
log.Infof("Session[%s] already exists.", targetIqn)
return nil
}

if err := iscsiadm_discovery(portal); err != nil {
if err := d.tools.iscsiadm_discovery(portal); err != nil {
log.Errorf("Failed in discovery of the target: %v", err)
return err
}

if err := iscsiadm_login(targetIqn, portal); err != nil {
if err := d.tools.iscsiadm_login(targetIqn, portal); err != nil {
log.Errorf("Failed in login of the target: %v", err)
return err
}

if err := iscsiadm_update_node_startup(targetIqn, portal); err != nil {
if err := d.tools.iscsiadm_update_node_startup(targetIqn, portal); err != nil {
log.Warnf("Failed to update target node.startup to manual: %v", err)
}

Expand All @@ -210,13 +221,13 @@ func (d *initiatorDriver) login(targetIqn string, portal string) error {
}

func (d *initiatorDriver) logout(targetIqn string, ip string) error {
if !hasSession(targetIqn, "") {
if !d.tools.hasSession(targetIqn, "") {
log.Infof("Session[%s] doesn't exist.", targetIqn)
return nil
}

portal := fmt.Sprintf("%s:%d", ip, ISCSIPort)
if err := iscsiadm_logout(targetIqn); err != nil {
if err := d.tools.iscsiadm_logout(targetIqn); err != nil {
log.Errorf("Failed in logout of the target.\nTarget [%s], Portal [%s], Err[%v]",
targetIqn, portal, err)
return err
Expand All @@ -228,13 +239,13 @@ func (d *initiatorDriver) logout(targetIqn string, ip string) error {
}

func (d *initiatorDriver) rescan(targetIqn string) error {
if !hasSession(targetIqn, "") {
if !d.tools.hasSession(targetIqn, "") {
msg := fmt.Sprintf("Session[%s] doesn't exist.", targetIqn)
log.Error(msg)
return errors.New(msg)
}

if err := iscsiadm_rescan(targetIqn); err != nil {
if err := d.tools.iscsiadm_rescan(targetIqn); err != nil {
log.Errorf("Failed in rescan of the target.\nTarget [%s], Err[%v]",
targetIqn, err)
return err
Expand Down
Loading

0 comments on commit 2c13a71

Please sign in to comment.