Kubernetes logging (#1054)

* wip

* Implementation of Kubernetes Logger

* Missing file

* Skip test in Travis
This commit is contained in:
Jake Sanders
2019-12-20 23:16:05 +00:00
committed by Asim Aslam
parent ae12fd1021
commit ce33e3b072
8 changed files with 224 additions and 15 deletions

View File

@@ -9,6 +9,7 @@ import (
"net/http"
"os"
"path"
"strconv"
"github.com/micro/go-micro/util/kubernetes/client/api"
"github.com/micro/go-micro/util/log"
@@ -26,6 +27,26 @@ type client struct {
opts *api.Options
}
// NewLocalDevClient returns a client that can be used with `kubectl proxy` on an optional port
func NewLocalDevClient(port ...int) *client {
var p int
if len(port) > 1 {
log.Fatal("Expected 0 or 1 port parameters")
}
if len(port) == 0 {
p = 8001
} else {
p = port[0]
}
return &client{
opts: &api.Options{
Client: http.DefaultClient,
Host: "http://localhost:" + strconv.Itoa(p),
Namespace: "default",
},
}
}
// NewClientInCluster creates a Kubernetes client for use from within a k8s pod.
func NewClientInCluster() *client {
host := "https://" + os.Getenv("KUBERNETES_SERVICE_HOST") + ":" + os.Getenv("KUBERNETES_SERVICE_PORT")
@@ -118,17 +139,29 @@ func (c *client) Get(r *Resource, labels map[string]string) error {
}
// Logs returns logs for a pod
func (c *client) Logs(podName string) (io.ReadCloser, error) {
func (c *client) Logs(podName string, options ...LogOption) (io.ReadCloser, error) {
opts := &LogOptions{}
for _, o := range options {
o(opts)
}
req := api.NewRequest(c.opts).
Get().
Resource("pod").
SubResource("log").
Name(podName)
if opts.AdditionalParams != nil {
req.Params(&api.Params{Additional: opts.AdditionalParams})
}
resp, err := req.Raw()
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
resp.Body.Close()
return nil, errors.New(resp.Request.URL.String() + ": " + resp.Status)
}
return resp.Body, nil
}