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

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


The following commit(s) were added to refs/heads/main by this push:
     new 8683c29  Added a new logging trait that can be used to control runtime 
logging
8683c29 is described below

commit 8683c29005f272b01c92857dd1f35449c0b21115
Author: Otavio Rodolfo Piske <[email protected]>
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 6f90900..378601f 100755
--- a/deploy/traits.yaml
+++ b/deploy/traits.yaml
@@ -301,7 +301,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
@@ -561,6 +561,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 e3d322e..d1f232e 100644
--- a/docs/modules/ROOT/nav.adoc
+++ b/docs/modules/ROOT/nav.adoc
@@ -50,6 +50,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 30e89bd..f8e0794 100644
--- a/pkg/trait/trait_register.go
+++ b/pkg/trait/trait_register.go
@@ -50,4 +50,5 @@ func init() {
        AddToTraits(newServiceBindingTrait)
        AddToTraits(newOwnerTrait)
        AddToTraits(newPdbTrait)
+       AddToTraits(newLoggingTraitTrait)
 }

Reply via email to