squakez commented on code in PR #5639:
URL: https://github.com/apache/camel-k/pull/5639#discussion_r1639728624


##########
e2e/advanced/environment_test.go:
##########
@@ -39,7 +40,58 @@ import (
        "github.com/apache/camel-k/v2/pkg/util/defaults"
 )
 
-func TestEnvironmentTrait(t *testing.T) {
+func TestEnvironmentTraitVars(t *testing.T) {
+       t.Parallel()
+
+       WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) {
+               operatorID := "camel-k-trait-environment"
+               g.Expect(CopyCamelCatalog(t, ctx, ns, operatorID)).To(Succeed())
+               g.Expect(CopyIntegrationKits(t, ctx, ns, 
operatorID)).To(Succeed())
+               g.Expect(KamelInstallWithID(t, ctx, operatorID, 
ns)).To(Succeed())
+
+               g.Eventually(SelectedPlatformPhase(t, ctx, ns, operatorID), 
TestTimeoutMedium).Should(Equal(v1.IntegrationPlatformPhaseReady))
+
+               // Create configmap
+               var cmData = make(map[string]string)
+               cmData["my-cm-key"] = "hello configmap"
+               err := CreatePlainTextConfigmap(t, ctx, ns, "my-cm", cmData)
+               require.NoError(t, err)
+
+               // Create secret
+               var secData = make(map[string]string)
+               secData["my-sec-key"] = "very top secret"
+               err = CreatePlainTextSecret(t, ctx, ns, "my-sec", secData)
+               require.NoError(t, err)

Review Comment:
   ditto



##########
pkg/util/property/property.go:
##########
@@ -53,6 +57,52 @@ func EncodePropertyFile(sourceProperties map[string]string) 
(string, error) {
        return buf.String(), nil
 }
 
+func FromMapToProperties(data interface{}, toString func(reflect.Value) 
string, loadProperties func(reflect.Value) (*properties.Properties, error)) 
(*properties.Properties, error) {
+       result := properties.NewProperties()
+       m := reflect.ValueOf(data)
+       for _, k := range m.MapKeys() {
+               key := k.String()
+               value := m.MapIndex(k)
+               if strings.HasSuffix(key, ".properties") {
+                       p, err := loadProperties(value)
+                       if err == nil {
+                               result.Merge(p)
+                       } else if _, _, err = result.Set(key, toString(value)); 
err != nil {
+                               return nil, fmt.Errorf("cannot assign %s to 
%s", value, key)
+                       }
+               } else if _, _, err := result.Set(key, toString(value)); err != 
nil {
+                       return nil, fmt.Errorf("cannot assign %s to %s", value, 
key)
+               }
+       }
+       return result, nil
+}
+
+func LoadPropertiesFromString(value string) (*properties.Properties, error) {
+       return properties.Load([]byte(value), properties.UTF8)
+}
+
+func LoadPropertiesFromConfigMap(ctx context.Context, c client.Client, ns 
string, name string) (*properties.Properties, error) {
+       cm := kubernetes.LookupConfigmap(ctx, c, ns, name)
+       if cm == nil {
+               return nil, fmt.Errorf("%s configmap not found in %s namespace, 
make sure to provide it before the Integration can run", name, ns)
+       }
+       return FromMapToProperties(cm.Data,
+               func(v reflect.Value) string { return v.String() },
+               func(v reflect.Value) (*properties.Properties, error) { return 
LoadPropertiesFromString(v.String()) })
+}
+
+func LoadPropertiesFromSecret(ctx context.Context, c client.Client, ns string, 
name string) (*properties.Properties, error) {
+       secret := kubernetes.LookupSecret(ctx, c, ns, name)
+       if secret == nil {
+               return nil, fmt.Errorf("%s secret not found in %s namespace, 
make sure to provide it before the Integration can run", name, ns)
+       }
+       return FromMapToProperties(secret.Data,

Review Comment:
   The operator should not read any data. Above all for secrets.



##########
pkg/apis/camel/v1/trait/environment.go:
##########
@@ -29,6 +29,7 @@ type EnvironmentTrait struct {
        HTTPProxy *bool `property:"http-proxy" json:"httpProxy,omitempty"`
        // A list of environment variables to be added to the integration 
container.
        // The syntax is KEY=VALUE, e.g., `MY_VAR="my value"`.
+       // The value may also be a reference to a configmap or secret, e.g. 
`MY_VAR=configmap:my-cm/my-cm-key`

Review Comment:
   You need to perform `make generate` to regen the spec and documentation.



##########
pkg/trait/environment.go:
##########
@@ -18,8 +18,11 @@ limitations under the License.
 package trait
 
 import (
+       "fmt"
        "os"
+       "strings"
 
+       "github.com/magiconair/properties"

Review Comment:
   We should try not to depend on this library any longer. It could have been 
find for CLI, but the operator should limit the exposure to third party 
libraries.



##########
e2e/advanced/environment_test.go:
##########
@@ -39,7 +40,58 @@ import (
        "github.com/apache/camel-k/v2/pkg/util/defaults"
 )
 
-func TestEnvironmentTrait(t *testing.T) {
+func TestEnvironmentTraitVars(t *testing.T) {
+       t.Parallel()
+
+       WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) {
+               operatorID := "camel-k-trait-environment"
+               g.Expect(CopyCamelCatalog(t, ctx, ns, operatorID)).To(Succeed())
+               g.Expect(CopyIntegrationKits(t, ctx, ns, 
operatorID)).To(Succeed())
+               g.Expect(KamelInstallWithID(t, ctx, operatorID, 
ns)).To(Succeed())
+
+               g.Eventually(SelectedPlatformPhase(t, ctx, ns, operatorID), 
TestTimeoutMedium).Should(Equal(v1.IntegrationPlatformPhaseReady))
+
+               // Create configmap
+               var cmData = make(map[string]string)
+               cmData["my-cm-key"] = "hello configmap"
+               err := CreatePlainTextConfigmap(t, ctx, ns, "my-cm", cmData)
+               require.NoError(t, err)

Review Comment:
   This is more for a unit test. I think we better use the gomega funcs to 
assert no errors.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to