-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: port name validation RFC-6335 (#2474)
## Description <!-- Describe this change, how it works, and the motivation behind it. --> Kubernetes adheres to [RFC-6335](https://www.rfc-archive.org/getrfc.php?rfc=6335#gsc.tab=0), which specifies that port names must be between 1 and 15 characters. However, this constraint is currently not enforced by the Kurtosis engine, leading to deployment failures in Kubernetes environments when port names exceed 15 characters. Here is an example that the Kurtosis validation step failed to catch: ![image (1)](https://github.com/kurtosis-tech/kurtosis/assets/28714795/cb2f8ee4-038f-4368-9901-e5432fc58e45) This PR resolves the issue by adding an additional validation step for port names. It also refactors the regex definitions in `service.go` and improves the test design for validating service and port names in `service_test.go`. ## Is this change user facing? YES <!-- If yes, please add the "user facing" label to the PR --> <!-- If yes, don't forget to include docs changes where relevant --> ## References (if applicable) <!-- Add relevant Github Issues, Discord threads, or other helpful information. --> <!-- You can auto-close issues by putting "Fixes #XXXX" here. --> - https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go#L326-L351 - 0xPolygon/kurtosis-cdk#146 (comment)
- Loading branch information
Showing
3 changed files
with
100 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 54 additions & 23 deletions
77
container-engine-lib/lib/backend_interface/objects/service/service_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,65 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidServiceName(t *testing.T) { | ||
require.True(t, IsServiceNameValid(ServiceName("a"))) | ||
require.True(t, IsServiceNameValid(ServiceName("abc"))) | ||
require.True(t, IsServiceNameValid(ServiceName("abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef123"))) | ||
} | ||
func TestServiceNameValidation(t *testing.T) { | ||
validServiceNames := []string{ | ||
"a", | ||
"abc", | ||
"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef123", // 63 characters | ||
"a-b", | ||
} | ||
|
||
func TestInvalidServiceName(t *testing.T) { | ||
require.False(t, IsServiceNameValid(ServiceName("1-geth-lighthouse"))) | ||
} | ||
invalidServiceNames := []string{ | ||
"1-geth-lighthouse", // 17 characters | ||
"-bc", | ||
"a--", | ||
"a_b", | ||
"a%b", | ||
"a:b", | ||
"a/b", | ||
"", | ||
"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef1234", // 64 characters | ||
} | ||
|
||
func TestServiceNameWithSpecialChars(t *testing.T) { | ||
require.True(t, IsServiceNameValid(ServiceName("a-b"))) | ||
require.False(t, IsServiceNameValid(ServiceName("-bc"))) | ||
require.False(t, IsServiceNameValid(ServiceName("a--"))) | ||
require.False(t, IsServiceNameValid(ServiceName("a_b"))) | ||
require.False(t, IsServiceNameValid(ServiceName("a%b"))) | ||
require.False(t, IsServiceNameValid(ServiceName("a:b"))) | ||
require.False(t, IsServiceNameValid(ServiceName("a/b"))) | ||
for _, name := range validServiceNames { | ||
require.True(t, IsServiceNameValid(ServiceName(name)), "expected valid service name: %s", name) | ||
} | ||
|
||
for _, name := range invalidServiceNames { | ||
require.False(t, IsServiceNameValid(ServiceName(name)), "expected invalid service name: %s", name) | ||
} | ||
} | ||
|
||
func TestServiceNameLength(t *testing.T) { | ||
require.False(t, IsServiceNameValid(ServiceName(""))) | ||
require.True(t, IsServiceNameValid(ServiceName("a"))) | ||
require.True(t, IsServiceNameValid(ServiceName("abc"))) | ||
require.True(t, IsServiceNameValid(ServiceName("abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef123"))) | ||
require.False(t, IsServiceNameValid(ServiceName("abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef1234"))) | ||
func TestPortNameValidation(t *testing.T) { | ||
validPortNames := []string{ | ||
"a", | ||
"abc", | ||
"abcdefabcdef123", // 15 characters | ||
"a-b", | ||
} | ||
|
||
invalidPortNames := []string{ | ||
"1-dummy-port", // 12 characters | ||
"-bc", | ||
"a--", | ||
"a_b", | ||
"a%b", | ||
"a:b", | ||
"a/b", | ||
"", | ||
"abcdefabcdef1234", // 16 characters | ||
} | ||
|
||
for _, name := range validPortNames { | ||
require.True(t, IsPortNameValid(name), "expected valid port name: %s", name) | ||
} | ||
|
||
for _, name := range invalidPortNames { | ||
require.False(t, IsPortNameValid(name), "expected invalid port name: %s", name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters