From 2215a02c6d11457c5e3f20a45dba1237bced54c4 Mon Sep 17 00:00:00 2001 From: markcampv Date: Thu, 3 Nov 2022 12:35:40 -0400 Subject: [PATCH] iterate through existing services to check for duplicate sidecar ports --- command/services/register/register.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/command/services/register/register.go b/command/services/register/register.go index 33e5e2ab53..955987d130 100644 --- a/command/services/register/register.go +++ b/command/services/register/register.go @@ -120,9 +120,24 @@ func (c *cmd) Run(args []string) int { c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) return 1 } + //Create existingsvcs to test for existing services + existingsvcs, err := client.Agent().Services() + if err != nil { + c.UI.Error(fmt.Sprintf("Error checking for existing services %s", err)) + } // Create all the services for _, svc := range svcs { + //iterate through existing services to check for duplicate sidecar ports + for _, existingsvc := range existingsvcs { + if svc.Connect.SidecarService.Port > 0 && existingsvc.Port > 0 { + if svc.Connect.SidecarService.Port == existingsvc.Port { + c.UI.Output(fmt.Sprintf("Error registering service %q. Service %q is using the same sidecar port %d. Please make sidecar ports unique to avoid address binding issues in Envoy", + svc.Name, existingsvc.Service, svc.Connect.SidecarService.Port)) + return 1 + } + } + } if err := client.Agent().ServiceRegister(svc); err != nil { c.UI.Error(fmt.Sprintf("Error registering service %q: %s", svc.Name, err))