2022-11-09 16:07:28 +00:00
|
|
|
package util
|
2022-11-09 12:19:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2022-11-09 16:07:28 +00:00
|
|
|
|
|
|
|
m "git.k-space.ee/k-space/logmower-shipper/pkg/mongo"
|
2022-11-09 12:19:56 +00:00
|
|
|
)
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
func ParseLogFilename(name string) (i m.KubeInfo, ok bool) {
|
2022-11-09 12:19:56 +00:00
|
|
|
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`
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
i.Pod, name, ok = strings.Cut(name, "_")
|
2022-11-09 12:19:56 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
i.Namespace, name, ok = strings.Cut(name, "_")
|
2022-11-09 12:19:56 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
i.ContainerName, name, ok = strings.Cut(name, "-")
|
2022-11-09 12:19:56 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
i.ContainerId = strings.TrimSuffix(name, ".log")
|
2022-11-09 12:19:56 +00:00
|
|
|
if !strings.HasSuffix(name, ".log") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
return i, true
|
2022-11-09 12:19:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
func Hostinfo(nodeName string) (h m.Host, err error) {
|
2022-11-09 12:19:56 +00:00
|
|
|
if nodeName == "" {
|
|
|
|
nodeName, err = os.Hostname()
|
|
|
|
if err != nil {
|
2022-11-09 16:07:28 +00:00
|
|
|
return h, fmt.Errorf("getting hostname: %w", err)
|
2022-11-09 12:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Name = strings.TrimSpace(nodeName)
|
|
|
|
|
|
|
|
id, err := os.ReadFile("/etc/machine-id")
|
|
|
|
if err != nil {
|
2022-11-09 16:07:28 +00:00
|
|
|
return h, fmt.Errorf("getting machineId: %w", err)
|
2022-11-09 12:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.Id = strings.TrimSpace(string(id))
|
|
|
|
|
|
|
|
h.Arch = runtime.GOARCH
|
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
return h, nil
|
2022-11-09 12:19:56 +00:00
|
|
|
}
|