-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2f9629
commit b7ec6ae
Showing
10 changed files
with
398 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package threescale |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.