Skip to content

Commit f474164

Browse files
Merge pull request #925 from flavianmissi/OCPBUGS-18969
OCPBUGS-18969: move pruner role creation from openshift-apiserver
2 parents c1b9062 + 0a931a4 commit f474164

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

pkg/resource/generatorimagepruner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type ImagePrunerGenerator struct {
2626

2727
func (g *ImagePrunerGenerator) List(cr *imageregistryv1.ImagePruner) ([]Mutator, error) {
2828
var mutators []Mutator
29+
mutators = append(mutators, newGeneratorPrunerClusterRole(g.listers.ClusterRoles, g.clients.RBAC))
2930
mutators = append(mutators, newGeneratorPrunerClusterRoleBinding(g.listers.ClusterRoleBindings, g.clients.RBAC))
3031
mutators = append(mutators, newGeneratorPrunerServiceAccount(g.listers.ServiceAccounts, g.clients.Core))
3132
mutators = append(mutators, newGeneratorServiceCA(g.listers.ConfigMaps, g.clients.Core))

pkg/resource/prunerclusterrole.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package resource
2+
3+
import (
4+
"context"
5+
6+
rbacapi "k8s.io/api/rbac/v1"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
rbacset "k8s.io/client-go/kubernetes/typed/rbac/v1"
10+
rbaclisters "k8s.io/client-go/listers/rbac/v1"
11+
)
12+
13+
var _ Mutator = &generatorPrunerClusterRole{}
14+
15+
type generatorPrunerClusterRole struct {
16+
lister rbaclisters.ClusterRoleLister
17+
client rbacset.RbacV1Interface
18+
}
19+
20+
func newGeneratorPrunerClusterRole(lister rbaclisters.ClusterRoleLister, client rbacset.RbacV1Interface) *generatorPrunerClusterRole {
21+
return &generatorPrunerClusterRole{
22+
lister: lister,
23+
client: client,
24+
}
25+
}
26+
27+
func (gcr *generatorPrunerClusterRole) Type() runtime.Object {
28+
return &rbacapi.ClusterRole{}
29+
}
30+
31+
func (gcr *generatorPrunerClusterRole) GetNamespace() string {
32+
return ""
33+
}
34+
35+
func (gcr *generatorPrunerClusterRole) GetName() string {
36+
return "system:image-pruner"
37+
}
38+
39+
func (gcr *generatorPrunerClusterRole) expected() (runtime.Object, error) {
40+
role := &rbacapi.ClusterRole{
41+
ObjectMeta: metav1.ObjectMeta{
42+
Name: gcr.GetName(),
43+
Namespace: gcr.GetNamespace(),
44+
},
45+
Rules: []rbacapi.PolicyRule{
46+
{
47+
Verbs: []string{"get", "list"},
48+
APIGroups: []string{""},
49+
Resources: []string{
50+
"pods",
51+
"replicationcontrollers",
52+
},
53+
},
54+
{
55+
Verbs: []string{"list"},
56+
APIGroups: []string{""},
57+
Resources: []string{
58+
"limitranges",
59+
},
60+
},
61+
{
62+
Verbs: []string{"get", "list"},
63+
APIGroups: []string{"build.openshift.io", ""},
64+
Resources: []string{
65+
"buildconfigs",
66+
"builds",
67+
},
68+
},
69+
{
70+
Verbs: []string{"get", "list"},
71+
APIGroups: []string{"apps.openshift.io", ""},
72+
Resources: []string{
73+
"deploymentconfigs",
74+
},
75+
},
76+
{
77+
Verbs: []string{"get", "list"},
78+
APIGroups: []string{"batch"},
79+
Resources: []string{
80+
"jobs",
81+
"cronjobs",
82+
},
83+
},
84+
{
85+
Verbs: []string{"get", "list"},
86+
APIGroups: []string{"apps"},
87+
Resources: []string{
88+
"daemonsets",
89+
"deployments",
90+
"replicasets",
91+
"statefulsets",
92+
},
93+
},
94+
{
95+
Verbs: []string{"delete"},
96+
APIGroups: []string{"image.openshift.io", ""},
97+
Resources: []string{
98+
"images",
99+
},
100+
},
101+
{
102+
Verbs: []string{"get", "list", "watch"},
103+
APIGroups: []string{"image.openshift.io", ""},
104+
Resources: []string{
105+
"images",
106+
"imagestreams",
107+
},
108+
},
109+
{
110+
Verbs: []string{"update"},
111+
APIGroups: []string{"image.openshift.io", ""},
112+
Resources: []string{
113+
"imagestreams/status",
114+
},
115+
},
116+
},
117+
}
118+
119+
return role, nil
120+
}
121+
122+
func (gcr *generatorPrunerClusterRole) Get() (runtime.Object, error) {
123+
return gcr.lister.Get(gcr.GetName())
124+
}
125+
126+
func (gcr *generatorPrunerClusterRole) Create() (runtime.Object, error) {
127+
return commonCreate(gcr, func(obj runtime.Object) (runtime.Object, error) {
128+
return gcr.client.ClusterRoles().Create(
129+
context.TODO(), obj.(*rbacapi.ClusterRole), metav1.CreateOptions{},
130+
)
131+
})
132+
}
133+
134+
func (gcr *generatorPrunerClusterRole) Update(o runtime.Object) (runtime.Object, bool, error) {
135+
return commonUpdate(gcr, o, func(obj runtime.Object) (runtime.Object, error) {
136+
return gcr.client.ClusterRoles().Update(
137+
context.TODO(), obj.(*rbacapi.ClusterRole), metav1.UpdateOptions{},
138+
)
139+
})
140+
}
141+
142+
func (gcr *generatorPrunerClusterRole) Delete(opts metav1.DeleteOptions) error {
143+
return gcr.client.ClusterRoles().Delete(
144+
context.TODO(), gcr.GetName(), opts,
145+
)
146+
}
147+
148+
func (g *generatorPrunerClusterRole) Owned() bool {
149+
return true
150+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package resource
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
rbacapi "k8s.io/api/rbac/v1"
8+
)
9+
10+
func TestImagePrunerRules(t *testing.T) {
11+
generator := newGeneratorPrunerClusterRole(nil, nil)
12+
expected := rbacapi.PolicyRule{
13+
Verbs: []string{"delete"},
14+
APIGroups: []string{"image.openshift.io"},
15+
Resources: []string{"images"},
16+
}
17+
r, err := generator.expected()
18+
if err != nil {
19+
t.Fatalf("error getting desired cluster role: %#v", err)
20+
}
21+
role, ok := r.(*rbacapi.ClusterRole)
22+
if !ok {
23+
t.Fatal("failed to cast object to ClusterRole")
24+
}
25+
26+
for _, rule := range role.Rules {
27+
if !reflect.DeepEqual(rule.Resources, expected.Resources) {
28+
continue
29+
}
30+
if !reflect.DeepEqual(rule.Verbs, expected.Verbs) {
31+
t.Error("images rule.Verbs differ")
32+
t.Errorf("want %#v", expected.Verbs)
33+
t.Errorf("got %#v", rule.Verbs)
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)