-
Notifications
You must be signed in to change notification settings - Fork 38
feat: restrict the membercluster name length to 63 #946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
d87f701
Restrict membercluster name to a maximum of 63 characters
jamyct a159847
remove validation from v1alpha1 API
jamyct 6fd1b17
add validation to APIs
jamyct a974e49
fix validation
jamyct caa0566
fix validation
jamyct e15893d
remove new type
jamyct edef393
switch restrictions to metav1.ObjectMeta identities
jamyct d318431
latest version
jamyct fa71b42
Use self.metadata.name instead of self.name
jamyct df5611f
update CEL
jamyct 1ed2227
fix CEL line
jamyct 5755525
create e2e test file for name validation
jamyct 43b6b29
fix issues
jamyct 21472e3
add regex e2e tests
jamyct b46b64f
remove testing example
jamyct 1a8e07f
Merge branch 'main' into restrict-memberclustername-length
jamyct f36f2a4
fix k8serrors capital
jamyct f6d3545
move from e2e to integration
jamyct 885d9a1
run goimports
jamyct 5068e83
move back to e2e for timebeing
jamyct 6c00ada
add positive e2e cases
jamyct 20e6fd4
run goimports
jamyct 0d0e028
fix import order & replace impersonateHubClient
jamyct e08ff73
remove line to delete mc
jamyct f1ea905
separate tests in two types: deny or allow
jamyct cb1e7d9
shorten error string
jamyct 6b33b10
remove unnecessary get validation
jamyct 50fe538
test v1 not v1beta1
jamyct f343941
move tests to apis placement directory
jamyct 6075a0b
Add context
jamyct 5b27590
removed unused variables from utils test file
jamyct 97ade6a
run goimports
jamyct ac29273
run goimports on suite test
jamyct 2f41141
fix import order arrangement
jamyct ffa954f
fix issues
jamyct 550fddd
remove unused file
jamyct 13b78d0
remove unused constants
jamyct 28adb83
run goimports
jamyct c6f2879
Merge branch 'main' of github.com:jamyct/fleet into restrict-membercl…
jamyct 032fbda
switch import from placement to cluster directory
jamyct 5d81c6d
add copyright banner to suit_test.go
jamyct File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
217 changes: 217 additions & 0 deletions
217
test/apis/cluster/v1/api_validation_integration_test.go
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| /* | ||
| Copyright (c) Microsoft Corporation. | ||
| Licensed under the MIT license. | ||
| */ | ||
| package v1 | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "reflect" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| clusterv1 "go.goms.io/fleet/apis/cluster/v1" | ||
| ) | ||
|
|
||
| var _ = Describe("Test cluster v1 API validation", func() { | ||
| Context("Test MemberCluster API validation - invalid cases", func() { | ||
| It("should deny creating API with invalid name size", func() { | ||
| var name = "abcdef-123456789-123456789-123456789-123456789-123456789-123456789-123456789" | ||
| // Create the API. | ||
| memberClusterName := &clusterv1.MemberCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| }, | ||
| Spec: clusterv1.MemberClusterSpec{ | ||
| Identity: rbacv1.Subject{ | ||
| Name: "fleet-member-agent-cluster-1", | ||
| Kind: "ServiceAccount", | ||
| Namespace: "fleet-system", | ||
| APIGroup: "", | ||
| }, | ||
| }, | ||
| } | ||
| By(fmt.Sprintf("expecting denial of CREATE API %s", name)) | ||
| var err = hubClient.Create(ctx, memberClusterName) | ||
| var statusErr *k8serrors.StatusError | ||
| Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create API call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8serrors.StatusError{}))) | ||
| Expect(statusErr.Status().Message).Should(ContainSubstring("metadata.name max length is 63")) | ||
| }) | ||
|
|
||
| It("should deny creating API with invalid name starting with non-alphanumeric character", func() { | ||
| var name = "-abcdef-123456789-123456789-123456789-123456789-123456789" | ||
| // Create the API. | ||
| memberClusterName := &clusterv1.MemberCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| }, | ||
| Spec: clusterv1.MemberClusterSpec{ | ||
| Identity: rbacv1.Subject{ | ||
| Name: "fleet-member-agent-cluster-1", | ||
| Kind: "ServiceAccount", | ||
| Namespace: "fleet-system", | ||
| APIGroup: "", | ||
| }, | ||
| }, | ||
| } | ||
| By(fmt.Sprintf("expecting denial of CREATE API %s", name)) | ||
| err := hubClient.Create(ctx, memberClusterName) | ||
| var statusErr *k8serrors.StatusError | ||
| Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create API call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8serrors.StatusError{}))) | ||
| Expect(statusErr.Status().Message).Should(ContainSubstring("a lowercase RFC 1123 subdomain")) | ||
| }) | ||
|
|
||
| It("should deny creating API with invalid name ending with non-alphanumeric character", func() { | ||
| var name = "abcdef-123456789-123456789-123456789-123456789-123456789-" | ||
| // Create the API. | ||
| memberClusterName := &clusterv1.MemberCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| }, | ||
| Spec: clusterv1.MemberClusterSpec{ | ||
| Identity: rbacv1.Subject{ | ||
| Name: "fleet-member-agent-cluster-1", | ||
| Kind: "ServiceAccount", | ||
| Namespace: "fleet-system", | ||
| APIGroup: "", | ||
| }, | ||
| }, | ||
| } | ||
| By(fmt.Sprintf("expecting denial of CREATE API %s", name)) | ||
| err := hubClient.Create(ctx, memberClusterName) | ||
| var statusErr *k8serrors.StatusError | ||
| Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create API call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8serrors.StatusError{}))) | ||
| Expect(statusErr.Status().Message).Should(ContainSubstring("a lowercase RFC 1123 subdomain")) | ||
| }) | ||
|
|
||
| It("should deny creating API with invalid name containing character that is not alphanumeric and not -", func() { | ||
| var name = "a_bcdef-123456789-123456789-123456789-123456789-123456789-123456789-123456789" | ||
| // Create the API. | ||
| memberClusterName := &clusterv1.MemberCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| }, | ||
| Spec: clusterv1.MemberClusterSpec{ | ||
| Identity: rbacv1.Subject{ | ||
| Name: "fleet-member-agent-cluster-1", | ||
| Kind: "ServiceAccount", | ||
| Namespace: "fleet-system", | ||
| APIGroup: "", | ||
| }, | ||
| }, | ||
| } | ||
| By(fmt.Sprintf("expecting denial of CREATE API %s", name)) | ||
| err := hubClient.Create(ctx, memberClusterName) | ||
| var statusErr *k8serrors.StatusError | ||
| Expect(errors.As(err, &statusErr)).To(BeTrue(), fmt.Sprintf("Create API call produced error %s. Error type wanted is %s.", reflect.TypeOf(err), reflect.TypeOf(&k8serrors.StatusError{}))) | ||
| Expect(statusErr.Status().Message).Should(ContainSubstring("a lowercase RFC 1123 subdomain")) | ||
| }) | ||
| }) | ||
|
|
||
| Context("Test Member Cluster creation API validation - valid cases", func() { | ||
| It("should allow creating API with valid name size", func() { | ||
| var name = "abc-123456789-123456789-123456789-123456789-123456789-123456789" | ||
| // Create the API. | ||
| memberClusterName := &clusterv1.MemberCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| }, | ||
| Spec: clusterv1.MemberClusterSpec{ | ||
| Identity: rbacv1.Subject{ | ||
| Name: "fleet-member-agent-cluster-1", | ||
| Kind: "ServiceAccount", | ||
| Namespace: "fleet-system", | ||
| APIGroup: "", | ||
| }, | ||
| }, | ||
| } | ||
| Expect(hubClient.Create(ctx, memberClusterName)).Should(Succeed()) | ||
| Expect(hubClient.Delete(ctx, memberClusterName)).Should(Succeed()) | ||
| }) | ||
|
|
||
| It("should allow creating API with valid name starting with alphabet character", func() { | ||
| var name = "abc-123456789" | ||
| // Create the API. | ||
| memberClusterName := &clusterv1.MemberCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| }, | ||
| Spec: clusterv1.MemberClusterSpec{ | ||
| Identity: rbacv1.Subject{ | ||
| Name: "fleet-member-agent-cluster-1", | ||
| Kind: "ServiceAccount", | ||
| Namespace: "fleet-system", | ||
| APIGroup: "", | ||
| }, | ||
| }, | ||
| } | ||
| Expect(hubClient.Create(ctx, memberClusterName)).Should(Succeed()) | ||
| Expect(hubClient.Delete(ctx, memberClusterName)).Should(Succeed()) | ||
| }) | ||
|
|
||
| It("should allow creating API with valid name starting with numeric character", func() { | ||
| var name = "123-123456789" | ||
| // Create the API. | ||
| memberClusterName := &clusterv1.MemberCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| }, | ||
| Spec: clusterv1.MemberClusterSpec{ | ||
| Identity: rbacv1.Subject{ | ||
| Name: "fleet-member-agent-cluster-1", | ||
| Kind: "ServiceAccount", | ||
| Namespace: "fleet-system", | ||
| APIGroup: "", | ||
| }, | ||
| }, | ||
| } | ||
| Expect(hubClient.Create(ctx, memberClusterName)).Should(Succeed()) | ||
| Expect(hubClient.Delete(ctx, memberClusterName)).Should(Succeed()) | ||
| }) | ||
|
|
||
| It("should allow creating API with valid name ending with alphabet character", func() { | ||
| var name = "123456789-abc" | ||
| // Create the API. | ||
| memberClusterName := &clusterv1.MemberCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| }, | ||
| Spec: clusterv1.MemberClusterSpec{ | ||
| Identity: rbacv1.Subject{ | ||
| Name: "fleet-member-agent-cluster-1", | ||
| Kind: "ServiceAccount", | ||
| Namespace: "fleet-system", | ||
| APIGroup: "", | ||
| }, | ||
| }, | ||
| } | ||
| Expect(hubClient.Create(ctx, memberClusterName)).Should(Succeed()) | ||
| Expect(hubClient.Delete(ctx, memberClusterName)).Should(Succeed()) | ||
| }) | ||
|
|
||
| It("should allow creating API with valid name ending with numeric character", func() { | ||
| var name = "123456789-123" | ||
| // Create the API. | ||
| memberClusterName := &clusterv1.MemberCluster{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| }, | ||
| Spec: clusterv1.MemberClusterSpec{ | ||
| Identity: rbacv1.Subject{ | ||
| Name: "fleet-member-agent-cluster-1", | ||
| Kind: "ServiceAccount", | ||
| Namespace: "fleet-system", | ||
| APIGroup: "", | ||
| }, | ||
| }, | ||
| } | ||
| Expect(hubClient.Create(ctx, memberClusterName)).Should(Succeed()) | ||
| Expect(hubClient.Delete(ctx, memberClusterName)).Should(Succeed()) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| Copyright (c) Microsoft Corporation. | ||
| Licensed under the MIT license. | ||
| */ | ||
| package v1 | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| "k8s.io/klog/v2" | ||
| "k8s.io/klog/v2/textlogger" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/envtest" | ||
| metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||
|
|
||
| clusterv1 "go.goms.io/fleet/apis/cluster/v1" | ||
| ) | ||
|
|
||
| var ( | ||
| hubTestEnv *envtest.Environment | ||
| hubClient client.Client | ||
| ctx context.Context | ||
| cancel context.CancelFunc | ||
| ) | ||
|
|
||
| func TestAPIs(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
|
|
||
| RunSpecs(t, "ClusterResourcePlacement Controller Suite") | ||
| } | ||
|
|
||
| var _ = BeforeSuite(func() { | ||
| By("Setup klog") | ||
| fs := flag.NewFlagSet("klog", flag.ContinueOnError) | ||
| klog.InitFlags(fs) | ||
| Expect(fs.Parse([]string{"--v", "5", "-add_dir_header", "true"})).Should(Succeed()) | ||
|
|
||
| ctx, cancel = context.WithCancel(context.TODO()) | ||
|
|
||
| By("bootstrap the test environment") | ||
| // Start the cluster. | ||
| hubTestEnv = &envtest.Environment{ | ||
| CRDDirectoryPaths: []string{ | ||
| filepath.Join("..", "..", "..", "..", "config", "crd", "bases"), | ||
| }, | ||
| ErrorIfCRDPathMissing: true, | ||
| } | ||
| hubCfg, err := hubTestEnv.Start() | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(hubCfg).NotTo(BeNil()) | ||
|
|
||
| Expect(clusterv1.AddToScheme(scheme.Scheme)).Should(Succeed()) | ||
|
|
||
| klog.InitFlags(flag.CommandLine) | ||
| flag.Parse() | ||
| // Create the hub controller manager. | ||
| hubCtrlMgr, err := ctrl.NewManager(hubCfg, ctrl.Options{ | ||
| Scheme: scheme.Scheme, | ||
| Metrics: metricsserver.Options{ | ||
| BindAddress: "0", | ||
| }, | ||
| Logger: textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(4))), | ||
| }) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| // Set up the client. | ||
| // The client must be one with cache (i.e. configured by the controller manager) to make | ||
| // use of the cache indexes. | ||
| hubClient = hubCtrlMgr.GetClient() | ||
| Expect(hubClient).NotTo(BeNil()) | ||
|
|
||
| go func() { | ||
| defer GinkgoRecover() | ||
| err = hubCtrlMgr.Start(ctx) | ||
| Expect(err).ToNot(HaveOccurred(), "failed to start manager for hub") | ||
| }() | ||
| }) | ||
|
|
||
| var _ = AfterSuite(func() { | ||
| defer klog.Flush() | ||
| cancel() | ||
|
|
||
| By("tearing down the test environment") | ||
| Expect(hubTestEnv.Stop()).Should(Succeed()) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.