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

fix(traits): annotations refactoring #5637

Merged
merged 2 commits into from
Jun 17, 2024
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
36 changes: 0 additions & 36 deletions addons/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package addons
import (
"testing"

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

"github.com/apache/camel-k/v2/addons/master"
"github.com/apache/camel-k/v2/addons/telemetry"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
Expand Down Expand Up @@ -68,37 +66,3 @@ func TestTraitConfiguration(t *testing.T) {
require.True(t, ok)
assert.True(t, *telemetry.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/telemetry.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("telemetry"))
telemetry, ok := c.GetTrait("telemetry").(*telemetry.TestTelemetryTrait)
require.True(t, ok)
assert.True(t, *telemetry.Enabled)
}
10 changes: 4 additions & 6 deletions e2e/common/misc/pipe_with_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ func TestPipeWithImage(t *testing.T) {

g.Eventually(IntegrationGeneration(t, ctx, ns, bindingID)).
Should(gstruct.PointTo(BeNumerically("==", 1)))
g.Eventually(Integration(t, ctx, ns, bindingID)).Should(WithTransform(Annotations, And(
g.Eventually(Integration(t, ctx, ns, bindingID)).Should(WithTransform(Annotations,
HaveKeyWithValue("test", "1"),
HaveKeyWithValue("trait.camel.apache.org/container.image", expectedImage),
)))
))
g.Eventually(IntegrationStatusImage(t, ctx, ns, bindingID)).
Should(Equal(expectedImage))
g.Eventually(IntegrationPodPhase(t, ctx, ns, bindingID), TestTimeoutShort).
Expand All @@ -73,10 +72,9 @@ func TestPipeWithImage(t *testing.T) {
g.Expect(KamelBindWithID(t, ctx, operatorID, ns, "my-own-timer-source", "my-own-log-sink", "--annotation", "trait.camel.apache.org/container.image="+expectedImage, "--annotation", "trait.camel.apache.org/jvm.enabled=false", "--annotation", "trait.camel.apache.org/kamelets.enabled=false", "--annotation", "trait.camel.apache.org/dependencies.enabled=false", "--annotation", "test=2", "--name", bindingID).Execute()).To(Succeed())
g.Eventually(IntegrationGeneration(t, ctx, ns, bindingID)).
Should(gstruct.PointTo(BeNumerically("==", 1)))
g.Eventually(Integration(t, ctx, ns, bindingID)).Should(WithTransform(Annotations, And(
g.Eventually(Integration(t, ctx, ns, bindingID)).Should(WithTransform(Annotations,
HaveKeyWithValue("test", "2"),
HaveKeyWithValue("trait.camel.apache.org/container.image", expectedImage),
)))
))
g.Eventually(IntegrationStatusImage(t, ctx, ns, bindingID)).
Should(Equal(expectedImage))
g.Eventually(IntegrationPodPhase(t, ctx, ns, bindingID), TestTimeoutShort).
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (o *bindCmdOptions) validate(cmd *cobra.Command, args []string) error {
}
catalog := trait.NewCatalog(client)

return validateTraits(catalog, extractTraitNames(o.Traits))
return trait.ValidateTraits(catalog, extractTraitNames(o.Traits))
}

func (o *bindCmdOptions) run(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -236,7 +236,7 @@ func (o *bindCmdOptions) run(cmd *cobra.Command, args []string) error {
binding.Spec.Integration = &v1.IntegrationSpec{}
}
catalog := trait.NewCatalog(client)
if err := configureTraits(o.Traits, &binding.Spec.Integration.Traits, catalog); err != nil {
if err := trait.ConfigureTraits(o.Traits, &binding.Spec.Integration.Traits, catalog); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/kit_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (command *kitCreateCommandOptions) run(cmd *cobra.Command, args []string) e
if err := command.parseAndConvertToTrait(command.Secrets, "mount.config"); err != nil {
return err
}
if err := configureTraits(command.Traits, &kit.Spec.Traits, catalog); err != nil {
if err := trait.ConfigureTraits(command.Traits, &kit.Spec.Traits, catalog); err != nil {
return err
}
existed := false
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (o *runCmdOptions) validate(cmd *cobra.Command) error {
}
catalog := trait.NewCatalog(client)

return validateTraits(catalog, extractTraitNames(o.Traits))
return trait.ValidateTraits(catalog, extractTraitNames(o.Traits))
}

func filterBuildPropertyFiles(maybePropertyFiles []string) []string {
Expand Down Expand Up @@ -561,7 +561,7 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.C

if len(o.Traits) > 0 {
catalog := trait.NewCatalog(c)
if err := configureTraits(o.Traits, &integration.Spec.Traits, catalog); err != nil {
if err := trait.ConfigureTraits(o.Traits, &integration.Spec.Traits, catalog); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func TestConfigureTraits(t *testing.T) {
catalog := trait.NewCatalog(client)

traits := v1.Traits{}
err = configureTraits(runCmdOptions.Traits, &traits, catalog)
err = trait.ConfigureTraits(runCmdOptions.Traits, &traits, catalog)

require.NoError(t, err)
traitMap, err := trait.ToTraitMap(traits)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/integration/build_kit.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ kits:
k := &existingKits[i]

action.L.Debug("Comparing existing kit with environment", "env kit", kit.Name, "existing kit", k.Name)
match, err := kitMatches(&kit, k)
match, err := kitMatches(action.client, &kit, k)
if err != nil {
return nil, fmt.Errorf("error occurred matches integration kits with environment for integration %s/%s: %w",
integration.Namespace, integration.Name, err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/integration/integration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler {
)
}

func integrationUpdateFunc(old *v1.Integration, it *v1.Integration) bool {
func integrationUpdateFunc(c client.Client, old *v1.Integration, it *v1.Integration) bool {
// Observe the time to first readiness metric
previous := old.Status.GetCondition(v1.IntegrationConditionReady)
next := it.Status.GetCondition(v1.IntegrationConditionReady)
Expand All @@ -99,7 +99,7 @@ func integrationUpdateFunc(old *v1.Integration, it *v1.Integration) bool {
updateIntegrationPhase(it.Name, string(it.Status.Phase))
// If traits have changed, the reconciliation loop must kick in as
// traits may have impact
sameTraits, err := trait.IntegrationsHaveSameTraits(old, it)
sameTraits, err := trait.IntegrationsHaveSameTraits(c, old, it)
if err != nil {
Log.ForIntegration(it).Error(
err,
Expand Down Expand Up @@ -324,7 +324,7 @@ func add(ctx context.Context, mgr manager.Manager, c client.Client, r reconcile.
return false
}

return integrationUpdateFunc(old, it)
return integrationUpdateFunc(c, old, it)
},
DeleteFunc: func(e event.DeleteEvent) bool {
// Evaluates to false if the object has been confirmed deleted
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/integration/kits.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ func integrationMatches(ctx context.Context, c client.Client, integration *v1.In
return false, err
}

itc, err := trait.NewSpecTraitsOptionsForIntegrationAndPlatform(integration, pl)
itc, err := trait.NewSpecTraitsOptionsForIntegrationAndPlatform(c, integration, pl)
if err != nil {
return false, err
}
ikc, err := trait.NewSpecTraitsOptionsForIntegrationKit(kit)
ikc, err := trait.NewSpecTraitsOptionsForIntegrationKit(c, kit)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func statusMatches(integration *v1.Integration, kit *v1.IntegrationKit, ilog *lo
}

// kitMatches returns whether the kit matches with the existing target kit.
func kitMatches(kit *v1.IntegrationKit, target *v1.IntegrationKit) (bool, error) {
func kitMatches(c client.Client, kit *v1.IntegrationKit, target *v1.IntegrationKit) (bool, error) {
version := kit.Status.Version
if version == "" {
// Defaults with the version that is going to be set during the kit initialization
Expand All @@ -184,11 +184,11 @@ func kitMatches(kit *v1.IntegrationKit, target *v1.IntegrationKit) (bool, error)
}

// We cannot have yet the status set
c1, err := trait.NewSpecTraitsOptionsForIntegrationKit(kit)
c1, err := trait.NewSpecTraitsOptionsForIntegrationKit(c, kit)
if err != nil {
return false, err
}
c2, err := trait.NewSpecTraitsOptionsForIntegrationKit(target)
c2, err := trait.NewSpecTraitsOptionsForIntegrationKit(c, target)
if err != nil {
return false, err
}
Expand Down
17 changes: 11 additions & 6 deletions pkg/controller/integration/kits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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/client"

"github.com/apache/camel-k/v2/pkg/trait"
"github.com/apache/camel-k/v2/pkg/util/test"
Expand Down Expand Up @@ -279,7 +280,10 @@ func TestHasMatchingTraits_KitNoTraitShouldNotBePicked(t *testing.T) {
},
}

ok, err := integrationAndKitHaveSameTraits(integration, kit)
c, err := test.NewFakeClient(integration, kit)
require.NoError(t, err)

ok, err := integrationAndKitHaveSameTraits(c, integration, kit)
require.NoError(t, err)
assert.False(t, ok)
}
Expand Down Expand Up @@ -326,8 +330,9 @@ func TestHasMatchingTraits_KitSameTraitShouldBePicked(t *testing.T) {
},
},
}

ok, err := integrationAndKitHaveSameTraits(integration, kit)
c, err := test.NewFakeClient(integration, kit)
require.NoError(t, err)
ok, err := integrationAndKitHaveSameTraits(c, integration, kit)
require.NoError(t, err)
assert.True(t, ok)
}
Expand Down Expand Up @@ -429,12 +434,12 @@ func TestHasNotMatchingSources(t *testing.T) {
assert.False(t, hsm2)
}

func integrationAndKitHaveSameTraits(i1 *v1.Integration, i2 *v1.IntegrationKit) (bool, error) {
itOpts, err := trait.NewSpecTraitsOptionsForIntegration(i1)
func integrationAndKitHaveSameTraits(c client.Client, i1 *v1.Integration, i2 *v1.IntegrationKit) (bool, error) {
itOpts, err := trait.NewSpecTraitsOptionsForIntegration(c, i1)
if err != nil {
return false, err
}
ikOpts, err := trait.NewSpecTraitsOptionsForIntegrationKit(i2)
ikOpts, err := trait.NewSpecTraitsOptionsForIntegrationKit(c, i2)
if err != nil {
return false, err
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/controller/integration/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"reflect"
"strconv"
"strings"

appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
Expand Down Expand Up @@ -131,9 +132,32 @@ func (action *monitorAction) Handle(ctx context.Context, integration *v1.Integra
return integration, err
}

action.checkTraitAnnotationsDeprecatedNotice(integration)

return action.monitorPods(ctx, environment, integration)
}

// Deprecated: to be removed in future versions, when we won't support any longer trait annotations into Integrations.
func (action *monitorAction) checkTraitAnnotationsDeprecatedNotice(integration *v1.Integration) {
if integration.Annotations != nil {
for k := range integration.Annotations {
if strings.HasPrefix(k, v1.TraitAnnotationPrefix) {
integration.Status.SetCondition(
v1.IntegrationConditionType("AnnotationTraitsDeprecated"),
corev1.ConditionTrue,
"DeprecationNotice",
"Annotation traits configuration is deprecated and will be removed soon. Use .spec.traits configuration instead.",
)
action.L.Infof(
"WARN: annotation traits configuration is deprecated and will be removed soon. Use .spec.traits configuration for %s integration instead.",
integration.Name,
)
return
}
}
}
}

func (action *monitorAction) monitorPods(ctx context.Context, environment *trait.Environment, integration *v1.Integration) (*v1.Integration, error) {
controller, err := action.newController(environment, integration)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/controller/integrationkit/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package integrationkit

import (
"context"
"strings"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/util/digest"
corev1 "k8s.io/api/core/v1"
)

// NewMonitorAction creates a new monitoring handling action for the kit.
Expand Down Expand Up @@ -64,5 +66,29 @@ func (action *monitorAction) Handle(ctx context.Context, kit *v1.IntegrationKit)
return kit, nil
}

action.checkTraitAnnotationsDeprecatedNotice(kit)

return nil, nil
}

// Deprecated: to be removed in future versions, when we won't support any longer trait annotations into IntegrationKits.
func (action *monitorAction) checkTraitAnnotationsDeprecatedNotice(integrationKit *v1.IntegrationKit) {
if integrationKit.Annotations != nil {
for k := range integrationKit.Annotations {
if strings.HasPrefix(k, v1.TraitAnnotationPrefix) {
integrationKit.Status.SetCondition(
v1.IntegrationKitConditionType("AnnotationTraitsDeprecated"),
corev1.ConditionTrue,
"DeprecationNotice",
"Annotation traits configuration is deprecated and will be removed soon. Use .spec.traits configuration instead.",
)

action.L.Infof(
"WARN: annotation traits configuration is deprecated and will be removed soon. Use .spec.traits configuration for %s integration kit instead.",
integrationKit.Name,
)
return
}
}
}
}
23 changes: 23 additions & 0 deletions pkg/controller/integrationplatform/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package integrationplatform
import (
"context"
"fmt"
"strings"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
platformutil "github.com/apache/camel-k/v2/pkg/platform"
Expand Down Expand Up @@ -124,6 +125,28 @@ func (action *monitorAction) Handle(ctx context.Context, platform *v1.Integratio
}

platform.Status.Phase = platformPhase
action.checkTraitAnnotationsDeprecatedNotice(platform)

return platform, nil
}

// Deprecated: to be removed in future versions, when we won't support any longer trait annotations into IntegrationPlatforms.
func (action *monitorAction) checkTraitAnnotationsDeprecatedNotice(platform *v1.IntegrationPlatform) {
if platform.Annotations != nil {
for k := range platform.Annotations {
if strings.HasPrefix(k, v1.TraitAnnotationPrefix) {
platform.Status.SetCondition(
v1.IntegrationPlatformConditionType("AnnotationTraitsDeprecated"),
corev1.ConditionTrue,
"DeprecationNotice",
"Annotation traits configuration is deprecated and will be removed soon. Use .spec.traits configuration instead.",
)
action.L.Infof(
"WARN: annotation traits configuration is deprecated and will be removed soon. Use .spec.traits configuration for %s platform instead.",
platform.Name,
)
return
}
}
}
}
Loading
Loading