Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CRD): Traits configuration schema #3235

Merged
merged 14 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/actions/kamel-config-cluster/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ runs:
if [ $? != 0 ]; then
set -e
echo "OLM not detected on cluster so downloading and installing"
kubectl apply -f https://github.com/operator-framework/operator-lifecycle-manager/releases/download/v0.17.0/crds.yaml
kubectl create -f https://github.com/operator-framework/operator-lifecycle-manager/releases/download/v0.21.2/crds.yaml
# wait for a while to be sure CRDs are installed
sleep 1
kubectl apply -f https://github.com/operator-framework/operator-lifecycle-manager/releases/download/v0.17.0/olm.yaml
kubectl create -f https://github.com/operator-framework/operator-lifecycle-manager/releases/download/v0.21.2/olm.yaml
fi
set -e

Expand Down
104 changes: 104 additions & 0 deletions addons/addons_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
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 addons

import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/apache/camel-k/addons/master"
"github.com/apache/camel-k/addons/tracing"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/trait"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTraitConfiguration(t *testing.T) {
env := trait.Environment{
Integration: &v1.Integration{
Spec: v1.IntegrationSpec{
Profile: v1.TraitProfileKubernetes,
Traits: v1.Traits{
Addons: map[string]v1.AddonTrait{
"master": trait.ToAddonTrait(t, map[string]interface{}{
"enabled": true,
"resourceName": "test-lock",
"labelKey": "test-label",
"labelValue": "test-value",
}),
"tracing": trait.ToAddonTrait(t, map[string]interface{}{
"enabled": true,
}),
},
},
},
},
}
c := trait.NewCatalog(nil)
require.NoError(t, c.Configure(&env))

require.NotNil(t, c.GetTrait("master"))
master, ok := c.GetTrait("master").(*master.TestMasterTrait)
require.True(t, ok)
assert.True(t, *master.Enabled)
assert.Equal(t, "test-lock", *master.ResourceName)
assert.Equal(t, "test-label", *master.LabelKey)
assert.Equal(t, "test-value", *master.LabelValue)

require.NotNil(t, c.GetTrait("tracing"))
tracing, ok := c.GetTrait("tracing").(*tracing.TestTracingTrait)
require.True(t, ok)
assert.True(t, *tracing.Enabled)
}

func TestTraitConfigurationFromAnnotations(t *testing.T) {
env := trait.Environment{
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"trait.camel.apache.org/master.enabled": "true",
"trait.camel.apache.org/master.resource-name": "test-lock",
"trait.camel.apache.org/master.label-key": "test-label",
"trait.camel.apache.org/master.label-value": "test-value",
"trait.camel.apache.org/tracing.enabled": "true",
},
},
Spec: v1.IntegrationSpec{
Profile: v1.TraitProfileKubernetes,
},
},
}
c := trait.NewCatalog(nil)
require.NoError(t, c.Configure(&env))

require.NotNil(t, c.GetTrait("master"))
master, ok := c.GetTrait("master").(*master.TestMasterTrait)
require.True(t, ok)
assert.True(t, *master.Enabled)
assert.Equal(t, "test-lock", *master.ResourceName)
assert.Equal(t, "test-label", *master.LabelKey)
assert.Equal(t, "test-value", *master.LabelValue)

require.NotNil(t, c.GetTrait("tracing"))
tracing, ok := c.GetTrait("tracing").(*tracing.TestTracingTrait)
require.True(t, ok)
assert.True(t, *tracing.Enabled)
}
10 changes: 8 additions & 2 deletions addons/keda/keda.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

kedav1alpha1 "github.com/apache/camel-k/addons/keda/duck/v1alpha1"
camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/pkg/apis/camel/v1/trait"
camelv1alpha1 "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/kamelet/repository"
"github.com/apache/camel-k/pkg/metadata"
Expand Down Expand Up @@ -72,8 +73,8 @@ const (
// The KEDA trait is disabled by default.
//
// +camel-k:trait=keda.
type kedaTrait struct {
trait.BaseTrait `property:",squash"`
type Trait struct {
traitv1.Trait `property:",squash" json:",inline"`
// Enables automatic configuration of the trait. Allows the trait to infer KEDA triggers from the Kamelets.
Auto *bool `property:"auto" json:"auto,omitempty"`
// Set the spec->replicas field on the top level controller to an explicit value if missing, to allow KEDA to recognize it as a scalable resource.
Expand All @@ -95,6 +96,11 @@ type kedaTrait struct {
Triggers []kedaTrigger `property:"triggers" json:"triggers,omitempty"`
}

type kedaTrait struct {
trait.BaseTrait
Trait `property:",squash"`
}

type kedaTrigger struct {
Type string `property:"type" json:"type,omitempty"`
Metadata map[string]string `property:"metadata" json:"metadata,omitempty"`
Expand Down
12 changes: 9 additions & 3 deletions addons/master/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime/pkg/client"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/pkg/metadata"
"github.com/apache/camel-k/pkg/resources"
"github.com/apache/camel-k/pkg/trait"
Expand All @@ -41,8 +42,8 @@ import (
// It's recommended to use a different service account than "default" when running the integration.
//
// +camel-k:trait=master.
type masterTrait struct {
trait.BaseTrait `property:",squash"`
type Trait struct {
traitv1.Trait `property:",squash" json:",inline"`
// Enables automatic configuration of the trait.
Auto *bool `property:"auto" json:"auto,omitempty"`
// When this flag is active, the operator analyzes the source code to add dependencies required by delegate endpoints.
Expand All @@ -57,7 +58,12 @@ type masterTrait struct {
// Label that will be used to identify all pods contending the lock. Defaults to "camel.apache.org/integration".
LabelKey *string `property:"label-key" json:"labelKey,omitempty"`
// Label value that will be used to identify all pods contending the lock. Defaults to the integration name.
LabelValue *string `property:"label-value" json:"labelValue,omitempty"`
LabelValue *string `property:"label-value" json:"labelValue,omitempty"`
}

type masterTrait struct {
trait.BaseTrait
Trait `property:",squash"`
delegateDependencies []string `json:"-"`
}

Expand Down
21 changes: 21 additions & 0 deletions addons/master/test_support.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
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 master

// Expose masterTrait type for testing.
type TestMasterTrait = masterTrait
4 changes: 2 additions & 2 deletions addons/strimzi/strimzi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestStrimziDirect(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, binding)
assert.Equal(t, "kafka:mytopic?brokers=my-cluster-kafka-bootstrap%3A9092", binding.URI)
assert.Nil(t, binding.Traits)
assert.Equal(t, camelv1.Traits{}, binding.Traits)
}

func TestStrimziLookup(t *testing.T) {
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestStrimziLookup(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, binding)
assert.Equal(t, "kafka:mytopicy?brokers=my-clusterx-kafka-bootstrap%3A9092", binding.URI)
assert.Nil(t, binding.Traits)
assert.Equal(t, camelv1.Traits{}, binding.Traits)
}

func asEndpointProperties(props map[string]string) *v1alpha1.EndpointProperties {
Expand Down
10 changes: 8 additions & 2 deletions addons/threescale/3scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

traitv1 "github.com/apache/camel-k/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/pkg/trait"
)

Expand All @@ -31,8 +32,8 @@ import (
// The 3scale trait is disabled by default.
//
// +camel-k:trait=3scale.
type threeScaleTrait struct {
trait.BaseTrait `property:",squash"`
type Trait struct {
traitv1.Trait `property:",squash" json:",inline"`
// Enables automatic configuration of the trait.
Auto *bool `property:"auto" json:"auto,omitempty"`
// The scheme to use to contact the service (default `http`)
Expand All @@ -45,6 +46,11 @@ type threeScaleTrait struct {
DescriptionPath *string `property:"description-path" json:"descriptionPath,omitempty"`
}

type threeScaleTrait struct {
trait.BaseTrait
Trait `property:",squash"`
}

const (
// ThreeScaleSchemeAnnotation --.
ThreeScaleSchemeAnnotation = "discovery.3scale.net/scheme"
Expand Down
21 changes: 21 additions & 0 deletions addons/tracing/test_support.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
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 tracing

// Expose tracingTrait type for testing.
type TestTracingTrait = tracingTrait
10 changes: 8 additions & 2 deletions addons/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/apache/camel-k/addons/tracing/discovery"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util"
)
Expand All @@ -34,8 +35,8 @@ import (
// The Tracing trait is disabled by default.
//
// +camel-k:trait=tracing.
type tracingTrait struct {
trait.BaseTrait `property:",squash"`
type Trait struct {
traitv1.Trait `property:",squash" json:",inline"`
// Enables automatic configuration of the trait, including automatic discovery of the tracing endpoint.
Auto *bool `property:"auto" json:"auto,omitempty"`
// The name of the service that publishes tracing data (defaults to the integration name)
Expand All @@ -48,6 +49,11 @@ type tracingTrait struct {
SamplerParam *string `property:"sampler-param" json:"samplerParam,omitempty"`
}

type tracingTrait struct {
trait.BaseTrait
Trait `property:",squash"`
}

const (
propEnabled = "propEnabled"
propEndpoint = "propEndpoint"
Expand Down
6 changes: 3 additions & 3 deletions addons/tracing/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ package tracing
import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/camel"

"github.com/stretchr/testify/assert"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)

func TestTracingTraitOnQuarkus(t *testing.T) {
Expand Down
Loading