Misc cleanup in the p5xApi + update examples
This commit is contained in:
parent
96956ba7ff
commit
9fc6a3f9ea
@ -22,7 +22,7 @@ spec:
|
|||||||
- --endpoint=$(CSI_ENDPOINT)
|
- --endpoint=$(CSI_ENDPOINT)
|
||||||
# - --logtostderr
|
# - --logtostderr
|
||||||
- --nodeid=$(NODE_NAME)
|
- --nodeid=$(NODE_NAME)
|
||||||
- --p5x-endpoint=http://172.20.0.22
|
- --p5x-endpoint=http://172.20.0.174
|
||||||
- --p5x-token=1
|
- --p5x-token=1
|
||||||
env:
|
env:
|
||||||
- name: CSI_ENDPOINT
|
- name: CSI_ENDPOINT
|
||||||
|
@ -31,7 +31,7 @@ spec:
|
|||||||
- --endpoint=$(CSI_ENDPOINT)
|
- --endpoint=$(CSI_ENDPOINT)
|
||||||
# - --logtostderr
|
# - --logtostderr
|
||||||
- --nodeid=$(NODE_NAME)
|
- --nodeid=$(NODE_NAME)
|
||||||
- --p5x-endpoint=http://172.20.0.22
|
- --p5x-endpoint=http://172.20.0.174
|
||||||
- --p5x-token=1
|
- --p5x-token=1
|
||||||
env:
|
env:
|
||||||
- name: CSI_ENDPOINT
|
- name: CSI_ENDPOINT
|
||||||
|
@ -44,9 +44,10 @@ metadata:
|
|||||||
name: example-pod-1
|
name: example-pod-1
|
||||||
namespace: p5x-system
|
namespace: p5x-system
|
||||||
spec:
|
spec:
|
||||||
|
nodeName: worker-4.k8s
|
||||||
containers:
|
containers:
|
||||||
- name: example-ct
|
- name: example-ct
|
||||||
image: fedora:39
|
image: fedora:41
|
||||||
command: ['/bin/bash', '-c', '--']
|
command: ['/bin/bash', '-c', '--']
|
||||||
args: ['while true; do sleep 30; done;']
|
args: ['while true; do sleep 30; done;']
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
@ -68,6 +69,7 @@ metadata:
|
|||||||
name: example-pod-2
|
name: example-pod-2
|
||||||
namespace: p5x-system
|
namespace: p5x-system
|
||||||
spec:
|
spec:
|
||||||
|
nodeName: worker-7.k8s
|
||||||
containers:
|
containers:
|
||||||
- name: example-ct
|
- name: example-ct
|
||||||
image: fedora:39
|
image: fedora:39
|
@ -18,9 +18,10 @@ type p5xApi struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type p5xVolume struct {
|
type p5xVolume struct {
|
||||||
VolumeId int64 `json:"volumeId"`
|
VolumeId int64 `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
SizeInBytes int64 `json:"sizeInBytes"`
|
SizeInBytes int64 `json:"size_in_bytes"`
|
||||||
|
Mountpoint string `json:"mountpoint,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p5x *p5xApi) CreateVolume(name string, sizeInBytes int64) (*p5xVolume, error) {
|
func (p5x *p5xApi) CreateVolume(name string, sizeInBytes int64) (*p5xVolume, error) {
|
||||||
@ -36,7 +37,7 @@ func (p5x *p5xApi) CreateVolume(name string, sizeInBytes int64) (*p5xVolume, err
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
resBody, err := p5x.MakeRequest(http.MethodPost, "volumes", body)
|
resBody, err := p5x.MakeRequest(http.MethodPost, "volume", body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -51,13 +52,13 @@ var ErrVolumeNotFound = errors.New("p5x volume not found")
|
|||||||
|
|
||||||
func (p5x *p5xApi) GetVolumeByName(name string) (*p5xVolume, error) {
|
func (p5x *p5xApi) GetVolumeByName(name string) (*p5xVolume, error) {
|
||||||
klog.Infof("p5x.GetVolumeByName: %s", name)
|
klog.Infof("p5x.GetVolumeByName: %s", name)
|
||||||
route := fmt.Sprintf("volumes/%s", name)
|
route := fmt.Sprintf("volume/%s", name)
|
||||||
resBody, err := p5x.MakeRequest(http.MethodGet, route, nil)
|
resBody, err := p5x.MakeRequest(http.MethodGet, route, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.Contains(string(resBody[:]), "Could not find volume with the name") {
|
if strings.Contains(string(resBody[:]), "Could not find a volume with that name") {
|
||||||
return nil, ErrVolumeNotFound
|
return nil, ErrVolumeNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +74,7 @@ func (p5x *p5xApi) GetVolumeByName(name string) (*p5xVolume, error) {
|
|||||||
|
|
||||||
func (p5x *p5xApi) DeleteVolume(volume *p5xVolume) error {
|
func (p5x *p5xApi) DeleteVolume(volume *p5xVolume) error {
|
||||||
klog.Info("p5x.DeleteVolume: ", volume)
|
klog.Info("p5x.DeleteVolume: ", volume)
|
||||||
route := fmt.Sprintf("volumes/%s", volume.Name)
|
route := fmt.Sprintf("volume/%s", volume.Name)
|
||||||
resBody, err := p5x.MakeRequest(http.MethodDelete, route, []byte(`{}`))
|
resBody, err := p5x.MakeRequest(http.MethodDelete, route, []byte(`{}`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -85,7 +86,7 @@ func (p5x *p5xApi) DeleteVolume(volume *p5xVolume) error {
|
|||||||
|
|
||||||
func (p5x *p5xApi) TransferVolume(volume *p5xVolume, nodeName string) (*p5xVolume, error) {
|
func (p5x *p5xApi) TransferVolume(volume *p5xVolume, nodeName string) (*p5xVolume, error) {
|
||||||
klog.Info("p5x.TransferVolume: ", volume, " | to node: ", nodeName)
|
klog.Info("p5x.TransferVolume: ", volume, " | to node: ", nodeName)
|
||||||
route := fmt.Sprintf("volumes/%s/transfer-to/%s", volume.Name, nodeName)
|
route := fmt.Sprintf("volume/transfer/%s/to/%s", volume.Name, nodeName)
|
||||||
resBody, err := p5x.MakeRequest(http.MethodPost, route, []byte(`{}`))
|
resBody, err := p5x.MakeRequest(http.MethodPost, route, []byte(`{}`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -101,60 +102,41 @@ func (p5x *p5xApi) TransferVolume(volume *p5xVolume, nodeName string) (*p5xVolum
|
|||||||
return vol, nil
|
return vol, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type MountVolumeRequest struct {
|
|
||||||
Mountpoint string `json:"mountpoint"`
|
|
||||||
Options map[string]string `json:"options"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p5x *p5xApi) MountVolume(volume *p5xVolume, mountpoint string, options map[string]string) (*p5xVolume, error) {
|
func (p5x *p5xApi) MountVolume(volume *p5xVolume, mountpoint string, options map[string]string) (*p5xVolume, error) {
|
||||||
klog.Info("p5x.MountVolume: ", volume, mountpoint, options)
|
klog.Info("p5x.MountVolume: ", volume, mountpoint, options)
|
||||||
req := &MountVolumeRequest{
|
volume.Mountpoint = mountpoint
|
||||||
Mountpoint: mountpoint,
|
|
||||||
Options: options,
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err := json.Marshal(req)
|
body, err := json.Marshal(volume)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
route := fmt.Sprintf("volumes/%s/mount", volume.Name)
|
_, err = p5x.MakeRequest(http.MethodPost, "volume/mount", body)
|
||||||
resBody, err := p5x.MakeRequest(http.MethodPost, route, body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
vol := &p5xVolume{}
|
|
||||||
err = json.Unmarshal(resBody, vol)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
klog.Info("p5x.MountVolume: Successfully mounted volume: ", volume.Name, mountpoint)
|
klog.Info("p5x.MountVolume: Successfully mounted volume: ", volume.Name, mountpoint)
|
||||||
return vol, nil
|
return volume, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p5x *p5xApi) UnmountVolume(volume *p5xVolume) (*p5xVolume, error) {
|
func (p5x *p5xApi) UnmountVolume(volume *p5xVolume) (*p5xVolume, error) {
|
||||||
klog.Info("p5x.UnmountVolume: ", volume)
|
klog.Info("p5x.UnmountVolume: ", volume)
|
||||||
|
|
||||||
route := fmt.Sprintf("volumes/%s/unmount", volume.Name)
|
route := fmt.Sprintf("volume/unmount/%s", volume.Name)
|
||||||
resBody, err := p5x.MakeRequest(http.MethodPost, route, []byte(`{}`))
|
_, err := p5x.MakeRequest(http.MethodPost, route, []byte(`{}`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
vol := &p5xVolume{}
|
volume.Mountpoint = ""
|
||||||
err = json.Unmarshal(resBody, vol)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
klog.Info("p5x.UnmountVolume: Successfully unmounted volume: ", volume.Name)
|
klog.Info("p5x.UnmountVolume: Successfully unmounted volume: ", volume.Name)
|
||||||
return vol, nil
|
return volume, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p5x *p5xApi) MakeRequest(method string, route string, body []byte) ([]byte, error) {
|
func (p5x *p5xApi) MakeRequest(method string, route string, body []byte) ([]byte, error) {
|
||||||
url := fmt.Sprintf("%s:%d/api/v1/%s", p5x.endpoint, p5x.port, route)
|
url := fmt.Sprintf("%s:%d/%s", p5x.endpoint, p5x.port, route)
|
||||||
klog.Infof("p5x.MakeRequest: [%s] %s", method, url)
|
klog.Infof("p5x.MakeRequest: [%s] %s", method, url)
|
||||||
|
|
||||||
var res *http.Response
|
var res *http.Response
|
||||||
|
Loading…
Reference in New Issue
Block a user