Skip to content

Commit 696fe8f

Browse files
committed
fix
1 parent 8928a4b commit 696fe8f

File tree

5 files changed

+18
-29
lines changed

5 files changed

+18
-29
lines changed

test/e2e/framework/auth/helpers.go

Lines changed: 10 additions & 4 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,9 +104,9 @@ 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) (*rbacv1.ClusterRoleBinding, 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, 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
@@ -122,10 +123,15 @@ func BindClusterRole(ctx context.Context, c bindingsGetter, clusterRole, ns stri
122123
}, metav1.CreateOptions{})
123124

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

128-
return clusterRoleBinding, 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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -573,15 +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-
clusterRoleBinding, 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 func() {
580-
if clusterRoleBinding != nil {
581-
ginkgo.By(fmt.Sprintf("Destroying ClusterRoleBindings %q for this suite.", clusterRoleBinding.Name))
582-
framework.ExpectNoError(f.ClientSet.RbacV1().ClusterRoleBindings().Delete(ctx, clusterRoleBinding.Name, metav1.DeleteOptions{}))
583-
}
584-
}()
579+
defer cleanupFunc(ctx)
585580

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

test/e2e/node/kubelet_authz.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,9 @@ func runKubeletAuthzTest(ctx context.Context, f *framework.Framework, endpoint,
100100

101101
ginkgo.By(fmt.Sprintf("Creating ClusterRoleBinding with ClusterRole %s with subject %s/%s", clusterRole.Name, ns, saName))
102102

103-
clusterRoleBinding, err := e2eauth.BindClusterRole(ctx, f.ClientSet.RbacV1(), clusterRole.Name, ns, subject)
103+
cleanupFunc, err := e2eauth.BindClusterRole(ctx, f.ClientSet.RbacV1(), clusterRole.Name, ns, subject)
104104
framework.ExpectNoError(err)
105-
defer func() {
106-
if clusterRoleBinding != nil {
107-
ginkgo.By(fmt.Sprintf("Destroying ClusterRoleBindings %q for this suite.", clusterRoleBinding.Name))
108-
framework.ExpectNoError(f.ClientSet.RbacV1().ClusterRoleBindings().Delete(ctx, clusterRoleBinding.Name, metav1.DeleteOptions{}))
109-
}
110-
}()
105+
defer cleanupFunc(ctx)
111106

112107
ginkgo.By("Waiting for Authorization Update.")
113108

test/e2e/storage/drivers/in_tree.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +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-
clusterRoleBinding, 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-
if clusterRoleBinding != nil {
172-
ginkgo.DeferCleanup(cs.RbacV1().ClusterRoleBindings().Delete, clusterRoleBinding.Name, *metav1.NewDeleteOptions(0))
173-
}
171+
ginkgo.DeferCleanup(cleanupFunc)
174172

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

test/e2e/storage/volume_provisioning.go

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

445-
clusterRoleBinding, 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 func() {
448-
if clusterRoleBinding != nil {
449-
ginkgo.By(fmt.Sprintf("Destroying ClusterRoleBindings %q for this suite.", clusterRoleBinding.Name))
450-
framework.ExpectNoError(f.ClientSet.RbacV1().ClusterRoleBindings().Delete(ctx, clusterRoleBinding.Name, metav1.DeleteOptions{}))
451-
}
452-
}()
447+
defer cleanupFunc(ctx)
453448

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

0 commit comments

Comments
 (0)