-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(manager): Add forbiddenAnnotations,forbiddenLabels to serviceOpt…
…ions
- Loading branch information
1 parent
5e13ac9
commit 8b4f7ad
Showing
10 changed files
with
500 additions
and
198 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
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 |
---|---|---|
@@ -0,0 +1,161 @@ | ||
//go:build e2e | ||
|
||
// Copyright 2020-2023 Project Capsule Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package e2e | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2" | ||
"github.com/projectcapsule/capsule/pkg/api" | ||
) | ||
|
||
var _ = Describe("creating a Service with user-specified labels and annotations", func() { | ||
tnt := &capsulev1beta2.Tenant{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "tenant-user-metadata-forbidden", | ||
}, | ||
Spec: capsulev1beta2.TenantSpec{ | ||
ServiceOptions: &api.ServiceOptions{ | ||
ForbiddenLabels: api.ForbiddenListSpec{ | ||
Exact: []string{"foo", "bar"}, | ||
Regex: "^gatsby-.*$", | ||
}, | ||
ForbiddenAnnotations: api.ForbiddenListSpec{ | ||
Exact: []string{"foo", "bar"}, | ||
Regex: "^gatsby-.*$", | ||
}, | ||
}, | ||
Owners: capsulev1beta2.OwnerListSpec{ | ||
{ | ||
Name: "gatsby", | ||
Kind: "User", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
JustBeforeEach(func() { | ||
EventuallyCreation(func() error { | ||
tnt.ResourceVersion = "" | ||
return k8sClient.Create(context.TODO(), tnt) | ||
}).Should(Succeed()) | ||
}) | ||
JustAfterEach(func() { | ||
Expect(k8sClient.Delete(context.TODO(), tnt)).Should(Succeed()) | ||
}) | ||
|
||
It("should allow", func() { | ||
By("specifying non-forbidden labels", func() { | ||
svc := NewService("") | ||
svc.SetLabels(map[string]string{"bim": "baz"}) | ||
ServiceCreation(svc, tnt.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) | ||
}) | ||
By("specifying non-forbidden annotations", func() { | ||
svc := NewService("") | ||
svc.SetAnnotations(map[string]string{"bim": "baz"}) | ||
ServiceCreation(svc, tnt.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) | ||
}) | ||
}) | ||
|
||
It("should fail when creating a Service", func() { | ||
By("specifying forbidden labels using exact match", func() { | ||
svc := NewService("") | ||
svc.SetLabels(map[string]string{"foo": "bar"}) | ||
ServiceCreation(svc, tnt.Spec.Owners[0], defaultTimeoutInterval).ShouldNot(Succeed()) | ||
}) | ||
By("specifying forbidden labels using regex match", func() { | ||
svc := NewService("") | ||
svc.SetLabels(map[string]string{"gatsby-foo": "bar"}) | ||
ServiceCreation(svc, tnt.Spec.Owners[0], defaultTimeoutInterval).ShouldNot(Succeed()) | ||
}) | ||
By("specifying forbidden annotations using exact match", func() { | ||
svc := NewService("") | ||
svc.SetAnnotations(map[string]string{"foo": "bar"}) | ||
ServiceCreation(svc, tnt.Spec.Owners[0], defaultTimeoutInterval).ShouldNot(Succeed()) | ||
}) | ||
By("specifying forbidden annotations using regex match", func() { | ||
svc := NewService("") | ||
svc.SetAnnotations(map[string]string{"gatsby-foo": "bar"}) | ||
ServiceCreation(svc, tnt.Spec.Owners[0], defaultTimeoutInterval).ShouldNot(Succeed()) | ||
}) | ||
}) | ||
|
||
It("should fail when updating a Service", func() { | ||
cs := ownerClient(tnt.Spec.Owners[0]) | ||
|
||
By("specifying forbidden labels using exact match", func() { | ||
svc := NewService("forbidden-labels-exact-match") | ||
ServiceCreation(svc, tnt.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) | ||
Consistently(func() error { | ||
if err := k8sClient.Get(context.Background(), types.NamespacedName{Name: svc.GetName()}, svc); err != nil { | ||
return nil | ||
} | ||
|
||
svc.SetLabels(map[string]string{"foo": "bar"}) | ||
|
||
_, err := cs.CoreV1().Services().Update(context.Background(), svc, metav1.UpdateOptions{}) | ||
|
||
return err | ||
}, 10*time.Second, time.Second).ShouldNot(Succeed()) | ||
}) | ||
By("specifying forbidden labels using regex match", func() { | ||
svc := NewService("forbidden-labels-regex-match") | ||
|
||
ServiceCreation(svc, tnt.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) | ||
Consistently(func() error { | ||
if err := k8sClient.Get(context.Background(), types.NamespacedName{Name: svc.GetName()}, svc); err != nil { | ||
return nil | ||
} | ||
|
||
svc.SetLabels(map[string]string{"gatsby-foo": "bar"}) | ||
|
||
_, err := cs.CoreV1().Services().Update(context.Background(), svc, metav1.UpdateOptions{}) | ||
|
||
return err | ||
}, 3*time.Second, time.Second).ShouldNot(Succeed()) | ||
}) | ||
By("specifying forbidden annotations using exact match", func() { | ||
svc := NewService("forbidden-annotations-exact-match") | ||
|
||
ServiceCreation(svc, tnt.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) | ||
rbacPatch(svc.GetName()) | ||
Consistently(func() error { | ||
if err := k8sClient.Get(context.Background(), types.NamespacedName{Name: svc.GetName()}, svc); err != nil { | ||
return nil | ||
} | ||
|
||
svc.SetAnnotations(map[string]string{"foo": "bar"}) | ||
|
||
_, err := cs.CoreV1().Services().Update(context.Background(), svc, metav1.UpdateOptions{}) | ||
|
||
return err | ||
}, 10*time.Second, time.Second).ShouldNot(Succeed()) | ||
}) | ||
By("specifying forbidden annotations using regex match", func() { | ||
svc := NewService("forbidden-annotations-regex-match") | ||
|
||
ServiceCreation(svc, tnt.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) | ||
rbacPatch(svc.GetName()) | ||
Consistently(func() error { | ||
if err := k8sClient.Get(context.Background(), types.NamespacedName{Name: svc.GetName()}, svc); err != nil { | ||
return nil | ||
} | ||
|
||
svc.SetAnnotations(map[string]string{"gatsby-foo": "bar"}) | ||
|
||
_, err := cs.CoreV1().Services().Update(context.Background(), svc, metav1.UpdateOptions{}) | ||
|
||
return err | ||
}, 10*time.Second, time.Second).ShouldNot(Succeed()) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.