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

fix panic converting structs to API in CSI endpoint #8659

Merged
merged 1 commit into from
Aug 12, 2020
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
24 changes: 24 additions & 0 deletions command/agent/csi_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ func (s *HTTPServer) CSIPluginSpecificRequest(resp http.ResponseWriter, req *htt
// structsCSIPluginToApi converts CSIPlugin, setting Expected the count of known plugin
// instances
func structsCSIPluginToApi(plug *structs.CSIPlugin) *api.CSIPlugin {
if plug == nil {
return nil
}
out := &api.CSIPlugin{
ID: plug.ID,
Provider: plug.Provider,
Expand Down Expand Up @@ -301,6 +304,9 @@ func structsCSIPluginToApi(plug *structs.CSIPlugin) *api.CSIPlugin {

// structsCSIVolumeToApi converts CSIVolume, creating the allocation array
func structsCSIVolumeToApi(vol *structs.CSIVolume) *api.CSIVolume {
if vol == nil {
return nil
}
out := &api.CSIVolume{
ID: vol.ID,
Name: vol.Name,
Expand Down Expand Up @@ -344,6 +350,9 @@ func structsCSIVolumeToApi(vol *structs.CSIVolume) *api.CSIVolume {

// structsCSIInfoToApi converts CSIInfo, part of CSIPlugin
func structsCSIInfoToApi(info *structs.CSIInfo) *api.CSIInfo {
if info == nil {
return nil
}
out := &api.CSIInfo{
PluginID: info.PluginID,
Healthy: info.Healthy,
Expand Down Expand Up @@ -380,6 +389,9 @@ func structsCSIInfoToApi(info *structs.CSIInfo) *api.CSIInfo {

// structsAllocListStubToApi converts AllocListStub, for CSIPlugin
func structsAllocListStubToApi(alloc *structs.AllocListStub) *api.AllocationListStub {
if alloc == nil {
return nil
}
out := &api.AllocationListStub{
ID: alloc.ID,
EvalID: alloc.EvalID,
Expand Down Expand Up @@ -416,6 +428,9 @@ func structsAllocListStubToApi(alloc *structs.AllocListStub) *api.AllocationList

// structsAllocDeploymentStatusToApi converts RescheduleTracker, part of AllocListStub
func structsAllocDeploymentStatusToApi(ads *structs.AllocDeploymentStatus) *api.AllocDeploymentStatus {
if ads == nil {
return nil
}
out := &api.AllocDeploymentStatus{
Healthy: ads.Healthy,
Timestamp: ads.Timestamp,
Expand All @@ -427,6 +442,9 @@ func structsAllocDeploymentStatusToApi(ads *structs.AllocDeploymentStatus) *api.

// structsRescheduleTrackerToApi converts RescheduleTracker, part of AllocListStub
func structsRescheduleTrackerToApi(rt *structs.RescheduleTracker) *api.RescheduleTracker {
if rt == nil {
return nil
}
out := &api.RescheduleTracker{}

for _, e := range rt.Events {
Expand All @@ -442,6 +460,9 @@ func structsRescheduleTrackerToApi(rt *structs.RescheduleTracker) *api.Reschedul

// structsTaskStateToApi converts TaskState, part of AllocListStub
func structsTaskStateToApi(ts *structs.TaskState) *api.TaskState {
if ts == nil {
return nil
}
out := &api.TaskState{
State: ts.State,
Failed: ts.Failed,
Expand All @@ -460,6 +481,9 @@ func structsTaskStateToApi(ts *structs.TaskState) *api.TaskState {

// structsTaskEventToApi converts TaskEvents, part of AllocListStub
func structsTaskEventToApi(te *structs.TaskEvent) *api.TaskEvent {
if te == nil {
return nil
}
out := &api.TaskEvent{
Type: te.Type,
Time: te.Time,
Expand Down
22 changes: 22 additions & 0 deletions command/agent/csi_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/state"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/kr/pretty"
Expand Down Expand Up @@ -99,3 +100,24 @@ func TestHTTP_CSIEndpointVolume(t *testing.T) {
require.Equal(t, 2, out.NodesHealthy)
})
}

// TestHTTP_CSIEndpoint_Cast is a smoke test for converting from structs to
// API structs
func TestHTTP_CSIEndpoint_Cast(t *testing.T) {
t.Parallel()

plugin := mock.CSIPlugin()
plugin.Nodes["node1"] = &structs.CSIInfo{
PluginID: plugin.ID,
AllocID: "alloc1",
NodeInfo: &structs.CSINodeInfo{ID: "instance-1", MaxVolumes: 3},
}
apiPlugin := structsCSIPluginToApi(plugin)
require.Equal(t,
plugin.Nodes["node1"].NodeInfo.MaxVolumes,
apiPlugin.Nodes["node1"].NodeInfo.MaxVolumes)

vol := mock.CSIVolume(plugin)
apiVol := structsCSIVolumeToApi(vol)
require.Equal(t, vol.MountOptions.MountFlags, apiVol.MountOptions.MountFlags)
}