Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds an API endpoint from the kubelet (/runningpods/). It is an endpoint on kubelet which is considered a "debug" endpoint, so it might be worth exposing through the options, but by default it is exposed in most k8s configs AFAICT.
- Loading branch information
Showing
4 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/cpuguy83/strongerrors" | ||
"github.com/virtual-kubelet/virtual-kubelet/log" | ||
"k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/serializer" | ||
) | ||
|
||
// ContainerLogsBackend is used in place of backend implementations for the provider's pods | ||
type RunningPodsBackend interface { | ||
// GetPods retrieves a list of all pods running on the provider (can be cached). | ||
GetPods(context.Context) ([]*v1.Pod, error) | ||
} | ||
|
||
func RunningPodsHandlerFunc(p RunningPodsBackend) http.HandlerFunc { | ||
scheme := runtime.NewScheme() | ||
v1.SchemeBuilder.AddToScheme(scheme) | ||
codecs := serializer.NewCodecFactory(scheme) | ||
|
||
return handleError(func(w http.ResponseWriter, req *http.Request) error { | ||
ctx := req.Context() | ||
ctx = log.WithLogger(ctx, log.L) | ||
pods, err := p.GetPods(ctx) | ||
if err != nil { | ||
return strongerrors.System(err) | ||
} | ||
|
||
// Borrowed from github.com/kubernetes/kubernetes/pkg/kubelet/server/server.go | ||
// encodePods creates an v1.PodList object from pods and returns the encoded | ||
// PodList. | ||
podList := new(v1.PodList) | ||
for _, pod := range pods { | ||
podList.Items = append(podList.Items, *pod) | ||
} | ||
codec := codecs.LegacyCodec(v1.SchemeGroupVersion) | ||
data, err := runtime.Encode(codec, podList) | ||
if err != nil { | ||
return strongerrors.System(err) | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write(data) | ||
if err != nil { | ||
return strongerrors.System(err) | ||
} | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters