Skip to content

Commit

Permalink
now able to get slurm logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Surax98 committed Oct 18, 2023
1 parent 3b1a633 commit 9166fa7
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/sidecars/slurm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
17 changes: 17 additions & 0 deletions pkg/interlink/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
123 changes: 123 additions & 0 deletions pkg/sidecars/slurm/GetLogs.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 9166fa7

Please sign in to comment.