You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2021/05/04 12:17:48 UTC

[camel-k] 01/05: Added a new logging trait that can be used to control runtime logging

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch release-1.4.x
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 5ae5a919c3e3353790d277d0ee8cfb393899321f
Author: Otavio Rodolfo Piske <op...@redhat.com>
AuthorDate: Thu Apr 29 14:39:29 2021 +0200

    Added a new logging trait that can be used to control runtime logging
    
    - Initially supports enabling/disabling logging colors
---
 deploy/traits.yaml                                 | 17 +++-
 docs/modules/ROOT/nav.adoc                         |  1 +
 docs/modules/traits/pages/gc.adoc                  |  2 +-
 .../modules/traits/pages/{gc.adoc => logging.adoc} | 14 ++--
 pkg/trait/logging.go                               | 59 +++++++++++++
 pkg/trait/logging_test.go                          | 96 ++++++++++++++++++++++
 pkg/trait/trait_register.go                        |  1 +
 7 files changed, 181 insertions(+), 9 deletions(-)

diff --git a/deploy/traits.yaml b/deploy/traits.yaml
index 75522d2..fc83fef 100755
--- a/deploy/traits.yaml
+++ b/deploy/traits.yaml
@@ -285,7 +285,7 @@ traits:
     description: Can be used to enable or disable a trait. All traits share this common
       property.
   - name: discovery-cache
-    type: ./pkg/trait.discoveryCacheType
+    type: camel-k/pkg/trait.discoveryCacheType
     description: Discovery client cache to be used, either `disabled`, `disk` or `memory`
       (default `memory`)
 - name: ingress
@@ -545,6 +545,21 @@ traits:
   - name: auto
     type: bool
     description: Enable automatic discovery of all trait properties.
+- name: logging
+  platform: false
+  profiles:
+  - Kubernetes
+  - Knative
+  - OpenShift
+  description: This trait is used to control logging options (such as color)
+  properties:
+  - name: enabled
+    type: bool
+    description: Can be used to enable or disable a trait. All traits share this common
+      property.
+  - name: color
+    type: bool
+    description: Colorize the log output
 - name: master
   platform: false
   profiles:
diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc
index ea9558a..37ba00b 100644
--- a/docs/modules/ROOT/nav.adoc
+++ b/docs/modules/ROOT/nav.adoc
@@ -49,6 +49,7 @@
 ** xref:traits:kamelets.adoc[Kamelets]
 ** xref:traits:knative-service.adoc[Knative Service]
 ** xref:traits:knative.adoc[Knative]
+** xref:traits:logging.adoc[Logging]
 ** xref:traits:master.adoc[Master]
 ** xref:traits:openapi.adoc[Openapi]
 ** xref:traits:owner.adoc[Owner]
diff --git a/docs/modules/traits/pages/gc.adoc b/docs/modules/traits/pages/gc.adoc
index 375057d..c1a6937 100755
--- a/docs/modules/traits/pages/gc.adoc
+++ b/docs/modules/traits/pages/gc.adoc
@@ -26,7 +26,7 @@ The following configuration options are available:
 | Can be used to enable or disable a trait. All traits share this common property.
 
 | gc.discovery-cache
-| ./pkg/trait.discoveryCacheType
+| camel-k/pkg/trait.discoveryCacheType
 | Discovery client cache to be used, either `disabled`, `disk` or `memory` (default `memory`)
 
 |===
diff --git a/docs/modules/traits/pages/gc.adoc b/docs/modules/traits/pages/logging.adoc
similarity index 65%
copy from docs/modules/traits/pages/gc.adoc
copy to docs/modules/traits/pages/logging.adoc
index 375057d..d2f979b 100755
--- a/docs/modules/traits/pages/gc.adoc
+++ b/docs/modules/traits/pages/logging.adoc
@@ -1,7 +1,7 @@
-= Gc Trait
+= Logging Trait
 
 // Start of autogenerated code - DO NOT EDIT! (description)
-The GC Trait garbage-collects all resources that are no longer necessary upon integration updates.
+This trait is used to control logging options (such as color)
 
 
 This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**.
@@ -13,7 +13,7 @@ This trait is available in the following profiles: **Kubernetes, Knative, OpenSh
 Trait properties can be specified when running any integration with the CLI:
 [source,console]
 ----
-$ kamel run --trait gc.[key]=[value] --trait gc.[key2]=[value2] integration.groovy
+$ kamel run --trait logging.[key]=[value] --trait logging.[key2]=[value2] integration.groovy
 ----
 The following configuration options are available:
 
@@ -21,13 +21,13 @@ The following configuration options are available:
 |===
 |Property | Type | Description
 
-| gc.enabled
+| logging.enabled
 | bool
 | Can be used to enable or disable a trait. All traits share this common property.
 
-| gc.discovery-cache
-| ./pkg/trait.discoveryCacheType
-| Discovery client cache to be used, either `disabled`, `disk` or `memory` (default `memory`)
+| logging.color
+| bool
+| Colorize the log output
 
 |===
 
diff --git a/pkg/trait/logging.go b/pkg/trait/logging.go
new file mode 100644
index 0000000..7c1776a
--- /dev/null
+++ b/pkg/trait/logging.go
@@ -0,0 +1,59 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You 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 trait
+
+import (
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/apache/camel-k/pkg/util"
+	"github.com/apache/camel-k/pkg/util/envvar"
+	"strconv"
+)
+
+const (
+	envVarQuarkusLogConsoleColor = "QUARKUS_LOG_CONSOLE_COLOR"
+)
+
+// This trait is used to control logging options (such as color)
+//
+// +camel-k:trait=logging
+type loggingTrait struct {
+	BaseTrait `property:",squash"`
+	// Colorize the log output
+	Color *bool `property:"color" json:"color,omitempty"`
+}
+
+func newLoggingTraitTrait() Trait {
+	return &loggingTrait{
+		BaseTrait: NewBaseTrait("logging", 800),
+		Color:     util.BoolP(true),
+	}
+}
+
+func (l loggingTrait) Configure(environment *Environment) (bool, error) {
+	if l.Enabled != nil && !*l.Enabled {
+		return false, nil
+	}
+
+	return environment.IntegrationInPhase(v1.IntegrationPhaseDeploying, v1.IntegrationPhaseRunning), nil
+}
+
+func (l loggingTrait) Apply(environment *Environment) error {
+	envvar.SetVal(&environment.EnvVars, envVarQuarkusLogConsoleColor, strconv.FormatBool(*l.Color))
+
+	return nil
+}
diff --git a/pkg/trait/logging_test.go b/pkg/trait/logging_test.go
new file mode 100644
index 0000000..020d65a
--- /dev/null
+++ b/pkg/trait/logging_test.go
@@ -0,0 +1,96 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You 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 trait
+
+import (
+	"context"
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/apache/camel-k/pkg/util/camel"
+	"github.com/apache/camel-k/pkg/util/kubernetes"
+	"github.com/stretchr/testify/assert"
+	corev1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"testing"
+)
+
+func createLoggingTestEnv(t *testing.T) *Environment {
+	c, err := camel.DefaultCatalog()
+	if err != nil {
+		panic(err)
+	}
+
+	res := &Environment{
+		C:            context.TODO(),
+		CamelCatalog: c,
+		Catalog:      NewCatalog(context.TODO(), nil),
+		Integration: &v1.Integration{
+			ObjectMeta: metav1.ObjectMeta{
+				Name:      "test",
+				Namespace: "ns",
+			},
+			Status: v1.IntegrationStatus{
+				Phase: v1.IntegrationPhaseDeploying,
+			},
+			Spec: v1.IntegrationSpec{
+				Profile: v1.TraitProfileOpenShift,
+			},
+		},
+		IntegrationKit: &v1.IntegrationKit{
+			Status: v1.IntegrationKitStatus{
+				Phase: v1.IntegrationKitPhaseBuildSubmitted,
+			},
+		},
+		Platform: &v1.IntegrationPlatform{
+			ObjectMeta: metav1.ObjectMeta{
+				Namespace: "ns",
+			},
+			Spec: v1.IntegrationPlatformSpec{
+				Cluster: v1.IntegrationPlatformClusterOpenShift,
+			},
+		},
+		EnvVars:        make([]corev1.EnvVar, 0),
+		ExecutedTraits: make([]Trait, 0),
+		Resources:      kubernetes.NewCollection(),
+	}
+
+	return res
+}
+
+func NewLoggingTestCatalog() *Catalog {
+	return NewCatalog(context.TODO(), nil)
+}
+
+func TestEmptyLoggingTrait(t *testing.T) {
+	env := createLoggingTestEnv(t)
+	err := NewLoggingTestCatalog().apply(env)
+
+	assert.Nil(t, err)
+	assert.NotEmpty(t, env.ExecutedTraits)
+
+	quarkusConsoleColor := false
+
+	for _, e := range env.EnvVars {
+		if e.Name == envVarQuarkusLogConsoleColor {
+			if e.Value == "true" {
+				quarkusConsoleColor = true
+			}
+		}
+	}
+
+	assert.True(t, quarkusConsoleColor)
+}
diff --git a/pkg/trait/trait_register.go b/pkg/trait/trait_register.go
index 4c06b37..a43a2d7 100644
--- a/pkg/trait/trait_register.go
+++ b/pkg/trait/trait_register.go
@@ -49,4 +49,5 @@ func init() {
 	AddToTraits(newServiceBindingTrait)
 	AddToTraits(newOwnerTrait)
 	AddToTraits(newPdbTrait)
+	AddToTraits(newLoggingTraitTrait)
 }