From 202baa1cfc0e517dc0a755800e0e3693efb98b71 Mon Sep 17 00:00:00 2001 From: James Oulman Date: Tue, 29 Oct 2024 17:03:22 -0400 Subject: [PATCH] Validate that Connect Native services define a port (#24329) --- .changelog/24329.txt | 3 +++ nomad/structs/services.go | 7 ++++++- nomad/structs/services_test.go | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 .changelog/24329.txt diff --git a/.changelog/24329.txt b/.changelog/24329.txt new file mode 100644 index 000000000..1e94cdc29 --- /dev/null +++ b/.changelog/24329.txt @@ -0,0 +1,3 @@ +```release-note:bug +connect: add validation to ensure that connect native services specify a port +``` diff --git a/nomad/structs/services.go b/nomad/structs/services.go index 8438eebdb..2b02a4fb5 100644 --- a/nomad/structs/services.go +++ b/nomad/structs/services.go @@ -846,7 +846,7 @@ func (s *Service) validateCheckPort(c *ServiceCheck) error { func (s *Service) validateConsulService(mErr *multierror.Error) { // check checks for _, c := range s.Checks { - // validat ethe check port + // validate the check port if err := s.validateCheckPort(c); err != nil { mErr.Errors = append(mErr.Errors, err) continue @@ -877,6 +877,11 @@ func (s *Service) validateConsulService(mErr *multierror.Error) { if s.Connect.IsNative() && len(s.TaskName) == 0 { mErr.Errors = append(mErr.Errors, fmt.Errorf("Service %s is Connect Native and requires setting the task", s.Name)) } + + // if service is connect native a port must be set on the service or consul will reject it + if s.Connect.IsNative() && s.PortLabel == "" { + mErr.Errors = append(mErr.Errors, fmt.Errorf("Service %s is Connect Native and requires setting the port", s.Name)) + } } } diff --git a/nomad/structs/services_test.go b/nomad/structs/services_test.go index 28691dc75..add2cb1aa 100644 --- a/nomad/structs/services_test.go +++ b/nomad/structs/services_test.go @@ -1889,7 +1889,8 @@ func TestService_Validate(t *testing.T) { { name: "Native Connect without task name", input: &Service{ - Name: "testservice", + Name: "testservice", + PortLabel: "8080", Connect: &ConsulConnect{ Native: true, }, @@ -1898,6 +1899,18 @@ func TestService_Validate(t *testing.T) { }, { name: "Native Connect with task name", + input: &Service{ + Name: "testservice", + PortLabel: "8080", + TaskName: "testtask", + Connect: &ConsulConnect{ + Native: true, + }, + }, + expErr: false, + }, + { + name: "Native Connect without port", input: &Service{ Name: "testservice", TaskName: "testtask", @@ -1905,6 +1918,19 @@ func TestService_Validate(t *testing.T) { Native: true, }, }, + expErr: true, + expErrStr: "Service testservice is Connect Native and requires setting the port", + }, + { + name: "Native Connect with port", + input: &Service{ + Name: "testservice", + TaskName: "testtask", + PortLabel: "8080", + Connect: &ConsulConnect{ + Native: true, + }, + }, expErr: false, }, { @@ -1996,8 +2022,8 @@ func TestService_Validate(t *testing.T) { { name: "provider consul with notes too long", input: &Service{ - Name: "testservice", - Provider: "consul", + Name: "testservice", + Provider: "consul", PortLabel: "port", Checks: []*ServiceCheck{ { @@ -2006,7 +2032,7 @@ func TestService_Validate(t *testing.T) { Path: "/", Interval: 1 * time.Second, Timeout: 3 * time.Second, - Notes: strings.Repeat("A", 256), + Notes: strings.Repeat("A", 256), }, }, },