Skip to content

Commit b4427a0

Browse files
committed
switch easy admission plugins to external clients
1 parent 1ed4596 commit b4427a0

File tree

10 files changed

+79
-23
lines changed

10 files changed

+79
-23
lines changed

pkg/authorization/admission/restrictusers/restrictusers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
"k8s.io/kubernetes/pkg/apis/rbac"
1515
kadmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
1616

17-
authorizationclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
18-
authorizationtypedclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset/typed/authorization/internalversion"
17+
authorizationclient "github.com/openshift/origin/pkg/authorization/generated/clientset"
18+
authorizationtypedclient "github.com/openshift/origin/pkg/authorization/generated/clientset/typed/authorization/v1"
1919
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
2020
userapi "github.com/openshift/origin/pkg/user/apis/user"
2121
usercache "github.com/openshift/origin/pkg/user/cache"

pkg/authorization/admission/restrictusers/restrictusers_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
1616
kadmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
1717

18-
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
19-
fakeauthorizationclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset/fake"
18+
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization/v1"
19+
fakeauthorizationclient "github.com/openshift/origin/pkg/authorization/generated/clientset/fake"
2020
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
2121
userapi "github.com/openshift/origin/pkg/user/apis/user"
2222
fakeuserclient "github.com/openshift/origin/pkg/user/generated/internalclientset/fake"

pkg/authorization/admission/restrictusers/subjectchecker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"k8s.io/kubernetes/pkg/apis/rbac"
1010
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
1111

12-
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
12+
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization/v1"
1313
userapi "github.com/openshift/origin/pkg/user/apis/user"
1414
userclient "github.com/openshift/origin/pkg/user/generated/internalclientset/typed/user/internalversion"
1515
)

pkg/authorization/admission/restrictusers/subjectchecker_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"k8s.io/kubernetes/pkg/apis/rbac"
1111
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
1212

13-
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
13+
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization/v1"
1414
userapi "github.com/openshift/origin/pkg/user/apis/user"
1515
fakeuserclient "github.com/openshift/origin/pkg/user/generated/internalclientset/fake"
1616
)

pkg/build/admission/strategyrestrictions/admission.go

+28-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
99
"k8s.io/apimachinery/pkg/runtime/schema"
1010
"k8s.io/apiserver/pkg/admission"
11+
kapi "k8s.io/kubernetes/pkg/api"
1112
kapihelper "k8s.io/kubernetes/pkg/api/helper"
1213
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
1314
authorizationclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/authorization/internalversion"
@@ -17,7 +18,7 @@ import (
1718
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
1819
"github.com/openshift/origin/pkg/authorization/util"
1920
buildapi "github.com/openshift/origin/pkg/build/apis/build"
20-
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
21+
buildclient "github.com/openshift/origin/pkg/build/generated/clientset"
2122
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
2223
"k8s.io/kubernetes/pkg/apis/authorization"
2324
)
@@ -182,13 +183,21 @@ func (a *buildByStrategy) checkBuildRequestAuthorization(req *buildapi.BuildRequ
182183
if err != nil {
183184
return admission.NewForbidden(attr, err)
184185
}
185-
return a.checkBuildAuthorization(build, attr)
186+
internalBuild := &buildapi.Build{}
187+
if err := kapi.Scheme.Convert(build, internalBuild, nil); err != nil {
188+
return admission.NewForbidden(attr, err)
189+
}
190+
return a.checkBuildAuthorization(internalBuild, attr)
186191
case buildapi.IsResourceOrLegacy("buildconfigs", gr):
187-
build, err := a.buildClient.Build().BuildConfigs(attr.GetNamespace()).Get(req.Name, metav1.GetOptions{})
192+
buildConfig, err := a.buildClient.Build().BuildConfigs(attr.GetNamespace()).Get(req.Name, metav1.GetOptions{})
188193
if err != nil {
189194
return admission.NewForbidden(attr, err)
190195
}
191-
return a.checkBuildConfigAuthorization(build, attr)
196+
internalBuildConfig := &buildapi.BuildConfig{}
197+
if err := kapi.Scheme.Convert(buildConfig, internalBuildConfig, nil); err != nil {
198+
return admission.NewForbidden(attr, err)
199+
}
200+
return a.checkBuildConfigAuthorization(internalBuildConfig, attr)
192201
default:
193202
return admission.NewForbidden(attr, fmt.Errorf("Unknown resource type %s for BuildRequest", attr.GetResource()))
194203
}
@@ -206,5 +215,19 @@ func (a *buildByStrategy) checkAccess(strategy buildapi.BuildStrategy, subjectAc
206215
}
207216

208217
func notAllowed(strategy buildapi.BuildStrategy, attr admission.Attributes) error {
209-
return admission.NewForbidden(attr, fmt.Errorf("build strategy %s is not allowed", buildapi.StrategyType(strategy)))
218+
return admission.NewForbidden(attr, fmt.Errorf("build strategy %s is not allowed", strategyTypeString(strategy)))
219+
}
220+
221+
func strategyTypeString(strategy buildapi.BuildStrategy) string {
222+
switch {
223+
case strategy.DockerStrategy != nil:
224+
return "Docker"
225+
case strategy.CustomStrategy != nil:
226+
return "Custom"
227+
case strategy.SourceStrategy != nil:
228+
return "Source"
229+
case strategy.JenkinsPipelineStrategy != nil:
230+
return "JenkinsPipeline"
231+
}
232+
return ""
210233
}

pkg/build/admission/strategyrestrictions/admission_test.go

+28-6
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ import (
1111
"k8s.io/apiserver/pkg/admission"
1212
"k8s.io/apiserver/pkg/authentication/user"
1313
clientgotesting "k8s.io/client-go/testing"
14+
kapi "k8s.io/kubernetes/pkg/api"
1415
"k8s.io/kubernetes/pkg/apis/authorization"
1516
fakekubeclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
1617
kubeadmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
1718

1819
buildapi "github.com/openshift/origin/pkg/build/apis/build"
19-
fakebuildclient "github.com/openshift/origin/pkg/build/generated/internalclientset/fake"
20+
buildapiv1 "github.com/openshift/origin/pkg/build/apis/build/v1"
21+
fakebuildclient "github.com/openshift/origin/pkg/build/generated/clientset/fake"
2022
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
23+
24+
_ "github.com/openshift/origin/pkg/build/apis/build/install"
2125
)
2226

2327
func TestBuildAdmission(t *testing.T) {
@@ -48,7 +52,7 @@ func TestBuildAdmission(t *testing.T) {
4852
{
4953
name: "allowed source build clone",
5054
object: testBuildRequest("test-build"),
51-
responseObject: testBuild(buildapi.BuildStrategy{SourceStrategy: &buildapi.SourceBuildStrategy{}}),
55+
responseObject: asV1Build(testBuild(buildapi.BuildStrategy{SourceStrategy: &buildapi.SourceBuildStrategy{}})),
5256
kind: buildapi.Kind("Build"),
5357
resource: buildapi.Resource("builds"),
5458
subResource: "clone",
@@ -70,7 +74,7 @@ func TestBuildAdmission(t *testing.T) {
7074
{
7175
name: "denied docker build clone",
7276
object: testBuildRequest("buildname"),
73-
responseObject: testBuild(buildapi.BuildStrategy{DockerStrategy: &buildapi.DockerBuildStrategy{}}),
77+
responseObject: asV1Build(testBuild(buildapi.BuildStrategy{DockerStrategy: &buildapi.DockerBuildStrategy{}})),
7478
kind: buildapi.Kind("Build"),
7579
resource: buildapi.Resource("builds"),
7680
subResource: "clone",
@@ -101,7 +105,7 @@ func TestBuildAdmission(t *testing.T) {
101105
},
102106
{
103107
name: "allowed build config instantiate",
104-
responseObject: testBuildConfig(buildapi.BuildStrategy{DockerStrategy: &buildapi.DockerBuildStrategy{}}),
108+
responseObject: asV1BuildConfig(testBuildConfig(buildapi.BuildStrategy{DockerStrategy: &buildapi.DockerBuildStrategy{}})),
105109
object: testBuildRequest("test-buildconfig"),
106110
kind: buildapi.Kind("Build"),
107111
resource: buildapi.Resource("buildconfigs"),
@@ -123,7 +127,7 @@ func TestBuildAdmission(t *testing.T) {
123127
},
124128
{
125129
name: "forbidden build config instantiate",
126-
responseObject: testBuildConfig(buildapi.BuildStrategy{CustomStrategy: &buildapi.CustomBuildStrategy{}}),
130+
responseObject: asV1BuildConfig(testBuildConfig(buildapi.BuildStrategy{CustomStrategy: &buildapi.CustomBuildStrategy{}})),
127131
object: testBuildRequest("buildname"),
128132
kind: buildapi.Kind("Build"),
129133
resource: buildapi.Resource("buildconfigs"),
@@ -164,7 +168,7 @@ func TestBuildAdmission(t *testing.T) {
164168
{
165169
name: "allowed jenkins pipeline build clone",
166170
object: testBuildRequest("test-build"),
167-
responseObject: testBuild(buildapi.BuildStrategy{JenkinsPipelineStrategy: &buildapi.JenkinsPipelineBuildStrategy{}}),
171+
responseObject: asV1Build(testBuild(buildapi.BuildStrategy{JenkinsPipelineStrategy: &buildapi.JenkinsPipelineBuildStrategy{}})),
168172
kind: buildapi.Kind("Build"),
169173
resource: buildapi.Resource("builds"),
170174
subResource: "clone",
@@ -250,6 +254,15 @@ func testBuild(strategy buildapi.BuildStrategy) *buildapi.Build {
250254
}
251255
}
252256

257+
func asV1Build(in *buildapi.Build) *buildapiv1.Build {
258+
out := &buildapiv1.Build{}
259+
err := kapi.Scheme.Convert(in, out, nil)
260+
if err != nil {
261+
panic(err)
262+
}
263+
return out
264+
}
265+
253266
func testBuildConfig(strategy buildapi.BuildStrategy) *buildapi.BuildConfig {
254267
return &buildapi.BuildConfig{
255268
ObjectMeta: metav1.ObjectMeta{
@@ -264,6 +277,15 @@ func testBuildConfig(strategy buildapi.BuildStrategy) *buildapi.BuildConfig {
264277
}
265278
}
266279

280+
func asV1BuildConfig(in *buildapi.BuildConfig) *buildapiv1.BuildConfig {
281+
out := &buildapiv1.BuildConfig{}
282+
err := kapi.Scheme.Convert(in, out, nil)
283+
if err != nil {
284+
panic(err)
285+
}
286+
return out
287+
}
288+
267289
func reviewResponse(allowed bool, msg string) *authorization.SubjectAccessReview {
268290
return &authorization.SubjectAccessReview{
269291
Status: authorization.SubjectAccessReviewStatus{

pkg/build/apis/build/v1/register.go

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ func Resource(resource string) schema.GroupResource {
2525
return SchemeGroupVersion.WithResource(resource).GroupResource()
2626
}
2727

28+
// LegacyResource takes an unqualified resource and returns back a Group qualified GroupResource
29+
func LegacyResource(resource string) schema.GroupResource {
30+
return LegacySchemeGroupVersion.WithResource(resource).GroupResource()
31+
}
32+
33+
// IsResourceOrLegacy checks if the provided GroupResources matches with the given
34+
// resource by looking up the API group and also the legacy API.
35+
func IsResourceOrLegacy(resource string, gr schema.GroupResource) bool {
36+
return gr == Resource(resource) || gr == LegacyResource(resource)
37+
}
38+
2839
// addKnownTypes adds types to API group
2940
func addKnownTypes(scheme *runtime.Scheme) error {
3041
scheme.AddKnownTypes(SchemeGroupVersion,

pkg/cmd/server/admission/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
kubeapiserveradmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
99
"k8s.io/kubernetes/pkg/quota"
1010

11-
authorizationclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
12-
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
11+
authorizationclient "github.com/openshift/origin/pkg/authorization/generated/clientset"
12+
buildclient "github.com/openshift/origin/pkg/build/generated/clientset"
1313
configapi "github.com/openshift/origin/pkg/cmd/server/api"
1414
imageapi "github.com/openshift/origin/pkg/image/apis/image"
1515
imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset"

pkg/cmd/server/admission/types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
kinternalinformers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion"
88
"k8s.io/kubernetes/pkg/quota"
99

10-
authorizationclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
11-
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
10+
authorizationclient "github.com/openshift/origin/pkg/authorization/generated/clientset"
11+
buildclient "github.com/openshift/origin/pkg/build/generated/clientset"
1212
configapi "github.com/openshift/origin/pkg/cmd/server/api"
1313
imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset"
1414
"github.com/openshift/origin/pkg/project/cache"

pkg/cmd/server/origin/admission/plugin_initializer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"os"
77
"time"
88

9-
authorizationclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset"
10-
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
9+
authorizationclient "github.com/openshift/origin/pkg/authorization/generated/clientset"
10+
buildclient "github.com/openshift/origin/pkg/build/generated/clientset"
1111
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
1212
configapi "github.com/openshift/origin/pkg/cmd/server/api"
1313
kubernetes "github.com/openshift/origin/pkg/cmd/server/kubernetes/master"

0 commit comments

Comments
 (0)