Validate port on mesh service registration (#12881)

Add validation to ensure connect native services have a port or socketpath specified on catalog registration.
This was the only missing piece to ensure all mesh services are validated for a port (or socketpath) specification on catalog registration.
This commit is contained in:
Riddhi Shah 2022-05-05 09:13:30 -07:00 committed by GitHub
parent 18193f2916
commit a9c96b6975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

4
.changelog/12881.txt Normal file
View File

@ -0,0 +1,4 @@
```release-note:enhancement
connect: add validation to ensure connect native services have a port or socketpath specified on catalog registration.
This was the only missing piece to ensure all mesh services are validated for a port (or socketpath) specification on catalog registration.
```

View File

@ -1335,8 +1335,7 @@ func (s *NodeService) Validate() error {
}
if s.Port == 0 && s.SocketPath == "" {
result = multierror.Append(result, fmt.Errorf(
"Port or SocketPath must be set for a Connect proxy"))
result = multierror.Append(result, fmt.Errorf("Port or SocketPath must be set for a %s", s.Kind))
}
if s.Connect.Native {
@ -1484,6 +1483,11 @@ func (s *NodeService) Validate() error {
}
}
if s.Connect.Native && s.Port == 0 && s.SocketPath == "" {
result = multierror.Append(result, fmt.Errorf(
"Port or SocketPath must be set for a Connect native service."))
}
return result
}

View File

@ -678,6 +678,10 @@ func TestStructs_NodeService_ValidateTerminatingGateway(t *testing.T) {
func(x *NodeService) { x.Proxy.Upstreams = []Upstream{{}} },
"Proxy.Upstreams configuration is invalid",
},
"port": {
func(x *NodeService) { x.Port = 0 },
"Port must be non-zero",
},
}
for name, tc := range cases {
@ -845,7 +849,7 @@ func TestStructs_NodeService_ValidateConnectProxy(t *testing.T) {
{
"connect-proxy: no port set",
func(x *NodeService) { x.Port = 0 },
"port or socketpath must",
fmt.Sprintf("Port or SocketPath must be set for a %s", ServiceKindConnectProxy),
},
{
@ -1285,6 +1289,15 @@ func TestStructs_NodeService_ValidateSidecarService(t *testing.T) {
}
}
func TestStructs_NodeService_ConnectNativeEmptyPortError(t *testing.T) {
ns := TestNodeService(t)
ns.Connect.Native = true
ns.Port = 0
err := ns.Validate()
assert.Error(t, err)
assert.Contains(t, err.Error(), "Port or SocketPath must be set for a Connect native service.")
}
func TestStructs_NodeService_IsSame(t *testing.T) {
ns := &NodeService{
ID: "node1",