Misc cleanup in the p5xApi + update examples

This commit is contained in:
Garrett Mills 2025-02-24 22:41:46 -05:00
parent 96956ba7ff
commit 9fc6a3f9ea
5 changed files with 24 additions and 40 deletions

View File

@ -22,7 +22,7 @@ spec:
- --endpoint=$(CSI_ENDPOINT)
# - --logtostderr
- --nodeid=$(NODE_NAME)
- --p5x-endpoint=http://172.20.0.22
- --p5x-endpoint=http://172.20.0.174
- --p5x-token=1
env:
- name: CSI_ENDPOINT

View File

@ -31,7 +31,7 @@ spec:
- --endpoint=$(CSI_ENDPOINT)
# - --logtostderr
- --nodeid=$(NODE_NAME)
- --p5x-endpoint=http://172.20.0.22
- --p5x-endpoint=http://172.20.0.174
- --p5x-token=1
env:
- name: CSI_ENDPOINT

View File

@ -44,9 +44,10 @@ metadata:
name: example-pod-1
namespace: p5x-system
spec:
nodeName: worker-4.k8s
containers:
- name: example-ct
image: fedora:39
image: fedora:41
command: ['/bin/bash', '-c', '--']
args: ['while true; do sleep 30; done;']
volumeMounts:
@ -68,6 +69,7 @@ metadata:
name: example-pod-2
namespace: p5x-system
spec:
nodeName: worker-7.k8s
containers:
- name: example-ct
image: fedora:39

View File

@ -18,9 +18,10 @@ type p5xApi struct {
}
type p5xVolume struct {
VolumeId int64 `json:"volumeId"`
VolumeId int64 `json:"id"`
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) {
@ -36,7 +37,7 @@ func (p5x *p5xApi) CreateVolume(name string, sizeInBytes int64) (*p5xVolume, err
return nil, err
}
resBody, err := p5x.MakeRequest(http.MethodPost, "volumes", body)
resBody, err := p5x.MakeRequest(http.MethodPost, "volume", body)
if err != nil {
return nil, err
}
@ -51,13 +52,13 @@ var ErrVolumeNotFound = errors.New("p5x volume not found")
func (p5x *p5xApi) GetVolumeByName(name string) (*p5xVolume, error) {
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)
if err != nil {
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
}
@ -73,7 +74,7 @@ func (p5x *p5xApi) GetVolumeByName(name string) (*p5xVolume, error) {
func (p5x *p5xApi) DeleteVolume(volume *p5xVolume) error {
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(`{}`))
if err != nil {
return err
@ -85,7 +86,7 @@ func (p5x *p5xApi) DeleteVolume(volume *p5xVolume) error {
func (p5x *p5xApi) TransferVolume(volume *p5xVolume, nodeName string) (*p5xVolume, error) {
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(`{}`))
if err != nil {
return nil, err
@ -101,60 +102,41 @@ func (p5x *p5xApi) TransferVolume(volume *p5xVolume, nodeName string) (*p5xVolum
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) {
klog.Info("p5x.MountVolume: ", volume, mountpoint, options)
req := &MountVolumeRequest{
Mountpoint: mountpoint,
Options: options,
}
volume.Mountpoint = mountpoint
body, err := json.Marshal(req)
body, err := json.Marshal(volume)
if err != nil {
return nil, err
}
route := fmt.Sprintf("volumes/%s/mount", volume.Name)
resBody, err := p5x.MakeRequest(http.MethodPost, route, body)
if err != nil {
return nil, err
}
vol := &p5xVolume{}
err = json.Unmarshal(resBody, vol)
_, err = p5x.MakeRequest(http.MethodPost, "volume/mount", body)
if err != nil {
return nil, err
}
klog.Info("p5x.MountVolume: Successfully mounted volume: ", volume.Name, mountpoint)
return vol, nil
return volume, nil
}
func (p5x *p5xApi) UnmountVolume(volume *p5xVolume) (*p5xVolume, error) {
klog.Info("p5x.UnmountVolume: ", volume)
route := fmt.Sprintf("volumes/%s/unmount", volume.Name)
resBody, err := p5x.MakeRequest(http.MethodPost, route, []byte(`{}`))
route := fmt.Sprintf("volume/unmount/%s", volume.Name)
_, err := p5x.MakeRequest(http.MethodPost, route, []byte(`{}`))
if err != nil {
return nil, err
}
vol := &p5xVolume{}
err = json.Unmarshal(resBody, vol)
if err != nil {
return nil, err
}
volume.Mountpoint = ""
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) {
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)
var res *http.Response