Validate that Connect Native services define a port (#24329)

This commit is contained in:
James Oulman
2024-10-29 17:03:22 -04:00
committed by GitHub
parent 3ca728819b
commit 202baa1cfc
3 changed files with 39 additions and 5 deletions

3
.changelog/24329.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
connect: add validation to ensure that connect native services specify a port
```

View File

@@ -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))
}
}
}

View File

@@ -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),
},
},
},