Move API handlers to separate package

This makes the package split a little cleaner and easier to import the
HTTP handlers for other consumers.
This commit is contained in:
Brian Goff
2018-09-18 10:05:54 -07:00
parent 74f76c75d5
commit da5e24ef4d
6 changed files with 197 additions and 150 deletions

42
vkubelet/api/exec.go Normal file
View File

@@ -0,0 +1,42 @@
package api
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
"k8s.io/kubernetes/pkg/kubelet/server/remotecommand"
)
// PodExecHandlerFunc makes an http handler func from a Provider which execs a command in a pod's container
// Note that this handler currently depends on gorrilla/mux to get url parts as variables.
// TODO(@cpuguy83): don't force gorilla/mux on consumers of this function
func PodExecHandlerFunc(backend remotecommand.Executor) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
namespace := vars["namespace"]
pod := vars["pod"]
container := vars["container"]
supportedStreamProtocols := strings.Split(req.Header.Get("X-Stream-Protocol-Version"), ",")
q := req.URL.Query()
command := q["command"]
// TODO: tty flag causes remotecommand.createStreams to wait for the wrong number of streams
streamOpts := &remotecommand.Options{
Stdin: true,
Stdout: true,
Stderr: true,
TTY: false,
}
idleTimeout := time.Second * 30
streamCreationTimeout := time.Second * 30
remotecommand.ServeExec(w, req, backend, fmt.Sprintf("%s-%s", namespace, pod), "", container, command, streamOpts, idleTimeout, streamCreationTimeout, supportedStreamProtocols)
}
}

31
vkubelet/api/helpers.go Normal file
View File

@@ -0,0 +1,31 @@
package api
import (
"io"
"net/http"
"github.com/cpuguy83/strongerrors/status"
"github.com/virtual-kubelet/virtual-kubelet/log"
)
type handlerFunc func(http.ResponseWriter, *http.Request) error
func handleError(f handlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
err := f(w, req)
if err == nil {
return
}
code, _ := status.HTTPCode(err)
w.WriteHeader(code)
io.WriteString(w, err.Error())
logger := log.G(req.Context()).WithError(err).WithField("httpStatusCode", code)
if code >= 500 {
logger.Error("Internal server error on request")
} else {
log.Trace(logger, "Error on request")
}
}
}

53
vkubelet/api/logs.go Normal file
View File

@@ -0,0 +1,53 @@
package api
import (
"context"
"io"
"net/http"
"strconv"
"github.com/cpuguy83/strongerrors"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
// ContainerLogsBackend is used in place of backend implementations for getting container logs
type ContainerLogsBackend interface {
GetContainerLogs(ctx context.Context, namespace, podName, containerName string, tail int) (string, error)
}
// PodLogsHandlerFunc creates an http handler function from a provider to serve logs from a pod
func PodLogsHandlerFunc(p ContainerLogsBackend) http.HandlerFunc {
return handleError(func(w http.ResponseWriter, req *http.Request) error {
vars := mux.Vars(req)
if len(vars) != 3 {
return strongerrors.NotFound(errors.New("not found"))
}
ctx := req.Context()
namespace := vars["namespace"]
pod := vars["pod"]
container := vars["container"]
tail := 10
q := req.URL.Query()
if queryTail := q.Get("tailLines"); queryTail != "" {
t, err := strconv.Atoi(queryTail)
if err != nil {
return strongerrors.InvalidArgument(errors.Wrap(err, "could not parse \"tailLines\""))
}
tail = t
}
podsLogs, err := p.GetContainerLogs(ctx, namespace, pod, container, tail)
if err != nil {
return errors.Wrap(err, "error getting container logs?)")
}
if _, err := io.WriteString(w, podsLogs); err != nil {
return strongerrors.Unknown(errors.Wrap(err, "error writing response to client"))
}
return nil
})
}

39
vkubelet/api/stats.go Normal file
View File

@@ -0,0 +1,39 @@
package api
import (
"context"
"encoding/json"
"net/http"
"github.com/cpuguy83/strongerrors"
"github.com/pkg/errors"
stats "k8s.io/kubernetes/pkg/kubelet/apis/stats/v1alpha1"
)
// PodMetricsBackend is used in place of backend implementations to get k8s pod metrics.
type PodMetricsBackend interface {
GetStatsSummary(context.Context) (*stats.Summary, error)
}
// PodMetricsHandlerFunc makes an HTTP handler for implementing the kubelet summary stats endpoint
func PodMetricsHandlerFunc(b PodMetricsBackend) http.HandlerFunc {
return handleError(func(w http.ResponseWriter, req *http.Request) error {
stats, err := b.GetStatsSummary(req.Context())
if err != nil {
if errors.Cause(err) == context.Canceled {
return strongerrors.Cancelled(err)
}
return errors.Wrap(err, "error getting status from provider")
}
b, err := json.Marshal(stats)
if err != nil {
return strongerrors.Unknown(errors.Wrap(err, "error marshalling stats"))
}
if _, err := w.Write(b); err != nil {
return strongerrors.Unknown(errors.Wrap(err, "could not write to client"))
}
return nil
})
}