Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support for downloading pod logs #4539

Merged
merged 27 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
129 changes: 97 additions & 32 deletions api/k8s/application/k8sApplicationRestHandler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package application

import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
Expand All @@ -26,10 +28,12 @@ import (
"github.com/devtron-labs/devtron/pkg/terminal"
"github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/google/uuid"
"github.com/gorilla/mux"
errors2 "github.com/juju/errors"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"io"
errors3 "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -41,6 +45,7 @@ type K8sApplicationRestHandler interface {
DeleteResource(w http.ResponseWriter, r *http.Request)
ListEvents(w http.ResponseWriter, r *http.Request)
GetPodLogs(w http.ResponseWriter, r *http.Request)
DownloadPodLogs(w http.ResponseWriter, r *http.Request)
GetTerminalSession(w http.ResponseWriter, r *http.Request)
GetResourceInfo(w http.ResponseWriter, r *http.Request)
GetHostUrlsByBatch(w http.ResponseWriter, r *http.Request)
Expand All @@ -62,9 +67,9 @@ type K8sApplicationRestHandlerImpl struct {
validator *validator.Validate
enforcerUtil rbac.EnforcerUtil
enforcerUtilHelm rbac.EnforcerUtilHelm
helmAppService client.HelmAppService
userService user.UserService
k8sCommonService k8s.K8sCommonService
helmAppService client.HelmAppService
userService user.UserService
k8sCommonService k8s.K8sCommonService
}

func NewK8sApplicationRestHandlerImpl(logger *zap.SugaredLogger, k8sApplicationService application2.K8sApplicationService, pump connector.Pump, terminalSessionHandler terminal.TerminalSessionHandler, enforcer casbin.Enforcer, enforcerUtilHelm rbac.EnforcerUtilHelm, enforcerUtil rbac.EnforcerUtil, helmAppService client.HelmAppService, userService user.UserService, k8sCommonService k8s.K8sCommonService, validator *validator.Validate) *K8sApplicationRestHandlerImpl {
Expand Down Expand Up @@ -608,11 +613,99 @@ func (handler *K8sApplicationRestHandlerImpl) GetPodLogs(w http.ResponseWriter,
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
handler.requestValidationAndRBAC(w, r, token, request)
lastEventId := r.Header.Get("Last-Event-ID")
kartik-579 marked this conversation as resolved.
Show resolved Hide resolved
isReconnect := false
if len(lastEventId) > 0 {
lastSeenMsgId, err := strconv.ParseInt(lastEventId, 10, 64)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
lastSeenMsgId = lastSeenMsgId + 1 //increased by one ns to avoid duplicate
t := v1.Unix(0, lastSeenMsgId)
request.K8sRequest.PodLogsRequest.SinceTime = &t
isReconnect = true
}
kartik-579 marked this conversation as resolved.
Show resolved Hide resolved
stream, err := handler.k8sApplicationService.GetPodLogs(r.Context(), request)
//err is handled inside StartK8sStreamWithHeartBeat method
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
}
defer cancel()
defer util.Close(stream, handler.logger)
handler.pump.StartK8sStreamWithHeartBeat(w, isReconnect, stream, err)
}

func (handler *K8sApplicationRestHandlerImpl) DownloadPodLogs(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("token")
request, err := handler.k8sApplicationService.ValidatePodLogsRequestQuery(r)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
handler.requestValidationAndRBAC(w, r, token, request)

// just to make sure follow flag is set to false when downloading logs
request.K8sRequest.PodLogsRequest.Follow = false
kartik-579 marked this conversation as resolved.
Show resolved Hide resolved

stream, err := handler.k8sApplicationService.GetPodLogs(r.Context(), request)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
}
defer cancel()
defer util.Close(stream, handler.logger)

var dataBuffer bytes.Buffer
bufReader := bufio.NewReader(stream)
eof := false
for !eof {
log, err := bufReader.ReadString('\n')
if err == io.EOF {
eof = true
// stop if we reached end of stream and the next line is empty
if log == "" {
break
}
} else if err != nil && err != io.EOF {
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
_, err = dataBuffer.Write([]byte(log))
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
kartik-579 marked this conversation as resolved.
Show resolved Hide resolved
}
podLogsFilename := fmt.Sprintf("podlogs-%s-%s.txt", request.K8sRequest.ResourceIdentifier.Name, uuid.New().String())
kartik-579 marked this conversation as resolved.
Show resolved Hide resolved
common.WriteOctetStreamResp(w, r, dataBuffer.Bytes(), podLogsFilename)
return
}

func (handler *K8sApplicationRestHandlerImpl) requestValidationAndRBAC(w http.ResponseWriter, r *http.Request, token string, request *k8s.ResourceRequestBean) {
if request.AppIdentifier != nil {
if request.DeploymentType == bean2.HelmInstalledType {
valid, err := handler.k8sApplicationService.ValidateResourceRequest(r.Context(), request.AppIdentifier, request.K8sRequest)
if err != nil || !valid {
handler.logger.Errorw("error in validating resource request", "err", err)
handler.logger.Errorw("error in validating resource request", "err", err, "request.AppIdentifier", request.AppIdentifier, "request.K8sRequest", request.K8sRequest)
apiError := util2.ApiError{
InternalMessage: "failed to validate the resource with error " + err.Error(),
UserMessage: "Failed to validate resource",
Expand Down Expand Up @@ -659,34 +752,6 @@ func (handler *K8sApplicationRestHandlerImpl) GetPodLogs(w http.ResponseWriter,
common.WriteJsonResp(w, errors.New("can not get pod logs as target cluster is not provided"), nil, http.StatusBadRequest)
return
}
lastEventId := r.Header.Get("Last-Event-ID")
isReconnect := false
if len(lastEventId) > 0 {
lastSeenMsgId, err := strconv.ParseInt(lastEventId, 10, 64)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
lastSeenMsgId = lastSeenMsgId + 1 //increased by one ns to avoid duplicate
t := v1.Unix(0, lastSeenMsgId)
request.K8sRequest.PodLogsRequest.SinceTime = &t
isReconnect = true
}
stream, err := handler.k8sApplicationService.GetPodLogs(r.Context(), request)
//err is handled inside StartK8sStreamWithHeartBeat method
ctx, cancel := context.WithCancel(r.Context())
if cn, ok := w.(http.CloseNotifier); ok {
go func(done <-chan struct{}, closed <-chan bool) {
select {
case <-done:
case <-closed:
cancel()
}
}(ctx.Done(), cn.CloseNotify())
}
defer cancel()
defer util.Close(stream, handler.logger)
handler.pump.StartK8sStreamWithHeartBeat(w, isReconnect, stream, err)
}

func (handler *K8sApplicationRestHandlerImpl) GetTerminalSession(w http.ResponseWriter, r *http.Request) {
Expand Down
11 changes: 10 additions & 1 deletion api/k8s/application/k8sApplicationRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,18 @@ func (impl *K8sApplicationRouterImpl) InitK8sApplicationRouter(k8sAppRouter *mux
//Queries("clusterId", "{clusterId}", "namespace", "${namespace}").
//Queries("sinceSeconds", "{sinceSeconds}").
Queries("follow", "{follow}").
Queries("tailLines", "{tailLines}").
//Queries("tailLines", "{tailLines}").
HandlerFunc(impl.k8sApplicationRestHandler.GetPodLogs).Methods("GET")

k8sAppRouter.Path("/pods/logs/download/{podName}").
Queries("containerName", "{containerName}").
//Queries("containerName", "{containerName}", "appId", "{appId}").
//Queries("clusterId", "{clusterId}", "namespace", "${namespace}").
//Queries("sinceSeconds", "{sinceSeconds}").
kartik-579 marked this conversation as resolved.
Show resolved Hide resolved
//Queries("follow", "{follow}").
//Queries("tailLines", "{tailLines}").
HandlerFunc(impl.k8sApplicationRestHandler.DownloadPodLogs).Methods("GET")

k8sAppRouter.Path("/pod/exec/session/{identifier}/{namespace}/{pod}/{shell}/{container}").
HandlerFunc(impl.k8sApplicationRestHandler.GetTerminalSession).Methods("GET")
k8sAppRouter.PathPrefix("/pod/exec/sockjs/ws").Handler(terminal.CreateAttachHandler("/pod/exec/sockjs/ws"))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set v1.8.0
github.com/devtron-labs/authenticator v0.4.33
github.com/devtron-labs/common-lib v0.0.9-0.20231226070212-c47f7a07ebf5
github.com/devtron-labs/common-lib v0.0.9-beta3
github.com/devtron-labs/protos v0.0.0-20230503113602-282404f70fd2
github.com/evanphx/json-patch v5.6.0+incompatible
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4 h1:YcpmyvADG
github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
github.com/devtron-labs/authenticator v0.4.33 h1:FpAV3ZgFluaRFcMwPpwxr/mwSipJ16XRvgABq3BzP5Y=
github.com/devtron-labs/authenticator v0.4.33/go.mod h1:ozNfT8WcruiSgnUbyp48WVfc41++W6xYXhKFp67lNTU=
github.com/devtron-labs/common-lib v0.0.9-0.20231226070212-c47f7a07ebf5 h1:+Nh2SMzAdgBr1tgdKAlF5cN0CvTPUj1V/sI5aRUrZnE=
github.com/devtron-labs/common-lib v0.0.9-0.20231226070212-c47f7a07ebf5/go.mod h1:pBThgympEjsza6GShqNNGCPBFXNDx0DGMc7ID/VHTAw=
github.com/devtron-labs/common-lib v0.0.9-beta3 h1:uLAx/z341oEoKiJoiwS8+qfHoRoA41gnOBEEPU+r0aI=
github.com/devtron-labs/common-lib v0.0.9-beta3/go.mod h1:95/DizzVXu1kHap/VwEvdxwgd+BvPVYc0bJzt8yqGDU=
github.com/devtron-labs/protos v0.0.0-20230503113602-282404f70fd2 h1:/IEIsJTxDZ3hv8uOoCaqdWCXqcv7nCAgX9AP/v84dUY=
github.com/devtron-labs/protos v0.0.0-20230503113602-282404f70fd2/go.mod h1:l85jxWHlcSo910hdUfRycL40yGzC6glE93V1sVxVPto=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
Expand Down
14 changes: 10 additions & 4 deletions pkg/k8s/application/k8sApplicationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,15 @@ func (impl *K8sApplicationServiceImpl) ValidatePodLogsRequestQuery(r *http.Reque
v, vars := r.URL.Query(), mux.Vars(r)
request := &k8s.ResourceRequestBean{}
podName := vars["podName"]
/*sinceSeconds, err := strconv.Atoi(v.Get("sinceSeconds"))
sinceSeconds, err := strconv.Atoi(v.Get("sinceSeconds"))
if err != nil {
sinceSeconds = 0
}*/
}
sinceTimeVar, err := strconv.ParseInt(v.Get("sinceTime"), 10, 64)
if err != nil {
sinceTimeVar = 0
}
sinceTime := metav1.Unix(sinceTimeVar, 0)
containerName, clusterIdString := v.Get("containerName"), v.Get("clusterId")
prevContainerLogs := v.Get("previous")
isPrevLogs, err := strconv.ParseBool(prevContainerLogs)
Expand All @@ -138,7 +143,8 @@ func (impl *K8sApplicationServiceImpl) ValidatePodLogsRequestQuery(r *http.Reque
GroupVersionKind: schema.GroupVersionKind{},
},
PodLogsRequest: k8s2.PodLogsRequest{
//SinceTime: sinceSeconds,
SinceSeconds: sinceSeconds,
SinceTime: &sinceTime,
TailLines: tailLines,
Follow: follow,
ContainerName: containerName,
Expand Down Expand Up @@ -313,7 +319,7 @@ func (impl *K8sApplicationServiceImpl) GetPodLogs(ctx context.Context, request *

resourceIdentifier := request.K8sRequest.ResourceIdentifier
podLogsRequest := request.K8sRequest.PodLogsRequest
resp, err := impl.K8sUtil.GetPodLogs(ctx, restConfig, resourceIdentifier.Name, resourceIdentifier.Namespace, podLogsRequest.SinceTime, podLogsRequest.TailLines, podLogsRequest.Follow, podLogsRequest.ContainerName, podLogsRequest.IsPrevContainerLogsEnabled)
resp, err := impl.K8sUtil.GetPodLogs(ctx, restConfig, resourceIdentifier.Name, resourceIdentifier.Namespace, podLogsRequest.SinceTime, podLogsRequest.TailLines, podLogsRequest.SinceSeconds, podLogsRequest.Follow, podLogsRequest.ContainerName, podLogsRequest.IsPrevContainerLogsEnabled)
if err != nil {
impl.logger.Errorw("error in getting pod logs", "err", err, "clusterId", clusterId)
return nil, err
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ github.com/devtron-labs/authenticator/jwt
github.com/devtron-labs/authenticator/middleware
github.com/devtron-labs/authenticator/oidc
github.com/devtron-labs/authenticator/password
# github.com/devtron-labs/common-lib v0.0.9-0.20231226070212-c47f7a07ebf5
# github.com/devtron-labs/common-lib v0.0.9-beta3
## explicit; go 1.20
github.com/devtron-labs/common-lib/blob-storage
github.com/devtron-labs/common-lib/pubsub-lib
Expand Down