未验证 提交 325399e8 编写于 作者: S Sebastian Florek 提交者: GitHub

Support more log options when downloading log file (#6179)

* Add option for timestamp to log download

* Add more options to log file download
上级 0602c08a
......@@ -20,6 +20,8 @@ import (
"strconv"
"strings"
v1 "k8s.io/api/core/v1"
"github.com/kubernetes/dashboard/src/app/backend/resource/networkpolicy"
"github.com/kubernetes/dashboard/src/app/backend/handler/parser"
......@@ -2678,16 +2680,19 @@ func (apiHandler *APIHandler) handleLogs(request *restful.Request, response *res
func (apiHandler *APIHandler) handleLogFile(request *restful.Request, response *restful.Response) {
k8sClient, err := apiHandler.cManager.Client(request)
opts := new(v1.PodLogOptions)
if err != nil {
errors.HandleInternalError(response, err)
return
}
namespace := request.PathParameter("namespace")
podID := request.PathParameter("pod")
containerID := request.PathParameter("container")
usePreviousLogs := request.QueryParameter("previous") == "true"
opts.Previous = request.QueryParameter("previous") == "true"
opts.Timestamps = request.QueryParameter("timestamps") == "true"
logStream, err := container.GetLogFile(k8sClient, namespace, podID, containerID, usePreviousLogs)
logStream, err := container.GetLogFile(k8sClient, namespace, podID, containerID, opts)
if err != nil {
errors.HandleInternalError(response, err)
return
......
......@@ -114,12 +114,12 @@ func readRawLogs(client kubernetes.Interface, namespace, podID string, logOption
// GetLogFile returns a stream to the log file which can be piped directly to the response. This avoids out of memory
// issues. Previous indicates to read archived logs created by log rotation or container crash
func GetLogFile(client kubernetes.Interface, namespace, podID string, container string, usePreviousLogs bool) (io.ReadCloser, error) {
func GetLogFile(client kubernetes.Interface, namespace, podID string, container string, opts *v1.PodLogOptions) (io.ReadCloser, error) {
logOptions := &v1.PodLogOptions{
Container: container,
Follow: false,
Previous: usePreviousLogs,
Timestamps: false,
Previous: opts.Previous,
Timestamps: opts.Timestamps,
}
logStream, err := openStream(client, namespace, podID, logOptions)
return logStream, err
......
......@@ -12,11 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import {HttpClient, HttpEventType, HttpRequest, HttpResponse} from '@angular/common/http';
import {HttpClient, HttpEventType, HttpParams, HttpRequest, HttpResponse} from '@angular/common/http';
import {Component, Inject, OnDestroy} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import {LogOptions} from '@api/root.api';
import {saveAs} from 'file-saver';
import {Subscription} from 'rxjs';
import {Subject, Subscription} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
import {LogService} from '../../services/global/logs';
......@@ -34,58 +36,65 @@ export interface LogsDownloadDialogMeta {
export class LogsDownloadDialog implements OnDestroy {
loaded = 0;
finished = false;
result: Blob;
downloadSubscription: Subscription;
error: number;
private _result: Blob;
private _error: number;
private _unsubscribe = new Subject<void>();
private get _logOptions(): LogOptions {
return {
previous: this.logService.getPrevious(),
timestamps: this.logService.getShowTimestamp(),
};
}
constructor(
public dialogRef: MatDialogRef<LogsDownloadDialog>,
private readonly _dialogRef: MatDialogRef<LogsDownloadDialog>,
@Inject(MAT_DIALOG_DATA) public data: LogsDownloadDialogMeta,
private readonly logService: LogService,
private readonly http_: HttpClient
) {
const logUrl = `api/v1/log/file/${data.namespace}/${data.pod}/${
data.container
}?previous=${this.logService.getPrevious()}`;
const logUrl = `api/v1/log/file/${data.namespace}/${data.pod}/${data.container}`;
this.downloadSubscription = this.http_
.request(new HttpRequest('GET', logUrl, {}, {reportProgress: true, responseType: 'blob'}))
this.http_
.request(
new HttpRequest(
'GET',
logUrl,
{},
{reportProgress: true, responseType: 'blob', params: new HttpParams({fromObject: this._logOptions})}
)
)
.pipe(takeUntil(this._unsubscribe))
.subscribe(
event => {
if (event.type === HttpEventType.DownloadProgress) {
this.loaded = event.loaded;
} else if (event instanceof HttpResponse) {
this.finished = true;
// @ts-ignore
this.result = new Blob([event.body], {type: 'text/plan'});
this._result = new Blob([event.body as BlobPart], {type: 'text/plan'});
}
},
error => {
this.error = error.status;
}
error => (this._error = error.status)
);
}
ngOnDestroy(): void {
if (this.downloadSubscription) {
this.downloadSubscription.unsubscribe();
}
this._unsubscribe.next();
this._unsubscribe.complete();
}
hasForbiddenError(): boolean {
return this.error !== undefined && this.error === 403;
return this._error !== undefined && this._error === 403;
}
save(): void {
saveAs(this.result, this.logService.getLogFileName(this.data.pod, this.data.container));
this.dialogRef.close();
saveAs(this._result, this.logService.getLogFileName(this.data.pod, this.data.container));
this._dialogRef.close();
}
abort(): void {
if (this.downloadSubscription) {
this.downloadSubscription.unsubscribe();
}
this.dialogRef.close();
this._dialogRef.close();
}
getDownloadMode(): string {
......
......@@ -1176,6 +1176,11 @@ export interface LogLineReference {
lineNum: number;
}
export type LogOptions = {
previous: boolean;
timestamps: boolean;
};
export interface Protocols {
protocols: string[];
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册