You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2018/12/07 11:06:17 UTC

[GitHub] nicolaferraro closed pull request #272: Allow using compressed sources in Knative profile

nicolaferraro closed pull request #272: Allow using compressed sources in Knative profile
URL: https://github.com/apache/camel-k/pull/272
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go
index 716392a5..eecec1b6 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 00000000..325e0f98
--- /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 d6b7b72c..46655757 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 805039c9..18594ad4 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
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


With regards,
Apache Git Services