-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add new api for pod log * update params * add eof for log stream end
- Loading branch information
1 parent
c5dd035
commit c29ea97
Showing
6 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
pkg/apiserver/cubeapi/resourcemanage/handle/proxypodlog.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters