Skip to content

Commit 007b004

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request #53298 from enj/automated-cherry-pick-of-#53239-upstream-release-1.8
Automatic merge from submit-queue. Automated cherry pick of #53239 Cherry pick of #53239 on release-1.8. #53239: Correct APIGroup for RoleBindingBuilder Subjects
2 parents 457ea0c + 3aedc8a commit 007b004

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

hack/.golint_failures

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ pkg/apis/policy
125125
pkg/apis/policy/v1alpha1
126126
pkg/apis/policy/v1beta1
127127
pkg/apis/policy/validation
128-
pkg/apis/rbac
129128
pkg/apis/rbac/v1
130129
pkg/apis/rbac/v1beta1
131130
pkg/apis/rbac/validation

pkg/apis/rbac/BUILD

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"])
33
load(
44
"@io_bazel_rules_go//go:def.bzl",
55
"go_library",
6+
"go_test",
67
)
78

89
go_library(
@@ -43,3 +44,16 @@ filegroup(
4344
],
4445
tags = ["automanaged"],
4546
)
47+
48+
go_test(
49+
name = "go_default_xtest",
50+
srcs = ["helpers_test.go"],
51+
deps = [
52+
":go_default_library",
53+
"//pkg/api:go_default_library",
54+
"//pkg/apis/rbac/install:go_default_library",
55+
"//pkg/apis/rbac/v1:go_default_library",
56+
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
57+
"//vendor/k8s.io/apimachinery/pkg/util/diff:go_default_library",
58+
],
59+
)

pkg/apis/rbac/helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,15 @@ func NewRoleBindingForClusterRole(roleName, namespace string) *RoleBindingBuilde
348348
// Groups adds the specified groups as the subjects of the RoleBinding.
349349
func (r *RoleBindingBuilder) Groups(groups ...string) *RoleBindingBuilder {
350350
for _, group := range groups {
351-
r.RoleBinding.Subjects = append(r.RoleBinding.Subjects, Subject{Kind: GroupKind, Name: group})
351+
r.RoleBinding.Subjects = append(r.RoleBinding.Subjects, Subject{Kind: GroupKind, APIGroup: GroupName, Name: group})
352352
}
353353
return r
354354
}
355355

356356
// Users adds the specified users as the subjects of the RoleBinding.
357357
func (r *RoleBindingBuilder) Users(users ...string) *RoleBindingBuilder {
358358
for _, user := range users {
359-
r.RoleBinding.Subjects = append(r.RoleBinding.Subjects, Subject{Kind: UserKind, Name: user})
359+
r.RoleBinding.Subjects = append(r.RoleBinding.Subjects, Subject{Kind: UserKind, APIGroup: GroupName, Name: user})
360360
}
361361
return r
362362
}

pkg/apis/rbac/helpers_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package rbac_test
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/apimachinery/pkg/util/diff"
25+
"k8s.io/kubernetes/pkg/api"
26+
"k8s.io/kubernetes/pkg/apis/rbac"
27+
"k8s.io/kubernetes/pkg/apis/rbac/v1"
28+
29+
// install RBAC types
30+
_ "k8s.io/kubernetes/pkg/apis/rbac/install"
31+
)
32+
33+
// TestHelpersRoundTrip confirms that the rbac.New* helper functions produce RBAC objects that match objects
34+
// that have gone through conversion and defaulting. This is required because these helper functions are
35+
// used to create the bootstrap RBAC policy which is used during reconciliation. If they produced objects
36+
// that did not match, reconciliation would incorrectly add duplicate data to the cluster's RBAC policy.
37+
func TestHelpersRoundTrip(t *testing.T) {
38+
rb := rbac.NewRoleBinding("role", "ns").Groups("g").SAs("ns", "sa").Users("u").BindingOrDie()
39+
rbcr := rbac.NewRoleBindingForClusterRole("role", "ns").Groups("g").SAs("ns", "sa").Users("u").BindingOrDie()
40+
crb := rbac.NewClusterBinding("role").Groups("g").SAs("ns", "sa").Users("u").BindingOrDie()
41+
42+
role := &rbac.Role{
43+
Rules: []rbac.PolicyRule{
44+
rbac.NewRule("verb").Groups("g").Resources("foo").RuleOrDie(),
45+
rbac.NewRule("verb").URLs("/foo").RuleOrDie(),
46+
},
47+
}
48+
clusterRole := &rbac.ClusterRole{
49+
Rules: []rbac.PolicyRule{
50+
rbac.NewRule("verb").Groups("g").Resources("foo").RuleOrDie(),
51+
rbac.NewRule("verb").URLs("/foo").RuleOrDie(),
52+
},
53+
}
54+
55+
for _, internalObj := range []runtime.Object{&rb, &rbcr, &crb, role, clusterRole} {
56+
v1Obj, err := api.Scheme.ConvertToVersion(internalObj, v1.SchemeGroupVersion)
57+
if err != nil {
58+
t.Errorf("err on %T: %v", internalObj, err)
59+
continue
60+
}
61+
api.Scheme.Default(v1Obj)
62+
roundTrippedObj, err := api.Scheme.ConvertToVersion(v1Obj, rbac.SchemeGroupVersion)
63+
if err != nil {
64+
t.Errorf("err on %T: %v", internalObj, err)
65+
continue
66+
}
67+
if !reflect.DeepEqual(internalObj, roundTrippedObj) {
68+
t.Errorf("err on %T: got difference:\n%s", internalObj, diff.ObjectDiff(internalObj, roundTrippedObj))
69+
continue
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)