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: us...@infra.apache.org