Skip to content

Commit

Permalink
Add container ports to integration deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti authored and nicolaferraro committed Jan 29, 2019
1 parent 673553d commit c07b579
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pkg/trait/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ func (c *Catalog) apply(environment *Environment) error {
}
}

for _, processor := range environment.PostProcessors {
err := processor(environment)
if err != nil {
return err
}
}

return nil
}

Expand Down
23 changes: 23 additions & 0 deletions pkg/trait/jolokia.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ limitations under the License.
package trait

import (
"errors"
"strconv"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/envvar"
corev1 "k8s.io/api/core/v1"

"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -82,5 +85,25 @@ func (t *jolokiaTrait) Apply(e *Environment) (err error) {
envvar.SetVal(&e.EnvVars, "AB_JOLOKIA_PASSWORD_RANDOM", strconv.FormatBool(*t.RandomPassword))
}

// Register a post processor to add a container port to the integration deployment
e.PostProcessors = append(e.PostProcessors, func(environment *Environment) error {
var container *corev1.Container
environment.Resources.VisitContainer(func(c *corev1.Container) {
if c.Name == environment.Integration.Name {
container = c
}
})
if container != nil {
container.Ports = append(container.Ports, corev1.ContainerPort{
Name: "jolokia",
ContainerPort: int32(t.Port),
Protocol: corev1.ProtocolTCP,
})
} else {
return errors.New("Cannot add Jolokia container port: no integration container")
}
return nil
})

return nil
}
23 changes: 22 additions & 1 deletion pkg/trait/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/envvar"
"github.com/pkg/errors"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -64,7 +65,7 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) {
// Expose the Prometheus endpoint
// Either update the existing service added by previously executed traits
// (e.g. the service trait) or add a new service resource
svc := e.Resources.GetService(func (svc *corev1.Service) bool {
svc := e.Resources.GetService(func(svc *corev1.Service) bool {
return svc.Name == e.Integration.Name
})
if svc == nil {
Expand All @@ -78,6 +79,26 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) {
}
svc.Spec.Ports = append(svc.Spec.Ports, port)

// Register a post processor to add a container port to the integration deployment
e.PostProcessors = append(e.PostProcessors, func(environment *Environment) error {
var container *corev1.Container
environment.Resources.VisitContainer(func(c *corev1.Container) {
if c.Name == environment.Integration.Name {
container = c
}
})
if container != nil {
container.Ports = append(container.Ports, corev1.ContainerPort{
Name: "prometheus",
ContainerPort: int32(t.Port),
Protocol: corev1.ProtocolTCP,
})
} else {
return errors.New("Cannot add Prometheus container port: no integration container")
}
return nil
})

// Add the ServiceMonitor resource
smt := t.getServiceMonitorFor(e)
e.Resources.Add(smt)
Expand Down
21 changes: 21 additions & 0 deletions pkg/trait/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package trait
import (
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/metadata"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -82,6 +83,26 @@ func (t *serviceTrait) Apply(e *Environment) (err error) {
}
svc.Spec.Ports = append(svc.Spec.Ports, port)

// Register a post processor to add a container port to the integration deployment
e.PostProcessors = append(e.PostProcessors, func(environment *Environment) error {
var container *corev1.Container
environment.Resources.VisitContainer(func(c *corev1.Container) {
if c.Name == environment.Integration.Name {
container = c
}
})
if container != nil {
container.Ports = append(container.Ports, corev1.ContainerPort{
Name: "http",
ContainerPort: int32(t.Port),
Protocol: corev1.ProtocolTCP,
})
} else {
return errors.New("Cannot add HTTP container port: no integration container")
}
return nil
})

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/trait/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"github.com/apache/camel-k/pkg/metadata"
"github.com/apache/camel-k/pkg/platform"
"github.com/apache/camel-k/pkg/util/kubernetes"
"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
)

// Identifiable represent an identifiable type
Expand Down Expand Up @@ -99,6 +99,7 @@ type Environment struct {
Context *v1alpha1.IntegrationContext
Integration *v1alpha1.Integration
Resources *kubernetes.Collection
PostProcessors []func(*Environment) error
Steps []builder.Step
BuildDir string
ExecutedTraits []Trait
Expand Down

0 comments on commit c07b579

Please sign in to comment.