Update k8s log options

This commit is contained in:
Asim Aslam 2019-12-24 17:33:05 +00:00
parent 81e20160f5
commit 5c8d1ae2b9
5 changed files with 40 additions and 29 deletions

View File

@ -24,7 +24,12 @@ func (k *klog) podLogStream(podName string, stream *kubeStream) {
p := make(map[string]string)
p["follow"] = "true"
body, err := k.client.Logs(podName, client.AdditionalParams(p))
// get the logs for the pod
body, err := k.client.Log(&client.Resource{
Name: podName,
Kind: "pod",
}, client.LogParams(p))
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return
@ -98,14 +103,14 @@ func (k *klog) Read(options ...log.ReadOption) ([]log.Record, error) {
o(opts)
}
logsToGet, err := k.getMatchingPods()
pods, err := k.getMatchingPods()
if err != nil {
return nil, err
}
var records []log.Record
for _, l := range logsToGet {
for _, pod := range pods {
logParams := make(map[string]string)
if !opts.Since.Equal(time.Time{}) {
@ -120,7 +125,11 @@ func (k *klog) Read(options ...log.ReadOption) ([]log.Record, error) {
logParams["follow"] = "true"
}
logs, err := k.client.Logs(l, client.AdditionalParams(logParams))
logs, err := k.client.Log(&client.Resource{
Name: pod,
Kind: "pod",
}, client.LogParams(logParams))
if err != nil {
return nil, err
}
@ -130,7 +139,7 @@ func (k *klog) Read(options ...log.ReadOption) ([]log.Record, error) {
for s.Scan() {
record := k.parse(s.Text())
record.Metadata["pod"] = l
record.Metadata["pod"] = pod
records = append(records, record)
}
}

View File

@ -138,20 +138,21 @@ func (c *client) Get(r *Resource, labels map[string]string) error {
Into(r.Value)
}
// Logs returns logs for a pod
func (c *client) Logs(podName string, options ...LogOption) (io.ReadCloser, error) {
opts := &LogOptions{}
for _, o := range options {
o(opts)
// Log returns logs for a pod
func (c *client) Log(r *Resource, opts ...LogOption) (io.ReadCloser, error) {
var options LogOptions
for _, o := range opts {
o(&options)
}
req := api.NewRequest(c.opts).
Get().
Resource("pod").
Resource(r.Kind).
SubResource("log").
Name(podName)
Name(r.Name)
if opts.AdditionalParams != nil {
req.Params(&api.Params{Additional: opts.AdditionalParams})
if options.Params != nil {
req.Params(&api.Params{Additional: options.Params})
}
resp, err := req.Raw()

View File

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

View File

@ -1,13 +0,0 @@
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

@ -0,0 +1,14 @@
package client
type LogOptions struct {
Params map[string]string
}
type LogOption func(*LogOptions)
// LogParams provides additional params for logs
func LogParams(p map[string]string) LogOption {
return func(l *LogOptions) {
l.Params = p
}
}