You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2020/12/11 14:34:27 UTC

[GitHub] [camel-k] mmelko opened a new pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

mmelko opened a new pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861


   This is still WIP. Currently the test is missing and it's only possible to specify the template from the file. Trait is applied only when `IntegrationPhaseRunning` state I had to move `util_content` and `util_sources` under `util/source` to being able to use those also in the trait package. 
   
   


----------------------------------------------------------------
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.

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



[GitHub] [camel-k] astefanutti commented on a change in pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
astefanutti commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r580414494



##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,196 @@
+/*
+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 trait
+
+import (
+	"fmt"
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/ghodss/yaml"
+	v12 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+
+	//"github.com/ghodss/yaml"
+	//	cmd "github.com/apache/camel-k/pkg/cmd"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+// The Pod trait allows to modify the custom PodTemplateSpec resource for the Integration pods
+//
+// +camel-k:trait=pdb
+type podTrait struct {
+	BaseTrait `property:",squash"`
+	Template  string `property:"template"`
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled == nil || !*t.Enabled {
+		return false, nil
+	}
+
+	if t.Template == "" {
+		return false, fmt.Errorf("template must be specified")
+	}
+
+	if _, err := t.parseTemplate(); err != nil {
+		return false, err
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseDeploying,
+		v1.IntegrationPhaseRunning,
+	), nil
+}
+
+func (t *podTrait) Apply(e *Environment) error {
+	var deployment *appsv1.Deployment
+
+	e.Resources.VisitDeployment(func(d *appsv1.Deployment) {
+		if d.Name == e.Integration.Name {
+			deployment = d
+		}
+	})
+
+	modifiedTemplate, err := t.mergeIntoTemplateSpec(deployment.Spec.Template, []byte(t.Template))
+	if err != nil {
+		return err
+	}
+	deployment.Spec.Template = modifiedTemplate
+	return nil
+}
+
+func (t *podTrait) parseTemplate() (*v12.PodTemplateSpec, error) {
+	var template *v12.PodTemplateSpec
+
+	if err := yaml.Unmarshal([]byte(t.Template), &template); err != nil {
+		return nil, err
+	}
+	return template, nil
+
+}
+
+func (t *podTrait) mergeIntoTemplateSpec(template v12.PodTemplateSpec, changesBytes []byte) (mergedTemplate v12.PodTemplateSpec, err error) {

Review comment:
       I think we are implementing what strategic merge patch functionally does. Before making the decision to implement the merging strategy ourselves, I think we have to assess whether we could re-use Kubernetes strategic merge patch:
   
   https://github.com/kubernetes/apimachinery/blob/10b38829b6211c0d672e14a126479221312d3368/pkg/util/strategicpatch/patch.go#L812
   
   Among the multiple benefits that could provide re-using strategic merge patch from Kubernetes, I see:
   - Leveraging the default `patchStrategy` field tag
   - Ability to specify replace directives




----------------------------------------------------------------
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.

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



[GitHub] [camel-k] mmelko commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
mmelko commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r625757170



##########
File path: docs/modules/traits/pages/pod.adoc
##########
@@ -0,0 +1,83 @@
+= Pod Trait
+
+// Start of autogenerated code - DO NOT EDIT! (description)
+   The pod trait allows the customization of the Integration pods.
+   It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+
+   This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+
+
+This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**.
+
+// End of autogenerated code - DO NOT EDIT! (description)
+== Example
+We have an integration that will read files from defined folder:
+[source,groovy]
+----
+from('file:///var/log')
+ .convertBodyTo(String.class)
+ .setBody().simple('${body}:  {{TEST_VARIABLE}} ')
+ .log('${body}')
+
+----
+The content of the folder might be autogenerated by bash script using a sidecar container. The pod template could be defined as follows:
+
+[source,yaml]
+----
+containers:
+  - name: integration

Review comment:
       So for now I will add notes into the doc about env variables and the command option.  And if this will get merged we might create a follow-up issue to address env variables and other options that could be possibly overwritten by the template option. 




-- 
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.

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



[GitHub] [camel-k] astefanutti commented on pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
astefanutti commented on pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#issuecomment-831781850


   @nicolaferraro @lburgazzoli would you be kind enough for a quick review now this is ready?


-- 
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.

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



[GitHub] [camel-k] mmelko edited a comment on pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
mmelko edited a comment on pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#issuecomment-801912534


   I updated the PR also with adding PodTemplateSpec into integration spec (https://github.com/apache/camel-k/issues/2096). Pod trait is now enabled when template will be passed. That is possible with `--pod-template` param from CLI e.g `kamel run integration.yaml --pod-template template.yaml`.  Template is merged with the deployment one using kubernetes strategic merge. 


----------------------------------------------------------------
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.

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



[GitHub] [camel-k] astefanutti commented on pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
astefanutti commented on pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#issuecomment-801931277


   Thanks a lot. Here are a couple of points:
   - I wonder if we should embed `PodSpec` instead of `PodTemplateSpec`. Annotations can already be passed via the Integration, and that would avoid opening the door to `Finalizers`, `OwnerReferences` and the like?
   - Should we filter out fields from the embedded  `PodTemplateSpec` / `PodSpec`? Are there any that do not functionally make sense embedding?
   - We have to add e2e tests covering the first use cases we have in mind, like:
     - Setting environment variables to the Integration container
     - Adding an init or side-car container
     - Setting `TopologySpreadConstraints`
     - ...


----------------------------------------------------------------
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.

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



[GitHub] [camel-k] astefanutti commented on a change in pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
astefanutti commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r617570511



##########
File path: go.mod
##########
@@ -13,6 +13,7 @@ require (
 	github.com/evanphx/json-patch v4.9.0+incompatible
 	github.com/fatih/structs v1.1.0
 	github.com/gertd/go-pluralize v0.1.1
+	github.com/ghodss/yaml v1.0.0

Review comment:
       It may be possible to use the `k8s.io/apimachinery/pkg/util/yaml` package, instead of introducing another dependency.

##########
File path: pkg/cmd/run.go
##########
@@ -721,6 +729,32 @@ func escapePropertyFileItem(item string) string {
 	return item
 }
 
+func resolvePodTemplate(templateSrc string, spec *v1.IntegrationSpec) (err error) {
+	// check if template is set
+	if templateSrc == "" {
+		return nil
+	}
+	var template v1.PodSpec
+
+	//check if value is a path to the file
+	if _, err := os.Stat(templateSrc); err == nil {
+		rsc, err := ResolveSources(context.TODO(), []string{templateSrc}, false)

Review comment:
       Can't the command context be used?

##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,102 @@
+/*
+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 trait
+
+import (
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/ghodss/yaml"
+	v12 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/strategicpatch"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+// The Pod trait allows to modify the custom PodTemplateSpec resource for the Integration pods

Review comment:
       I'd suggest:
   
   > The pod trait allows the customization of the Integration pods.
   >
   > It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
   > 
   > This can be used to customize the container where Camel routes execute, by using the `integration` container name.

##########
File path: pkg/cmd/run.go
##########
@@ -97,6 +98,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *runCmdOptions)
 	cmd.Flags().StringArray("property-file", nil, "Bind a property file to the integration. E.g. \"--property-file integration.properties\"")
 	cmd.Flags().StringArray("label", nil, "Add a label to the integration. E.g. \"--label my.company=hello\"")
 	cmd.Flags().StringArray("source", nil, "Add source file to your integration, this is added to the list of files listed as arguments of the command")
+	cmd.Flags().String("pod-template", "", "Custom PodTemplateSpec that will be used in integrations")

Review comment:
       I'd suggest "The path of the YAML file containing a PodSpec template to be used for the Integration pods".

##########
File path: pkg/apis/camel/v1/integration_types.go
##########
@@ -37,6 +37,7 @@ type IntegrationSpec struct {
 	Dependencies       []string                `json:"dependencies,omitempty"`
 	Profile            TraitProfile            `json:"profile,omitempty"`
 	Traits             map[string]TraitSpec    `json:"traits,omitempty"`
+	PodTemplate        *PodSpecTemplate        `json:"template,omitempty"`

Review comment:
       It may be more explicit to name the JSON field `podTemplate`?

##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,102 @@
+/*
+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 trait
+
+import (
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/ghodss/yaml"
+	v12 "k8s.io/api/core/v1"

Review comment:
       `v12` -> `corev1`

##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,102 @@
+/*
+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 trait
+
+import (
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/ghodss/yaml"
+	v12 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/strategicpatch"

Review comment:
       Can't `k8s.io/apimachinery/pkg/util/strategicpatch` be used directly?

##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,102 @@
+/*
+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 trait
+
+import (
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/ghodss/yaml"
+	v12 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/strategicpatch"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+// The Pod trait allows to modify the custom PodTemplateSpec resource for the Integration pods
+//
+// +camel-k:trait=pod
+type podTrait struct {
+	BaseTrait `property:",squash"`
+	Template  string `property:"template"`
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled != nil && !*t.Enabled {
+		return false, nil
+	}
+
+	if e.Integration!=nil && e.Integration.Spec.PodTemplate == nil {
+		return false, nil
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseDeploying,
+		v1.IntegrationPhaseRunning,
+	), nil
+}
+
+func (t *podTrait) Apply(e *Environment) error {
+	var deployment *appsv1.Deployment
+
+	e.Resources.VisitDeployment(func(d *appsv1.Deployment) {

Review comment:
       The other kind of deployment, like KnativeService or CronJob, must be visited as well.

##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,102 @@
+/*
+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 trait
+
+import (
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/ghodss/yaml"
+	v12 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/strategicpatch"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+// The Pod trait allows to modify the custom PodTemplateSpec resource for the Integration pods
+//
+// +camel-k:trait=pod
+type podTrait struct {
+	BaseTrait `property:",squash"`
+	Template  string `property:"template"`

Review comment:
       I think the `template` option can be removed?

##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,102 @@
+/*
+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 trait
+
+import (
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/ghodss/yaml"
+	v12 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/util/strategicpatch"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+// The Pod trait allows to modify the custom PodTemplateSpec resource for the Integration pods
+//
+// +camel-k:trait=pod
+type podTrait struct {
+	BaseTrait `property:",squash"`
+	Template  string `property:"template"`
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled != nil && !*t.Enabled {
+		return false, nil
+	}
+
+	if e.Integration!=nil && e.Integration.Spec.PodTemplate == nil {
+		return false, nil
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseDeploying,
+		v1.IntegrationPhaseRunning,
+	), nil
+}
+
+func (t *podTrait) Apply(e *Environment) error {
+	var deployment *appsv1.Deployment
+
+	e.Resources.VisitDeployment(func(d *appsv1.Deployment) {
+		if d.Name == e.Integration.Name {
+			deployment = d
+		}
+	})
+
+	patch, err := json.Marshal(e.Integration.Spec.PodTemplate.Spec)
+	if err != nil {
+		return err
+	}
+
+	patchedTemplate, err := patchTemplate(deployment.Spec.Template.Spec, patch)
+	if patchedTemplate != nil {
+		deployment.Spec.Template.Spec = *patchedTemplate
+	}
+	return nil
+}
+
+func (t *podTrait) parseTemplate() (*v12.PodTemplateSpec, error) {

Review comment:
       I think this can be removed?

##########
File path: e2e/common/traits/pod_test.go
##########
@@ -0,0 +1,64 @@
+// +build integration
+
+// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "knative"
+
+/*
+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 traits
+
+import (
+	"testing"
+
+	. "github.com/apache/camel-k/e2e/support"
+	camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	. "github.com/onsi/gomega"
+	v1 "k8s.io/api/core/v1"
+)
+
+func TestPodTrait(t *testing.T) {
+	WithNewTestNamespace(t, func(ns string) {
+		name := "pod-template-test"
+		Expect(Kamel("install", "-n", ns).Execute()).To(Succeed())
+		Expect(Kamel("run", "-n", ns, "files/PodTest.groovy",
+			"--name", name,
+			"--pod-template", "files/template.yaml",
+		).Execute()).To(Succeed())
+
+		//check integration is deployed
+		Eventually(IntegrationPodPhase(ns, name), TestTimeoutLong).Should(Equal(v1.PodRunning))
+		Eventually(IntegrationCondition(ns, name, camelv1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(v1.ConditionTrue))
+
+		//check that integrations is working and reading data created by sidecar container
+		Eventually(IntegrationLogs(ns, name), TestTimeoutShort).Should(ContainSubstring("Content from the sidecar container"))
+		pod := IntegrationPod(ns, name)()
+
+		//check if ENV variable is applied
+		envValue := getEnvVar("TEST_VARIABLE", pod.Spec)
+		Eventually(envValue).Should(Equal("pod_test_value"))

Review comment:
       `Expect` / `To` can be used here.




-- 
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.

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



[GitHub] [camel-k] astefanutti commented on a change in pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
astefanutti commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r542171432



##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,94 @@
+/*
+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 trait
+
+import (
+	"fmt"
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	source "github.com/apache/camel-k/pkg/util/source"
+	jsonpatch "github.com/evanphx/json-patch"
+	v12 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/apimachinery/pkg/util/yaml"
+
+	//"github.com/ghodss/yaml"
+	//	cmd "github.com/apache/camel-k/pkg/cmd"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+// The Pod trait allows to configure the custom PodTemplateSpec resource for the Integration pods.
+//
+// +camel-k:trait=pdb
+type podTrait struct {
+	BaseTrait `property:",squash"`
+	Template  string `property:"template"`
+	source    source.Source
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled == nil || !*t.Enabled {
+		return false, nil
+	}
+
+	if t.Template == "" {
+		return false, fmt.Errorf("template must be specified")
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseRunning,

Review comment:
       My understanding is that the template should also be applied during the `IntegrationPhaseDeploying` phase also.

##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,94 @@
+/*
+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 trait
+
+import (
+	"fmt"
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	source "github.com/apache/camel-k/pkg/util/source"
+	jsonpatch "github.com/evanphx/json-patch"
+	v12 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/apimachinery/pkg/util/yaml"
+
+	//"github.com/ghodss/yaml"
+	//	cmd "github.com/apache/camel-k/pkg/cmd"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+// The Pod trait allows to configure the custom PodTemplateSpec resource for the Integration pods.
+//
+// +camel-k:trait=pdb
+type podTrait struct {
+	BaseTrait `property:",squash"`
+	Template  string `property:"template"`
+	source    source.Source
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled == nil || !*t.Enabled {
+		return false, nil
+	}
+
+	if t.Template == "" {
+		return false, fmt.Errorf("template must be specified")
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseRunning,
+	), nil
+}
+
+func (t *podTrait) Apply(e *Environment) error {
+	src, err := source.ResolveSources(t.Ctx, []string{t.Template}, false)

Review comment:
       My understanding is that the resolution of the source should be done by the CLI, and the content should either be included in the Integration or a ConfigMap, referenced from the Integration. The reconciliation should not rely on local nor remote file, and should be idempotent.




----------------------------------------------------------------
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.

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



[GitHub] [camel-k] mmelko commented on pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
mmelko commented on pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#issuecomment-781433064


   I've created custom method for merging resources based on map[string]interface{}. I still need to add more tests and sort out the configmaps.  If this will be accepted method could be moved to some `*utils.go`


----------------------------------------------------------------
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.

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



[GitHub] [camel-k] lburgazzoli commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r625643467



##########
File path: docs/modules/traits/pages/pod.adoc
##########
@@ -0,0 +1,83 @@
+= Pod Trait
+
+// Start of autogenerated code - DO NOT EDIT! (description)
+   The pod trait allows the customization of the Integration pods.
+   It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+
+   This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+
+
+This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**.
+
+// End of autogenerated code - DO NOT EDIT! (description)
+== Example
+We have an integration that will read files from defined folder:
+[source,groovy]
+----
+from('file:///var/log')
+ .convertBodyTo(String.class)
+ .setBody().simple('${body}:  {{TEST_VARIABLE}} ')
+ .log('${body}')
+
+----
+The content of the folder might be autogenerated by bash script using a sidecar container. The pod template could be defined as follows:
+
+[source,yaml]
+----
+containers:
+  - name: integration

Review comment:
       How would this work when you have competing changes to the integration container ?
   i.e. 
   
   1. what would be the result of `kamel run --env TEST_VARIABLE=abc --pod-template template.yaml` ?
   2. what woudl happen if the user sets a custom entry point for the integration container ?




-- 
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.

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



[GitHub] [camel-k] mmelko commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
mmelko commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r625705337



##########
File path: docs/modules/traits/pages/pod.adoc
##########
@@ -0,0 +1,83 @@
+= Pod Trait
+
+// Start of autogenerated code - DO NOT EDIT! (description)
+   The pod trait allows the customization of the Integration pods.
+   It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+
+   This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+
+
+This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**.
+
+// End of autogenerated code - DO NOT EDIT! (description)
+== Example
+We have an integration that will read files from defined folder:
+[source,groovy]
+----
+from('file:///var/log')
+ .convertBodyTo(String.class)
+ .setBody().simple('${body}:  {{TEST_VARIABLE}} ')
+ .log('${body}')
+
+----
+The content of the folder might be autogenerated by bash script using a sidecar container. The pod template could be defined as follows:
+
+[source,yaml]
+----
+containers:
+  - name: integration

Review comment:
       1. TEST_VARIABLE=abc will be overwritten by values from the template, due to changes from the template are actually applied by pod trait in later stages. 
   2. I've tried to add there command from the example: `command: [ "/bin/sh" , "-c", "while true; do echo $(date -u) 'Content from the sidecar altered container' > /var/log/file.txt; sleep 1;done" ]` and nothing really happened. Command hasn't been overwritten due to `jvm` trait is executed after the `pod` trait. 




-- 
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.

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



[GitHub] [camel-k] astefanutti commented on pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
astefanutti commented on pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#issuecomment-790749521


   @mmelko I've created #2096, that would be the corner stone place to store the PodSpec.


----------------------------------------------------------------
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.

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



[GitHub] [camel-k] lburgazzoli commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
lburgazzoli commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r625735441



##########
File path: docs/modules/traits/pages/pod.adoc
##########
@@ -0,0 +1,83 @@
+= Pod Trait
+
+// Start of autogenerated code - DO NOT EDIT! (description)
+   The pod trait allows the customization of the Integration pods.
+   It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+
+   This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+
+
+This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**.
+
+// End of autogenerated code - DO NOT EDIT! (description)
+== Example
+We have an integration that will read files from defined folder:
+[source,groovy]
+----
+from('file:///var/log')
+ .convertBodyTo(String.class)
+ .setBody().simple('${body}:  {{TEST_VARIABLE}} ')
+ .log('${body}')
+
+----
+The content of the folder might be autogenerated by bash script using a sidecar container. The pod template could be defined as follows:
+
+[source,yaml]
+----
+containers:
+  - name: integration

Review comment:
       So I think the behavior is ok for the sake of this PR to get merged but I think we need to open some follow up issues and iterate a little bit over the expected behavior as:
   - the pod-template is well, a template so I'd expect it not to be "immutable" so, in the cli example I provided above, I'd expect the `TEST_VARIABLE` to reflect what it is set by the `--env` flag and not the value from the template. Of course in this case, you know what the template content is so you could workaround this but I'm thinking about a case where the template is configured at an higher level (i.e. IntegrationPlatform) and you don't know if you are overwriting, or if your changes have no effect because the template is overriding some of your config
   - the fact that a `TEST_VARIABLE` is taken from the template and the `command` not, make things a little confusing so we should probably limit/document the relation between the template and the `integration` container
   




-- 
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.

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



[GitHub] [camel-k] astefanutti commented on pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
astefanutti commented on pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#issuecomment-791448775


   Now that we validated strategic merge patch is a good candidate, we should find the place where the pod template should be stored.
   
   It may be worth exploring #2096 as a possible solution, embedding a `PodSpec` field inside the Integration resource, and adding an option to the `kamel run` CLI command, that would source the template into this field.


----------------------------------------------------------------
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.

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



[GitHub] [camel-k] mmelko commented on pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
mmelko commented on pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#issuecomment-801951308


   1. That's a valid point. If we don't need to update/configure metadata for the template (which we probably don't) embedding only the`PodSpec` would be probably much more safe.
   2. I guess it depends on what we want to allow to configure. I can imagine that we won't allow to modify for example the network properties...I need to investigate it more to understand how/where the pod is being created  
   3. Adding into my TODO.. 


----------------------------------------------------------------
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.

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



[GitHub] [camel-k] astefanutti merged pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
astefanutti merged pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861


   


-- 
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.

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



[GitHub] [camel-k] mmelko commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
mmelko commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r621991358



##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,121 @@
+/*
+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 trait
+
+import (
+	"fmt"
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"k8s.io/api/batch/v1beta1"
+	corev1 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/apimachinery/pkg/util/strategicpatch"
+	serving "knative.dev/serving/pkg/apis/serving/v1"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+//    The pod trait allows the customization of the Integration pods.
+//    It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+//
+//    This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+//
+// +camel-k:trait=pod
+type podTrait struct {
+	BaseTrait `property:",squash"`
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled != nil && !*t.Enabled {
+		return false, nil
+	}
+
+	if e.Integration != nil && e.Integration.Spec.PodTemplate == nil {
+		return false, nil
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseDeploying,
+		v1.IntegrationPhaseRunning,
+	), nil
+}
+
+func (t *podTrait) Apply(e *Environment) error {
+	changes := e.Integration.Spec.PodTemplate.Spec
+	var patchedPodSpec *corev1.PodSpec
+	strategy, err := e.DetermineControllerStrategy()
+	if err != nil {
+		return fmt.Errorf("unable to determine the controller stratedy")
+	}
+
+	if strategy == ControllerStrategyCronJob {
+		e.Resources.VisitCronJob(func(c *v1beta1.CronJob) {
+			if c.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&c.Spec.JobTemplate.Spec.Template.Spec, changes); err == nil {
+					c.Spec.JobTemplate.Spec.Template.Spec = *patchedPodSpec
+				}
+			}
+		})
+	} else if strategy == ControllerStrategyDeployment {
+		e.Resources.VisitDeployment(func(d *appsv1.Deployment) {
+			if d.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&d.Spec.Template.Spec, changes); err == nil {
+					d.Spec.Template.Spec = *patchedPodSpec
+				}
+			}
+		})
+	} else {
+		e.Resources.VisitKnativeService(func(s *serving.Service) {
+			if s.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&s.Spec.Template.Spec.PodSpec, changes); err == nil {
+					s.Spec.Template.Spec.PodSpec = * patchedPodSpec
+				}
+			}
+		})
+	}
+
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func (t *podTrait) applyChangesTo(podSpec *corev1.PodSpec, changes v1.PodSpec) (patchedPodSpec *corev1.PodSpec, err error) {
+	patch, err := json.Marshal(changes)
+	if err != nil {
+		return nil, err
+	}
+
+	sourceJson, err := json.Marshal(podSpec)
+	if err != nil {
+		return
+	}
+
+	patched, err := strategicpatch.StrategicMergePatch(sourceJson, patch, corev1.PodSpec{})
+	if err != nil {
+		return
+	}
+
+	err = json.Unmarshal(patched, &patchedPodSpec)
+	return

Review comment:
       actually `Unmarshal` returns only `err` so that won't work
   `func Unmarshal(data []byte, v interface{}) error {`




-- 
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.

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



[GitHub] [camel-k] mmelko commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
mmelko commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r617613012



##########
File path: pkg/apis/camel/v1/integration_types.go
##########
@@ -37,6 +37,7 @@ type IntegrationSpec struct {
 	Dependencies       []string                `json:"dependencies,omitempty"`
 	Profile            TraitProfile            `json:"profile,omitempty"`
 	Traits             map[string]TraitSpec    `json:"traits,omitempty"`
+	PodTemplate        *PodSpecTemplate        `json:"template,omitempty"`

Review comment:
       I kept the "template" to preserve the kubernetes naming: 
   ```
   spec:
     template:
       spec: 
   ```
   




-- 
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.

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



[GitHub] [camel-k] astefanutti commented on pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
astefanutti commented on pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#issuecomment-832450415


   Thanks!


-- 
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.

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



[GitHub] [camel-k] astefanutti commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
astefanutti commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r622031402



##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,121 @@
+/*
+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 trait
+
+import (
+	"fmt"
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"k8s.io/api/batch/v1beta1"
+	corev1 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/apimachinery/pkg/util/strategicpatch"
+	serving "knative.dev/serving/pkg/apis/serving/v1"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+//    The pod trait allows the customization of the Integration pods.
+//    It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+//
+//    This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+//
+// +camel-k:trait=pod
+type podTrait struct {
+	BaseTrait `property:",squash"`
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled != nil && !*t.Enabled {
+		return false, nil
+	}
+
+	if e.Integration != nil && e.Integration.Spec.PodTemplate == nil {
+		return false, nil
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseDeploying,
+		v1.IntegrationPhaseRunning,
+	), nil
+}
+
+func (t *podTrait) Apply(e *Environment) error {
+	changes := e.Integration.Spec.PodTemplate.Spec
+	var patchedPodSpec *corev1.PodSpec
+	strategy, err := e.DetermineControllerStrategy()
+	if err != nil {
+		return fmt.Errorf("unable to determine the controller stratedy")
+	}
+
+	if strategy == ControllerStrategyCronJob {
+		e.Resources.VisitCronJob(func(c *v1beta1.CronJob) {
+			if c.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&c.Spec.JobTemplate.Spec.Template.Spec, changes); err == nil {
+					c.Spec.JobTemplate.Spec.Template.Spec = *patchedPodSpec
+				}
+			}
+		})
+	} else if strategy == ControllerStrategyDeployment {
+		e.Resources.VisitDeployment(func(d *appsv1.Deployment) {
+			if d.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&d.Spec.Template.Spec, changes); err == nil {
+					d.Spec.Template.Spec = *patchedPodSpec
+				}
+			}
+		})
+	} else {
+		e.Resources.VisitKnativeService(func(s *serving.Service) {
+			if s.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&s.Spec.Template.Spec.PodSpec, changes); err == nil {
+					s.Spec.Template.Spec.PodSpec = * patchedPodSpec
+				}
+			}
+		})
+	}
+
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func (t *podTrait) applyChangesTo(podSpec *corev1.PodSpec, changes v1.PodSpec) (patchedPodSpec *corev1.PodSpec, err error) {
+	patch, err := json.Marshal(changes)
+	if err != nil {
+		return nil, err
+	}
+
+	sourceJson, err := json.Marshal(podSpec)
+	if err != nil {
+		return
+	}
+
+	patched, err := strategicpatch.StrategicMergePatch(sourceJson, patch, corev1.PodSpec{})
+	if err != nil {
+		return
+	}
+
+	err = json.Unmarshal(patched, &patchedPodSpec)
+	return

Review comment:
       Ah right, I got confused by those empty return statements and named return variables :)




-- 
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.

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



[GitHub] [camel-k] astefanutti commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
astefanutti commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r621951297



##########
File path: go.mod
##########
@@ -13,6 +13,7 @@ require (
 	github.com/evanphx/json-patch v4.9.0+incompatible
 	github.com/fatih/structs v1.1.0
 	github.com/gertd/go-pluralize v0.1.1
+	github.com/ghodss/yaml v1.0.0

Review comment:
       This can be removed now.

##########
File path: pkg/cmd/run.go
##########
@@ -35,10 +35,12 @@ import (
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
 	"github.com/spf13/pflag"
+	//"gopkg.in/yaml.v3"

Review comment:
       This can be removed.

##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,121 @@
+/*
+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 trait
+
+import (
+	"fmt"
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"k8s.io/api/batch/v1beta1"
+	corev1 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/apimachinery/pkg/util/strategicpatch"
+	serving "knative.dev/serving/pkg/apis/serving/v1"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+//    The pod trait allows the customization of the Integration pods.
+//    It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+//
+//    This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+//
+// +camel-k:trait=pod
+type podTrait struct {
+	BaseTrait `property:",squash"`
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled != nil && !*t.Enabled {
+		return false, nil
+	}
+
+	if e.Integration != nil && e.Integration.Spec.PodTemplate == nil {
+		return false, nil
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseDeploying,
+		v1.IntegrationPhaseRunning,
+	), nil
+}
+
+func (t *podTrait) Apply(e *Environment) error {
+	changes := e.Integration.Spec.PodTemplate.Spec
+	var patchedPodSpec *corev1.PodSpec
+	strategy, err := e.DetermineControllerStrategy()
+	if err != nil {
+		return fmt.Errorf("unable to determine the controller stratedy")
+	}
+
+	if strategy == ControllerStrategyCronJob {
+		e.Resources.VisitCronJob(func(c *v1beta1.CronJob) {
+			if c.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&c.Spec.JobTemplate.Spec.Template.Spec, changes); err == nil {
+					c.Spec.JobTemplate.Spec.Template.Spec = *patchedPodSpec
+				}
+			}
+		})
+	} else if strategy == ControllerStrategyDeployment {
+		e.Resources.VisitDeployment(func(d *appsv1.Deployment) {
+			if d.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&d.Spec.Template.Spec, changes); err == nil {
+					d.Spec.Template.Spec = *patchedPodSpec
+				}
+			}
+		})
+	} else {
+		e.Resources.VisitKnativeService(func(s *serving.Service) {
+			if s.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&s.Spec.Template.Spec.PodSpec, changes); err == nil {
+					s.Spec.Template.Spec.PodSpec = * patchedPodSpec
+				}
+			}
+		})
+	}
+
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func (t *podTrait) applyChangesTo(podSpec *corev1.PodSpec, changes v1.PodSpec) (patchedPodSpec *corev1.PodSpec, err error) {
+	patch, err := json.Marshal(changes)
+	if err != nil {
+		return nil, err
+	}
+
+	sourceJson, err := json.Marshal(podSpec)
+	if err != nil {
+		return
+	}
+
+	patched, err := strategicpatch.StrategicMergePatch(sourceJson, patch, corev1.PodSpec{})
+	if err != nil {
+		return
+	}
+
+	err = json.Unmarshal(patched, &patchedPodSpec)
+	return

Review comment:
       This can be changed to `return json.Unmarshal(patched, &patchedPodSpec)`.

##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,121 @@
+/*
+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 trait
+
+import (
+	"fmt"
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"k8s.io/api/batch/v1beta1"
+	corev1 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/apimachinery/pkg/util/strategicpatch"
+	serving "knative.dev/serving/pkg/apis/serving/v1"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+//    The pod trait allows the customization of the Integration pods.
+//    It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+//
+//    This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+//
+// +camel-k:trait=pod
+type podTrait struct {
+	BaseTrait `property:",squash"`
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled != nil && !*t.Enabled {
+		return false, nil
+	}
+
+	if e.Integration != nil && e.Integration.Spec.PodTemplate == nil {
+		return false, nil
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseDeploying,
+		v1.IntegrationPhaseRunning,
+	), nil
+}
+
+func (t *podTrait) Apply(e *Environment) error {
+	changes := e.Integration.Spec.PodTemplate.Spec
+	var patchedPodSpec *corev1.PodSpec
+	strategy, err := e.DetermineControllerStrategy()
+	if err != nil {
+		return fmt.Errorf("unable to determine the controller stratedy")
+	}
+
+	if strategy == ControllerStrategyCronJob {
+		e.Resources.VisitCronJob(func(c *v1beta1.CronJob) {
+			if c.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&c.Spec.JobTemplate.Spec.Template.Spec, changes); err == nil {
+					c.Spec.JobTemplate.Spec.Template.Spec = *patchedPodSpec
+				}
+			}
+		})
+	} else if strategy == ControllerStrategyDeployment {
+		e.Resources.VisitDeployment(func(d *appsv1.Deployment) {
+			if d.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&d.Spec.Template.Spec, changes); err == nil {
+					d.Spec.Template.Spec = *patchedPodSpec
+				}
+			}
+		})
+	} else {
+		e.Resources.VisitKnativeService(func(s *serving.Service) {
+			if s.Name == e.Integration.Name {
+				if patchedPodSpec, err = t.applyChangesTo(&s.Spec.Template.Spec.PodSpec, changes); err == nil {
+					s.Spec.Template.Spec.PodSpec = * patchedPodSpec
+				}
+			}
+		})
+	}
+
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func (t *podTrait) applyChangesTo(podSpec *corev1.PodSpec, changes v1.PodSpec) (patchedPodSpec *corev1.PodSpec, err error) {

Review comment:
       Is `patchedPodSpec *corev1.PodSpec` needed in the return signature?

##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,121 @@
+/*
+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 trait
+
+import (
+	"fmt"
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"k8s.io/api/batch/v1beta1"
+	corev1 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/apimachinery/pkg/util/strategicpatch"
+	serving "knative.dev/serving/pkg/apis/serving/v1"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+//    The pod trait allows the customization of the Integration pods.
+//    It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+//
+//    This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+//
+// +camel-k:trait=pod
+type podTrait struct {
+	BaseTrait `property:",squash"`
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled != nil && !*t.Enabled {
+		return false, nil
+	}
+
+	if e.Integration != nil && e.Integration.Spec.PodTemplate == nil {
+		return false, nil
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseDeploying,
+		v1.IntegrationPhaseRunning,
+	), nil
+}
+
+func (t *podTrait) Apply(e *Environment) error {
+	changes := e.Integration.Spec.PodTemplate.Spec
+	var patchedPodSpec *corev1.PodSpec
+	strategy, err := e.DetermineControllerStrategy()
+	if err != nil {
+		return fmt.Errorf("unable to determine the controller stratedy")
+	}
+
+	if strategy == ControllerStrategyCronJob {

Review comment:
       Maybe a switch statement would be more appropriate?

##########
File path: docs/modules/traits/pages/pod.adoc
##########
@@ -0,0 +1,33 @@
+= Pod Trait
+
+// Start of autogenerated code - DO NOT EDIT! (description)
+   The pod trait allows the customization of the Integration pods.
+   It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+
+   This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+
+
+This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**.
+
+// End of autogenerated code - DO NOT EDIT! (description)
+// Start of autogenerated code - DO NOT EDIT! (configuration)

Review comment:
       Could you add the example from the e2e test?

##########
File path: pkg/trait/pod_test.go
##########
@@ -0,0 +1,157 @@
+package trait
+
+import (
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/apache/camel-k/pkg/util/kubernetes"
+	"github.com/stretchr/testify/assert"
+	"gopkg.in/yaml.v2"
+	appsv1 "k8s.io/api/apps/v1"
+	corev1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"testing"
+)
+
+func TestConfigurePodTraitDoesSucceed(t *testing.T) {
+	trait, environment, _ := createPodTest("")
+	configured, err := trait.Configure(environment)
+
+	assert.True(t, configured)
+	assert.Nil(t, err)
+
+	configured, err = trait.Configure(environment)
+
+	assert.True(t, configured)
+	assert.Nil(t, err)
+}
+
+//
+func TestSimpleChange(t *testing.T) {
+	templateString := `containers:
+  - name: second-container
+    env:
+      - name: test
+        value: test`
+	template := testPodTemplateSpec(t, templateString)
+
+	assert.Equal(t, 3, len(template.Spec.Containers))
+}
+
+func TestMergeArrays(t *testing.T) {
+	templateString :=
+		"{containers: [{name: second-container, " +
+		"env: [{name: SOME_VARIABLE, value: SOME_VALUE}, {name: SOME_VARIABLE2, value: SOME_VALUE2}]}, " +
+		"{name: integration, env: [{name: TEST_ADDED_CUSTOM_VARIABLE, value: value}]}" +
+		"]" +
+		"}"
+	templateSpec := testPodTemplateSpec(t, templateString)
+
+//	assert.Equal(t, "test-template", templateSpec.Name)
+	assert.NotNil(t, getContainer(templateSpec.Spec.Containers, "second-container"))
+	assert.Equal(t, "SOME_VALUE", containsEnvVariables(templateSpec, "second-container", "SOME_VARIABLE"))
+	assert.Equal(t, "SOME_VALUE2", containsEnvVariables(templateSpec, "second-container", "SOME_VARIABLE2"))
+	assert.True(t, len(getContainer(templateSpec.Spec.Containers, "integration").Env) > 1)
+	assert.Equal(t, "value", containsEnvVariables(templateSpec, "integration", "TEST_ADDED_CUSTOM_VARIABLE"))
+}
+
+func TestChangeEnvVariables(t *testing.T) {
+	templateString := "{containers: [" +
+		"{name: second, env: [{name: TEST_VARIABLE, value: TEST_VALUE}]}, " +
+		"{name: integration, env: [{name: CAMEL_K_DIGEST, value: new_value}]}" +
+		"]}"
+	templateSpec := testPodTemplateSpec(t, templateString)
+
+	//check if env var was added in second container
+	assert.Equal(t, containsEnvVariables(templateSpec, "second", "TEST_VARIABLE"), "TEST_VALUE")
+	assert.Equal(t, 3, len(getContainer(templateSpec.Spec.Containers, "second").Env))
+
+	//check if env var was changed
+	assert.Equal(t, containsEnvVariables(templateSpec, "integration", "CAMEL_K_DIGEST"), "new_value")
+}
+
+func createPodTest(templateString string) (*podTrait, *Environment, *appsv1.Deployment) {
+	trait := newPodTrait().(*podTrait)
+	enabled := true
+	trait.Enabled = &enabled
+
+	specTemplateYamlString := "{metadata: {name: example-template, creationTimestamp: null, " +
+		"labels: {camel.apache.org/integration: test}}, " +
+		"spec: {volumes: [" +
+		"{name: i-source-000," + "configMap: {name: test-source-000, items: [{key: content, path: test.groovy}], defaultMode: 420}}, " +
+		"{name: application-properties, configMap: {name: test-application-properties, items: [{key: application.properties, path: application.properties}], defaultMode: 420}}], " +
+		"containers: [" +
+		"{name: second, env: [{name: SOME_VARIABLE, value: SOME_VALUE}, {name: SOME_VARIABLE2, value: SOME_VALUE2}]}," +
+		"{name: integration, command: [/bin/sh, '-c'], env: [{name: CAMEL_K_DIGEST, value: vO3wwJHC7-uGEiFFVac0jq6rZT5EZNw56Ae5gKKFZZsk}, {name: CAMEL_K_CONF, value: /etc/camel/conf/application.properties}, {name: CAMEL_K_CONF_D, value: /etc/camel/conf.d},{name: CAMEL_K_VERSION, value: 1.3.0-SNAPSHOT}, {name: CAMEL_K_INTEGRATION, value: test}, {name: CAMEL_K_RUNTIME_VERSION, value: 1.5.0}, {name: CAMEL_K_MOUNT_PATH_CONFIGMAPS, value: /etc/camel/conf.d/_configmaps}, {name: CAMEL_K_MOUNT_PATH_SECRETS, value: /etc/camel/conf.d/_secrets}, {name: NAMESPACE, valueFrom: {fieldRef: {apiVersion: v1, fieldPath: metadata.namespace}}}, {name: POD_NAME, valueFrom: {fieldRef: {apiVersion: v1, fieldPath: metadata.name}}}], imagePullPolicy: IfNotPresent, volumeMounts: [{name: i-source-000, mountPath: /etc/camel/sources/i-source-000}, {name: application-properties, mountPath: /etc/camel/conf}], terminationMessagePolicy: File, image: 'image-registry.openshift-image-registry.svc:5000/podtrait/camel-k-kit-bv
 d7utv170hult6ju26g@sha256:1c091437ef986f2852733da5f3fce7a5f48a5ea51e409f0bdcb0c13ff620e6b2', workingDir: /deployments, args: ['echo exec java -cp ./resources:/etc/camel/conf:/etc/camel/resources:/etc/camel/sources/i-source-000 io.quarkus.runner.GeneratedMain']}], restartPolicy: Always, terminationGracePeriodSeconds: 90, dnsPolicy: ClusterFirst, securityContext: {}, schedulerName: default-scheduler}}"
+	var template corev1.PodTemplateSpec
+	_ = yaml.Unmarshal([]byte(specTemplateYamlString), &template)
+
+	var podTemplateIt v1.PodSpec
+	if templateString != "" {
+		_ = yaml.Unmarshal([]byte(templateString), &podTemplateIt)
+	}
+
+	deployment := &appsv1.Deployment{
+		ObjectMeta: metav1.ObjectMeta{
+			Name: "pod-template-test-integration",
+		},
+		Spec: appsv1.DeploymentSpec{
+			Template: template,
+		},
+	}
+
+	environment := &Environment{
+		Integration: &v1.Integration{
+			ObjectMeta: metav1.ObjectMeta{
+				Name: "pod-template-test-integration",
+			},
+			Status: v1.IntegrationStatus{
+				Phase: v1.IntegrationPhaseDeploying,
+			},
+			Spec: v1.IntegrationSpec{
+				PodTemplate: &v1.PodSpecTemplate{
+					Spec: podTemplateIt,
+				},
+			},
+		},
+
+		Resources: kubernetes.NewCollection(deployment),
+	}
+	return trait, environment, deployment
+}
+
+func containsEnvVariables(template corev1.PodTemplateSpec, containerName string, name string) string {
+	container := getContainer(template.Spec.Containers, containerName)
+
+	for i := range container.Env {
+		envv := container.Env[i]
+		if envv.Name == name {
+			return envv.Value
+		}
+	}
+	return "not found!"
+}
+
+func getContainer(containers []corev1.Container, name string) *corev1.Container {
+	for i := range containers {
+		if containers[i].Name == name {
+			return &containers[i]
+		}
+	}
+	return nil
+}
+
+func testPodTemplateSpec(t *testing.T, template string) corev1.PodTemplateSpec {
+	trait, environment, _ := createPodTest(template)
+	//trait.Template = template

Review comment:
       This can be removed.




-- 
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.

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



[GitHub] [camel-k] astefanutti commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
astefanutti commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r625739973



##########
File path: docs/modules/traits/pages/pod.adoc
##########
@@ -0,0 +1,83 @@
+= Pod Trait
+
+// Start of autogenerated code - DO NOT EDIT! (description)
+   The pod trait allows the customization of the Integration pods.
+   It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+
+   This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+
+
+This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**.
+
+// End of autogenerated code - DO NOT EDIT! (description)
+== Example
+We have an integration that will read files from defined folder:
+[source,groovy]
+----
+from('file:///var/log')
+ .convertBodyTo(String.class)
+ .setBody().simple('${body}:  {{TEST_VARIABLE}} ')
+ .log('${body}')
+
+----
+The content of the folder might be autogenerated by bash script using a sidecar container. The pod template could be defined as follows:
+
+[source,yaml]
+----
+containers:
+  - name: integration

Review comment:
       Right, we need to decide what is the precedence. I see three layers:
   * operator
   * user provided pod template
   * any other user provided options (that affects the integration pod)
   
   The first decision to make is whether the operator is prescriptive or not, that is whether it's possible to override the operator defined spec by the template. Then whether the pod template overwrites other user defined options, or the other way around. For the later, I would be inclined to think it's better from an end-user standpoint to have the specific options have precedence over the more general pod template. For the former, that depends on the level of power we want to delegate to the users.




-- 
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.

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



[GitHub] [camel-k] mmelko commented on a change in pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
mmelko commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r542224099



##########
File path: pkg/trait/pod.go
##########
@@ -0,0 +1,94 @@
+/*
+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 trait
+
+import (
+	"fmt"
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	source "github.com/apache/camel-k/pkg/util/source"
+	jsonpatch "github.com/evanphx/json-patch"
+	v12 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/util/json"
+	"k8s.io/apimachinery/pkg/util/yaml"
+
+	//"github.com/ghodss/yaml"
+	//	cmd "github.com/apache/camel-k/pkg/cmd"
+
+	appsv1 "k8s.io/api/apps/v1"
+)
+
+// The Pod trait allows to configure the custom PodTemplateSpec resource for the Integration pods.
+//
+// +camel-k:trait=pdb
+type podTrait struct {
+	BaseTrait `property:",squash"`
+	Template  string `property:"template"`
+	source    source.Source
+}
+
+func newPodTrait() Trait {
+	return &podTrait{
+		BaseTrait: NewBaseTrait("pod", 1800),
+	}
+}
+
+func (t *podTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled == nil || !*t.Enabled {
+		return false, nil
+	}
+
+	if t.Template == "" {
+		return false, fmt.Errorf("template must be specified")
+	}
+
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseRunning,
+	), nil
+}
+
+func (t *podTrait) Apply(e *Environment) error {
+	src, err := source.ResolveSources(t.Ctx, []string{t.Template}, false)

Review comment:
       ah .. that makes perfect sense 




----------------------------------------------------------------
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.

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



[GitHub] [camel-k] mmelko commented on pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
mmelko commented on pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#issuecomment-801912534


   I update the PR also with adding PodTemplateSpec into integration spec (https://github.com/apache/camel-k/issues/2096). Pod trait is now enabled when template will be passed. That is possible with `--pod-template` param from CLI e.g `kamel run integration.yaml --pod-template template.yaml`.  Template is merged with the deployment one using kubernetes strategic merge. 


----------------------------------------------------------------
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.

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



[GitHub] [camel-k] astefanutti commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
astefanutti commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r617618357



##########
File path: pkg/apis/camel/v1/integration_types.go
##########
@@ -37,6 +37,7 @@ type IntegrationSpec struct {
 	Dependencies       []string                `json:"dependencies,omitempty"`
 	Profile            TraitProfile            `json:"profile,omitempty"`
 	Traits             map[string]TraitSpec    `json:"traits,omitempty"`
+	PodTemplate        *PodSpecTemplate        `json:"template,omitempty"`

Review comment:
       Ah right, that's the whole point, I've been too prompt :)




-- 
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.

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



[GitHub] [camel-k] mmelko commented on a change in pull request #1861: PodSpecable Integration

Posted by GitBox <gi...@apache.org>.
mmelko commented on a change in pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#discussion_r625705337



##########
File path: docs/modules/traits/pages/pod.adoc
##########
@@ -0,0 +1,83 @@
+= Pod Trait
+
+// Start of autogenerated code - DO NOT EDIT! (description)
+   The pod trait allows the customization of the Integration pods.
+   It applies the `PodSpecTemplate` struct contained in the Integration `.spec.podTemplate` field, into the Integration deployment Pods template, using strategic merge patch.
+
+   This can be used to customize the container where Camel routes execute, by using the `integration` container name.
+
+
+This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**.
+
+// End of autogenerated code - DO NOT EDIT! (description)
+== Example
+We have an integration that will read files from defined folder:
+[source,groovy]
+----
+from('file:///var/log')
+ .convertBodyTo(String.class)
+ .setBody().simple('${body}:  {{TEST_VARIABLE}} ')
+ .log('${body}')
+
+----
+The content of the folder might be autogenerated by bash script using a sidecar container. The pod template could be defined as follows:
+
+[source,yaml]
+----
+containers:
+  - name: integration

Review comment:
       1. TEST_VARIABLE=abc will be overwritten by values from the template, due to changes from the template are actually applied by pod trait in later stages. 
   2. I've tried to add there command from the example: `command: [ "/bin/sh" , "-c", "while true; do echo $(date -u) 'Content from the sidecar altered container' > /var/log/file.txt; sleep 1;done" ]` and nothing really happened. Command hasn't been overwritten. 




-- 
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.

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



[GitHub] [camel-k] mmelko commented on pull request #1861: WIP: Add podTrait to specify customPodTemplateSpec

Posted by GitBox <gi...@apache.org>.
mmelko commented on pull request #1861:
URL: https://github.com/apache/camel-k/pull/1861#issuecomment-824023421


   I've created a custom PodTemplateSpec based on  `v1core.PodTemplateSpec`  https://github.com/apache/camel-k/pull/1861/files#diff-df704f21201b6d30fe851b7bee054f77855f8b5720e77d93e8462fffb9c71cb3R225 to reduce the modification posibilities. I've also added simple e2e test to update ENV properties and add sidecar container.


-- 
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.

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