logmower-shipper/pkg/mongoStruct/gather.go

61 lines
1.1 KiB
Go
Raw Normal View History

package mongoStruct
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
func ParseLogName(name string) (m KubeInfo, ok bool) {
name = filepath.Base(name)
// https://github.com/kubernetes/design-proposals-archive/blob/8da1442ea29adccea40693357d04727127e045ed/node/kubelet-cri-logging.md
// <pod_name>_<pod_namespace>_<container_name>-<container_id>.log`
m.Pod, name, ok = strings.Cut(name, "_")
if !ok {
return
}
m.Namespace, name, ok = strings.Cut(name, "_")
if !ok {
return
}
m.ContainerName, name, ok = strings.Cut(name, "-")
if !ok {
return
}
m.ContainerId = strings.TrimSuffix(name, ".log")
if !strings.HasSuffix(name, ".log") {
return
}
return m, true
}
func (h *HostInfo) Populate(nodeName string) (err error) {
if nodeName == "" {
nodeName, err = os.Hostname()
if err != nil {
return fmt.Errorf("getting hostname: %w", err)
}
}
h.Name = strings.TrimSpace(nodeName)
id, err := os.ReadFile("/etc/machine-id")
if err != nil {
return fmt.Errorf("getting machineId: %w", err)
}
h.Id = strings.TrimSpace(string(id))
h.Arch = runtime.GOARCH
return nil
}