-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20869 from mborsz/logs-deck
Implement 'logs' lens
- Loading branch information
Showing
8 changed files
with
215 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
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,59 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
load("//def:ts.bzl", "rollup_bundle", "ts_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["logs.go"], | ||
importpath = "k8s.io/test-infra/prow/spyglass/lenses/logs", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//prow/spyglass/api:go_default_library", | ||
"//prow/spyglass/lenses:go_default_library", | ||
"@com_github_sirupsen_logrus//:go_default_library", | ||
], | ||
) | ||
|
||
ts_library( | ||
name = "script", | ||
srcs = ["logs.ts"], | ||
deps = [ | ||
"//prow/spyglass/lenses:lens_api", | ||
], | ||
) | ||
|
||
rollup_bundle( | ||
name = "script_bundle", | ||
entry_point = ":logs.ts", | ||
deps = [ | ||
":script", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "template", | ||
srcs = ["template.html"], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "resources", | ||
srcs = [ | ||
"logs.css", | ||
":script_bundle.min", | ||
], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "package-srcs", | ||
srcs = glob(["**"]), | ||
tags = ["automanaged"], | ||
visibility = ["//visibility:private"], | ||
) | ||
|
||
filegroup( | ||
name = "all-srcs", | ||
srcs = [":package-srcs"], | ||
tags = ["automanaged"], | ||
visibility = ["//visibility:public"], | ||
) |
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,13 @@ | ||
body { | ||
padding-bottom: 20px; | ||
} | ||
.copy { | ||
margin-left: 15px; | ||
} | ||
.link { | ||
color: #fff; | ||
display: inline-block; | ||
margin-left: 10px; | ||
font-size: 14px; | ||
font-family: monospace; | ||
} |
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,105 @@ | ||
/* | ||
Copyright 2021 The Kubernetes 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 logs | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/test-infra/prow/spyglass/api" | ||
"k8s.io/test-infra/prow/spyglass/lenses" | ||
) | ||
|
||
const ( | ||
name = "logs" | ||
title = "Master and node logs" | ||
priority = 20 | ||
) | ||
|
||
func init() { | ||
lenses.RegisterLens(Lens{}) | ||
} | ||
|
||
// Lens prints link to master and node logs. | ||
type Lens struct{} | ||
|
||
// Config returns the lens's configuration. | ||
func (lens Lens) Config() lenses.LensConfig { | ||
return lenses.LensConfig{ | ||
Name: name, | ||
Title: title, | ||
Priority: priority, | ||
} | ||
} | ||
|
||
// Header renders the content of <head> from template.html. | ||
func (lens Lens) Header(artifacts []api.Artifact, resourceDir string, config json.RawMessage) string { | ||
output, err := renderTemplate(resourceDir, "header", nil) | ||
if err != nil { | ||
logrus.Warnf("Failed to render header: %v", err) | ||
return "Error: " + err.Error() | ||
} | ||
return output | ||
} | ||
|
||
func renderTemplate(resourceDir, block string, params interface{}) (string, error) { | ||
t, err := template.ParseFiles(filepath.Join(resourceDir, "template.html")) | ||
if err != nil { | ||
return "", fmt.Errorf("Failed to parse template: %v", err) | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := t.ExecuteTemplate(&buf, block, params); err != nil { | ||
return "", fmt.Errorf("Failed to execute template: %v", err) | ||
} | ||
return buf.String(), nil | ||
} | ||
|
||
// Body renders link to logs. | ||
func (lens Lens) Body(artifacts []api.Artifact, resourceDir string, data string, config json.RawMessage) string { | ||
if len(artifacts) == 0 { | ||
return "No artifacts found" | ||
} | ||
content, err := artifacts[0].ReadAll() | ||
if err != nil { | ||
logrus.WithError(err).Warn("Failed to read artifact file.") | ||
return fmt.Sprintf("Failed to read artifact file: %v", err) | ||
} | ||
|
||
params := struct { | ||
LogsURL string | ||
}{ | ||
LogsURL: strings.TrimSpace(string(content)), | ||
} | ||
|
||
output, err := renderTemplate(resourceDir, "body", params) | ||
if err != nil { | ||
logrus.Warnf("Failed to render body: %v", err) | ||
return "Error: " + err.Error() | ||
} | ||
return output | ||
} | ||
|
||
// Callback does nothing. | ||
func (lens Lens) Callback(artifacts []api.Artifact, resourceDir string, data string, config json.RawMessage) string { | ||
return "" | ||
} |
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,25 @@ | ||
// https://stackoverflow.com/a/49121680 | ||
|
||
function copyMessage(val: string) { | ||
const selBox = document.createElement('textarea'); | ||
selBox.style.position = 'fixed'; | ||
selBox.style.left = '0'; | ||
selBox.style.top = '0'; | ||
selBox.style.opacity = '0'; | ||
selBox.value = val; | ||
document.body.appendChild(selBox); | ||
selBox.focus(); | ||
selBox.select(); | ||
document.execCommand('copy'); | ||
document.body.removeChild(selBox); | ||
} | ||
|
||
async function handleCopy(this: HTMLButtonElement) { | ||
copyMessage(this.dataset.link || ""); | ||
} | ||
|
||
window.addEventListener('load', () => { | ||
for (const button of Array.from(document.querySelectorAll<HTMLButtonElement>("button.copy"))) { | ||
button.addEventListener('click', handleCopy); | ||
} | ||
}); |
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,8 @@ | ||
{{define "header"}} | ||
<link rel="stylesheet" href="logs.css"> | ||
<script type="text/javascript" src="script_bundle.min.js"></script> | ||
{{end}} | ||
|
||
{{define "body"}} | ||
<button class="copy" data-link="{{.LogsURL}}">Copy</button><div class="link">{{.LogsURL}}</div> | ||
{{end}} |