Skip to content

Commit

Permalink
Fix apache#1325: add 3scale addon
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Mar 16, 2020
1 parent a8f8db9 commit aa88c5c
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 5 deletions.
27 changes: 27 additions & 0 deletions addons/register_3scale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
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 addons

import (
"github.com/apache/camel-k/addons/threescale"
"github.com/apache/camel-k/pkg/trait"
)

func init() {
trait.AddToTraits(threescale.NewThreeScaleTrait)
}
138 changes: 138 additions & 0 deletions addons/threescale/3scale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
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 threescale

import (
"strconv"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/trait"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// The 3scale trait can be used to automatically create annotations that allow
// 3scale to discover the generated service and make it available for API management.
//
// The 3scale trait is disabled by default.
//
// +camel-k:trait=3scale
type threeScaleTrait struct {
trait.BaseTrait `property:",squash"`
// Enables automatic configuration of the trait.
Auto *bool `property:"auto"`
// The scheme to use to contact the service (default `http`)
Scheme string `property:"scheme"`
// The path where the API is published (default `/`)
Path string `property:"path"`
// The port where the service is exposed (default `80`)
Port int `property:"port"`
// The path where the Open-API specification is published (default `/api-doc`)
DescriptionPath *string `property:"description-path"`
}

const (
// ThreeScaleSchemeAnnotation --
ThreeScaleSchemeAnnotation = "discovery.3scale.net/scheme"
// ThreeScaleSchemeDefaultValue --
ThreeScaleSchemeDefaultValue = "http"

// ThreeScalePortAnnotation --
ThreeScalePortAnnotation = "discovery.3scale.net/port"
// ThreeScalePortDefaultValue --
ThreeScalePortDefaultValue = 80

// ThreeScalePathAnnotation --
ThreeScalePathAnnotation = "discovery.3scale.net/path"
// ThreeScalePathDefaultValue --
ThreeScalePathDefaultValue = "/"

// ThreeScaleDescriptionPathAnnotation --
ThreeScaleDescriptionPathAnnotation = "discovery.3scale.net/description-path"
// ThreeScaleDescriptionPathDefaultValue --
ThreeScaleDescriptionPathDefaultValue = "/api-doc"

// ThreeScaleDiscoveryLabel --
ThreeScaleDiscoveryLabel = "discovery.3scale.net"
// ThreeScaleDiscoveryLabelEnabled --
ThreeScaleDiscoveryLabelEnabled = "true"
)

// NewThreeScaleTrait --
func NewThreeScaleTrait() trait.Trait {
return &threeScaleTrait{
BaseTrait: trait.NewBaseTrait("3scale", trait.TraitOrderPostProcessResources),
}
}

func (t *threeScaleTrait) Configure(e *trait.Environment) (bool, error) {
if t.Enabled == nil || !*t.Enabled {
// disabled by default
return false, nil
}

if !e.IntegrationInPhase(v1.IntegrationPhaseDeploying) {
return false, nil
}

if t.Auto == nil || *t.Auto {
if t.Scheme == "" {
t.Scheme = ThreeScaleSchemeDefaultValue
}
if t.Path == "" {
t.Path = ThreeScalePathDefaultValue
}
if t.Port == 0 {
t.Port = ThreeScalePortDefaultValue
}
if t.DescriptionPath == nil {
openAPI := ThreeScaleDescriptionPathDefaultValue
t.DescriptionPath = &openAPI
}
}
return true, nil
}

func (t *threeScaleTrait) Apply(e *trait.Environment) error {
if svc := e.Resources.GetServiceForIntegration(e.Integration); svc != nil {
t.addLabelsAndAnnotations(&svc.ObjectMeta)
}
return nil
}

func (t *threeScaleTrait) addLabelsAndAnnotations(obj *metav1.ObjectMeta) {
if obj.Labels == nil {
obj.Labels = make(map[string]string)
}
obj.Labels[ThreeScaleDiscoveryLabel] = ThreeScaleDiscoveryLabelEnabled

if obj.Annotations == nil {
obj.Annotations = make(map[string]string)
}
if t.Scheme != "" {
obj.Annotations[ThreeScaleSchemeAnnotation] = t.Scheme
}
if t.Path != "" {
obj.Annotations[ThreeScalePathAnnotation] = t.Path
}
if t.Port != 0 {
obj.Annotations[ThreeScalePortAnnotation] = strconv.Itoa(t.Port)
}
if t.DescriptionPath != nil && *t.DescriptionPath != "" {
obj.Annotations[ThreeScaleDescriptionPathAnnotation] = *t.DescriptionPath
}
}
114 changes: 114 additions & 0 deletions addons/threescale/3scale_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
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 threescale

import (
"testing"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestThreeScaleDisabled(t *testing.T) {
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

e := &trait.Environment{
CamelCatalog: catalog,
}

threeScale := NewThreeScaleTrait()
enabled, err := threeScale.Configure(e)
assert.Nil(t, err)
assert.False(t, enabled)
}

func TestThreeScaleInjection(t *testing.T) {
svc, e := createEnvironment(t)
threeScale := NewThreeScaleTrait()
enabled := true
threeScale.(*threeScaleTrait).Enabled = &enabled
ok, err := threeScale.Configure(e)
assert.Nil(t, err)
assert.True(t, ok)

err = threeScale.Apply(e)
assert.Nil(t, err)

assert.Equal(t, "true", svc.Labels["discovery.3scale.net"])
assert.Equal(t, "http", svc.Annotations["discovery.3scale.net/scheme"])
assert.Equal(t, "/", svc.Annotations["discovery.3scale.net/path"])
assert.Equal(t, "80", svc.Annotations["discovery.3scale.net/port"])
assert.Equal(t, "/api-doc", svc.Annotations["discovery.3scale.net/description-path"])
}

func TestThreeScaleInjectionNoAPIPath(t *testing.T) {
svc, e := createEnvironment(t)
threeScale := NewThreeScaleTrait()
enabled := true
threeScale.(*threeScaleTrait).Enabled = &enabled
noPath := ""
threeScale.(*threeScaleTrait).DescriptionPath = &noPath
ok, err := threeScale.Configure(e)
assert.Nil(t, err)
assert.True(t, ok)

err = threeScale.Apply(e)
assert.Nil(t, err)

assert.Equal(t, "true", svc.Labels["discovery.3scale.net"])
assert.Equal(t, "http", svc.Annotations["discovery.3scale.net/scheme"])
assert.Equal(t, "/", svc.Annotations["discovery.3scale.net/path"])
assert.Equal(t, "80", svc.Annotations["discovery.3scale.net/port"])
_, p := svc.Annotations["discovery.3scale.net/description-path"]
assert.False(t, p)
}

func createEnvironment(t *testing.T) (*corev1.Service, *trait.Environment) {
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

e := trait.Environment{
CamelCatalog: catalog,
}

svc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"camel.apache.org/integration": "test",
},
},
}
e.Resources = kubernetes.NewCollection(&svc)

it := v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseDeploying,
},
}
e.Integration = &it
return &svc, &e
}
1 change: 1 addition & 0 deletions addons/threescale/zz_generated_doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package threescale
36 changes: 36 additions & 0 deletions build/maven/pom-runtime.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@
<groupId>com.oracle.substratevm</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand All @@ -119,6 +123,10 @@
<groupId>com.oracle.substratevm</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand All @@ -130,6 +138,10 @@
<groupId>com.oracle.substratevm</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand All @@ -141,6 +153,10 @@
<groupId>com.oracle.substratevm</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand All @@ -152,6 +168,10 @@
<groupId>com.oracle.substratevm</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand All @@ -163,6 +183,10 @@
<groupId>com.oracle.substratevm</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand All @@ -174,6 +198,10 @@
<groupId>com.oracle.substratevm</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand All @@ -185,6 +213,10 @@
<groupId>com.oracle.substratevm</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand All @@ -196,6 +228,10 @@
<groupId>com.oracle.substratevm</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand Down
Loading

0 comments on commit aa88c5c

Please sign in to comment.