mirror of https://github.com/k3s-io/k3s
Lookup PX api port from k8s service
Fixes #70033 Signed-off-by: Harsh Desai <harsh@portworx.com>pull/564/head
parent
7712766daf
commit
c6a2d24fa2
|
@ -34,7 +34,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
osdMgmtPort = "9001"
|
osdMgmtDefaultPort = 9001
|
||||||
osdDriverVersion = "v1"
|
osdDriverVersion = "v1"
|
||||||
pxdDriverName = "pxd"
|
pxdDriverName = "pxd"
|
||||||
pvcClaimLabel = "pvc"
|
pvcClaimLabel = "pvc"
|
||||||
|
@ -254,8 +254,8 @@ func isClientValid(client *osdclient.Client) (bool, error) {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createDriverClient(hostname string) (*osdclient.Client, error) {
|
func createDriverClient(hostname string, port int32) (*osdclient.Client, error) {
|
||||||
client, err := volumeclient.NewDriverClient("http://"+hostname+":"+osdMgmtPort,
|
client, err := volumeclient.NewDriverClient(fmt.Sprintf("http://%s:%d", hostname, port),
|
||||||
pxdDriverName, osdDriverVersion, pxDriverName)
|
pxdDriverName, osdDriverVersion, pxDriverName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -280,8 +280,31 @@ func createDriverClient(hostname string) (*osdclient.Client, error) {
|
||||||
// the Portworx node that will own/owns the data.
|
// the Portworx node that will own/owns the data.
|
||||||
func (util *PortworxVolumeUtil) getPortworxDriver(volumeHost volume.VolumeHost, localOnly bool) (volumeapi.VolumeDriver, error) {
|
func (util *PortworxVolumeUtil) getPortworxDriver(volumeHost volume.VolumeHost, localOnly bool) (volumeapi.VolumeDriver, error) {
|
||||||
var err error
|
var err error
|
||||||
|
kubeClient := volumeHost.GetKubeClient()
|
||||||
|
if kubeClient == nil {
|
||||||
|
err = fmt.Errorf("Failed to get kubeclient when creating portworx client")
|
||||||
|
glog.Errorf(err.Error())
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := metav1.GetOptions{}
|
||||||
|
svc, err := kubeClient.CoreV1().Services(api.NamespaceSystem).Get(pxServiceName, opts)
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Failed to get service. Err: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if svc == nil {
|
||||||
|
err = fmt.Errorf("Service: %v not found. Consult Portworx docs to deploy it.", pxServiceName)
|
||||||
|
glog.Errorf(err.Error())
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var pxAPIPort int32 = osdMgmtDefaultPort
|
||||||
|
|
||||||
if localOnly {
|
if localOnly {
|
||||||
util.portworxClient, err = createDriverClient(volumeHost.GetHostName())
|
pxAPIPort = lookupPXAPIPortFromService(svc)
|
||||||
|
util.portworxClient, err = createDriverClient(volumeHost.GetHostName(), pxAPIPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
|
@ -296,28 +319,10 @@ func (util *PortworxVolumeUtil) getPortworxDriver(volumeHost volume.VolumeHost,
|
||||||
}
|
}
|
||||||
|
|
||||||
// create new client
|
// create new client
|
||||||
util.portworxClient, err = createDriverClient(volumeHost.GetHostName()) // for backward compatibility
|
util.portworxClient, err = createDriverClient(volumeHost.GetHostName(), pxAPIPort) // for backward compatibility
|
||||||
if err != nil || util.portworxClient == nil {
|
if err != nil || util.portworxClient == nil {
|
||||||
// Create client from portworx service
|
// Create client from portworx k8s service
|
||||||
kubeClient := volumeHost.GetKubeClient()
|
util.portworxClient, err = createDriverClient(svc.Spec.ClusterIP, pxAPIPort)
|
||||||
if kubeClient == nil {
|
|
||||||
klog.Error("Failed to get kubeclient when creating portworx client")
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := metav1.GetOptions{}
|
|
||||||
svc, err := kubeClient.CoreV1().Services(api.NamespaceSystem).Get(pxServiceName, opts)
|
|
||||||
if err != nil {
|
|
||||||
klog.Errorf("Failed to get service. Err: %v", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if svc == nil {
|
|
||||||
klog.Errorf("Service: %v not found. Consult Portworx docs to deploy it.", pxServiceName)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
util.portworxClient, err = createDriverClient(svc.Spec.ClusterIP)
|
|
||||||
if err != nil || util.portworxClient == nil {
|
if err != nil || util.portworxClient == nil {
|
||||||
klog.Errorf("Failed to connect to portworx service. Err: %v", err)
|
klog.Errorf("Failed to connect to portworx service. Err: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -330,3 +335,14 @@ func (util *PortworxVolumeUtil) getPortworxDriver(volumeHost volume.VolumeHost,
|
||||||
|
|
||||||
return volumeclient.VolumeDriver(util.portworxClient), nil
|
return volumeclient.VolumeDriver(util.portworxClient), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lookupPXAPIPortFromService goes over all the ports in the given service and returns the target
|
||||||
|
// port for osdMgmtDefaultPort
|
||||||
|
func lookupPXAPIPortFromService(svc *v1.Service) int32 {
|
||||||
|
for _, p := range svc.Spec.Ports {
|
||||||
|
if p.Port == osdMgmtDefaultPort {
|
||||||
|
return p.TargetPort.IntVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return osdMgmtDefaultPort // default
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue