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

Use csi-lib-iscsi #16

Merged
merged 2 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 111 additions & 18 deletions pkg/iscsi/iscsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package iscsi
import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/container-storage-interface/spec/lib/go/csi"
iscsi_lib "github.com/kubernetes-csi/csi-lib-iscsi/iscsi"
"k8s.io/kubernetes/pkg/util/mount"
"k8s.io/kubernetes/pkg/volume/util"
)
Expand All @@ -38,6 +40,14 @@ func getISCSIInfo(req *csi.NodePublishVolumeRequest) (*iscsiDisk, error) {
portalList := req.GetVolumeContext()["portals"]
secretParams := req.GetVolumeContext()["secret"]
secret := parseSecret(secretParams)
sessionSecret, err := parseSessionSecret(secret)
if err != nil {
return nil, err
}
discoverySecret, err := parseDiscoverySecret(secret)
if err != nil {
return nil, err
}

portal := portalMounter(tp)
var bkportal []string
Expand All @@ -64,16 +74,45 @@ func getISCSIInfo(req *csi.NodePublishVolumeRequest) (*iscsiDisk, error) {
chapSession = true
}

var lunVal int32
if lun != "" {
l, err := strconv.Atoi(lun)
if err != nil {
return nil, err
}
lunVal = int32(l)
}

return &iscsiDisk{
VolName: volName,
Portals: bkportal,
Iqn: iqn,
lun: lun,
Iface: iface,
chapDiscovery: chapDiscovery,
chapSession: chapSession,
secret: secret,
InitiatorName: initiatorName}, nil
VolName: volName,
Portals: bkportal,
Iqn: iqn,
lun: lunVal,
Iface: iface,
chapDiscovery: chapDiscovery,
chapSession: chapSession,
secret: secret,
sessionSecret: sessionSecret,
discoverySecret: discoverySecret,
InitiatorName: initiatorName}, nil
}

func buildISCSIConnector(iscsiInfo *iscsiDisk) *iscsi_lib.Connector {
c := iscsi_lib.Connector{
VolumeName: iscsiInfo.VolName,
TargetIqn: iscsiInfo.Iqn,
TargetPortals: iscsiInfo.Portals,
Multipath: len(iscsiInfo.Portals) > 1,
}

if iscsiInfo.sessionSecret != (iscsi_lib.Secrets{}) {
c.SessionSecrets = iscsiInfo.sessionSecret
if iscsiInfo.discoverySecret != (iscsi_lib.Secrets{}) {
c.DiscoverySecrets = iscsiInfo.discoverySecret
}
}

return &c
}

func getISCSIDiskMounter(iscsiInfo *iscsiDisk, req *csi.NodePublishVolumeRequest) *iscsiDiskMounter {
Expand All @@ -90,6 +129,7 @@ func getISCSIDiskMounter(iscsiInfo *iscsiDisk, req *csi.NodePublishVolumeRequest
exec: mount.NewOsExec(),
targetPath: req.GetTargetPath(),
deviceUtil: util.NewDeviceHandler(util.NewIOHandler()),
connector: buildISCSIConnector(iscsiInfo),
}
}

Expand Down Expand Up @@ -118,16 +158,68 @@ func parseSecret(secretParams string) map[string]string {
return secret
}

func parseSessionSecret(secretParams map[string]string) (iscsi_lib.Secrets, error) {
var ok bool
secret := iscsi_lib.Secrets{}

if len(secretParams) == 0 {
return secret, nil
}

if secret.UserName, ok = secretParams["node.session.auth.username"]; !ok {
return iscsi_lib.Secrets{}, fmt.Errorf("node.session.auth.username not found in secret")
}
if secret.Password, ok = secretParams["node.session.auth.password"]; !ok {
return iscsi_lib.Secrets{}, fmt.Errorf("node.session.auth.password not found in secret")
}
if secret.UserNameIn, ok = secretParams["node.session.auth.username_in"]; !ok {
return iscsi_lib.Secrets{}, fmt.Errorf("node.session.auth.username_in not found in secret")
}
if secret.PasswordIn, ok = secretParams["node.session.auth.password_in"]; !ok {
return iscsi_lib.Secrets{}, fmt.Errorf("node.session.auth.password_in not found in secret")
}

secret.SecretsType = "chap"
return secret, nil
}

func parseDiscoverySecret(secretParams map[string]string) (iscsi_lib.Secrets, error) {
var ok bool
secret := iscsi_lib.Secrets{}

if len(secretParams) == 0 {
return secret, nil
}

if secret.UserName, ok = secretParams["node.sendtargets.auth.username"]; !ok {
return iscsi_lib.Secrets{}, fmt.Errorf("node.sendtargets.auth.username not found in secret")
}
if secret.Password, ok = secretParams["node.sendtargets.auth.password"]; !ok {
return iscsi_lib.Secrets{}, fmt.Errorf("node.sendtargets.auth.password not found in secret")
}
if secret.UserNameIn, ok = secretParams["node.sendtargets.auth.username_in"]; !ok {
return iscsi_lib.Secrets{}, fmt.Errorf("node.sendtargets.auth.username_in not found in secret")
}
if secret.PasswordIn, ok = secretParams["node.sendtargets.auth.password_in"]; !ok {
return iscsi_lib.Secrets{}, fmt.Errorf("node.sendtargets.auth.password_in not found in secret")
}

secret.SecretsType = "chap"
return secret, nil
}

type iscsiDisk struct {
Portals []string
Iqn string
lun string
Iface string
chapDiscovery bool
chapSession bool
secret map[string]string
InitiatorName string
VolName string
Portals []string
Iqn string
lun int32
Iface string
chapDiscovery bool
chapSession bool
secret map[string]string
sessionSecret iscsi_lib.Secrets
discoverySecret iscsi_lib.Secrets
InitiatorName string
VolName string
}

type iscsiDiskMounter struct {
Expand All @@ -139,6 +231,7 @@ type iscsiDiskMounter struct {
exec mount.Exec
deviceUtil util.DeviceUtil
targetPath string
connector *iscsi_lib.Connector
}

type iscsiDiskUnmounter struct {
Expand Down
Loading