This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 29df016d062b1696a9d8f2c670758a56f2e33e95 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Thu Jul 18 17:31:07 2019 +0200 Stable container name #826 (pr review) --- pkg/trait/container.go | 63 +++++++++++++++++++++++++++++++++++--------- pkg/trait/deployment.go | 37 -------------------------- pkg/trait/knative_service.go | 54 ------------------------------------- 3 files changed, 51 insertions(+), 103 deletions(-) diff --git a/pkg/trait/container.go b/pkg/trait/container.go index 2a127a8..d3044d9 100644 --- a/pkg/trait/container.go +++ b/pkg/trait/container.go @@ -19,6 +19,9 @@ package trait import ( "fmt" + "strings" + + "github.com/apache/camel-k/pkg/util/envvar" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" serving "github.com/knative/serving/pkg/apis/serving/v1alpha1" @@ -29,7 +32,7 @@ import ( ) const ( - defaultContainerName = "integration-container" + defaultContainerName = "integration" containerTraitID = "container" ) @@ -80,23 +83,59 @@ func (t *containerTrait) Configure(e *Environment) (bool, error) { } func (t *containerTrait) Apply(e *Environment) error { + container := corev1.Container{ + Name: t.Name, + Image: e.Integration.Status.Image, + Env: make([]corev1.EnvVar, 0), + } - // - // Add mounted volumes as resources - // - e.Resources.VisitDeployment(func(deployment *appsv1.Deployment) { - for i := 0; i < len(deployment.Spec.Template.Spec.Containers); i++ { - container := &deployment.Spec.Template.Spec.Containers[i] - container.Name = t.Name + // combine Environment of integration with platform, kit, integration + for key, value := range e.CollectConfigurationPairs("env") { + envvar.SetVal(&container.Env, key, value) + } + + envvar.SetVal(&container.Env, "CAMEL_K_DIGEST", e.Integration.Status.Digest) + envvar.SetVal(&container.Env, "CAMEL_K_ROUTES", strings.Join(e.ComputeSourcesURI(), ",")) + envvar.SetVal(&container.Env, "CAMEL_K_CONF", "/etc/camel/conf/application.properties") + envvar.SetVal(&container.Env, "CAMEL_K_CONF_D", "/etc/camel/conf.d") - t.configureResources(e, container) + t.configureResources(e, &container) + + e.Resources.VisitDeployment(func(deployment *appsv1.Deployment) { + for _, envVar := range e.EnvVars { + envvar.SetVar(&container.Env, envVar) } + + e.ConfigureVolumesAndMounts( + &deployment.Spec.Template.Spec.Volumes, + &container.VolumeMounts, + ) + + deployment.Spec.Template.Spec.Containers = append(deployment.Spec.Template.Spec.Containers, container) }) + e.Resources.VisitKnativeService(func(service *serving.Service) { - container := &service.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container - container.Name = t.Name + for _, env := range e.EnvVars { + switch { + case env.ValueFrom == nil: + envvar.SetVar(&container.Env, env) + case env.ValueFrom.FieldRef != nil && env.ValueFrom.FieldRef.FieldPath == "metadata.namespace": + envvar.SetVar(&container.Env, corev1.EnvVar{Name: env.Name, Value: e.Integration.Namespace}) + case env.ValueFrom.FieldRef != nil: + t.L.Infof("Environment variable %s uses fieldRef and cannot be set on a Knative service", env.Name) + case env.ValueFrom.ResourceFieldRef != nil: + t.L.Infof("Environment variable %s uses resourceFieldRef and cannot be set on a Knative service", env.Name) + default: + envvar.SetVar(&container.Env, env) + } + } + + e.ConfigureVolumesAndMounts( + &service.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Volumes, + &container.VolumeMounts, + ) - t.configureResources(e, container) + service.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container = container }) if t.Expose != nil && *t.Expose { diff --git a/pkg/trait/deployment.go b/pkg/trait/deployment.go index 157ef84..b867366 100644 --- a/pkg/trait/deployment.go +++ b/pkg/trait/deployment.go @@ -18,10 +18,7 @@ limitations under the License. package trait import ( - "strings" - "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "github.com/apache/camel-k/pkg/util/envvar" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -121,28 +118,6 @@ func (t *deploymentTrait) Apply(e *Environment) error { // ********************************** func (t *deploymentTrait) getDeploymentFor(e *Environment) *appsv1.Deployment { - paths := e.ComputeSourcesURI() - environment := make([]corev1.EnvVar, 0) - - // combine Environment of integration with platform, kit, integration - for key, value := range e.CollectConfigurationPairs("env") { - envvar.SetVal(&environment, key, value) - } - - // camel-k runtime - envvar.SetVal(&environment, "CAMEL_K_ROUTES", strings.Join(paths, ",")) - envvar.SetVal(&environment, "CAMEL_K_CONF", "/etc/camel/conf/application.properties") - envvar.SetVal(&environment, "CAMEL_K_CONF_D", "/etc/camel/conf.d") - - // add a dummy env var to trigger deployment if everything but the code - // has been changed - envvar.SetVal(&environment, "CAMEL_K_DIGEST", e.Integration.Status.Digest) - - // add env vars from traits - for _, envVar := range e.EnvVars { - envvar.SetVar(&environment, envVar) - } - // create a copy to avoid sharing the underlying annotation map annotations := make(map[string]string) if e.Integration.Annotations != nil { @@ -183,22 +158,10 @@ func (t *deploymentTrait) getDeploymentFor(e *Environment) *appsv1.Deployment { }, Spec: corev1.PodSpec{ ServiceAccountName: e.Integration.Spec.ServiceAccountName, - Containers: []corev1.Container{ - { - Name: e.Integration.Name, - Image: e.Integration.Status.Image, - Env: environment, - }, - }, }, }, }, } - e.ConfigureVolumesAndMounts( - &deployment.Spec.Template.Spec.Volumes, - &deployment.Spec.Template.Spec.Containers[0].VolumeMounts, - ) - return &deployment } diff --git a/pkg/trait/knative_service.go b/pkg/trait/knative_service.go index 1c916cb..8c1f334 100644 --- a/pkg/trait/knative_service.go +++ b/pkg/trait/knative_service.go @@ -19,13 +19,11 @@ package trait import ( "strconv" - "strings" "github.com/apache/camel-k/pkg/util/kubernetes" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/metadata" - "github.com/apache/camel-k/pkg/util/envvar" serving "github.com/knative/serving/pkg/apis/serving/v1alpha1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -209,10 +207,6 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) *serving.Service { }, Spec: serving.RevisionSpec{ ServiceAccountName: e.Integration.Spec.ServiceAccountName, - Container: corev1.Container{ - Image: e.Integration.Status.Image, - Env: make([]corev1.EnvVar, 0), - }, }, }, }, @@ -220,53 +214,5 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) *serving.Service { }, } - paths := e.ComputeSourcesURI() - environment := &svc.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container.Env - - // combine Environment of integration with kit, integration - for key, value := range e.CollectConfigurationPairs("env") { - envvar.SetVal(environment, key, value) - } - - // add a dummy env var to trigger deployment if everything but the code - // has been changed - envvar.SetVal(environment, "CAMEL_K_DIGEST", e.Integration.Status.Digest) - - envvar.SetVal(environment, "CAMEL_K_ROUTES", strings.Join(paths, ",")) - envvar.SetVal(environment, "CAMEL_K_CONF", "/etc/camel/conf/application.properties") - envvar.SetVal(environment, "CAMEL_K_CONF_D", "/etc/camel/conf.d") - - // add env vars from traits - for _, envVar := range t.getAllowedEnvVars(e) { - envvar.SetVar(&svc.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container.Env, envVar) - } - - e.ConfigureVolumesAndMounts( - &svc.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Volumes, - &svc.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container.VolumeMounts, - ) - return &svc } - -func (t *knativeServiceTrait) getAllowedEnvVars(e *Environment) []corev1.EnvVar { - res := make([]corev1.EnvVar, 0, len(e.EnvVars)) - for _, env := range e.EnvVars { - switch { - case env.ValueFrom == nil: - res = append(res, env) - case env.ValueFrom.FieldRef != nil && env.ValueFrom.FieldRef.FieldPath == "metadata.namespace": - res = append(res, corev1.EnvVar{ - Name: env.Name, - Value: e.Integration.Namespace, - }) - case env.ValueFrom.FieldRef != nil: - t.L.Infof("Environment variable %s uses fieldRef and cannot be set on a Knative service", env.Name) - case env.ValueFrom.ResourceFieldRef != nil: - t.L.Infof("Environment variable %s uses resourceFieldRef and cannot be set on a Knative service", env.Name) - default: - res = append(res, env) - } - } - return res -}