This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/master by this push: new f90356f Allow using compressed sources in Knative profile #270 f90356f is described below commit f90356ff79bda43382640d8407e6ad25ece5e667 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Fri Dec 7 11:39:52 2018 +0100 Allow using compressed sources in Knative profile #270 --- pkg/trait/knative.go | 17 +++++-- pkg/trait/knative_test.go | 95 +++++++++++++++++++++++++++++++++++++++ pkg/util/kubernetes/collection.go | 5 +++ pkg/util/util.go | 14 ++++++ 4 files changed, 127 insertions(+), 4 deletions(-) diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go index 716392a..eecec1b 100644 --- a/pkg/trait/knative.go +++ b/pkg/trait/knative.go @@ -21,11 +21,12 @@ import ( "encoding/json" "fmt" - "github.com/operator-framework/operator-sdk/pkg/sdk" - "github.com/pkg/errors" "strconv" "strings" + "github.com/operator-framework/operator-sdk/pkg/sdk" + "github.com/pkg/errors" + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/metadata" @@ -118,9 +119,17 @@ func (t *knativeTrait) getServiceFor(e *Environment) (*serving.Service, error) { envName := fmt.Sprintf("KAMEL_K_ROUTE_%03d", i) environment[envName] = s.Content - src := fmt.Sprintf("env:%s", envName) + params := make([]string, 0) if s.Language != "" { - src = src + "?language=" + string(s.Language) + params = append(params, "language="+string(s.Language)) + } + if s.Compression { + params = append(params, "compression=true") + } + + src := fmt.Sprintf("env:%s", envName) + if len(params) > 0 { + src = fmt.Sprintf("%s?%s", src, strings.Join(params, "&")) } sources = append(sources, src) diff --git a/pkg/trait/knative_test.go b/pkg/trait/knative_test.go new file mode 100644 index 0000000..325e0f9 --- /dev/null +++ b/pkg/trait/knative_test.go @@ -0,0 +1,95 @@ +/* +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 ( + "testing" + + "github.com/apache/camel-k/pkg/util" + + "github.com/apache/camel-k/pkg/util/kubernetes" + serving "github.com/knative/serving/pkg/apis/serving/v1alpha1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" + "github.com/stretchr/testify/assert" +) + +func TestKnativeTraitWithCompressedSources(t *testing.T) { + content := "H4sIAAAAAAAA/+JKK8rP1VAvycxNLbIqyUzOVtfkUlBQUNAryddQz8lPt8rMS8tX1+QCAAAA//8BAAD//3wZ4pUoAAAA" + + env := Environment{ + Integration: &v1alpha1.Integration{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "ns", + }, + Status: v1alpha1.IntegrationStatus{ + Phase: v1alpha1.IntegrationPhaseDeploying, + }, + Spec: v1alpha1.IntegrationSpec{ + Profile: v1alpha1.TraitProfileKnative, + Sources: []v1alpha1.SourceSpec{ + { + Language: v1alpha1.LanguageJavaScript, + Name: "routes.js", + Content: content, + Compression: true, + }, + }, + }, + }, + Platform: &v1alpha1.IntegrationPlatform{ + Spec: v1alpha1.IntegrationPlatformSpec{ + Cluster: v1alpha1.IntegrationPlatformClusterOpenShift, + Build: v1alpha1.IntegrationPlatformBuildSpec{ + PublishStrategy: v1alpha1.IntegrationPlatformBuildPublishStrategyS2I, + Registry: "registry", + }, + }, + }, + EnvVars: make(map[string]string), + ExecutedTraits: make([]ID, 0), + Resources: kubernetes.NewCollection(), + } + + err := NewCatalog().apply(&env) + + assert.Nil(t, err) + assert.NotEmpty(t, env.ExecutedTraits) + assert.Contains(t, env.ExecutedTraits, ID("knative")) + assert.NotNil(t, env.EnvVars["KAMEL_KNATIVE_CONFIGURATION"]) + + services := 0 + env.Resources.VisitKnativeService(func(service *serving.Service) { + services++ + + vars := service.Spec.RunLatest.Configuration.RevisionTemplate.Spec.Container.Env + + routes := util.LookupEnvVar(vars, "CAMEL_K_ROUTES") + assert.NotNil(t, routes) + assert.Equal(t, "env:KAMEL_K_ROUTE_000?language=js&compression=true", routes.Value) + + route := util.LookupEnvVar(vars, "KAMEL_K_ROUTE_000") + assert.NotNil(t, route) + assert.Equal(t, content, route.Value) + }) + + assert.True(t, services > 0) + assert.True(t, env.Resources.Size() > 0) +} diff --git a/pkg/util/kubernetes/collection.go b/pkg/util/kubernetes/collection.go index d6b7b72..4665575 100644 --- a/pkg/util/kubernetes/collection.go +++ b/pkg/util/kubernetes/collection.go @@ -38,6 +38,11 @@ func NewCollection() *Collection { } } +// Size returns the number of resources belonging to the collection +func (c *Collection) Size() int { + return len(c.items) +} + // Items returns all resources belonging to the collection func (c *Collection) Items() []runtime.Object { return c.items diff --git a/pkg/util/util.go b/pkg/util/util.go index 805039c..18594ad 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -23,6 +23,8 @@ import ( "path" "syscall" + "k8s.io/api/core/v1" + "github.com/pkg/errors" ) @@ -98,3 +100,15 @@ func WriteFileWithContent(buildDir string, relativePath string, content string) } return nil } + +// LookupEnvVar -- +func LookupEnvVar(vars []v1.EnvVar, name string) *v1.EnvVar { + for _, e := range vars { + if e.Name == name { + ev := e + return &ev + } + } + + return nil +}