squakez commented on code in PR #6845: URL: https://github.com/apache/camel-k/pull/6845#discussion_r4083637760
########## e2e/arkmq/setup/setup.sh: ########## @@ -0,0 +1,41 @@ +#!/bin/bash + +# --------------------------------------------------------------------------- +# 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. +# --------------------------------------------------------------------------- + +#### +# +# This script takes care of ArkMQ operator setup for e2e tests +# +#### + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +kubectl create namespace arkmq --dry-run=client -o yaml | kubectl apply -f - +kubectl apply --server-side -f https://github.com/arkmq-org/arkmq-org-broker-operator/releases/latest/download/arkmq-org-broker-operator.yaml -n arkmq Review Comment: Better to stick to a release version instead of `latest`. We can possibly add a var to control it. ########## pkg/util/bindings/arkmq.go: ########## @@ -0,0 +1,217 @@ +/* +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 bindings + +import ( + "fmt" + + camelv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" + arkmqv1beta1 "github.com/apache/camel-k/v2/pkg/apis/duck/arkmq/v1beta1" + "github.com/apache/camel-k/v2/pkg/client/arkmq/clientset/internalclientset" + "github.com/apache/camel-k/v2/pkg/util/uri" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +func init() { + RegisterBindingProvider(ArkMQBindingProvider{}) +} + +// camelArkMQ represents the configuration required by Camel JMS component for ArkMQ. +type camelArkMQ struct { + queueName string + properties map[string]string +} + +// ArkMQBindingProvider allows connecting to an ArkMQ queue via Binding. +type ArkMQBindingProvider struct { + Client internalclientset.Interface +} + +func (a ArkMQBindingProvider) ID() string { + return "arkmq" +} + +func (a ArkMQBindingProvider) Translate(ctx BindingContext, _ EndpointContext, endpoint camelv1.Endpoint) (*Binding, error) { + if endpoint.Ref == nil { + // IMPORTANT: just pass through if this provider cannot manage the binding. Another provider in the chain may take care of it. + return nil, nil + } + gv, err := schema.ParseGroupVersion(endpoint.Ref.APIVersion) + if err != nil { + return nil, err + } + if gv.Group != arkmqv1beta1.ArkMQGroup { + // IMPORTANT: just pass through if this provider cannot manage the binding. Another provider in the chain may take care of it. + return nil, nil + } + + camelArkMQ, err := a.toCamelArkMQ(ctx, endpoint) + if err != nil { + return nil, err + } + arkmqURI := "jms:queue:" + camelArkMQ.queueName + arkmqURI = uri.AppendParameters(arkmqURI, camelArkMQ.properties) + + return &Binding{ + URI: arkmqURI, + }, nil +} + +// toCamelArkMQ serializes an endpoint to a camelArkMQ struct. +func (a ArkMQBindingProvider) toCamelArkMQ(ctx BindingContext, endpoint camelv1.Endpoint) (*camelArkMQ, error) { + if endpoint.Ref.Kind != arkmqv1beta1.ArkMQKindAddress { + return nil, fmt.Errorf("invalid endpoint kind %q. Can only work with %s kind", + endpoint.Ref.Kind, arkmqv1beta1.ArkMQKindAddress) + } + + return a.fromAddressToCamel(ctx, endpoint) +} + +// Verify and transform an ActiveMQArtemisAddress resource to Camel JMS queue endpoint parameters. +func (a ArkMQBindingProvider) fromAddressToCamel(ctx BindingContext, endpoint camelv1.Endpoint) (*camelArkMQ, error) { + props, err := endpoint.Properties.GetPropertyMap() + if err != nil { + return nil, err + } + if props == nil { + props = make(map[string]string) + } + + queueName := endpoint.Ref.Name + if props["brokerURL"] == "" { + address, err := a.lookupAddress(ctx, endpoint) + if err != nil { + return nil, err + } + + if address.Spec.RoutingType == "multicast" { + return nil, fmt.Errorf("multicast addresses (topics) are not supported on queue binding %s", endpoint.Ref.Name) + } + + if address.Spec.QueueName != "" { + queueName = address.Spec.QueueName + } else if address.Spec.AddressName != "" { + queueName = address.Spec.AddressName + } + + brokerURL, err := a.lookupBrokerURL(ctx, address, endpoint) + if err != nil { + return nil, err + } + + props["brokerURL"] = brokerURL + } + + return &camelArkMQ{ + queueName: queueName, + properties: props, + }, nil +} + +func (a ArkMQBindingProvider) lookupBrokerURL(ctx BindingContext, address *arkmqv1beta1.ActiveMQArtemisAddress, endpoint camelv1.Endpoint) (string, error) { + clusterName := address.Spec.ApplyTo + if clusterName == "" && address.Labels != nil { + clusterName = address.Labels[arkmqv1beta1.ArkMQBrokerLabel] + } + if clusterName == "" { + return "", fmt.Errorf("no %q label or applyTo defined on address %s", arkmqv1beta1.ArkMQBrokerLabel, endpoint.Ref.Name) + } + + namespace := endpoint.Ref.Namespace + if namespace == "" { + namespace = ctx.Namespace + } + + return a.getBrokerURL(ctx, clusterName, namespace) +} + +func (a ArkMQBindingProvider) getBrokerURL(ctx BindingContext, clusterName, namespace string) (string, error) { + client := a.Client + if client == nil { + arkmqClient, err := internalclientset.NewForConfig(ctx.Client.GetConfig()) + if err != nil { + return "", err + } + client = arkmqClient + } + + broker, err := client.BrokerV1beta1().ActiveMQArtemises(namespace).Get(ctx.Ctx, clusterName, v1.GetOptions{}) + if err != nil { + return "", err + } + + port := int32(61616) + for _, p := range broker.Status.PortStatus { + if p.Name == "core" || p.Name == "all" || p.Name == "openwire" { + if p.Port > 0 { + port = p.Port + break + } + } + } + + if namespace != "" { + return fmt.Sprintf("tcp://%s-hdls-svc.%s.svc:%d", clusterName, namespace, port), nil + } + return fmt.Sprintf("tcp://%s-hdls-svc:%d", clusterName, port), nil Review Comment: We probably want to verify the Service is existing and get the address from there? ########## e2e/arkmq/setup/setup.sh: ########## @@ -0,0 +1,41 @@ +#!/bin/bash + +# --------------------------------------------------------------------------- +# 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. +# --------------------------------------------------------------------------- + +#### +# +# This script takes care of ArkMQ operator setup for e2e tests +# +#### + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +kubectl create namespace arkmq --dry-run=client -o yaml | kubectl apply -f - +kubectl apply --server-side -f https://github.com/arkmq-org/arkmq-org-broker-operator/releases/latest/download/arkmq-org-broker-operator.yaml -n arkmq +kubectl rollout status deployment arkmq-org-broker-operator -n arkmq --timeout=180s + +# Wait for CRDs to be established +kubectl wait --for=condition=established crd/activemqartemises.broker.amq.io --timeout=60s +kubectl wait --for=condition=established crd/activemqartemisaddresses.broker.amq.io --timeout=60s + +#### Setup an ActiveMQ Artemis broker +kubectl apply -f $SCRIPT_DIR/broker.yaml -n arkmq +kubectl wait activemqartemis/my-broker --for=condition=Ready --timeout=300s -n arkmq + +#### Setup an ActiveMQ Artemis queue address +kubectl apply -f $SCRIPT_DIR/queue.yaml -n arkmq Review Comment: Can we add any waiting condition on the queue? ########## pkg/util/bindings/arkmq.go: ########## @@ -0,0 +1,217 @@ +/* +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 bindings + +import ( + "fmt" + + camelv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" + arkmqv1beta1 "github.com/apache/camel-k/v2/pkg/apis/duck/arkmq/v1beta1" + "github.com/apache/camel-k/v2/pkg/client/arkmq/clientset/internalclientset" + "github.com/apache/camel-k/v2/pkg/util/uri" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +func init() { + RegisterBindingProvider(ArkMQBindingProvider{}) +} + +// camelArkMQ represents the configuration required by Camel JMS component for ArkMQ. +type camelArkMQ struct { + queueName string + properties map[string]string +} + +// ArkMQBindingProvider allows connecting to an ArkMQ queue via Binding. +type ArkMQBindingProvider struct { + Client internalclientset.Interface +} + +func (a ArkMQBindingProvider) ID() string { + return "arkmq" +} + +func (a ArkMQBindingProvider) Translate(ctx BindingContext, _ EndpointContext, endpoint camelv1.Endpoint) (*Binding, error) { + if endpoint.Ref == nil { + // IMPORTANT: just pass through if this provider cannot manage the binding. Another provider in the chain may take care of it. + return nil, nil + } + gv, err := schema.ParseGroupVersion(endpoint.Ref.APIVersion) + if err != nil { + return nil, err + } + if gv.Group != arkmqv1beta1.ArkMQGroup { + // IMPORTANT: just pass through if this provider cannot manage the binding. Another provider in the chain may take care of it. + return nil, nil + } + + camelArkMQ, err := a.toCamelArkMQ(ctx, endpoint) + if err != nil { + return nil, err + } + arkmqURI := "jms:queue:" + camelArkMQ.queueName + arkmqURI = uri.AppendParameters(arkmqURI, camelArkMQ.properties) + + return &Binding{ + URI: arkmqURI, + }, nil +} + +// toCamelArkMQ serializes an endpoint to a camelArkMQ struct. +func (a ArkMQBindingProvider) toCamelArkMQ(ctx BindingContext, endpoint camelv1.Endpoint) (*camelArkMQ, error) { + if endpoint.Ref.Kind != arkmqv1beta1.ArkMQKindAddress { + return nil, fmt.Errorf("invalid endpoint kind %q. Can only work with %s kind", + endpoint.Ref.Kind, arkmqv1beta1.ArkMQKindAddress) + } + + return a.fromAddressToCamel(ctx, endpoint) +} + +// Verify and transform an ActiveMQArtemisAddress resource to Camel JMS queue endpoint parameters. +func (a ArkMQBindingProvider) fromAddressToCamel(ctx BindingContext, endpoint camelv1.Endpoint) (*camelArkMQ, error) { + props, err := endpoint.Properties.GetPropertyMap() + if err != nil { + return nil, err + } + if props == nil { + props = make(map[string]string) + } + + queueName := endpoint.Ref.Name + if props["brokerURL"] == "" { + address, err := a.lookupAddress(ctx, endpoint) + if err != nil { + return nil, err + } + + if address.Spec.RoutingType == "multicast" { + return nil, fmt.Errorf("multicast addresses (topics) are not supported on queue binding %s", endpoint.Ref.Name) + } + + if address.Spec.QueueName != "" { + queueName = address.Spec.QueueName + } else if address.Spec.AddressName != "" { + queueName = address.Spec.AddressName + } + + brokerURL, err := a.lookupBrokerURL(ctx, address, endpoint) + if err != nil { + return nil, err + } + + props["brokerURL"] = brokerURL + } + + return &camelArkMQ{ + queueName: queueName, + properties: props, + }, nil +} + +func (a ArkMQBindingProvider) lookupBrokerURL(ctx BindingContext, address *arkmqv1beta1.ActiveMQArtemisAddress, endpoint camelv1.Endpoint) (string, error) { + clusterName := address.Spec.ApplyTo + if clusterName == "" && address.Labels != nil { + clusterName = address.Labels[arkmqv1beta1.ArkMQBrokerLabel] + } + if clusterName == "" { + return "", fmt.Errorf("no %q label or applyTo defined on address %s", arkmqv1beta1.ArkMQBrokerLabel, endpoint.Ref.Name) Review Comment: I think that, by default, the resource is reconciled by the existing broker in the same namespace. In that case we can fallback to it, looking for it. ########## pkg/util/bindings/arkmq.go: ########## @@ -0,0 +1,217 @@ +/* +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 bindings + +import ( + "fmt" + + camelv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" + arkmqv1beta1 "github.com/apache/camel-k/v2/pkg/apis/duck/arkmq/v1beta1" + "github.com/apache/camel-k/v2/pkg/client/arkmq/clientset/internalclientset" + "github.com/apache/camel-k/v2/pkg/util/uri" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +func init() { + RegisterBindingProvider(ArkMQBindingProvider{}) +} + +// camelArkMQ represents the configuration required by Camel JMS component for ArkMQ. +type camelArkMQ struct { + queueName string + properties map[string]string +} + +// ArkMQBindingProvider allows connecting to an ArkMQ queue via Binding. +type ArkMQBindingProvider struct { + Client internalclientset.Interface +} + +func (a ArkMQBindingProvider) ID() string { + return "arkmq" +} + +func (a ArkMQBindingProvider) Translate(ctx BindingContext, _ EndpointContext, endpoint camelv1.Endpoint) (*Binding, error) { + if endpoint.Ref == nil { + // IMPORTANT: just pass through if this provider cannot manage the binding. Another provider in the chain may take care of it. + return nil, nil + } + gv, err := schema.ParseGroupVersion(endpoint.Ref.APIVersion) + if err != nil { + return nil, err + } + if gv.Group != arkmqv1beta1.ArkMQGroup { + // IMPORTANT: just pass through if this provider cannot manage the binding. Another provider in the chain may take care of it. + return nil, nil + } + + camelArkMQ, err := a.toCamelArkMQ(ctx, endpoint) + if err != nil { + return nil, err + } + arkmqURI := "jms:queue:" + camelArkMQ.queueName + arkmqURI = uri.AppendParameters(arkmqURI, camelArkMQ.properties) + + return &Binding{ + URI: arkmqURI, + }, nil +} + +// toCamelArkMQ serializes an endpoint to a camelArkMQ struct. +func (a ArkMQBindingProvider) toCamelArkMQ(ctx BindingContext, endpoint camelv1.Endpoint) (*camelArkMQ, error) { + if endpoint.Ref.Kind != arkmqv1beta1.ArkMQKindAddress { + return nil, fmt.Errorf("invalid endpoint kind %q. Can only work with %s kind", + endpoint.Ref.Kind, arkmqv1beta1.ArkMQKindAddress) + } + + return a.fromAddressToCamel(ctx, endpoint) +} + +// Verify and transform an ActiveMQArtemisAddress resource to Camel JMS queue endpoint parameters. +func (a ArkMQBindingProvider) fromAddressToCamel(ctx BindingContext, endpoint camelv1.Endpoint) (*camelArkMQ, error) { + props, err := endpoint.Properties.GetPropertyMap() + if err != nil { + return nil, err + } + if props == nil { + props = make(map[string]string) + } + + queueName := endpoint.Ref.Name + if props["brokerURL"] == "" { + address, err := a.lookupAddress(ctx, endpoint) + if err != nil { + return nil, err + } + + if address.Spec.RoutingType == "multicast" { + return nil, fmt.Errorf("multicast addresses (topics) are not supported on queue binding %s", endpoint.Ref.Name) + } + + if address.Spec.QueueName != "" { + queueName = address.Spec.QueueName + } else if address.Spec.AddressName != "" { + queueName = address.Spec.AddressName + } + + brokerURL, err := a.lookupBrokerURL(ctx, address, endpoint) + if err != nil { + return nil, err + } + + props["brokerURL"] = brokerURL + } + + return &camelArkMQ{ + queueName: queueName, + properties: props, + }, nil +} + +func (a ArkMQBindingProvider) lookupBrokerURL(ctx BindingContext, address *arkmqv1beta1.ActiveMQArtemisAddress, endpoint camelv1.Endpoint) (string, error) { + clusterName := address.Spec.ApplyTo + if clusterName == "" && address.Labels != nil { + clusterName = address.Labels[arkmqv1beta1.ArkMQBrokerLabel] + } + if clusterName == "" { + return "", fmt.Errorf("no %q label or applyTo defined on address %s", arkmqv1beta1.ArkMQBrokerLabel, endpoint.Ref.Name) + } + + namespace := endpoint.Ref.Namespace + if namespace == "" { + namespace = ctx.Namespace + } + + return a.getBrokerURL(ctx, clusterName, namespace) +} + +func (a ArkMQBindingProvider) getBrokerURL(ctx BindingContext, clusterName, namespace string) (string, error) { + client := a.Client + if client == nil { + arkmqClient, err := internalclientset.NewForConfig(ctx.Client.GetConfig()) + if err != nil { + return "", err + } + client = arkmqClient + } + + broker, err := client.BrokerV1beta1().ActiveMQArtemises(namespace).Get(ctx.Ctx, clusterName, v1.GetOptions{}) + if err != nil { + return "", err + } + + port := int32(61616) + for _, p := range broker.Status.PortStatus { + if p.Name == "core" || p.Name == "all" || p.Name == "openwire" { + if p.Port > 0 { + port = p.Port + break + } + } + } + + if namespace != "" { + return fmt.Sprintf("tcp://%s-hdls-svc.%s.svc:%d", clusterName, namespace, port), nil + } + return fmt.Sprintf("tcp://%s-hdls-svc:%d", clusterName, port), nil +} + +func (a ArkMQBindingProvider) lookupAddress(ctx BindingContext, endpoint camelv1.Endpoint) (*arkmqv1beta1.ActiveMQArtemisAddress, error) { + client := a.Client + if client == nil { + arkmqClient, err := internalclientset.NewForConfig(ctx.Client.GetConfig()) + if err != nil { + return nil, err + } + client = arkmqClient + } + + namespace := endpoint.Ref.Namespace + if namespace == "" { + namespace = ctx.Namespace + } + + // first check by ActiveMQArtemisAddress name + address, err := client.BrokerV1beta1().ActiveMQArtemisAddresses(namespace).Get(ctx.Ctx, endpoint.Ref.Name, v1.GetOptions{}) + if err != nil && !k8serrors.IsNotFound(err) { + return nil, err + } + if err == nil { + return address, nil + } + + // if not found, then look at spec.queueName or spec.addressName + addresses, err := client.BrokerV1beta1().ActiveMQArtemisAddresses(namespace).List(ctx.Ctx, v1.ListOptions{}) Review Comment: I don't think we need this one. The user provides clearly the resource name to use, so, if missing, we can just report the error. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
