Skip to content

Commit

Permalink
Fix #1325: add 3scale addon
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Mar 17, 2020
1 parent a2f9629 commit b7ec6ae
Show file tree
Hide file tree
Showing 10 changed files with 398 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
3 changes: 2 additions & 1 deletion docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
** xref:configuration/configmap-secret.adoc[ConfigMap/Secret]
* xref:traits/traits.adoc[Traits]
// Start of autogenerated code - DO NOT EDIT! (trait-nav)
** xref:traits/3scale.adoc[3scale]
** xref:traits/affinity.adoc[Affinity]
** xref:traits/builder.adoc[Builder]
** xref:traits/camel.adoc[Camel]
Expand All @@ -33,12 +34,12 @@
** xref:traits/knative-service.adoc[Knative Service]
** xref:traits/knative.adoc[Knative]
** xref:traits/master.adoc[Master]
** xref:traits/openapi.adoc[Openapi]
** xref:traits/owner.adoc[Owner]
** xref:traits/platform.adoc[Platform]
** xref:traits/prometheus.adoc[Prometheus]
** xref:traits/pull-secret.adoc[Pull Secret]
** xref:traits/quarkus.adoc[Quarkus]
** xref:traits/openapi.adoc[Rest Dsl]
** xref:traits/route.adoc[Route]
** xref:traits/service.adoc[Service]
// End of autogenerated code - DO NOT EDIT! (trait-nav)
Expand Down
52 changes: 52 additions & 0 deletions docs/modules/ROOT/pages/traits/3scale.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
= 3scale Trait

// Start of autogenerated code - DO NOT EDIT! (description)
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.


This trait is available in the following profiles: **Kubernetes, Knative, OpenShift**.

// End of autogenerated code - DO NOT EDIT! (description)
// Start of autogenerated code - DO NOT EDIT! (configuration)
== Configuration

Trait properties can be specified when running any integration with the CLI:
```
kamel run --trait 3scale.[key]=[value] --trait 3scale.[key2]=[value2] integration.groovy
```
The following configuration options are available:

[cols="2,1,5a"]
|===
|Property | Type | Description

| 3scale.enabled
| bool
| Can be used to enable or disable a trait. All traits share this common property.

| 3scale.auto
| bool
| Enables automatic configuration of the trait.

| 3scale.scheme
| string
| The scheme to use to contact the service (default `http`)

| 3scale.path
| string
| The path where the API is published (default `/`)

| 3scale.port
| int
| The port where the service is exposed (default `80`)

| 3scale.description-path
| string
| The path where the Open-API specification is published (default `/api-doc`)

|===

// End of autogenerated code - DO NOT EDIT! (configuration)
Loading

0 comments on commit b7ec6ae

Please sign in to comment.