Skip to content

Commit 6b16be7

Browse files
committed
UPSTREAM: 131249: Kubelet: Randomize ClusterRole name in e2e
1 parent 73a5adc commit 6b16be7

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

test/e2e/framework/auth/helpers.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sync"
2323
"time"
2424

25+
"github.com/onsi/ginkgo/v2"
2526
authorizationv1 "k8s.io/api/authorization/v1"
2627
rbacv1 "k8s.io/api/rbac/v1"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -103,13 +104,13 @@ func WaitForNamedAuthorizationUpdate(ctx context.Context, c v1authorization.Subj
103104

104105
// BindClusterRole binds the cluster role at the cluster scope. If RBAC is not enabled, nil
105106
// is returned with no action.
106-
func BindClusterRole(ctx context.Context, c bindingsGetter, clusterRole, ns string, subjects ...rbacv1.Subject) error {
107+
func BindClusterRole(ctx context.Context, c bindingsGetter, clusterRole, ns string, subjects ...rbacv1.Subject) (func(ctx context.Context), error) {
107108
if !IsRBACEnabled(ctx, c) {
108-
return nil
109+
return func(ctx context.Context) {}, nil
109110
}
110111

111112
// Since the namespace names are unique, we can leave this lying around so we don't have to race any caches
112-
_, err := c.ClusterRoleBindings().Create(ctx, &rbacv1.ClusterRoleBinding{
113+
clusterRoleBinding, err := c.ClusterRoleBindings().Create(ctx, &rbacv1.ClusterRoleBinding{
113114
ObjectMeta: metav1.ObjectMeta{
114115
Name: ns + "--" + clusterRole,
115116
},
@@ -122,10 +123,15 @@ func BindClusterRole(ctx context.Context, c bindingsGetter, clusterRole, ns stri
122123
}, metav1.CreateOptions{})
123124

124125
if err != nil {
125-
return fmt.Errorf("binding clusterrole/%s for %q for %v: %w", clusterRole, ns, subjects, err)
126+
return nil, fmt.Errorf("binding clusterrole/%s for %q for %v: %w", clusterRole, ns, subjects, err)
126127
}
127128

128-
return nil
129+
cleanupFunc := func(ctx context.Context) {
130+
ginkgo.By(fmt.Sprintf("Destroying ClusterRoleBindings %q for this suite.", clusterRoleBinding.Name))
131+
framework.ExpectNoError(c.ClusterRoleBindings().Delete(ctx, clusterRoleBinding.Name, metav1.DeleteOptions{}))
132+
}
133+
134+
return cleanupFunc, nil
129135
}
130136

131137
// BindClusterRoleInNamespace binds the cluster role at the namespace scope. If RBAC is not enabled, nil

test/e2e/kubectl/kubectl.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,10 @@ var _ = SIGDescribe("Kubectl client", func() {
573573

574574
ginkgo.By("adding rbac permissions")
575575
// grant the view permission widely to allow inspection of the `invalid` namespace and the default namespace
576-
err := e2eauth.BindClusterRole(ctx, f.ClientSet.RbacV1(), "view", f.Namespace.Name,
576+
cleanupFunc, err := e2eauth.BindClusterRole(ctx, f.ClientSet.RbacV1(), "view", f.Namespace.Name,
577577
rbacv1.Subject{Kind: rbacv1.ServiceAccountKind, Namespace: f.Namespace.Name, Name: "default"})
578578
framework.ExpectNoError(err)
579+
defer cleanupFunc(ctx)
579580

580581
err = e2eauth.WaitForAuthorizationUpdate(ctx, f.ClientSet.AuthorizationV1(),
581582
serviceaccount.MakeUsername(f.Namespace.Name, "default"),

test/e2e/node/kubelet_authz.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ var _ = SIGDescribe(feature.KubeletFineGrainedAuthz, func() {
5959
func runKubeletAuthzTest(ctx context.Context, f *framework.Framework, endpoint, authzSubresource string) string {
6060
ns := f.Namespace.Name
6161
saName := authzSubresource
62-
crName := authzSubresource
6362
verb := "get"
6463
resource := "nodes"
6564

@@ -73,11 +72,11 @@ func runKubeletAuthzTest(ctx context.Context, f *framework.Framework, endpoint,
7372
}, metav1.CreateOptions{})
7473
framework.ExpectNoError(err)
7574

76-
ginkgo.By(fmt.Sprintf("Creating ClusterRole %s with for %s/%s", crName, resource, authzSubresource))
75+
ginkgo.By(fmt.Sprintf("Creating ClusterRole with prefix %s with for %s/%s", authzSubresource, resource, authzSubresource))
7776

78-
_, err = f.ClientSet.RbacV1().ClusterRoles().Create(ctx, &rbacv1.ClusterRole{
77+
clusterRole, err := f.ClientSet.RbacV1().ClusterRoles().Create(ctx, &rbacv1.ClusterRole{
7978
ObjectMeta: metav1.ObjectMeta{
80-
Name: crName,
79+
GenerateName: authzSubresource + "-",
8180
},
8281
Rules: []rbacv1.PolicyRule{
8382
{
@@ -88,17 +87,22 @@ func runKubeletAuthzTest(ctx context.Context, f *framework.Framework, endpoint,
8887
},
8988
}, metav1.CreateOptions{})
9089
framework.ExpectNoError(err)
90+
defer func() {
91+
ginkgo.By(fmt.Sprintf("Destroying ClusterRoles %q for this suite.", clusterRole.Name))
92+
framework.ExpectNoError(f.ClientSet.RbacV1().ClusterRoles().Delete(ctx, clusterRole.Name, metav1.DeleteOptions{}))
93+
}()
9194

9295
subject := rbacv1.Subject{
9396
Kind: rbacv1.ServiceAccountKind,
9497
Namespace: ns,
9598
Name: saName,
9699
}
97100

98-
ginkgo.By(fmt.Sprintf("Creating ClusterRoleBinding with ClusterRole %s with subject %s/%s", crName, ns, saName))
101+
ginkgo.By(fmt.Sprintf("Creating ClusterRoleBinding with ClusterRole %s with subject %s/%s", clusterRole.Name, ns, saName))
99102

100-
err = e2eauth.BindClusterRole(ctx, f.ClientSet.RbacV1(), crName, ns, subject)
103+
cleanupFunc, err := e2eauth.BindClusterRole(ctx, f.ClientSet.RbacV1(), clusterRole.Name, ns, subject)
101104
framework.ExpectNoError(err)
105+
defer cleanupFunc(ctx)
102106

103107
ginkgo.By("Waiting for Authorization Update.")
104108

test/e2e/storage/drivers/in_tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ func (n *nfsDriver) PrepareTest(ctx context.Context, f *framework.Framework) *st
165165

166166
// TODO(mkimuram): cluster-admin gives too much right but system:persistent-volume-provisioner
167167
// is not enough. We should create new clusterrole for testing.
168-
err := e2eauth.BindClusterRole(ctx, cs.RbacV1(), "cluster-admin", ns.Name,
168+
cleanupFunc, err := e2eauth.BindClusterRole(ctx, cs.RbacV1(), "cluster-admin", ns.Name,
169169
rbacv1.Subject{Kind: rbacv1.ServiceAccountKind, Namespace: ns.Name, Name: "default"})
170170
framework.ExpectNoError(err)
171-
ginkgo.DeferCleanup(cs.RbacV1().ClusterRoleBindings().Delete, ns.Name+"--"+"cluster-admin", *metav1.NewDeleteOptions(0))
171+
ginkgo.DeferCleanup(cleanupFunc)
172172

173173
err = e2eauth.WaitForAuthorizationUpdate(ctx, cs.AuthorizationV1(),
174174
serviceaccount.MakeUsername(ns.Name, "default"),

test/e2e/storage/volume_provisioning.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,9 @@ var _ = utils.SIGDescribe("Dynamic Provisioning", func() {
442442
Name: serviceAccountName,
443443
}
444444

445-
err := e2eauth.BindClusterRole(ctx, c.RbacV1(), "system:persistent-volume-provisioner", ns, subject)
445+
cleanupFunc, err := e2eauth.BindClusterRole(ctx, c.RbacV1(), "system:persistent-volume-provisioner", ns, subject)
446446
framework.ExpectNoError(err)
447+
defer cleanupFunc(ctx)
447448

448449
roleName := "leader-locking-nfs-provisioner"
449450
_, err = f.ClientSet.RbacV1().Roles(ns).Create(ctx, &rbacv1.Role{

0 commit comments

Comments
 (0)