-
Notifications
You must be signed in to change notification settings - Fork 9
/
logs.go
47 lines (36 loc) · 1.5 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package kubernetes
import (
"context"
"io"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/runtime"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
)
func (r *Cluster) FetchLogsTo(ctx context.Context, w io.Writer, reference *runtimepb.ContainerReference, opts runtime.FetchLogsOpts) error {
cpr := &kubedef.ContainerPodReference{}
if err := reference.Opaque.UnmarshalTo(cpr); err != nil {
return fnerrors.InternalError("invalid reference: %w", err)
}
return fetchPodLogs(ctx, r.cli, w, cpr.Namespace, cpr.PodName, cpr.Container, opts)
}
func fetchPodLogs(ctx context.Context, cli *kubernetes.Clientset, w io.Writer, namespace, podName, containerName string, opts runtime.FetchLogsOpts) error {
logOpts := &corev1.PodLogOptions{Follow: opts.Follow, Container: containerName, Previous: opts.FetchLastFailure, Timestamps: opts.IncludeTimestamps}
if opts.TailLines > 0 {
var tailLines int64 = int64(opts.TailLines)
logOpts.TailLines = &tailLines
}
logsReq := cli.CoreV1().Pods(namespace).GetLogs(podName, logOpts)
content, err := logsReq.Stream(ctx)
if err != nil {
return err
}
defer content.Close()
_, err = io.Copy(w, content)
return err
}