Skip to content

Commit

Permalink
fix log api (#335)
Browse files Browse the repository at this point in the history
* add new api for pod log

* update params

* add eof for log stream end
  • Loading branch information
tiancandevloper authored Sep 5, 2023
1 parent c5dd035 commit c29ea97
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# v1.8.8

## BugFix
- fix log api [#335](https://github.com/kubecube-io/KubeCube/pull/335)
- fix proxy log stream [#334](https://github.com/kubecube-io/KubeCube/pull/334)

## Dependencies

- hnc v1.0
- nginx-ingress v0.46.0
- helm 3.5
- metrics-server v0.4.1
- elasticsearch 7.8
- kubecube-monitoring 15.4.8
- thanos 3.18.0
- logseer v1.0.0
- logagent v1.0.0
- kubecube-audit v1.2.0
- kubecube-webconsole v1.2.4

# v1.8.7

## BugFix
Expand Down
1 change: 1 addition & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func registerCubeAPI(cfg *Config) http.Handler {
k8sApiExtend.Any("/clusters/:cluster/namespaces/:namespace/:resourceType/:resourceName", extendHandler.ExtendHandle)
k8sApiExtend.Any("/clusters/:cluster/namespaces/:namespace/:resourceType", extendHandler.ExtendHandle)
k8sApiExtend.GET("/clusters/:cluster/namespaces/:namespace/logs/:resourceName", resourcemanage.GetPodContainerLog)
k8sApiExtend.GET("/clusters/:cluster/namespaces/:namespace/proxy/logs/:resourceName", resourcemanage.GetProxyPodContainerLog)
k8sApiExtend.POST("/clusters/:cluster/yaml/deploy", yamlDeployHandler.Deploy)
k8sApiExtend.GET("/ingressDomainSuffix", resourcemanage.IngressDomainSuffix)
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/apiserver/cubeapi/resourcemanage/handle/extend.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ func (e *ExtendHandler) ExtendHandle(c *gin.Context) {
return
}

// GetPodContainerLog get k8s pod log and page log
// Deprecated: Use GetProxyPodContainerLog instead, get better performance
func GetPodContainerLog(c *gin.Context) {
cluster := c.Param("cluster")
namespace := c.Param("namespace")
Expand All @@ -135,6 +137,28 @@ func GetPodContainerLog(c *gin.Context) {
podLog.HandleLogs(c)
}

// GetProxyPodContainerLog get native k8s pod container log
func GetProxyPodContainerLog(c *gin.Context) {
cluster := c.Param("cluster")
namespace := c.Param("namespace")
condition := parseQueryParams(c)
// k8s client
client := clients.Interface().Kubernetes(cluster)
if client == nil {
response.FailReturn(c, errcode.ClusterNotFoundError(cluster))
return
}
// access
username := c.GetString(constants.UserName)
access := resources.NewSimpleAccess(cluster, username, namespace)
if allow := access.AccessAllow("", "pods", "list"); !allow {
response.FailReturn(c, errcode.ForbiddenErr)
return
}
podLog := NewProxyPodLog(client, namespace, condition)
podLog.HandleLogs(c)
}

// GetFeatureConfig shows layout of integrated components
// all users have read-only access ability
func GetFeatureConfig(c *gin.Context) {
Expand Down
154 changes: 154 additions & 0 deletions pkg/apiserver/cubeapi/resourcemanage/handle/proxypodlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
Copyright 2021 KubeCube Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resourcemanage

import (
"bufio"
"context"
"errors"
"io"
"strconv"

"github.com/gin-gonic/gin"
"github.com/kubecube-io/kubecube/pkg/clog"
"github.com/kubecube-io/kubecube/pkg/multicluster/client"
"github.com/kubecube-io/kubecube/pkg/utils/constants"
"github.com/kubecube-io/kubecube/pkg/utils/errcode"
"github.com/kubecube-io/kubecube/pkg/utils/filter"
"github.com/kubecube-io/kubecube/pkg/utils/response"
v1 "k8s.io/api/core/v1"
)

// NOTE: This file is copied from k8s.io/kubernetes/dashboard/src/app/backend/resource/container/logs.go.
// We have expanded some function and delete some we did not use, such as HandleLogs.

type ProxyPodLog struct {
ctx context.Context
client client.Client
namespace string
filterCondition *filter.Condition
}

func NewProxyPodLog(client client.Client, namespace string, condition *filter.Condition) ProxyPodLog {
ctx := context.Background()
return ProxyPodLog{
ctx: ctx,
client: client,
namespace: namespace,
filterCondition: condition,
}
}

// HandleLogs @router /api/v1/namespaces/{namespace}/proxy/pods/{resourceName}/log [get]
// resourceName: pod name, required
// namespace: pod namespace, required
// container: container name, required
// tailLines: tail lines, required
// timestamps: show timestamps or not, optional
// limitBytes: limit bytes, required
// sinceSeconds: since seconds, optional
// follow: follow or not, optional
func (podLog *ProxyPodLog) HandleLogs(c *gin.Context) {
k8sClient := podLog.client.ClientSet()

namespace := c.Param("namespace")
pod := c.Param("resourceName")
container := c.Query("container")
tailLines := c.Query("tailLines")
timestamps := c.Query("timestamps")
limitBytes := c.Query("limitBytes")
sinceSeconds := c.Query("sinceSeconds")
follow := c.Query("follow")

if len(tailLines) == 0 {
response.FailReturn(c, errcode.ParamsMissing("tailLines"))
return
}
lines, err := strconv.ParseInt(tailLines, 10, 64)
if err != nil {
response.FailReturn(c, errcode.ParamsInvalid(err))
return
}
isTimestamps, _ := strconv.ParseBool(timestamps)
isFollow, _ := strconv.ParseBool(follow)
if len(limitBytes) == 0 {
response.FailReturn(c, errcode.ParamsMissing("limitBytes"))
return
}
limit, err := strconv.ParseInt(limitBytes, 10, 64)
if err != nil {
response.FailReturn(c, errcode.ParamsInvalid(err))
return
}
seconds := int64(0)
if len(sinceSeconds) > 0 {
seconds, err = strconv.ParseInt(sinceSeconds, 10, 64)
if err != nil {
response.FailReturn(c, errcode.ParamsInvalid(err))
return
}
}
logOptions := &v1.PodLogOptions{
Container: container,
Follow: isFollow,
Timestamps: isTimestamps,
TailLines: &lines,
LimitBytes: &limit,
}
if seconds > 0 {
logOptions.SinceSeconds = &seconds
}
logStream, err := k8sClient.CoreV1().Pods(namespace).GetLogs(pod, logOptions).Stream(c)
if err != nil {
clog.Warn("get log details fail: %v", err)
response.FailReturn(c, errcode.BadRequest(err))
return
}
defer func(logStream io.ReadCloser) {
_ = logStream.Close()
}(logStream)
writer := c.Writer
header := writer.Header()
header.Set(constants.HttpHeaderTransferEncoding, constants.HttpHeaderChunked)
header.Set(constants.HttpHeaderContentType, constants.HttpHeaderTextHtml)
r := bufio.NewReader(logStream)
for {
bytes, err := r.ReadBytes('\n')
if err != nil {
if errors.Is(err, io.EOF) {
if isFollow {
_, err := writer.Write([]byte("EOF"))
if err != nil {
response.FailReturn(c, errcode.BadRequest(err))
return
}
}
return
}
clog.Warn("read log fail: %v", err)
response.FailReturn(c, errcode.BadRequest(err))
return
}
_, err = writer.Write(bytes)
if err != nil {
clog.Warn("write log fail: %v", err)
response.FailReturn(c, errcode.BadRequest(err))
return
}
writer.Flush()
}
}
3 changes: 3 additions & 0 deletions pkg/utils/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const (
HttpHeaderContentType = "Content-type"
HttpHeaderContentDisposition = "Content-Disposition"
HttpHeaderContentTypeOctet = "application/octet-stream"
HttpHeaderTransferEncoding = "Transfer-Encoding"
HttpHeaderChunked = "chunked"
HttpHeaderTextHtml = "text/html"

ImpersonateUserKey = "Impersonate-User"
ImpersonateGroupKey = "Impersonate-Group"
Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/errcode/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ func AlreadyExist(resource interface{}) *ErrorInfo {
func ParamsInvalid(err error) *ErrorInfo {
return New(paramsInvalid, err)
}

func ParamsMissing(param string) *ErrorInfo {
return New(missingParam, param)
}

0 comments on commit c29ea97

Please sign in to comment.