Dedup common code for fetching portworx driver

This commit is contained in:
Harsh Desai 2017-05-24 13:59:12 -07:00
parent bbfda9cdfe
commit ad4f21f26c
2 changed files with 55 additions and 67 deletions

View File

@ -43,6 +43,7 @@ go_library(
"//vendor/github.com/libopenstorage/openstorage/api/client:go_default_library", "//vendor/github.com/libopenstorage/openstorage/api/client:go_default_library",
"//vendor/github.com/libopenstorage/openstorage/api/client/volume:go_default_library", "//vendor/github.com/libopenstorage/openstorage/api/client/volume:go_default_library",
"//vendor/github.com/libopenstorage/openstorage/api/spec:go_default_library", "//vendor/github.com/libopenstorage/openstorage/api/spec:go_default_library",
"//vendor/github.com/libopenstorage/openstorage/volume:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library", "//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library", "//vendor/k8s.io/apimachinery/pkg/types:go_default_library",

View File

@ -22,6 +22,7 @@ import (
osdclient "github.com/libopenstorage/openstorage/api/client" osdclient "github.com/libopenstorage/openstorage/api/client"
volumeclient "github.com/libopenstorage/openstorage/api/client/volume" volumeclient "github.com/libopenstorage/openstorage/api/client/volume"
osdspec "github.com/libopenstorage/openstorage/api/spec" osdspec "github.com/libopenstorage/openstorage/api/spec"
volumeapi "github.com/libopenstorage/openstorage/volume"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/v1" "k8s.io/kubernetes/pkg/api/v1"
@ -42,17 +43,12 @@ type PortworxVolumeUtil struct {
// CreateVolume creates a Portworx volume. // CreateVolume creates a Portworx volume.
func (util *PortworxVolumeUtil) CreateVolume(p *portworxVolumeProvisioner) (string, int, map[string]string, error) { func (util *PortworxVolumeUtil) CreateVolume(p *portworxVolumeProvisioner) (string, int, map[string]string, error) {
if util.portworxClient == nil || !isValid(util.portworxClient) { driver, err := util.getPortworxDriver(p.plugin.host)
var err error if err != nil || driver == nil {
util.portworxClient, err = getPortworxClient(p.plugin.host) glog.Errorf("Failed to get portworx driver. Err: %v", err)
if err != nil || util.portworxClient == nil { return "", 0, nil, err
glog.Errorf("Failed to get portworx client. Err: %v", err)
return "", 0, nil, err
}
} }
driver := volumeclient.VolumeDriver(util.portworxClient)
capacity := p.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)] capacity := p.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
// Portworx Volumes are specified in GB // Portworx Volumes are specified in GB
requestGB := int(volume.RoundUpSize(capacity.Value(), 1024*1024*1024)) requestGB := int(volume.RoundUpSize(capacity.Value(), 1024*1024*1024))
@ -79,17 +75,13 @@ func (util *PortworxVolumeUtil) CreateVolume(p *portworxVolumeProvisioner) (stri
// DeleteVolume deletes a Portworx volume // DeleteVolume deletes a Portworx volume
func (util *PortworxVolumeUtil) DeleteVolume(d *portworxVolumeDeleter) error { func (util *PortworxVolumeUtil) DeleteVolume(d *portworxVolumeDeleter) error {
if util.portworxClient == nil || !isValid(util.portworxClient) { driver, err := util.getPortworxDriver(d.plugin.host)
var err error if err != nil || driver == nil {
util.portworxClient, err = getPortworxClient(d.plugin.host) glog.Errorf("Failed to get portworx driver. Err: %v", err)
if err != nil || util.portworxClient == nil { return err
glog.Errorf("Failed to get portworx client. Err: %v", err)
return err
}
} }
driver := volumeclient.VolumeDriver(util.portworxClient) err = driver.Delete(d.volumeID)
err := driver.Delete(d.volumeID)
if err != nil { if err != nil {
glog.V(2).Infof("Error deleting Portworx Volume (%v): %v", d.volName, err) glog.V(2).Infof("Error deleting Portworx Volume (%v): %v", d.volName, err)
return err return err
@ -99,16 +91,12 @@ func (util *PortworxVolumeUtil) DeleteVolume(d *portworxVolumeDeleter) error {
// AttachVolume attaches a Portworx Volume // AttachVolume attaches a Portworx Volume
func (util *PortworxVolumeUtil) AttachVolume(m *portworxVolumeMounter) (string, error) { func (util *PortworxVolumeUtil) AttachVolume(m *portworxVolumeMounter) (string, error) {
if util.portworxClient == nil || !isValid(util.portworxClient) { driver, err := util.getPortworxDriver(m.plugin.host)
var err error if err != nil || driver == nil {
util.portworxClient, err = getPortworxClient(m.plugin.host) glog.Errorf("Failed to get portworx driver. Err: %v", err)
if err != nil || util.portworxClient == nil { return "", err
glog.Errorf("Failed to get portworx client. Err: %v", err)
return "", err
}
} }
driver := volumeclient.VolumeDriver(util.portworxClient)
devicePath, err := driver.Attach(m.volName) devicePath, err := driver.Attach(m.volName)
if err != nil { if err != nil {
glog.V(2).Infof("Error attaching Portworx Volume (%v): %v", m.volName, err) glog.V(2).Infof("Error attaching Portworx Volume (%v): %v", m.volName, err)
@ -119,17 +107,13 @@ func (util *PortworxVolumeUtil) AttachVolume(m *portworxVolumeMounter) (string,
// DetachVolume detaches a Portworx Volume // DetachVolume detaches a Portworx Volume
func (util *PortworxVolumeUtil) DetachVolume(u *portworxVolumeUnmounter) error { func (util *PortworxVolumeUtil) DetachVolume(u *portworxVolumeUnmounter) error {
if util.portworxClient == nil || !isValid(util.portworxClient) { driver, err := util.getPortworxDriver(u.plugin.host)
var err error if err != nil || driver == nil {
util.portworxClient, err = getPortworxClient(u.plugin.host) glog.Errorf("Failed to get portworx driver. Err: %v", err)
if err != nil || util.portworxClient == nil { return err
glog.Errorf("Failed to get portworx client. Err: %v", err)
return err
}
} }
driver := volumeclient.VolumeDriver(util.portworxClient) err = driver.Detach(u.volName)
err := driver.Detach(u.volName)
if err != nil { if err != nil {
glog.V(2).Infof("Error detaching Portworx Volume (%v): %v", u.volName, err) glog.V(2).Infof("Error detaching Portworx Volume (%v): %v", u.volName, err)
return err return err
@ -139,17 +123,13 @@ func (util *PortworxVolumeUtil) DetachVolume(u *portworxVolumeUnmounter) error {
// MountVolume mounts a Portworx Volume on the specified mountPath // MountVolume mounts a Portworx Volume on the specified mountPath
func (util *PortworxVolumeUtil) MountVolume(m *portworxVolumeMounter, mountPath string) error { func (util *PortworxVolumeUtil) MountVolume(m *portworxVolumeMounter, mountPath string) error {
if util.portworxClient == nil || !isValid(util.portworxClient) { driver, err := util.getPortworxDriver(m.plugin.host)
var err error if err != nil || driver == nil {
util.portworxClient, err = getPortworxClient(m.plugin.host) glog.Errorf("Failed to get portworx driver. Err: %v", err)
if err != nil || util.portworxClient == nil { return err
glog.Errorf("Failed to get portworx client. Err: %v", err)
return err
}
} }
driver := volumeclient.VolumeDriver(util.portworxClient) err = driver.Mount(m.volName, mountPath)
err := driver.Mount(m.volName, mountPath)
if err != nil { if err != nil {
glog.V(2).Infof("Error mounting Portworx Volume (%v) on Path (%v): %v", m.volName, mountPath, err) glog.V(2).Infof("Error mounting Portworx Volume (%v) on Path (%v): %v", m.volName, mountPath, err)
return err return err
@ -159,17 +139,13 @@ func (util *PortworxVolumeUtil) MountVolume(m *portworxVolumeMounter, mountPath
// UnmountVolume unmounts a Portworx Volume // UnmountVolume unmounts a Portworx Volume
func (util *PortworxVolumeUtil) UnmountVolume(u *portworxVolumeUnmounter, mountPath string) error { func (util *PortworxVolumeUtil) UnmountVolume(u *portworxVolumeUnmounter, mountPath string) error {
if util.portworxClient == nil || !isValid(util.portworxClient) { driver, err := util.getPortworxDriver(u.plugin.host)
var err error if err != nil || driver == nil {
util.portworxClient, err = getPortworxClient(u.plugin.host) glog.Errorf("Failed to get portworx driver. Err: %v", err)
if err != nil || util.portworxClient == nil { return err
glog.Errorf("Failed to get portworx client. Err: %v", err)
return err
}
} }
driver := volumeclient.VolumeDriver(util.portworxClient) err = driver.Unmount(u.volName, mountPath)
err := driver.Unmount(u.volName, mountPath)
if err != nil { if err != nil {
glog.V(2).Infof("Error unmounting Portworx Volume (%v) on Path (%v): %v", u.volName, mountPath, err) glog.V(2).Infof("Error unmounting Portworx Volume (%v) on Path (%v): %v", u.volName, mountPath, err)
return err return err
@ -177,33 +153,43 @@ func (util *PortworxVolumeUtil) UnmountVolume(u *portworxVolumeUnmounter, mountP
return nil return nil
} }
func isValid(client *osdclient.Client) bool { func isClientValid(client *osdclient.Client) (bool, error) {
if client == nil {
return false, nil
}
_, err := client.Versions(osdapi.OsdVolumePath) _, err := client.Versions(osdapi.OsdVolumePath)
if err != nil { if err != nil {
glog.Errorf("portworx client failed driver versions check. Err: %v", err) glog.Errorf("portworx client failed driver versions check. Err: %v", err)
return false return false, err
} }
return true return true, nil
} }
func testDriverClient(hostname string) (*osdclient.Client, error) { func createDriverClient(hostname string) (*osdclient.Client, error) {
client, err := volumeclient.NewDriverClient("http://"+hostname+":"+osdMgmtPort, client, err := volumeclient.NewDriverClient("http://"+hostname+":"+osdMgmtPort,
pxdDriverName, osdDriverVersion) pxdDriverName, osdDriverVersion)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if isValid(client) { if isValid, err := isClientValid(client); isValid {
return client, nil return client, nil
} else { } else {
return nil, nil return nil, err
} }
} }
func getPortworxClient(volumeHost volume.VolumeHost) (*osdclient.Client, error) { func (util *PortworxVolumeUtil) getPortworxDriver(volumeHost volume.VolumeHost) (volumeapi.VolumeDriver, error) {
pxClient, err := testDriverClient(volumeHost.GetHostName()) // for backward compatibility if isValid, _ := isClientValid(util.portworxClient); isValid {
if err != nil || pxClient == nil { return volumeclient.VolumeDriver(util.portworxClient), nil
}
// create new client
var err error
util.portworxClient, err = createDriverClient(volumeHost.GetHostName()) // for backward compatibility
if err != nil || util.portworxClient == nil {
// Create client from portworx service // Create client from portworx service
kubeClient := volumeHost.GetKubeClient() kubeClient := volumeHost.GetKubeClient()
if kubeClient == nil { if kubeClient == nil {
@ -219,19 +205,20 @@ func getPortworxClient(volumeHost volume.VolumeHost) (*osdclient.Client, error)
} }
if svc == nil { if svc == nil {
glog.Errorf("Service: %v not found. Consult Portworx install docs to "+ glog.Errorf("Service: %v not found. Consult Portworx docs to deploy it.", pxServiceName)
"deploy it.", pxServiceName)
return nil, err return nil, err
} }
pxClient, err = testDriverClient(svc.Spec.ClusterIP) util.portworxClient, err = createDriverClient(svc.Spec.ClusterIP)
if err != nil || pxClient == nil { if err != nil || util.portworxClient == nil {
glog.Errorf("Failed to connect to portworx service. Err: %v", err) glog.Errorf("Failed to connect to portworx service. Err: %v", err)
return nil, err return nil, err
} }
glog.Infof("Using portworx service at: %v as api endpoint", svc.Spec.ClusterIP) glog.Infof("Using portworx service at: %v as api endpoint", svc.Spec.ClusterIP)
} else {
glog.Infof("Using portworx service at: %v as api endpoint", volumeHost.GetHostName())
} }
return pxClient, nil return volumeclient.VolumeDriver(util.portworxClient), nil
} }