Skip to content

Commit

Permalink
Use single path for node output
Browse files Browse the repository at this point in the history
  • Loading branch information
sushrk committed Apr 30, 2024
1 parent 48fd8d1 commit f86d227
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
17 changes: 9 additions & 8 deletions pkg/resource/introspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ const (
GetAllResourcesPath = "/resources/all"
GetResourcesSummaryPath = "/resources/summary"
// Use path "/datastore/all" to get all pods stored in the cache,
// "/datastore/listpods/<nodename>" to get list of all pods on a node (ie ListPods output)
// "datastore/listrunningpods/<nodename>" to get list of all running pods on a node (ie GetRunningPodsOnNode output)
// "/datastore/node/<nodename>" to get list of all pods on a node (ie ListPods output), and list of all running pods on a node (ie GetRunningPodsOnNode output)
GetDatastoreResourcePrefix = "/datastore/"
InvalidDSRequestMessage = "Invalid request, valid requests for datastore are /datastore/all, /datastore/listpods/<nodename>, datastore/listrunningpods/<nodename>"
InvalidDSRequestMessage = "Invalid request, valid requests for datastore are /datastore/all, /datastore/node/<nodename>"
)

type IntrospectHandler struct {
Expand Down Expand Up @@ -136,7 +135,7 @@ func (i *IntrospectHandler) DatastoreResourceHandler(w http.ResponseWriter, r *h
request := r.URL.Path[len(GetDatastoreResourcePrefix):]
request, nodeName, found := strings.Cut(request, "/")
if !found {
if request != "all" && !slices.Contains([]string{"listpods", "listrunningpods"}, request) {
if request != "all" && !slices.Contains([]string{"node"}, request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(InvalidDSRequestMessage))
return
Expand All @@ -145,20 +144,22 @@ func (i *IntrospectHandler) DatastoreResourceHandler(w http.ResponseWriter, r *h
var err error
var isError bool
switch request {
case "listpods":
case "node":
nodeResponse := make(map[string]interface{})
var podList *v1.PodList
if podList, err = i.PodAPIWrapper.ListPods(nodeName); err != nil {
isError = true
break
}
response[nodeName] = getPodNameSpaceName(podList.Items)
case "listrunningpods":
nodeResponse["Pods"] = getPodNameSpaceName(podList.Items)

var pods []v1.Pod
if pods, err = i.PodAPIWrapper.GetRunningPodsOnNode(nodeName); err != nil {
isError = true
break
}
response[nodeName] = getPodNameSpaceName(pods)
nodeResponse["RunningPods"] = getPodNameSpaceName(pods)
response[nodeName] = nodeResponse
case "all":
response["PodDatastore"] = i.PodAPIWrapper.Introspect()
default:
Expand Down
24 changes: 7 additions & 17 deletions pkg/resource/introspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,14 @@ var (
types.NamespacedName{Namespace: mockNS, Name: mockPod2}.String(),
},
}
mockListPodsResponse = map[string]interface{}{
mockNodeName: map[string]interface{}{
mockNodePodsResponse = map[string]interface{}{
"Pods": map[string]interface{}{
"PodList": []interface{}{
types.NamespacedName{Namespace: defaultNS, Name: mockPod1}.String(),
types.NamespacedName{Namespace: mockNS, Name: mockPod2}.String(),
},
},
}
mockGetRunningPodsOnNode = map[string]interface{}{
mockNodeName: map[string]interface{}{
"RunningPods": map[string]interface{}{
"PodList": []interface{}{
types.NamespacedName{Namespace: mockNS, Name: mockPod1}.String(),
},
Expand Down Expand Up @@ -185,21 +183,13 @@ func TestIntrospectHandler_DatastoreResourceHandler(t *testing.T) {
name: "TestListPods, verify response for list of pods on the node",
prepare: func(f *fields) {
f.mockPodAPIWrapper.EXPECT().ListPods(mockNodeName).Return(mockListPods, nil)
},
args: args{
request: GetDatastoreResourcePrefix + "listpods/" + mockNodeName,
response: mockListPodsResponse,
statusCode: http.StatusOK,
},
},
{
name: "TestGetRunningPodsOnNode, verify response for list of running pods on the node",
prepare: func(f *fields) {
f.mockPodAPIWrapper.EXPECT().GetRunningPodsOnNode(mockNodeName).Return(mockPods, nil)
},
args: args{
request: GetDatastoreResourcePrefix + "listrunningpods/" + mockNodeName,
response: mockGetRunningPodsOnNode,
request: GetDatastoreResourcePrefix + "node/" + mockNodeName,
response: map[string]interface{}{
mockNodeName: mockNodePodsResponse,
},
statusCode: http.StatusOK,
},
},
Expand Down

0 comments on commit f86d227

Please sign in to comment.