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

@@ -34,6 +34,7 @@ type Request struct {
type Params struct {
LabelSelector map[string]string
Annotations map[string]string
Additional map[string]string
}
// verb sets method
@@ -136,6 +137,9 @@ func (r *Request) Params(p *Params) *Request {
// set and overwrite the value
r.params.Set("labelSelector", value)
}
for k, v := range p.Additional {
r.params.Set(k, v)
}
return r
}

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
}

View File

@@ -26,7 +26,7 @@ type Kubernetes interface {
// List lists API resources
List(*Resource) error
// Logs gets logs from a pod
Logs(string) (io.ReadCloser, error)
Logs(string, ...LogOption) (io.ReadCloser, error)
}
// NewService returns default micro kubernetes service definition

View File

@@ -0,0 +1,13 @@
package client
type LogOptions struct {
AdditionalParams map[string]string
}
type LogOption func(*LogOptions)
func AdditionalParams(p map[string]string) LogOption {
return func(l *LogOptions) {
l.AdditionalParams = p
}
}

View File

@@ -79,7 +79,7 @@ type Container struct {
Ports []ContainerPort `json:"ports,omitempty"`
}
// PodSpec
// PodSpec is a pod
type PodSpec struct {
Containers []Container `json:"containers"`
}
@@ -131,3 +131,7 @@ type Deployment struct {
type DeploymentList struct {
Items []Deployment `json:"items"`
}
type PodList struct {
Items []Template `json:"items"`
}