astefanutti commented on a change in pull request #825: Misleading
"service.enabled=false" when used in combination with the prometheus trait i
URL: https://github.com/apache/camel-k/pull/825#discussion_r304386059
##########
File path: pkg/trait/service.go
##########
@@ -96,79 +84,31 @@ func (t *serviceTrait) Configure(e *Environment) (bool,
error) {
}
func (t *serviceTrait) Apply(e *Environment) (err error) {
- // Either update the existing service added by previously executed
traits
- // (e.g. the prometheus trait) or add a new service resource
- svc := e.Resources.GetService(func(svc *corev1.Service) bool {
- return svc.Name == e.Integration.Name
- })
+ svc := e.Resources.GetServiceForIntegration(e.Integration)
if svc == nil {
- svc = getServiceFor(e)
- e.Resources.Add(svc)
- }
- port := corev1.ServicePort{
- Name: t.PortName,
- Port: int32(t.Port),
- Protocol: corev1.ProtocolTCP,
- TargetPort: intstr.FromString(t.ContainerPortName),
- }
- svc.Spec.Ports = append(svc.Spec.Ports, port)
-
- // Mark the service as a user service
- svc.Labels["camel.apache.org/service.type"] = v1alpha1.ServiceTypeUser
-
- // Register a post processor to add a container port to the integration
deployment
- e.PostProcessors = append(e.PostProcessors, func(environment
*Environment) error {
- container := environment.Resources.GetContainer(func(c
*corev1.Container) bool {
- return c.Name == environment.Integration.Name
- })
-
- if container != nil {
- container.Ports = append(container.Ports,
corev1.ContainerPort{
- Name: t.ContainerPortName,
- ContainerPort: int32(t.ContainerPort),
- Protocol: corev1.ProtocolTCP,
- })
-
- message := fmt.Sprintf("%s(%s/%d) -> %s(%s/%d)",
- svc.Name, port.Name, port.Port,
- container.Name, t.ContainerPortName,
t.ContainerPort,
- )
-
- environment.Integration.Status.SetCondition(
- v1alpha1.IntegrationConditionServiceAvailable,
- corev1.ConditionTrue,
-
v1alpha1.IntegrationConditionServiceAvailableReason,
- message,
- )
- } else {
- return fmt.Errorf("cannot add %s container port: no
integration container", t.ContainerPortName)
- }
- return nil
- })
-
- return nil
-}
-
-func getServiceFor(e *Environment) *corev1.Service {
- svc := corev1.Service{
- TypeMeta: metav1.TypeMeta{
- Kind: "Service",
- APIVersion: "v1",
- },
- ObjectMeta: metav1.ObjectMeta{
- Name: e.Integration.Name,
- Namespace: e.Integration.Namespace,
- Labels: map[string]string{
- "camel.apache.org/integration":
e.Integration.Name,
+ svc := corev1.Service{
+ TypeMeta: metav1.TypeMeta{
+ Kind: "Service",
+ APIVersion: "v1",
},
- },
- Spec: corev1.ServiceSpec{
- Ports: []corev1.ServicePort{},
- Selector: map[string]string{
- "camel.apache.org/integration":
e.Integration.Name,
+ ObjectMeta: metav1.ObjectMeta{
+ Name: e.Integration.Name,
+ Namespace: e.Integration.Namespace,
+ Labels: map[string]string{
+ "camel.apache.org/integration":
e.Integration.Name,
+ },
},
- },
+ Spec: corev1.ServiceSpec{
+ Ports: []corev1.ServicePort{},
+ Selector: map[string]string{
+ "camel.apache.org/integration":
e.Integration.Name,
+ },
+ },
+ }
+
+ // add a new service if not already created
+ e.Resources.Add(&svc)
Review comment:
I would return the service and add it to the resources to respect the method
signature / convention.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services