Skip to content

Commit

Permalink
backport of commit 202baa1 (#24331)
Browse files Browse the repository at this point in the history
Co-authored-by: James Oulman <[email protected]>
  • Loading branch information
hc-github-team-nomad-core and oulman authored Oct 29, 2024
1 parent b9d1c88 commit f8e5ece
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/24329.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
connect: add validation to ensure that connect native services specify a port
```
7 changes: 6 additions & 1 deletion nomad/structs/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
}

Expand Down
34 changes: 30 additions & 4 deletions nomad/structs/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -1898,13 +1899,38 @@ 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",
Connect: &ConsulConnect{
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,
},
{
Expand Down Expand Up @@ -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{
{
Expand All @@ -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),
},
},
},
Expand Down

0 comments on commit f8e5ece

Please sign in to comment.