From 9166fa7bd022dc10d91ce6d40d4f4d2ce76237c4 Mon Sep 17 00:00:00 2001 From: Surax98 Date: Fri, 13 Oct 2023 09:47:13 +0000 Subject: [PATCH] now able to get slurm logs --- cmd/sidecars/slurm/main.go | 1 + pkg/common/types.go | 1 + pkg/interlink/logs.go | 17 +++++ pkg/sidecars/slurm/GetLogs.go | 123 ++++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 pkg/sidecars/slurm/GetLogs.go diff --git a/cmd/sidecars/slurm/main.go b/cmd/sidecars/slurm/main.go index cc335140..2dfc364b 100644 --- a/cmd/sidecars/slurm/main.go +++ b/cmd/sidecars/slurm/main.go @@ -27,6 +27,7 @@ func main() { mutex.HandleFunc("/status", slurm.StatusHandler) mutex.HandleFunc("/create", slurm.SubmitHandler) mutex.HandleFunc("/delete", slurm.StopHandler) + mutex.HandleFunc("/getLogs", slurm.GetLogsHandler) err := http.ListenAndServe(":"+commonIL.InterLinkConfigInst.Sidecarport, mutex) if err != nil { diff --git a/pkg/common/types.go b/pkg/common/types.go index 3b05ece0..439ff716 100644 --- a/pkg/common/types.go +++ b/pkg/common/types.go @@ -76,6 +76,7 @@ type ContainerLogOpts struct { type LogStruct struct { Namespace string `json:"Namespace"` + PodUID string `json:"PodUID"` PodName string `json:"PodName"` ContainerName string `json:"ContainerName"` Opts ContainerLogOpts `json:"Opts"` diff --git a/pkg/interlink/logs.go b/pkg/interlink/logs.go index 10e3b6df..5beb4a72 100644 --- a/pkg/interlink/logs.go +++ b/pkg/interlink/logs.go @@ -10,6 +10,7 @@ import ( "github.com/containerd/containerd/log" commonIL "github.com/intertwin-eu/interlink/pkg/common" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func GetLogsHandler(w http.ResponseWriter, r *http.Request) { @@ -29,6 +30,15 @@ func GetLogsHandler(w http.ResponseWriter, r *http.Request) { return } + pod, err := Clientset.CoreV1().Pods(req2.Namespace).Get(Ctx, req2.PodName, metav1.GetOptions{}) + if err != nil { + statusCode = http.StatusInternalServerError + w.WriteHeader(statusCode) + log.G(Ctx).Error(err) + return + } + req2.PodUID = string(pod.UID) + if (req2.Opts.Tail != 0 && req2.Opts.LimitBytes != 0) || (req2.Opts.SinceSeconds != 0 && !req2.Opts.SinceTime.IsZero()) { statusCode = http.StatusInternalServerError w.WriteHeader(statusCode) @@ -47,6 +57,13 @@ func GetLogsHandler(w http.ResponseWriter, r *http.Request) { return } + bodyBytes, err = json.Marshal(req2) + if err != nil { + statusCode = http.StatusInternalServerError + w.WriteHeader(statusCode) + log.G(Ctx).Error(err) + return + } reader := bytes.NewReader(bodyBytes) req, err := http.NewRequest(http.MethodPost, commonIL.InterLinkConfigInst.Sidecarurl+":"+commonIL.InterLinkConfigInst.Sidecarport+"/getLogs", reader) if err != nil { diff --git a/pkg/sidecars/slurm/GetLogs.go b/pkg/sidecars/slurm/GetLogs.go new file mode 100644 index 00000000..ce415911 --- /dev/null +++ b/pkg/sidecars/slurm/GetLogs.go @@ -0,0 +1,123 @@ +package slurm + +import ( + "encoding/json" + "errors" + "io" + "net/http" + "strings" + "time" + + OSexec "os/exec" + + "github.com/containerd/containerd/log" + commonIL "github.com/intertwin-eu/interlink/pkg/common" +) + +func GetLogsHandler(w http.ResponseWriter, r *http.Request) { + log.G(Ctx).Info("Docker Sidecar: received GetLogs call") + var req commonIL.LogStruct + statusCode := http.StatusOK + currentTime := time.Now() + + bodyBytes, err := io.ReadAll(r.Body) + if err != nil { + statusCode = http.StatusInternalServerError + w.WriteHeader(statusCode) + w.Write([]byte("Some errors occurred while checking container status. Check Docker Sidecar's logs")) + log.G(Ctx).Error(err) + return + } + + err = json.Unmarshal(bodyBytes, &req) + if err != nil { + statusCode = http.StatusInternalServerError + w.WriteHeader(statusCode) + w.Write([]byte("Some errors occurred while checking container status. Check Docker Sidecar's logs")) + log.G(Ctx).Error(err) + return + } + + var cmd *OSexec.Cmd + if req.Opts.Timestamps { + log.G(Ctx).Error(errors.New("Not Implemented")) + statusCode = http.StatusInternalServerError + w.WriteHeader(statusCode) + return + } else { + JIDs = append(JIDs, JidStruct{PodUID: "58b735d3-3973-4474-bdda-949978cd13a3", JID: "123456"}) + for _, jid := range JIDs { + if jid.PodUID == req.PodUID { + cmd = OSexec.Command("cat", "slurm-"+jid.JID+".out") + } + } + } + + output, err := cmd.CombinedOutput() + + if err != nil { + log.G(Ctx).Error(err) + statusCode = http.StatusInternalServerError + w.WriteHeader(statusCode) + return + } + + var returnedLogs string + + if req.Opts.Tail != 0 { + var lastLines []string + + splittedLines := strings.Split(string(output), "\n") + + if req.Opts.Tail > len(splittedLines) { + lastLines = splittedLines + } else { + lastLines = splittedLines[len(splittedLines)-req.Opts.Tail-1:] + } + + for _, line := range lastLines { + returnedLogs += line + "\n" + } + } else { + var lastBytes []byte + if req.Opts.LimitBytes > len(output) { + lastBytes = output + } else { + lastBytes = output[len(output)-req.Opts.LimitBytes-1:] + } + + returnedLogs = string(lastBytes) + } + + if req.Opts.Timestamps && (req.Opts.SinceSeconds != 0 || !req.Opts.SinceTime.IsZero()) { + temp := returnedLogs + returnedLogs = "" + splittedLogs := strings.Split(temp, "\n") + timestampFormat := "2006-01-02T15:04:05.999999999Z" + + for _, Log := range splittedLogs { + part := strings.SplitN(Log, " ", 2) + timestampString := part[0] + timestamp, err := time.Parse(timestampFormat, timestampString) + if err != nil { + continue + } + if req.Opts.SinceSeconds != 0 { + if currentTime.Sub(timestamp).Seconds() > float64(req.Opts.SinceSeconds) { + returnedLogs += Log + "\n" + } + } else { + if timestamp.Sub(req.Opts.SinceTime).Seconds() >= 0 { + returnedLogs += Log + "\n" + } + } + } + } + + if statusCode != http.StatusOK { + w.Write([]byte("Some errors occurred while checking container status. Check Docker Sidecar's logs")) + } else { + w.WriteHeader(statusCode) + w.Write([]byte(returnedLogs)) + } +}