Skip to content

Commit

Permalink
Allow using compressed sources in Knative profile #270
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli authored and nicolaferraro committed Dec 7, 2018
1 parent b599fc0 commit f90356f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pkg/trait/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
95 changes: 95 additions & 0 deletions pkg/trait/knative_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions pkg/util/kubernetes/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"path"
"syscall"

"k8s.io/api/core/v1"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
}

0 comments on commit f90356f

Please sign in to comment.