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

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

commit 5c164ae1b51f07a05e52c795246b0e29723ddf34
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Tue Nov 30 13:26:32 2021 +0100

    chore(trait/container): more refactoring
---
 e2e/common/config/config_test.go | 28 +++------------
 pkg/cmd/run.go                   |  6 ++--
 pkg/cmd/run_help.go              |  4 +--
 pkg/trait/container.go           | 76 +++++++++++++++++-----------------------
 pkg/util/resource/config.go      |  4 +--
 5 files changed, 44 insertions(+), 74 deletions(-)

diff --git a/e2e/common/config/config_test.go b/e2e/common/config/config_test.go
index 9dcf9f3..f764fdc 100644
--- a/e2e/common/config/config_test.go
+++ b/e2e/common/config/config_test.go
@@ -23,9 +23,7 @@ limitations under the License.
 package resources
 
 import (
-       "fmt"
        "io/ioutil"
-       "os"
        "testing"
 
        . "github.com/onsi/gomega"
@@ -164,6 +162,7 @@ func TestRunConfigExamples(t *testing.T) {
                        Eventually(IntegrationConditionStatus(ns, 
"config-file-route", v1.IntegrationConditionReady), 
TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
                        Eventually(IntegrationLogs(ns, "config-file-route"), 
TestTimeoutShort).Should(ContainSubstring("the file body"))
                        Expect(Kamel("delete", "--all", "-n", 
ns).Execute()).To(Succeed())
+                       Eventually(AutogeneratedConfigmapsCount(ns), 
TestTimeoutShort).Should(Equal(0))
                })
 
                // Resource File
@@ -174,6 +173,7 @@ func TestRunConfigExamples(t *testing.T) {
                        Eventually(IntegrationConditionStatus(ns, 
"resource-file-route", v1.IntegrationConditionReady), 
TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
                        Eventually(IntegrationLogs(ns, "resource-file-route"), 
TestTimeoutShort).Should(ContainSubstring("the file body"))
                        Expect(Kamel("delete", "--all", "-n", 
ns).Execute()).To(Succeed())
+                       Eventually(AutogeneratedConfigmapsCount(ns), 
TestTimeoutShort).Should(Equal(0))
                })
 
                t.Run("Plain text resource file with destination path", func(t 
*testing.T) {
@@ -182,6 +182,7 @@ func TestRunConfigExamples(t *testing.T) {
                        Eventually(IntegrationConditionStatus(ns, 
"resource-file-location-route", v1.IntegrationConditionReady), 
TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
                        Eventually(IntegrationLogs(ns, 
"resource-file-location-route"), TestTimeoutShort).Should(ContainSubstring("the 
file body"))
                        Expect(Kamel("delete", "--all", "-n", 
ns).Execute()).To(Succeed())
+                       Eventually(AutogeneratedConfigmapsCount(ns), 
TestTimeoutShort).Should(Equal(0))
                })
 
                t.Run("Binary (zip) resource file", func(t *testing.T) {
@@ -190,6 +191,7 @@ func TestRunConfigExamples(t *testing.T) {
                        Eventually(IntegrationConditionStatus(ns, 
"resource-file-binary-route", v1.IntegrationConditionReady), 
TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
                        Eventually(IntegrationLogs(ns, 
"resource-file-binary-route"), TestTimeoutShort).Should(ContainSubstring("the 
file body"))
                        Expect(Kamel("delete", "--all", "-n", 
ns).Execute()).To(Succeed())
+                       Eventually(AutogeneratedConfigmapsCount(ns), 
TestTimeoutShort).Should(Equal(0))
                })
 
                t.Run("Base64 compressed binary resource file", func(t 
*testing.T) {
@@ -204,6 +206,7 @@ func TestRunConfigExamples(t *testing.T) {
                        Eventually(IntegrationConditionStatus(ns, 
"resource-file-base64-encoded-route", v1.IntegrationConditionReady), 
TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
                        Eventually(IntegrationLogs(ns, 
"resource-file-base64-encoded-route"), 
TestTimeoutShort).Should(ContainSubstring(string(expectedBytes)))
                        Expect(Kamel("delete", "--all", "-n", 
ns).Execute()).To(Succeed())
+                       Eventually(AutogeneratedConfigmapsCount(ns), 
TestTimeoutShort).Should(Equal(0))
                })
 
                // Build-Properties
@@ -244,26 +247,5 @@ func TestRunConfigExamples(t *testing.T) {
                        Expect(Kamel("delete", "--all", "-n", 
ns).Execute()).To(Succeed())
                })
 
-               // Resource File: generated configmap must be deleted when 
Integration is deleted
-
-               t.Run("Plain text sync resource file generated configmap", 
func(t *testing.T) {
-                       var tmpFile *os.File
-                       var err error
-                       if tmpFile, err = ioutil.TempFile("", "camel-k-"); err 
!= nil {
-                               t.Error(err)
-                       }
-                       assert.Nil(t, tmpFile.Close())
-                       assert.Nil(t, ioutil.WriteFile(tmpFile.Name(), 
[]byte("Hello from test!"), 0o644))
-
-                       Expect(Kamel("run", "-n", ns, 
"./files/resource-file-location-route.groovy", "--resource", 
fmt.Sprintf("file:%s@/tmp/file.txt", tmpFile.Name())).Execute()).To(Succeed())
-                       Eventually(IntegrationPodPhase(ns, 
"resource-file-location-route"), 
TestTimeoutMedium).Should(Equal(corev1.PodRunning))
-                       Eventually(IntegrationConditionStatus(ns, 
"resource-file-location-route", v1.IntegrationConditionReady), 
TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
-                       Eventually(IntegrationLogs(ns, 
"resource-file-location-route"), 
TestTimeoutShort).Should(ContainSubstring("Hello from test!"))
-
-                       Expect(Kamel("delete", "--all", "-n", 
ns).Execute()).To(Succeed())
-                       // When the integration is deleted, then, also the 
autogenerated configmaps must be cleaned
-                       Eventually(AutogeneratedConfigmapsCount(ns), 
TestTimeoutShort).Should(Equal(0))
-               })
-
        })
 }
diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index da6eb5e..5ff377e 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -226,13 +226,13 @@ func (o *runCmdOptions) validate() error {
        }
 
        // Deprecation warning
-       if o.PropertyFiles != nil {
+       if o.PropertyFiles != nil && len(o.PropertyFiles) > 0 {
                fmt.Println("Warn: --property-file has been deprecated. You 
should use --property file:/path/to/conf.properties instead.")
        }
-       if o.ConfigMaps != nil {
+       if o.ConfigMaps != nil && len(o.ConfigMaps) > 0 {
                fmt.Println("Warn: --configmap has been deprecated. You should 
use --config configmap:my-configmap instead.")
        }
-       if o.Secrets != nil {
+       if o.Secrets != nil && len(o.Secrets) > 0 {
                fmt.Println("Warn: --secret has been deprecated. You should use 
--config secret:my-secret instead.")
        }
 
diff --git a/pkg/cmd/run_help.go b/pkg/cmd/run_help.go
index c87df46..dcbacf5 100644
--- a/pkg/cmd/run_help.go
+++ b/pkg/cmd/run_help.go
@@ -33,8 +33,6 @@ import (
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 )
 
-var invalidPaths = []string{"/etc/camel", "/deployments/dependencies"}
-
 //nolint
 func hashFrom(contents ...[]byte) string {
        // SHA1 because we need to limit the length to less than 64 chars
@@ -93,7 +91,7 @@ func applyOption(ctx context.Context, config 
*resource.Config, integration *v1.I
                if err != nil {
                        return maybeGenCm, err
                }
-               maybeGenCm, err = resource.ConvertFileToConfigmap(ctx, c, 
resourceSpec, config, integration.Namespace, resourceType)
+               maybeGenCm, err = resource.ConvertFileToConfigmap(ctx, c, 
resourceSpec, config, integration.Namespace, integration.Name, resourceType)
                if err != nil {
                        return maybeGenCm, err
                }
diff --git a/pkg/trait/container.go b/pkg/trait/container.go
index 09f4e53..cc0ca1b 100644
--- a/pkg/trait/container.go
+++ b/pkg/trait/container.go
@@ -255,31 +255,31 @@ func (t *containerTrait) configureContainer(e 
*Environment) error {
        envvar.SetVal(&container.Env, "CAMEL_K_CONF_D", camel.ConfDPath)
 
        e.addSourcesProperties()
+       if props, err := e.computeApplicationProperties(); err != nil {
+               return err
+       } else if props != nil {
+               e.Resources.Add(props)
+       }
 
        t.configureResources(e, &container)
-
        if IsTrue(t.Expose) {
                t.configureService(e, &container)
        }
        t.configureCapabilities(e)
 
+       var volumes *[]corev1.Volume
+       var containers *[]corev1.Container
+       visited := false
+
        // Deployment
        if err := e.Resources.VisitDeploymentE(func(deployment 
*appsv1.Deployment) error {
                for _, envVar := range e.EnvVars {
                        envvar.SetVar(&container.Env, envVar)
                }
-               if props, err := e.computeApplicationProperties(); err != nil {
-                       return err
-               } else if props != nil {
-                       e.Resources.Add(props)
-               }
-
-               err := t.configureVolumesAndMounts(e, 
&deployment.Spec.Template.Spec.Volumes, &container.VolumeMounts)
-               if err != nil {
-                       return err
-               }
-               deployment.Spec.Template.Spec.Containers = 
append(deployment.Spec.Template.Spec.Containers, container)
 
+               volumes = &deployment.Spec.Template.Spec.Volumes
+               containers = &deployment.Spec.Template.Spec.Containers
+               visited = true
                return nil
        }); err != nil {
                return err
@@ -301,19 +301,10 @@ func (t *containerTrait) configureContainer(e 
*Environment) error {
                                envvar.SetVar(&container.Env, env)
                        }
                }
-               if props, err := e.computeApplicationProperties(); err != nil {
-                       return err
-               } else if props != nil {
-                       e.Resources.Add(props)
-               }
-
-               e.configureVolumesAndMounts(
-                       &service.Spec.ConfigurationSpec.Template.Spec.Volumes,
-                       &container.VolumeMounts,
-               )
-
-               service.Spec.ConfigurationSpec.Template.Spec.Containers = 
append(service.Spec.ConfigurationSpec.Template.Spec.Containers, container)
 
+               volumes = &service.Spec.ConfigurationSpec.Template.Spec.Volumes
+               containers = 
&service.Spec.ConfigurationSpec.Template.Spec.Containers
+               visited = true
                return nil
        }); err != nil {
                return err
@@ -324,41 +315,40 @@ func (t *containerTrait) configureContainer(e 
*Environment) error {
                for _, envVar := range e.EnvVars {
                        envvar.SetVar(&container.Env, envVar)
                }
-               if props, err := e.computeApplicationProperties(); err != nil {
-                       return err
-               } else if props != nil {
-                       e.Resources.Add(props)
-               }
-
-               e.configureVolumesAndMounts(
-                       &cron.Spec.JobTemplate.Spec.Template.Spec.Volumes,
-                       &container.VolumeMounts,
-               )
-
-               cron.Spec.JobTemplate.Spec.Template.Spec.Containers = 
append(cron.Spec.JobTemplate.Spec.Template.Spec.Containers, container)
 
+               volumes = &cron.Spec.JobTemplate.Spec.Template.Spec.Volumes
+               containers = 
&cron.Spec.JobTemplate.Spec.Template.Spec.Containers
+               visited = true
                return nil
        }); err != nil {
                return err
        }
 
+       if visited {
+               // Volumes declared in the Integration resources
+               e.configureVolumesAndMounts(volumes, &container.VolumeMounts)
+               // Volumes declared in the trait config/resource options
+               err := t.configureVolumesAndMounts(volumes, 
&container.VolumeMounts)
+               if err != nil {
+                       return err
+               }
+               *containers = append(*containers, container)
+       }
+
        return nil
 }
 
-func (t *containerTrait) configureVolumesAndMounts(e *Environment, vols 
*[]corev1.Volume, mnts *[]corev1.VolumeMount) error {
-       // Volumes declared in the Integration resources
-       e.configureVolumesAndMounts(vols, mnts)
-       // Volumes declared in the trait config/resource options
+func (t *containerTrait) configureVolumesAndMounts(vols *[]corev1.Volume, mnts 
*[]corev1.VolumeMount) error {
        for _, c := range t.Configs {
                if conf, parseErr := utilResource.ParseConfig(c); parseErr == 
nil {
-                       t.mountResource(e, vols, mnts, conf)
+                       t.mountResource(vols, mnts, conf)
                } else {
                        return parseErr
                }
        }
        for _, r := range t.Resources {
                if res, parseErr := utilResource.ParseResource(r); parseErr == 
nil {
-                       t.mountResource(e, vols, mnts, res)
+                       t.mountResource(vols, mnts, res)
                } else {
                        return parseErr
                }
@@ -367,7 +357,7 @@ func (t *containerTrait) configureVolumesAndMounts(e 
*Environment, vols *[]corev
        return nil
 }
 
-func (t *containerTrait) mountResource(e *Environment, vols *[]corev1.Volume, 
mnts *[]corev1.VolumeMount, conf *utilResource.Config) {
+func (t *containerTrait) mountResource(vols *[]corev1.Volume, mnts 
*[]corev1.VolumeMount, conf *utilResource.Config) {
        refName := kubernetes.SanitizeLabel(conf.Name())
        vol := getVolume(refName, string(conf.StorageType()), conf.Name(), 
conf.Key(), conf.Key())
        mnt := getMount(refName, conf.DestinationPath(), "")
diff --git a/pkg/util/resource/config.go b/pkg/util/resource/config.go
index a09f372..d900622 100644
--- a/pkg/util/resource/config.go
+++ b/pkg/util/resource/config.go
@@ -208,7 +208,7 @@ func parse(item string, contentType ContentType) (*Config, 
error) {
 // taking care to create the Configmap on the cluster. The method will change 
the value of config parameter
 // to reflect the conversion applied transparently.
 func ConvertFileToConfigmap(ctx context.Context, c client.Client, resourceSpec 
v1.ResourceSpec, config *Config,
-       namespace string, resourceType v1.ResourceType) (*corev1.ConfigMap, 
error) {
+       namespace string, integrationName string, resourceType v1.ResourceType) 
(*corev1.ConfigMap, error) {
        if config.DestinationPath() == "" {
                config.resourceKey = filepath.Base(config.Name())
                // As we are changing the resource to a configmap type
@@ -223,7 +223,7 @@ func ConvertFileToConfigmap(ctx context.Context, c 
client.Client, resourceSpec v
                config.resourceKey = filepath.Base(config.DestinationPath())
                config.destinationPath = filepath.Dir(config.DestinationPath())
        }
-       genCmName := fmt.Sprintf("cm-%s", 
hashFrom([]byte(resourceSpec.Content), resourceSpec.RawContent))
+       genCmName := fmt.Sprintf("cm-%s", hashFrom([]byte(integrationName), 
[]byte(resourceSpec.Content), resourceSpec.RawContent))
        cm := kubernetes.NewConfigmap(namespace, genCmName, 
filepath.Base(config.Name()), config.Key(), resourceSpec.Content, 
resourceSpec.RawContent)
        err := c.Create(ctx, cm)
        if err != nil {

Reply via email to