Skip to content

Commit 9a9032f

Browse files
committed
image: transform api groups to legacy resources in admission
1 parent 3e9c0f0 commit 9a9032f

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

pkg/api/latest/latest.go

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ func OriginLegacyKind(gvk unversioned.GroupVersionKind) bool {
5555
return OriginKind(gvk) && gvk.Group == ""
5656
}
5757

58+
// IsOriginAPIGroup returns true if the provided group name belongs to Origin API.
59+
func IsOriginAPIGroup(groupName string) bool {
60+
for _, v := range Versions {
61+
if v.Group == groupName {
62+
return true
63+
}
64+
}
65+
return false
66+
}
67+
5868
// IsKindInAnyOriginGroup returns true if OpenShift owns the kind described in any apiVersion.
5969
// TODO: this may not work once we divide builds/deployments/images into their own API groups
6070
func IsKindInAnyOriginGroup(kind string) bool {

pkg/image/admission/imagepolicy/rules/accept.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"k8s.io/kubernetes/pkg/api/unversioned"
77

8+
"github.com/openshift/origin/pkg/api/latest"
89
"github.com/openshift/origin/pkg/image/admission/imagepolicy/api"
910
)
1011

@@ -18,14 +19,14 @@ type Accepter interface {
1819
type mappedAccepter map[unversioned.GroupResource]Accepter
1920

2021
func (a mappedAccepter) Covers(gr unversioned.GroupResource) bool {
21-
_, ok := a[gr]
22+
_, ok := a[transformToLegacy(gr)]
2223
return ok
2324
}
2425

2526
// Accepts returns true if no Accepter is registered for the group resource in attributes,
2627
// or if the registered Accepter also returns true.
2728
func (a mappedAccepter) Accepts(attr *ImagePolicyAttributes) bool {
28-
accepter, ok := a[attr.Resource]
29+
accepter, ok := a[transformToLegacy(attr.Resource)]
2930
if !ok {
3031
return true
3132
}
@@ -51,13 +52,13 @@ func NewExecutionRulesAccepter(rules []api.ImageExecutionPolicyRule, integratedR
5152
}
5253
rule.ImageCondition.MatchImageLabelSelectors = selectors
5354
for gr := range over {
54-
a, ok := mapped[gr]
55+
a, ok := mapped[transformToLegacy(gr)]
5556
if !ok {
5657
a = &executionAccepter{
57-
covers: gr,
58+
covers: transformToLegacy(gr),
5859
integratedRegistryMatcher: integratedRegistryMatcher,
5960
}
60-
mapped[gr] = a
61+
mapped[transformToLegacy(gr)] = a
6162
}
6263
byResource := a.(*executionAccepter)
6364
byResource.rules = append(byResource.rules, rule)
@@ -83,11 +84,20 @@ func NewExecutionRulesAccepter(rules []api.ImageExecutionPolicyRule, integratedR
8384
}
8485

8586
func (r *executionAccepter) Covers(gr unversioned.GroupResource) bool {
86-
return r.covers == gr
87+
return transformToLegacy(r.covers) == transformToLegacy(gr)
88+
}
89+
90+
// transformToLegacy transforms the given resource to legacy resource if the API group is
91+
// set and the group is one of the Origin API groups.
92+
func transformToLegacy(resource unversioned.GroupResource) unversioned.GroupResource {
93+
if len(resource.Group) > 0 && latest.IsOriginAPIGroup(resource.Group) {
94+
return unversioned.GroupResource{Resource: resource.Resource, Group: ""}
95+
}
96+
return resource
8797
}
8898

8999
func (r *executionAccepter) Accepts(attrs *ImagePolicyAttributes) bool {
90-
if attrs.Resource != r.covers {
100+
if !r.Covers(attrs.Resource) {
91101
return true
92102
}
93103

pkg/image/admission/imagepolicy/rules/accept_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ func TestAccept(t *testing.T) {
262262
{ImageCondition: api.ImageCondition{OnResources: []unversioned.GroupResource{podResource, {Resource: "services"}}}},
263263
{ImageCondition: api.ImageCondition{OnResources: []unversioned.GroupResource{{Resource: "services", Group: "extra"}}}},
264264
{ImageCondition: api.ImageCondition{OnResources: []unversioned.GroupResource{{Resource: "nodes", Group: "extra"}}}},
265+
{ImageCondition: api.ImageCondition{OnResources: []unversioned.GroupResource{{Resource: "deploymentconfigs", Group: ""}}}},
266+
{ImageCondition: api.ImageCondition{OnResources: []unversioned.GroupResource{{Resource: "deployments", Group: "extensions"}}}},
267+
{ImageCondition: api.ImageCondition{OnResources: []unversioned.GroupResource{{Resource: "users", Group: "user.openshift.io"}}}},
265268
},
266269
matcher: nameSet{},
267270
covers: map[unversioned.GroupResource]bool{
@@ -270,6 +273,16 @@ func TestAccept(t *testing.T) {
270273
unversioned.GroupResource{Group: "extra", Resource: "services"}: true,
271274
unversioned.GroupResource{Group: "extra", Resource: "nodes"}: true,
272275
unversioned.GroupResource{Resource: "nodes"}: false,
276+
277+
// Make sure the legacy and grouped resources are treated as same
278+
unversioned.GroupResource{Group: "", Resource: "deploymentconfigs"}: true,
279+
unversioned.GroupResource{Group: "apps.openshift.io", Resource: "deploymentconfigs"}: true,
280+
unversioned.GroupResource{Group: "user.openshift.io", Resource: "users"}: true,
281+
unversioned.GroupResource{Group: "", Resource: "users"}: true,
282+
283+
// Deployments are Kubernetes, not Origin API group
284+
unversioned.GroupResource{Group: "extensions", Resource: "deployments"}: true,
285+
unversioned.GroupResource{Group: "", Resource: "deployments"}: false,
273286
},
274287
},
275288
}

0 commit comments

Comments
 (0)