Skip to content

Commit

Permalink
feat(trait): Provide the service type from the trait configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo authored and tadayosi committed Sep 9, 2022
1 parent b5e38af commit 4d24894
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 28 deletions.
16 changes: 12 additions & 4 deletions config/crd/bases/camel.apache.org_integrationplatforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1587,9 +1587,13 @@ spec:
traits share this common property.
type: boolean
nodePort:
description: Enable Service to be exposed as NodePort (default
`false`).
description: 'Enable Service to be exposed as NodePort (default
`false`). Deprecated: Use service type instead.'
type: boolean
type:
description: The type of service to be used, either 'ClusterIP',
'NodePort' or 'LoadBalancer'.
type: string
type: object
service-binding:
description: The configuration of Service Binding trait
Expand Down Expand Up @@ -3220,9 +3224,13 @@ spec:
traits share this common property.
type: boolean
nodePort:
description: Enable Service to be exposed as NodePort (default
`false`).
description: 'Enable Service to be exposed as NodePort (default
`false`). Deprecated: Use service type instead.'
type: boolean
type:
description: The type of service to be used, either 'ClusterIP',
'NodePort' or 'LoadBalancer'.
type: string
type: object
service-binding:
description: The configuration of Service Binding trait
Expand Down
8 changes: 6 additions & 2 deletions config/crd/bases/camel.apache.org_integrations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7278,9 +7278,13 @@ spec:
traits share this common property.
type: boolean
nodePort:
description: Enable Service to be exposed as NodePort (default
`false`).
description: 'Enable Service to be exposed as NodePort (default
`false`). Deprecated: Use service type instead.'
type: boolean
type:
description: The type of service to be used, either 'ClusterIP',
'NodePort' or 'LoadBalancer'.
type: string
type: object
service-binding:
description: The configuration of Service Binding trait
Expand Down
8 changes: 6 additions & 2 deletions config/crd/bases/camel.apache.org_kameletbindings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7578,9 +7578,13 @@ spec:
All traits share this common property.
type: boolean
nodePort:
description: Enable Service to be exposed as NodePort
(default `false`).
description: 'Enable Service to be exposed as NodePort
(default `false`). Deprecated: Use service type instead.'
type: boolean
type:
description: The type of service to be used, either 'ClusterIP',
'NodePort' or 'LoadBalancer'.
type: string
type: object
service-binding:
description: The configuration of Service Binding trait
Expand Down
18 changes: 18 additions & 0 deletions docs/modules/ROOT/partials/apis/camel-k-crds.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5739,10 +5739,28 @@ bool
Enable Service to be exposed as NodePort (default `false`).
Deprecated: Use service type instead.
|`type` +
*xref:#_camel_apache_org_v1_trait_ServiceType[ServiceType]*
|
The type of service to be used, either 'ClusterIP', 'NodePort' or 'LoadBalancer'.
|===
[#_camel_apache_org_v1_trait_ServiceType]
=== ServiceType(`string` alias)
*Appears on:*
* <<#_camel_apache_org_v1_trait_ServiceTrait, ServiceTrait>>
[#_camel_apache_org_v1_trait_TolerationTrait]
=== TolerationTrait
Expand Down
5 changes: 5 additions & 0 deletions docs/modules/traits/pages/service.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ The following configuration options are available:
| service.node-port
| bool
| Enable Service to be exposed as NodePort (default `false`).
Deprecated: Use service type instead.

| service.type
| github.com/apache/camel-k/pkg/apis/camel/v1/trait.ServiceType
| The type of service to be used, either 'ClusterIP', 'NodePort' or 'LoadBalancer'.

|===

Expand Down
45 changes: 45 additions & 0 deletions e2e/global/common/traits/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,50 @@ func TestServiceTrait(t *testing.T) {

Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})

t.Run("NodePort service from Type", func(t *testing.T) {
Expect(KamelRunWithID(operatorID, ns, "files/PlatformHttpServer.java",
"-t", "service.enabled=true",
"-t", "service.type=NodePort").Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, "platform-http-server"), TestTimeoutLong).Should(Equal(corev1.PodRunning))

//
// Service names can vary with the ExternalName Service
// sometimes being created first and being given the root name
//
Eventually(ServicesByType(ns, corev1.ServiceTypeNodePort), TestTimeoutLong).ShouldNot(BeEmpty())

Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})

t.Run("ClusterIP service from Type", func(t *testing.T) {
Expect(KamelRunWithID(operatorID, ns, "files/PlatformHttpServer.java",
"-t", "service.enabled=true",
"-t", "service.type=ClusterIP").Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, "platform-http-server"), TestTimeoutLong).Should(Equal(corev1.PodRunning))

//
// Service names can vary with the ExternalName Service
// sometimes being created first and being given the root name
//
Eventually(ServicesByType(ns, corev1.ServiceTypeClusterIP), TestTimeoutLong).ShouldNot(BeEmpty())

Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})

t.Run("LoadBalancer service from Type", func(t *testing.T) {
Expect(KamelRunWithID(operatorID, ns, "files/PlatformHttpServer.java",
"-t", "service.enabled=true",
"-t", "service.type=LoadBalancer").Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, "platform-http-server"), TestTimeoutLong).Should(Equal(corev1.PodRunning))

//
// Service names can vary with the ExternalName Service
// sometimes being created first and being given the root name
//
Eventually(ServicesByType(ns, corev1.ServiceTypeLoadBalancer), TestTimeoutLong).ShouldNot(BeEmpty())

Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})
})
}
16 changes: 12 additions & 4 deletions helm/camel-k/crds/crd-integration-platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1587,9 +1587,13 @@ spec:
traits share this common property.
type: boolean
nodePort:
description: Enable Service to be exposed as NodePort (default
`false`).
description: 'Enable Service to be exposed as NodePort (default
`false`). Deprecated: Use service type instead.'
type: boolean
type:
description: The type of service to be used, either 'ClusterIP',
'NodePort' or 'LoadBalancer'.
type: string
type: object
service-binding:
description: The configuration of Service Binding trait
Expand Down Expand Up @@ -3220,9 +3224,13 @@ spec:
traits share this common property.
type: boolean
nodePort:
description: Enable Service to be exposed as NodePort (default
`false`).
description: 'Enable Service to be exposed as NodePort (default
`false`). Deprecated: Use service type instead.'
type: boolean
type:
description: The type of service to be used, either 'ClusterIP',
'NodePort' or 'LoadBalancer'.
type: string
type: object
service-binding:
description: The configuration of Service Binding trait
Expand Down
8 changes: 6 additions & 2 deletions helm/camel-k/crds/crd-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7278,9 +7278,13 @@ spec:
traits share this common property.
type: boolean
nodePort:
description: Enable Service to be exposed as NodePort (default
`false`).
description: 'Enable Service to be exposed as NodePort (default
`false`). Deprecated: Use service type instead.'
type: boolean
type:
description: The type of service to be used, either 'ClusterIP',
'NodePort' or 'LoadBalancer'.
type: string
type: object
service-binding:
description: The configuration of Service Binding trait
Expand Down
8 changes: 6 additions & 2 deletions helm/camel-k/crds/crd-kamelet-binding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7578,9 +7578,13 @@ spec:
All traits share this common property.
type: boolean
nodePort:
description: Enable Service to be exposed as NodePort
(default `false`).
description: 'Enable Service to be exposed as NodePort
(default `false`). Deprecated: Use service type instead.'
type: boolean
type:
description: The type of service to be used, either 'ClusterIP',
'NodePort' or 'LoadBalancer'.
type: string
type: object
service-binding:
description: The configuration of Service Binding trait
Expand Down
20 changes: 20 additions & 0 deletions pkg/apis/camel/v1/trait/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,25 @@ type ServiceTrait struct {
// To automatically detect from the code if a Service needs to be created.
Auto *bool `property:"auto" json:"auto,omitempty"`
// Enable Service to be exposed as NodePort (default `false`).
// Deprecated: Use service type instead.
NodePort *bool `property:"node-port" json:"nodePort,omitempty"`
// The type of service to be used, either 'ClusterIP', 'NodePort' or 'LoadBalancer'.
Type *ServiceType `property:"type" json:"type,omitempty"`
}

type ServiceType string

const (
// ServiceTypeClusterIP means a service will only be accessible inside the
// cluster, via the cluster IP.
ServiceTypeClusterIP ServiceType = "ClusterIP"

// ServiceTypeNodePort means a service will be exposed on one port of
// every node, in addition to 'ClusterIP' type.
ServiceTypeNodePort ServiceType = "NodePort"

// ServiceTypeLoadBalancer means a service will be exposed via an
// external load balancer (if the cloud provider supports it), in addition
// to 'NodePort' type.
ServiceTypeLoadBalancer ServiceType = "LoadBalancer"
)
5 changes: 5 additions & 0 deletions pkg/apis/camel/v1/trait/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions pkg/resources/resources.go

Large diffs are not rendered by default.

21 changes: 18 additions & 3 deletions pkg/trait/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package trait

import (
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -90,7 +92,6 @@ func (t *serviceTrait) Configure(e *Environment) (bool, error) {
return false, nil
}
}

return true, nil
}

Expand All @@ -100,9 +101,23 @@ func (t *serviceTrait) Apply(e *Environment) error {
if svc == nil {
svc = getServiceFor(e)

if pointer.BoolDeref(t.NodePort, false) {
svc.Spec.Type = corev1.ServiceTypeNodePort
var serviceType corev1.ServiceType
if t.Type != nil {
switch *t.Type {
case traitv1.ServiceTypeClusterIP:
serviceType = corev1.ServiceTypeClusterIP
case traitv1.ServiceTypeNodePort:
serviceType = corev1.ServiceTypeNodePort
case traitv1.ServiceTypeLoadBalancer:
serviceType = corev1.ServiceTypeLoadBalancer
default:
return fmt.Errorf("unsupported service type: %s", *t.Type)
}
} else if pointer.BoolDeref(t.NodePort, false) {
t.L.ForIntegration(e.Integration).Infof("Integration %s/%s should no more use the flag node-port as it is deprecated, use type instead", e.Integration.Namespace, e.Integration.Name)
serviceType = corev1.ServiceTypeNodePort
}
svc.Spec.Type = serviceType
}
e.Resources.Add(svc)
return nil
Expand Down
7 changes: 6 additions & 1 deletion resources/traits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,12 @@ traits:
description: To automatically detect from the code if a Service needs to be created.
- name: node-port
type: bool
description: Enable Service to be exposed as NodePort (default `false`).
description: 'Enable Service to be exposed as NodePort (default `false`). Deprecated:
Use service type instead.'
- name: type
type: github.com/apache/camel-k/pkg/apis/camel/v1/trait.ServiceType
description: The type of service to be used, either 'ClusterIP', 'NodePort' or
'LoadBalancer'.
- name: service-binding
platform: false
profiles:
Expand Down

0 comments on commit 4d24894

Please sign in to comment.