diff --git a/pkg/trait/catalog.go b/pkg/trait/catalog.go index 23114622aa..defa0e1fcb 100644 --- a/pkg/trait/catalog.go +++ b/pkg/trait/catalog.go @@ -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 } diff --git a/pkg/trait/jolokia.go b/pkg/trait/jolokia.go index c3ffbc83bb..b7c70804b3 100644 --- a/pkg/trait/jolokia.go +++ b/pkg/trait/jolokia.go @@ -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" ) @@ -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 } diff --git a/pkg/trait/prometheus.go b/pkg/trait/prometheus.go index 3e71587b21..3ee31e92db 100644 --- a/pkg/trait/prometheus.go +++ b/pkg/trait/prometheus.go @@ -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" @@ -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 { @@ -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) diff --git a/pkg/trait/service.go b/pkg/trait/service.go index 043444ca4a..b8c14b20e7 100644 --- a/pkg/trait/service.go +++ b/pkg/trait/service.go @@ -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" @@ -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 } diff --git a/pkg/trait/types.go b/pkg/trait/types.go index 4f7470d9de..a8484922c1 100644 --- a/pkg/trait/types.go +++ b/pkg/trait/types.go @@ -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 @@ -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