Skip to content

Commit

Permalink
fix(traits): logging for synthetic kits
Browse files Browse the repository at this point in the history
Ref apache#5519

(cherry picked from commit apache/camel-k@bafa3cd38)
  • Loading branch information
squakez authored and github-actions[bot] committed May 21, 2024
1 parent 4b2c1a5 commit 53c2dbc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
42 changes: 42 additions & 0 deletions pkg/trait/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -119,6 +120,47 @@ func TestHealthTrait(t *testing.T) {

}

func createNominalHealthTrait(t *testing.T) (*healthTrait, *Environment) {
t.Helper()
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)
trait, _ := newHealthTrait().(*healthTrait)

environment := &Environment{
CamelCatalog: catalog,
Catalog: NewCatalog(nil),
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "integration-name",
Generation: 1,
},
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseRunning,
},
},
Resources: kubernetes.NewCollection(),
}

deployment := appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: appsv1.SchemeGroupVersion.String(),
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{Name: "integration"},
},
},
},
},
}
environment.Resources.Add(&deployment)

return trait, environment
}

func TestApplyHealthTraitSyntheticKit(t *testing.T) {
enabled := true
ht, environment := createNominalHealthTrait(t)
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (t *knativeTrait) Configure(e *Environment) (bool, *TraitCondition, error)
return true, nil, nil
}

// This is true only when the user set the enabled flag on and the auto flag off
// This is true only when the user set the enabled flag on and the auto flag off.
func (t *knativeTrait) isForcefullyEnabled() bool {
return pointer.BoolDeref(t.Enabled, false) && !pointer.BoolDeref(t.Auto, true)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/knative_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (t *knativeServiceTrait) SelectControllerStrategy(e *Environment) (*Control
return nil, nil
}

// This is true only when the user set the enabled flag on and the auto flag off
// This is true only when the user set the enabled flag on and the auto flag off.
func (t *knativeServiceTrait) isForcefullyEnabled() bool {
return pointer.BoolDeref(t.Enabled, false) && !pointer.BoolDeref(t.Auto, true)
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/trait/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,12 @@ func (l loggingTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
if !pointer.BoolDeref(l.Enabled, true) {
return false, NewIntegrationConditionUserDisabled("Logging"), nil
}
if e.CamelCatalog == nil {
return false, NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}

return e.IntegrationInRunningPhases(), nil, nil
}

func (l loggingTrait) Apply(e *Environment) error {
if e.CamelCatalog.Runtime.Capabilities["logging"].RuntimeProperties != nil {
if e.CamelCatalog != nil && e.CamelCatalog.Runtime.Capabilities["logging"].RuntimeProperties != nil {
l.setCatalogConfiguration(e)
} else {
l.setEnvConfiguration(e)
Expand Down
19 changes: 19 additions & 0 deletions pkg/trait/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/envvar"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/test"
)
Expand Down Expand Up @@ -151,3 +152,21 @@ func TestJsonLoggingTrait(t *testing.T) {
assert.Equal(t, "${camel.k.logging.jsonPrettyPrint}", env.ApplicationProperties["quarkus.log.console.json.pretty-print"])
assert.Equal(t, "", env.ApplicationProperties["quarkus.console.color"])
}

func TestDefaultQuarkusLogging(t *testing.T) {
env := createDefaultLoggingTestEnv(t)
// Simulate a synthetic Integration Kit for which the catalog is not available
env.CamelCatalog = nil
env.IntegrationKit.Labels = map[string]string{
v1.IntegrationKitTypeLabel: v1.IntegrationKitTypeSynthetic,
}
env.EnvVars = []corev1.EnvVar{}
conditions, err := NewLoggingTestCatalog().apply(env)

require.NoError(t, err)
assert.NotEmpty(t, conditions)
assert.NotEmpty(t, env.ExecutedTraits)

assert.Equal(t, &corev1.EnvVar{Name: "QUARKUS_LOG_LEVEL", Value: "INFO"}, envvar.Get(env.EnvVars, envVarQuarkusLogLevel))
assert.Equal(t, &corev1.EnvVar{Name: "QUARKUS_LOG_CONSOLE_JSON", Value: "false"}, envvar.Get(env.EnvVars, envVarQuarkusLogConsoleJSON))
}

0 comments on commit 53c2dbc

Please sign in to comment.