Thundercloud12 commented on code in PR #6845: URL: https://github.com/apache/camel-k/pull/6845#discussion_r4091514143
########## pkg/util/bindings/arkmq.go: ########## @@ -0,0 +1,295 @@ +/* +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/util/kubernetes" + "github.com/apache/camel-k/v2/pkg/util/uri" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/runtime/schema" + ctrl "sigs.k8s.io/controller-runtime/pkg/client" +) + +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{} + +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) { + switch endpoint.Ref.Kind { + case arkmqv1beta1.ArkMQKindBroker: + return a.fromBrokerToCamel(ctx, endpoint) + case arkmqv1beta1.ArkMQKindAddress: + return a.fromAddressToCamel(ctx, endpoint) + } + + return nil, fmt.Errorf("invalid endpoint kind %q. Can only work with %s or %s kind", + endpoint.Ref.Kind, arkmqv1beta1.ArkMQKindBroker, arkmqv1beta1.ArkMQKindAddress) +} + +// Verify and transform an ActiveMQArtemis broker resource to Camel JMS queue endpoint parameters. +func (a ArkMQBindingProvider) fromBrokerToCamel(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 := props["destination"] + if queueName == "" { + queueName = props["queue"] + } + if queueName == "" { + return nil, fmt.Errorf("invalid endpoint configuration: missing destination or queue property on %s %s", + endpoint.Ref.Kind, endpoint.Ref.Name) + } + delete(props, "destination") + delete(props, "queue") + + if props["brokerURL"] == "" { + namespace := endpoint.Ref.Namespace + if namespace == "" { + namespace = ctx.Namespace + } + + brokerURL, err := a.getBrokerURL(ctx, endpoint.Ref.Name, namespace) + if err != nil { + return nil, err + } + + props["brokerURL"] = brokerURL + } + + return &camelArkMQ{ + queueName: queueName, + properties: props, + }, nil +} + +// 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) Review Comment: @squakez according to jms mapping multicast is treated as a topic and initially i thought it to tightly focus on queues -- 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]
