From fc05f7fb4497dd85bae141e3d7d974a16c73537f Mon Sep 17 00:00:00 2001 From: Andre Mauricio Zelak Date: Fri, 31 Jan 2025 16:28:48 -0300 Subject: [PATCH] Fix RabbitMQ endpoint Only the IPv6 addresses must be enclosed in square brackets, the IPv4 adresses and hostname can't be enclosed. Test plan: Pass: Verify ptp-notification in IPv4 and IPv6 system. Pass: Verify ptp-notification operations (pull status, subscribe, list, delete) Pass: Verify both ptp-notification V1 and V2 operations Closes-bug: 2097136 Change-Id: I7792b2648bd609ca49fbec840eaf7e687ae25c7d Signed-off-by: Andre Mauricio Zelak --- .../scripts/init/ptptracking_start.py | 26 +++++++++++++++---- .../scripts/init/ptptracking_start_v2.py | 24 ++++++++++++++--- .../trackingfunctionsdk/services/daemon.py | 8 +++--- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/helm-charts/custom/ptp-notification-helm/ptp-notification-helm/ptp-notification/resources/scripts/init/ptptracking_start.py b/helm-charts/custom/ptp-notification-helm/ptp-notification-helm/ptp-notification/resources/scripts/init/ptptracking_start.py index 7bba342..a301f6e 100644 --- a/helm-charts/custom/ptp-notification-helm/ptp-notification-helm/ptp-notification/resources/scripts/init/ptptracking_start.py +++ b/helm-charts/custom/ptp-notification-helm/ptp-notification-helm/ptp-notification/resources/scripts/init/ptptracking_start.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2023 Wind River Systems, Inc. +# Copyright (c) 2023-2025 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -18,10 +18,28 @@ import json import time import glob import re +import ipaddress import oslo_messaging from oslo_config import cfg from trackingfunctionsdk.services.daemon import DaemonControl +def build_rabbitmq_endpoint(user, password, host, port): + """Build RabitMQ endpoint URL""" + + """Only IPv6 addresses are enclosed in square brackets.""" + # 'rabbit://admin:admin@127.0.0.1:5672/' + # 'rabbit://admin:admin@registration.notification.svc.cluster.local:5672/' + # 'rabbit://admin:admin@[::1]:5672/' + endpoint_format = 'rabbit://{0}:{1}@{2}:{3}' + try: + ip = ipaddress.ip_address(host) + if ip.version == 6: + endpoint_format = 'rabbit://{0}:{1}@[{2}]:{3}' + except ValueError: + pass + + return endpoint_format.format(user, password, host, port) + THIS_NAMESPACE = os.environ.get("THIS_NAMESPACE", 'notification') THIS_NODE_NAME = os.environ.get("THIS_NODE_NAME", 'controller-1') THIS_POD_IP = os.environ.get("THIS_POD_IP", '127.0.0.1') @@ -30,14 +48,12 @@ REGISTRATION_PASS = os.environ.get("REGISTRATION_PASS", "admin") REGISTRATION_PORT = os.environ.get("REGISTRATION_PORT", "5672") REGISTRATION_HOST = os.environ.get("REGISTRATION_HOST", 'registration.notification.svc.cluster.local') -# 'rabbit://admin:admin@[127.0.0.1]:5672/' -# 'rabbit://admin:admin@[::1]:5672/' -REGISTRATION_TRANSPORT_ENDPOINT = 'rabbit://{0}:{1}@[{2}]:{3}'.format( +REGISTRATION_TRANSPORT_ENDPOINT = build_rabbitmq_endpoint( REGISTRATION_USER, REGISTRATION_PASS, REGISTRATION_HOST, REGISTRATION_PORT) NOTIFICATION_BROKER_USER = os.environ.get("NOTIFICATIONSERVICE_USER", "admin") NOTIFICATION_BROKER_PASS = os.environ.get("NOTIFICATIONSERVICE_PASS", "admin") NOTIFICATION_BROKER_PORT = os.environ.get("NOTIFICATIONSERVICE_PORT", "5672") -NOTIFICATION_TRANSPORT_ENDPOINT = 'rabbit://{0}:{1}@[{2}]:{3}'.format( +NOTIFICATION_TRANSPORT_ENDPOINT = build_rabbitmq_endpoint( NOTIFICATION_BROKER_USER, NOTIFICATION_BROKER_PASS, THIS_POD_IP, NOTIFICATION_BROKER_PORT) PTP_DEVICE_SIMULATED = os.environ.get("PTP_DEVICE_SIMULATED", False) PTP_HOLDOVER_SECONDS = os.environ.get("PTP_HOLDOVER_SECONDS", 30) diff --git a/helm-charts/custom/ptp-notification-helm/ptp-notification-helm/ptp-notification/resources/scripts/init/ptptracking_start_v2.py b/helm-charts/custom/ptp-notification-helm/ptp-notification-helm/ptp-notification/resources/scripts/init/ptptracking_start_v2.py index 33a0790..1e41ad8 100644 --- a/helm-charts/custom/ptp-notification-helm/ptp-notification-helm/ptp-notification/resources/scripts/init/ptptracking_start_v2.py +++ b/helm-charts/custom/ptp-notification-helm/ptp-notification-helm/ptp-notification/resources/scripts/init/ptptracking_start_v2.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2021-2023 Wind River Systems, Inc. +# Copyright (c) 2021-2025 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -10,6 +10,7 @@ import json import logging import os import re +import ipaddress from pathlib import Path @@ -20,6 +21,23 @@ from trackingfunctionsdk.services.daemon import DaemonControl LOG = logging.getLogger(__name__) log_helper.config_logger(LOG) +def build_rabbitmq_endpoint(user, password, host, port): + """Build RabitMQ endpoint URL""" + + """Only IPv6 addresses are enclosed in square brackets.""" + # 'rabbit://admin:admin@127.0.0.1:5672/' + # 'rabbit://admin:admin@registration.notification.svc.cluster.local:5672/' + # 'rabbit://admin:admin@[::1]:5672/' + endpoint_format = 'rabbit://{0}:{1}@{2}:{3}' + try: + ip = ipaddress.ip_address(host) + if ip.version == 6: + endpoint_format = 'rabbit://{0}:{1}@[{2}]:{3}' + except ValueError: + pass + + return endpoint_format.format(user, password, host, port) + THIS_NAMESPACE = os.environ.get("THIS_NAMESPACE", 'notification') THIS_NODE_NAME = os.environ.get("THIS_NODE_NAME", 'controller-0') THIS_POD_IP = os.environ.get("THIS_POD_IP", '127.0.0.1') @@ -33,14 +51,14 @@ REGISTRATION_HOST = os.environ.get("REGISTRATION_HOST", 'localhost') # 'rabbit://admin:admin@[127.0.0.1]:5672/' # 'rabbit://admin:admin@[::1]:5672/' -REGISTRATION_TRANSPORT_ENDPOINT = 'rabbit://{0}:{1}@[{2}]:{3}'.format( +REGISTRATION_TRANSPORT_ENDPOINT = build_rabbitmq_endpoint( REGISTRATION_USER, REGISTRATION_PASS, REGISTRATION_HOST, REGISTRATION_PORT) NOTIFICATION_BROKER_USER = os.environ.get("NOTIFICATIONSERVICE_USER", "guest") NOTIFICATION_BROKER_PASS = os.environ.get("NOTIFICATIONSERVICE_PASS", "guest") NOTIFICATION_BROKER_PORT = os.environ.get("NOTIFICATIONSERVICE_PORT", "5672") -NOTIFICATION_TRANSPORT_ENDPOINT = 'rabbit://{0}:{1}@[{2}]:{3}'.format( +NOTIFICATION_TRANSPORT_ENDPOINT = build_rabbitmq_endpoint( NOTIFICATION_BROKER_USER, NOTIFICATION_BROKER_PASS, THIS_POD_IP, NOTIFICATION_BROKER_PORT) diff --git a/notificationservice-base/docker/ptptrackingfunction/trackingfunctionsdk/services/daemon.py b/notificationservice-base/docker/ptptrackingfunction/trackingfunctionsdk/services/daemon.py index ff59cbf..145dad3 100644 --- a/notificationservice-base/docker/ptptrackingfunction/trackingfunctionsdk/services/daemon.py +++ b/notificationservice-base/docker/ptptrackingfunction/trackingfunctionsdk/services/daemon.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2021-2024 Wind River Systems, Inc. +# Copyright (c) 2021-2025 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -89,9 +89,11 @@ class PtpWatcherDefault: self.node_name = self.daemon_context['THIS_NODE_NAME'] self.namespace = self.daemon_context.get('THIS_NAMESPACE', 'notification') - broker_transport_endpoint = self.daemon_context['NOTIFICATION_TRANSPORT_ENDPOINT'] + broker_transport_endpoint = \ + self.daemon_context['NOTIFICATION_TRANSPORT_ENDPOINT'] - registration_transport_endpoint = self.daemon_context['REGISTRATION_TRANSPORT_ENDPOINT'] + registration_transport_endpoint = \ + self.daemon_context['REGISTRATION_TRANSPORT_ENDPOINT'] self.broker_endpoint = RpcEndpointInfo(broker_transport_endpoint) self.registration_broker_endpoint = RpcEndpointInfo(registration_transport_endpoint)