mirror of
https://github.com/lingble/talos.git
synced 2025-12-21 15:07:07 +00:00
fix(osd): Read talos service logs from file (#663)
Signed-off-by: Brad Beam <brad.beam@talos-systems.com>
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containerd/cgroups"
|
"github.com/containerd/cgroups"
|
||||||
@@ -271,41 +272,19 @@ func (r *Registrator) Dmesg(ctx context.Context, in *empty.Empty) (data *proto.D
|
|||||||
// be requested and the contents of the log file are streamed in chunks.
|
// be requested and the contents of the log file are streamed in chunks.
|
||||||
// nolint: gocyclo
|
// nolint: gocyclo
|
||||||
func (r *Registrator) Logs(req *proto.LogsRequest, l proto.OSD_LogsServer) (err error) {
|
func (r *Registrator) Logs(req *proto.LogsRequest, l proto.OSD_LogsServer) (err error) {
|
||||||
var (
|
var filename string
|
||||||
client *containerd.Client
|
|
||||||
ctx context.Context
|
|
||||||
pods []*pod
|
|
||||||
task *tasks.GetResponse
|
|
||||||
)
|
|
||||||
|
|
||||||
pods, err = podInfo(req.Namespace)
|
switch {
|
||||||
if err != nil {
|
case req.Namespace == "system" || req.Id == "kubelet" || req.Id == "kubeadm":
|
||||||
|
filename = filepath.Join("/var/log", req.Id+".log")
|
||||||
|
default:
|
||||||
|
if filename, err = k8slogs(req); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
client, ctx, err = connect(req.Namespace)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// nolint: errcheck
|
|
||||||
defer client.Close()
|
|
||||||
|
|
||||||
for _, containers := range pods {
|
|
||||||
for _, container := range containers.Containers {
|
|
||||||
if container.Display != req.Id {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if container.LogFile == "" {
|
|
||||||
task, err = client.TaskService().Get(ctx, &tasks.GetRequest{ContainerID: container.ID})
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
container.LogFile = task.Process.Stdout
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var file *os.File
|
var file *os.File
|
||||||
file, err = os.OpenFile(container.LogFile, os.O_RDONLY, 0)
|
file, err = os.OpenFile(filename, os.O_RDONLY, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -322,8 +301,6 @@ func (r *Registrator) Logs(req *proto.LogsRequest, l proto.OSD_LogsServer) (err
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -462,3 +439,45 @@ func (r *Registrator) DF(ctx context.Context, in *empty.Empty) (reply *proto.DFR
|
|||||||
|
|
||||||
return reply, multiErr.ErrorOrNil()
|
return reply, multiErr.ErrorOrNil()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func k8slogs(req *proto.LogsRequest) (string, error) {
|
||||||
|
var (
|
||||||
|
client *containerd.Client
|
||||||
|
ctx context.Context
|
||||||
|
err error
|
||||||
|
pods []*pod
|
||||||
|
task *tasks.GetResponse
|
||||||
|
)
|
||||||
|
|
||||||
|
pods, err = podInfo(req.Namespace)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
client, ctx, err = connect(req.Namespace)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
// nolint: errcheck
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
for _, containers := range pods {
|
||||||
|
for _, container := range containers.Containers {
|
||||||
|
if container.Display != req.Id {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if container.LogFile == "" {
|
||||||
|
task, err = client.TaskService().Get(ctx, &tasks.GetRequest{ContainerID: container.ID})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
container.LogFile = task.Process.Stdout
|
||||||
|
}
|
||||||
|
|
||||||
|
return container.LogFile, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user