Skip to content

Commit

Permalink
fix(trait): watch for resource versions...
Browse files Browse the repository at this point in the history
...instead of for their content.
  • Loading branch information
squakez committed Mar 6, 2024
1 parent 91ae17f commit c401351
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 118 deletions.
3 changes: 2 additions & 1 deletion docs/modules/ROOT/partials/apis/camel-k-crds.adoc
Expand Up @@ -7706,7 +7706,8 @@ bool
Enable "hot reload" when a secret/configmap mounted is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken in account.
marked with `camel.apache.org/integration` label to be taken in account. The resource will be watched for any kind change, also for
changes in metadata.
|`scanKameletsImplicitLabelSecrets` +
bool
Expand Down
3 changes: 2 additions & 1 deletion docs/modules/traits/pages/mount.adoc
Expand Up @@ -48,7 +48,8 @@ Syntax: [configmap\|secret]:name[/key][@path], where name represents the resourc
| mount.hot-reload
| bool
| Enable "hot reload" when a secret/configmap mounted is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken in account.
marked with `camel.apache.org/integration` label to be taken in account. The resource will be watched for any kind change, also for
changes in metadata.

| mount.scan-kamelets-implicit-label-secrets
| bool
Expand Down
6 changes: 4 additions & 2 deletions helm/camel-k/crds/crd-integration-platform.yaml
Expand Up @@ -1476,7 +1476,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down Expand Up @@ -3359,7 +3360,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down
6 changes: 4 additions & 2 deletions helm/camel-k/crds/crd-integration-profile.yaml
Expand Up @@ -1358,7 +1358,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down Expand Up @@ -3130,7 +3131,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down
6 changes: 4 additions & 2 deletions helm/camel-k/crds/crd-integration.yaml
Expand Up @@ -7377,7 +7377,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down Expand Up @@ -9107,7 +9108,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down
3 changes: 2 additions & 1 deletion helm/camel-k/crds/crd-kamelet-binding.yaml
Expand Up @@ -7674,7 +7674,8 @@ spec:
description: Enable "hot reload" when a secret/configmap
mounted is edited (default `false`). The configmap/secret
must be marked with `camel.apache.org/integration` label
to be taken in account.
to be taken in account. The resource will be watched
for any kind change, also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down
3 changes: 2 additions & 1 deletion helm/camel-k/crds/crd-pipe.yaml
Expand Up @@ -7672,7 +7672,8 @@ spec:
description: Enable "hot reload" when a secret/configmap
mounted is edited (default `false`). The configmap/secret
must be marked with `camel.apache.org/integration` label
to be taken in account.
to be taken in account. The resource will be watched
for any kind change, also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/camel/v1/trait/mount.go
Expand Up @@ -36,7 +36,8 @@ type MountTrait struct {
// A list of Persistent Volume Claims to be mounted. Syntax: [pvcname:/container/path]
Volumes []string `property:"volumes" json:"volumes,omitempty"`
// Enable "hot reload" when a secret/configmap mounted is edited (default `false`). The configmap/secret must be
// marked with `camel.apache.org/integration` label to be taken in account.
// marked with `camel.apache.org/integration` label to be taken in account. The resource will be watched for any kind change, also for
// changes in metadata.
HotReload *bool `property:"hot-reload" json:"hotReload,omitempty"`
// Deprecated: include your properties in an explicit property file backed by a secret.
// Let the operator to scan for secret labeled with `camel.apache.org/kamelet` and `camel.apache.org/kamelet.configuration`.
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/integration/build_kit.go
Expand Up @@ -46,7 +46,7 @@ func (action *buildKitAction) CanHandle(integration *v1.Integration) bool {
func (action *buildKitAction) Handle(ctx context.Context, integration *v1.Integration) (*v1.Integration, error) {
// TODO: we may need to add a timeout strategy, i.e give up after some time in case of an unrecoverable error.

secrets, configmaps := getIntegrationSecretsAndConfigmaps(ctx, action.client, integration)
secrets, configmaps := getIntegrationSecretAndConfigmapResourceVersions(ctx, action.client, integration)
hash, err := digest.ComputeForIntegration(integration, configmaps, secrets)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/integration/integration_controller.go
Expand Up @@ -595,7 +595,7 @@ func (r *reconcileIntegration) Reconcile(ctx context.Context, request reconcile.
}

func (r *reconcileIntegration) update(ctx context.Context, base *v1.Integration, target *v1.Integration, log *log.Logger) error {
secrets, configmaps := getIntegrationSecretsAndConfigmaps(ctx, r.client, target)
secrets, configmaps := getIntegrationSecretAndConfigmapResourceVersions(ctx, r.client, target)
d, err := digest.ComputeForIntegration(target, configmaps, secrets)
if err != nil {
return err
Expand Down
55 changes: 34 additions & 21 deletions pkg/controller/integration/monitor.go
Expand Up @@ -30,6 +30,7 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/utils/pointer"

ctrl "sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -41,6 +42,7 @@ import (
"github.com/apache/camel-k/v2/pkg/util/digest"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
utilResource "github.com/apache/camel-k/v2/pkg/util/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// NewMonitorAction is an action used to monitor manager Integrations.
Expand Down Expand Up @@ -228,7 +230,7 @@ func isInIntegrationKitFailed(status v1.IntegrationStatus) bool {
}

func (action *monitorAction) checkDigestAndRebuild(ctx context.Context, integration *v1.Integration, kit *v1.IntegrationKit) (*v1.Integration, error) {
secrets, configmaps := getIntegrationSecretsAndConfigmaps(ctx, action.client, integration)
secrets, configmaps := getIntegrationSecretAndConfigmapResourceVersions(ctx, action.client, integration)
hash, err := digest.ComputeForIntegration(integration, configmaps, secrets)
if err != nil {
return nil, err
Expand Down Expand Up @@ -279,29 +281,40 @@ func isIntegrationKitResetRequired(integration *v1.Integration, kit *v1.Integrat
return false
}

func getIntegrationSecretsAndConfigmaps(ctx context.Context, client client.Client, integration *v1.Integration) ([]*corev1.Secret, []*corev1.ConfigMap) {
configmaps := make([]*corev1.ConfigMap, 0)
secrets := make([]*corev1.Secret, 0)
if integration.Spec.Traits.Mount != nil {
for _, c := range integration.Spec.Traits.Mount.Configs {
// getIntegrationSecretAndConfigmapResourceVersions returns the list of resource versions only useful to watch for changes.
func getIntegrationSecretAndConfigmapResourceVersions(ctx context.Context, client client.Client, integration *v1.Integration) ([]string, []string) {
configmaps := make([]string, 0)
secrets := make([]string, 0)
if integration.Spec.Traits.Mount != nil && pointer.BoolDeref(integration.Spec.Traits.Mount.HotReload, false) {
mergedResources := make([]string, 0)
mergedResources = append(mergedResources, integration.Spec.Traits.Mount.Configs...)
mergedResources = append(mergedResources, integration.Spec.Traits.Mount.Resources...)
for _, c := range mergedResources {
if conf, parseErr := utilResource.ParseConfig(c); parseErr == nil {
if conf.StorageType() == utilResource.StorageTypeConfigmap {
configmap := kubernetes.LookupConfigmap(ctx, client, integration.Namespace, conf.Name())
configmaps = append(configmaps, configmap)
cm := corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: corev1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: integration.Namespace,
Name: conf.Name(),
},
}
configmaps = append(configmaps, kubernetes.LookupResourceVersion(ctx, client, &cm))
} else if conf.StorageType() == utilResource.StorageTypeSecret {
secret := kubernetes.LookupSecret(ctx, client, integration.Namespace, conf.Name())
secrets = append(secrets, secret)
}
}
}
for _, r := range integration.Spec.Traits.Mount.Resources {
if conf, parseErr := utilResource.ParseConfig(r); parseErr == nil {
if conf.StorageType() == utilResource.StorageTypeConfigmap {
configmap := kubernetes.LookupConfigmap(ctx, client, integration.Namespace, conf.Name())
configmaps = append(configmaps, configmap)
} else if conf.StorageType() == utilResource.StorageTypeSecret {
secret := kubernetes.LookupSecret(ctx, client, integration.Namespace, conf.Name())
secrets = append(secrets, secret)
sec := corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: corev1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: integration.Namespace,
Name: conf.Name(),
},
}
secrets = append(secrets, kubernetes.LookupResourceVersion(ctx, client, &sec))
}
}
}
Expand Down
85 changes: 85 additions & 0 deletions pkg/controller/integration/monitor_test.go
@@ -0,0 +1,85 @@
/*
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 integration

import (
"context"
"testing"

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

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

"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/test"

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

func TestGetIntegrationSecretAndConfigmapResourceVersions(t *testing.T) {
cm := kubernetes.NewConfigMap("default", "cm-test", "test.txt", "test.txt", "xyz", nil)
sec := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "sec-test",
Namespace: "default",
},
Immutable: pointer.Bool(true),
}
sec.Data = map[string][]byte{
"test.txt": []byte("hello"),
}
it := &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "my-it",
Namespace: "default",
},
TypeMeta: metav1.TypeMeta{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: v1.IntegrationKind,
},
Spec: v1.IntegrationSpec{
Traits: v1.Traits{
Mount: &trait.MountTrait{
Configs: []string{"configmap:cm-test"},
Resources: []string{"secret:sec-test"},
},
},
},
}
c, err := test.NewFakeClient(cm, sec)
assert.Nil(t, err)
// Default hot reload (false)
configmaps, secrets := getIntegrationSecretAndConfigmapResourceVersions(context.TODO(), c, it)
assert.Len(t, configmaps, 0)
assert.Len(t, secrets, 0)
// Enabled hot reload (true)
it.Spec.Traits.Mount.HotReload = pointer.Bool(true)
configmaps, secrets = getIntegrationSecretAndConfigmapResourceVersions(context.TODO(), c, it)
assert.Len(t, configmaps, 1)
assert.Len(t, secrets, 1)
// We cannot guess resource version value. It should be enough to have any non empty value though.
assert.NotEqual(t, "", configmaps[0])
assert.NotEqual(t, "", secrets[0])
}
Expand Up @@ -1476,7 +1476,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down Expand Up @@ -3359,7 +3360,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down
Expand Up @@ -1358,7 +1358,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down Expand Up @@ -3130,7 +3131,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down
Expand Up @@ -7377,7 +7377,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down Expand Up @@ -9107,7 +9108,8 @@ spec:
description: Enable "hot reload" when a secret/configmap mounted
is edited (default `false`). The configmap/secret must be
marked with `camel.apache.org/integration` label to be taken
in account.
in account. The resource will be watched for any kind change,
also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down
Expand Up @@ -7674,7 +7674,8 @@ spec:
description: Enable "hot reload" when a secret/configmap
mounted is edited (default `false`). The configmap/secret
must be marked with `camel.apache.org/integration` label
to be taken in account.
to be taken in account. The resource will be watched
for any kind change, also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down
3 changes: 2 additions & 1 deletion pkg/resources/config/crd/bases/camel.apache.org_pipes.yaml
Expand Up @@ -7672,7 +7672,8 @@ spec:
description: Enable "hot reload" when a secret/configmap
mounted is edited (default `false`). The configmap/secret
must be marked with `camel.apache.org/integration` label
to be taken in account.
to be taken in account. The resource will be watched
for any kind change, also for changes in metadata.
type: boolean
resources:
description: 'A list of resources (text or binary content)
Expand Down

0 comments on commit c401351

Please sign in to comment.