Skip to content

Commit

Permalink
Merge pull request #20869 from mborsz/logs-deck
Browse files Browse the repository at this point in the history
Implement 'logs' lens
  • Loading branch information
k8s-ci-robot authored Feb 17, 2021
2 parents 6eb3a5d + caba88b commit 47f2db0
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
1 change: 1 addition & 0 deletions prow/cmd/deck/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ go_library(
"//prow/spyglass/lenses/common:go_default_library",
"//prow/spyglass/lenses/coverage:go_default_library",
"//prow/spyglass/lenses/junit:go_default_library",
"//prow/spyglass/lenses/logs:go_default_library",
"//prow/spyglass/lenses/metadata:go_default_library",
"//prow/spyglass/lenses/podinfo:go_default_library",
"//prow/spyglass/lenses/restcoverage:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions prow/cmd/deck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import (
_ "k8s.io/test-infra/prow/spyglass/lenses/buildlog"
_ "k8s.io/test-infra/prow/spyglass/lenses/coverage"
_ "k8s.io/test-infra/prow/spyglass/lenses/junit"
_ "k8s.io/test-infra/prow/spyglass/lenses/logs"
_ "k8s.io/test-infra/prow/spyglass/lenses/metadata"
_ "k8s.io/test-infra/prow/spyglass/lenses/podinfo"
_ "k8s.io/test-infra/prow/spyglass/lenses/restcoverage"
Expand Down
3 changes: 3 additions & 0 deletions prow/spyglass/lenses/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ filegroup(
"//prow/spyglass/lenses/buildlog:template",
"//prow/spyglass/lenses/coverage:template",
"//prow/spyglass/lenses/junit:template",
"//prow/spyglass/lenses/logs:template",
"//prow/spyglass/lenses/metadata:template",
"//prow/spyglass/lenses/podinfo:template",
"//prow/spyglass/lenses/restcoverage:template",
Expand All @@ -21,6 +22,7 @@ filegroup(
"//prow/spyglass/lenses/buildlog:resources",
"//prow/spyglass/lenses/coverage:resources",
"//prow/spyglass/lenses/junit:resources",
"//prow/spyglass/lenses/logs:resources",
"//prow/spyglass/lenses/metadata:resources",
"//prow/spyglass/lenses/podinfo:resources",
"//prow/spyglass/lenses/restcoverage:resources",
Expand Down Expand Up @@ -58,6 +60,7 @@ filegroup(
"//prow/spyglass/lenses/common:all-srcs",
"//prow/spyglass/lenses/coverage:all-srcs",
"//prow/spyglass/lenses/junit:all-srcs",
"//prow/spyglass/lenses/logs:all-srcs",
"//prow/spyglass/lenses/metadata:all-srcs",
"//prow/spyglass/lenses/podinfo:all-srcs",
"//prow/spyglass/lenses/restcoverage:all-srcs",
Expand Down
59 changes: 59 additions & 0 deletions prow/spyglass/lenses/logs/BUILD.bazel
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"],
)
13 changes: 13 additions & 0 deletions prow/spyglass/lenses/logs/logs.css
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;
}
105 changes: 105 additions & 0 deletions prow/spyglass/lenses/logs/logs.go
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 ""
}
25 changes: 25 additions & 0 deletions prow/spyglass/lenses/logs/logs.ts
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);
}
});
8 changes: 8 additions & 0 deletions prow/spyglass/lenses/logs/template.html
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}}

0 comments on commit 47f2db0

Please sign in to comment.