Skip to content

Commit

Permalink
Fix #219: use deployment in Knative when cannot scale to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro authored and lburgazzoli committed Dec 6, 2018
1 parent ec51786 commit 46fe56b
Show file tree
Hide file tree
Showing 9 changed files with 508 additions and 68 deletions.
131 changes: 131 additions & 0 deletions pkg/metadata/http.go
@@ -0,0 +1,131 @@
/*
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 metadata

import (
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"regexp"
"strings"
)

var httpURIs = map[string]bool{
"ahc": true,
"ahc-ws": true,
"atmosphere-websocket": true,
"cxf": true,
"cxfrs": true,
"grpc": true,
"jetty": true,
"netty-http": true,
"netty4-http": true,
"rest": true,
"restlet": true,
"servlet": true,
"spark-rest": true,
"spring-ws": true,
"undertow": true,
"websocket": true,
"knative": true,
}

var passiveURIs = map[string]bool{
"bean": true,
"binding": true,
"browse": true,
"class": true,
"controlbus": true,
"dataformat": true,
"dataset": true,
"direct": true,
"direct-vm": true,
"language": true,
"log": true,
"mock": true,
"properties": true,
"ref": true,
"seda": true,
"stub": true,
"test": true,
"validator": true,
"vm": true,
}

var restIndicator = regexp.MustCompile(".*rest\\s*\\([^)]*\\).*")
var xmlRestIndicator = regexp.MustCompile(".*<\\s*rest\\s+[^>]*>.*")

// requiresHTTPService returns true if the integration needs to expose itself through HTTP
func requiresHTTPService(source v1alpha1.SourceSpec, fromURIs []string) bool {
if hasRestIndicator(source) {
return true
}
return containsHTTPURIs(fromURIs)
}

// hasOnlyPassiveEndpoints returns true if the integration has no endpoint that needs to remain always active
func hasOnlyPassiveEndpoints(source v1alpha1.SourceSpec, fromURIs []string) bool {
passivePlusHTTP := make(map[string]bool)
for k, v := range passiveURIs {
passivePlusHTTP[k] = v
}
for k, v := range httpURIs {
passivePlusHTTP[k] = v
}
return containsOnlyURIsIn(fromURIs, passivePlusHTTP)
}

func containsHTTPURIs(fromURI []string) bool {
for _, uri := range fromURI {
prefix := getURIPrefix(uri)
if enabled, ok := httpURIs[prefix]; ok && enabled {
return true
}
}
return false
}

func containsOnlyURIsIn(fromURI []string, allowed map[string]bool) bool {
for _, uri := range fromURI {
prefix := getURIPrefix(uri)
if enabled, ok := allowed[prefix]; !ok || !enabled {
return false
}
}
return true
}

func getURIPrefix(uri string) string {
parts := strings.SplitN(uri, ":", 2)
if len(parts) > 0 {
return parts[0]
}
return ""
}

func hasRestIndicator(source v1alpha1.SourceSpec) bool {
pat := getRestIndicatorRegexpsForLanguage(source.Language)
return pat.MatchString(source.Content)
}

func getRestIndicatorRegexpsForLanguage(language v1alpha1.Language) *regexp.Regexp {
switch language {
case v1alpha1.LanguageXML:
return xmlRestIndicator
default:
return restIndicator
}
}
57 changes: 53 additions & 4 deletions pkg/metadata/metadata.go
Expand Up @@ -19,19 +19,68 @@ package metadata

import (
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"sort"
)

// ExtractAll returns metadata information from all listed source codes
func ExtractAll(sources []v1alpha1.SourceSpec) IntegrationMetadata {
// neutral metadata
meta := IntegrationMetadata{
Language: "",
Dependencies: []string{},
FromURIs: []string{},
ToURIs: []string{},
PassiveEndpoints: true,
RequiresHTTPService: false,
}
for _, source := range sources {
meta = merge(meta, Extract(source))
}
return meta
}

func merge(m1 IntegrationMetadata, m2 IntegrationMetadata) IntegrationMetadata {
language := m2.Language
if m1.Language != "" && m1.Language != language {
language = ""
}
deps := make(map[string]bool)
for _, d := range m1.Dependencies {
deps[d] = true
}
for _, d := range m2.Dependencies {
deps[d] = true
}
allDependencies := make([]string, 0)
for k := range deps {
allDependencies = append(allDependencies, k)
}
sort.Strings(allDependencies)
return IntegrationMetadata{
Language: language,
FromURIs: append(m1.FromURIs, m2.FromURIs...),
ToURIs: append(m1.ToURIs, m2.ToURIs...),
Dependencies: allDependencies,
RequiresHTTPService: m1.RequiresHTTPService || m2.RequiresHTTPService,
PassiveEndpoints: m1.PassiveEndpoints && m2.PassiveEndpoints,
}
}

// Extract returns metadata information from the source code
func Extract(source v1alpha1.SourceSpec) IntegrationMetadata {
language := discoverLanguage(source)
fromURIs := discoverFromURIs(source, language)
toURIs := discoverToURIs(source, language)
dependencies := discoverDependencies(source, fromURIs, toURIs)
requiresHTTPService := requiresHTTPService(source, fromURIs)
passiveEndpoints := hasOnlyPassiveEndpoints(source, fromURIs)
return IntegrationMetadata{
Language: language,
FromURIs: fromURIs,
ToURIs: toURIs,
Dependencies: dependencies,
Language: language,
FromURIs: fromURIs,
ToURIs: toURIs,
Dependencies: dependencies,
RequiresHTTPService: requiresHTTPService,
PassiveEndpoints: passiveEndpoints,
}
}

Expand Down

0 comments on commit 46fe56b

Please sign in to comment.