Skip to content

Commit

Permalink
feat: Allow to disable logs with global.logs-enabled = false
Browse files Browse the repository at this point in the history
  • Loading branch information
keskad committed Nov 25, 2023
1 parent f9c518b commit 946b65a
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 6 deletions.
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
FROM alpine:3.18 AS workspaceBuilder
FROM alpine:3.18 AS builder
COPY .build/batchv1-controller /batchv1-controller
RUN chmod +x /batchv1-controller

FROM scratch
COPY --from=workspaceBuilder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=workspaceBuilder /batchv1-controller /batchv1-controller
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /batchv1-controller /batchv1-controller

WORKDIR "/"
USER 65161
ENTRYPOINT ["/batchv1-controller"]
13 changes: 13 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ common for all controllers based on `pipelines-feedback-core`.

[Configuring controller Globally, per Namespace and per Pipeline](./pkgs/config/USAGE.md)
---------------------------------------------------------------

Global configuration reference
------------------------------

Pipelines Feedback Core has a core set of settings which are not associated with any _Feedback Receiver_, _Controller_, _Store_ or _Feedback Provider_. Those configuration options are called `global`.

| Name | Default value | Description |
|----------------------------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| dashboard-url | | Go-template formatted URL to any dashboard e.g. OpenShift Pipelines, Tekton Dashboard, other. <br/> Example: `https://console-openshift-console.apps.my-cluster.org/k8s/ns/{{ .namespace }}/tekton.dev~v1beta1~PipelineRun/{{ .name }}` |
| logs-enabled | true | Fetch logs from builds [true/false] |
| logs-max-line-length | 64 | How many characters a single log line could have |
| logs-max-full-length-lines-count | 10 | How many log lines should be returned |
| logs-split-separator | (...) | A string that replaces ending in truncated logs |
3 changes: 2 additions & 1 deletion pkgs/app/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ func (app *PipelinesFeedbackApp) Run() error {
Name: "global",
AllowedFields: []string{
"dashboard-url",
"logs-enabled",
"logs-max-line-length",
"max-full-length-lines-count",
"logs-max-full-length-lines-count",
"logs-split-separator",
},
})
Expand Down
12 changes: 11 additions & 1 deletion pkgs/contract/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"time"
)

type ConfigurationData interface {
GetOrDefault(keyName string, defaultVal string) string
}

// PipelineInfo is a point-in-time Pipeline status including the SCM information
type PipelineInfo struct {
ctx JobContext
Expand All @@ -25,6 +29,7 @@ type PipelineInfo struct {
retrievalNum int
labels labels.Labels
annotations labels.Labels
globalCfg ConfigurationData
logs func() string
_logs string
}
Expand Down Expand Up @@ -142,6 +147,10 @@ func (pi PipelineInfo) GetNamespace() string {

// GetLogs is returning truncated logs. It is a lazy-loaded method, fetches logs on demand. After first fetch logs are kept in the memory
func (pi PipelineInfo) GetLogs() string {
// logs can be disabled in settings
if strings.Trim(strings.ToLower(pi.globalCfg.GetOrDefault("logs-enabled", "true")), " ") == "false" {
return ""
}
if pi._logs == "" {
pi._logs = pi.logs()
}
Expand All @@ -163,7 +172,7 @@ func PipelineInfoWithUrl(url string) func(pipelineInfo *PipelineInfo) {
}

func NewPipelineInfo(scm JobContext, namespace string, name string, instanceName string, dateStarted time.Time,
stages []PipelineStage, labels labels.Labels, annotations labels.Labels, options ...func(info *PipelineInfo)) *PipelineInfo {
stages []PipelineStage, labels labels.Labels, annotations labels.Labels, globalCfg ConfigurationData, options ...func(info *PipelineInfo)) *PipelineInfo {
pi := PipelineInfo{
ctx: scm,
name: name,
Expand All @@ -174,6 +183,7 @@ func NewPipelineInfo(scm JobContext, namespace string, name string, instanceName
labels: labels,
annotations: annotations,
url: "",
globalCfg: globalCfg,
logs: func() string {
return ""
},
Expand Down
52 changes: 52 additions & 0 deletions pkgs/contract/pipeline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package contract_test

import (
"github.com/kube-cicd/pipelines-feedback-core/pkgs/config"
"github.com/kube-cicd/pipelines-feedback-core/pkgs/contract"
"github.com/kube-cicd/pipelines-feedback-core/pkgs/fake"
"github.com/kube-cicd/pipelines-feedback-core/pkgs/logging"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/labels"
"testing"
"time"
)

func TestPipelineInfo_GetLogs(t *testing.T) {
cfg := config.NewData("", map[string]string{}, &fake.NullValidator{}, logging.NewInternalLogger())
pi := contract.NewPipelineInfo(
contract.JobContext{
Commit: "123",
Reference: "test",
RepoHttpsUrl: "",
PrId: "",
OrganizationName: "",
RepositoryName: "",
TechnicalJob: "",
},
"test-ns",
"bread-pipeline",
"a-slice-123",
time.Now(),
[]contract.PipelineStage{
{Name: "clone", Status: contract.PipelineSucceeded},
},
labels.Set{},
labels.Set{},
&cfg,
contract.PipelineInfoWithLogsCollector(func() string {
return "test123"
}),
)

assert.Equal(t, "test123", pi.GetLogs(), "Expecting logs returned, as by default logs are enabled - 'global.logs-enable == false' not defined")

// ------
// Step 2
// ------
// and now we disable logs by overwriting configuration by pointer
cfg = config.NewData("", map[string]string{
"logs-enabled": "false",
}, &fake.NullValidator{}, logging.NewInternalLogger())

assert.Equal(t, "", pi.GetLogs(), "Expecting empty logs - when 'global.logs-enable == false'")
}
1 change: 1 addition & 0 deletions pkgs/controller/generic_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestGenericController_ReconcileWithDelayedRetries(t *testing.T) {
},
labels.Set{},
labels.Set{},
&config.Data{},
)

receiver := &fake.Receiver{}
Expand Down
1 change: 1 addition & 0 deletions pkgs/implementation/batchjob/batchv1_job_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (bjp *BatchV1JobProvider) ReceivePipelineInfo(ctx context.Context, name str
},
labels.Set(job.Labels),
labels.Set(job.Annotations),
&globalCfg,
contract.PipelineInfoWithUrl(dashboardUrl),
contract.PipelineInfoWithLogsCollector(logs),
)
Expand Down
2 changes: 1 addition & 1 deletion pkgs/k8s/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func ReadRequestStream(ctx context.Context, req *rest.Request) string {
// TruncateLogs is truncating logs with a maximum lines number, maximum line length
func TruncateLogs(logs string, data config.Data) string {
maxLineLength, _ := strconv.Atoi(data.GetOrDefault("logs-max-line-length", "64"))
maxFullLengthLines, _ := strconv.Atoi(data.GetOrDefault("max-full-length-lines-count", "10"))
maxFullLengthLines, _ := strconv.Atoi(data.GetOrDefault("logs-max-full-length-lines-count", "10"))
lineSplitSeparator := data.GetOrDefault("logs-split-separator", "(...)")
maxLogsLength := (maxFullLengthLines * maxLineLength) + (maxFullLengthLines * len(lineSplitSeparator))

Expand Down
4 changes: 4 additions & 0 deletions pkgs/store/operator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package store_test

import (
"github.com/kube-cicd/pipelines-feedback-core/pkgs/config"
"github.com/kube-cicd/pipelines-feedback-core/pkgs/contract"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/fields"
Expand All @@ -20,6 +21,7 @@ func createBreadBookPipeline() *contract.PipelineInfo {
[]contract.PipelineStage{},
fields.Set{},
fields.Set{},
&config.Data{},
contract.PipelineInfoWithUrl("https://dashboard.tekton.local/pipeline-some/pipeline"),
contract.PipelineInfoWithLogsCollector(func() string {
return "Baked!"
Expand All @@ -40,6 +42,7 @@ func TestOperator_CountHowManyTimesKubernetesResourceReceived(t *testing.T) {
[]contract.PipelineStage{},
fields.Set{},
fields.Set{},
&config.Data{},
contract.PipelineInfoWithUrl("https://dashboard.tekton.local/pipeline-some/pipeline"),
contract.PipelineInfoWithLogsCollector(func() string {
return "Baked!"
Expand All @@ -61,6 +64,7 @@ func TestOperator_CountHowManyTimesKubernetesResourceReceived(t *testing.T) {
[]contract.PipelineStage{},
fields.Set{},
fields.Set{},
&config.Data{},
contract.PipelineInfoWithUrl("https://dashboard.tekton.local/pipeline-some/pipeline"),
contract.PipelineInfoWithLogsCollector(func() string {
return "Created!"
Expand Down

0 comments on commit 946b65a

Please sign in to comment.