Skip to content

Commit

Permalink
Add the /runningpods/ api endpoint
Browse files Browse the repository at this point in the history
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
sargun committed May 9, 2019
1 parent d6e945b commit cceb51d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/virtual-kubelet/commands/root/http.go
Expand Up @@ -86,7 +86,7 @@ func setupHTTPServer(ctx context.Context, p providers.Provider, cfg *apiServerCo
}

mux := http.NewServeMux()
vkubelet.AttachPodRoutes(p, mux)
vkubelet.AttachPodRoutes(p, mux, true)

s := &http.Server{
Handler: mux,
Expand Down
53 changes: 53 additions & 0 deletions vkubelet/api/pods.go
@@ -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
})
}
11 changes: 8 additions & 3 deletions vkubelet/apiserver.go
Expand Up @@ -21,9 +21,14 @@ type ServeMux interface {
}

// PodHandler creates an http handler for interacting with pods/containers.
func PodHandler(p providers.Provider) http.Handler {
func PodHandler(p providers.Provider, debug bool) http.Handler {
r := mux.NewRouter()

// This matches the behaviour in the reference kubelet
r.StrictSlash(true)
if debug {
r.HandleFunc("/runningpods/", api.RunningPodsHandlerFunc(p)).Methods("GET")
}
r.HandleFunc("/containerLogs/{namespace}/{pod}/{container}", api.PodLogsHandlerFunc(p)).Methods("GET")
r.HandleFunc("/exec/{namespace}/{pod}/{container}", api.PodExecHandlerFunc(p)).Methods("POST")
r.NotFoundHandler = http.HandlerFunc(NotFound)
Expand Down Expand Up @@ -58,8 +63,8 @@ func MetricsSummaryHandler(p providers.Provider) http.Handler {
//
// Callers should take care to namespace the serve mux as they see fit, however
// these routes get called by the Kubernetes API server.
func AttachPodRoutes(p providers.Provider, mux ServeMux) {
mux.Handle("/", InstrumentHandler(PodHandler(p)))
func AttachPodRoutes(p providers.Provider, mux ServeMux, debug bool) {
mux.Handle("/", InstrumentHandler(PodHandler(p, debug)))
}

// AttachMetricsRoutes adds the http routes for pod/node metrics to the passed in serve mux.
Expand Down

0 comments on commit cceb51d

Please sign in to comment.