@@ -7,34 +7,20 @@ import (
77
88 "k8s.io/klog"
99
10- kapierrors "k8s.io/apimachinery/pkg/api/errors"
1110 "k8s.io/apimachinery/pkg/runtime/schema"
1211 "k8s.io/apimachinery/pkg/util/sets"
1312 "k8s.io/apiserver/pkg/admission"
1413 "k8s.io/apiserver/pkg/admission/initializer"
1514 "k8s.io/apiserver/pkg/authorization/authorizer"
16- "k8s.io/kubernetes/pkg/apis/apps"
17- "k8s.io/kubernetes/pkg/apis/batch"
18- kapi "k8s.io/kubernetes/pkg/apis/core"
19- "k8s.io/kubernetes/pkg/apis/extensions"
15+ coreapi "k8s.io/kubernetes/pkg/apis/core"
2016 "k8s.io/kubernetes/pkg/auth/nodeidentifier"
2117
22- oapps "github.com/openshift/api/apps"
23- "github.com/openshift/api/security"
24- "github.com/openshift/origin/pkg/api/imagereferencemutators"
25- "github.com/openshift/origin/pkg/api/legacy"
2618 configlatest "github.com/openshift/origin/pkg/cmd/server/apis/config/latest"
2719 "github.com/openshift/origin/pkg/scheduler/admission/apis/podnodeconstraints"
2820)
2921
3022const PluginName = "scheduling.openshift.io/PodNodeConstraints"
3123
32- // kindsToIgnore is a list of kinds that contain a PodSpec that
33- // we choose not to handle in this plugin
34- var kindsToIgnore = []schema.GroupKind {
35- extensions .Kind ("DaemonSet" ),
36- }
37-
3824func Register (plugins * admission.Plugins ) {
3925 plugins .Register (PluginName ,
4026 func (config io.Reader ) (admission.Interface , error ) {
@@ -81,11 +67,6 @@ func shouldCheckResource(resource schema.GroupResource, kind schema.GroupKind) (
8167 if ! shouldCheck {
8268 return false , nil
8369 }
84- for _ , ignore := range kindsToIgnore {
85- if ignore == expectedKind {
86- return false , nil
87- }
88- }
8970 if expectedKind != kind {
9071 return false , fmt .Errorf ("Unexpected resource kind %v for resource %v" , & kind , & resource )
9172 }
@@ -94,28 +75,7 @@ func shouldCheckResource(resource schema.GroupResource, kind schema.GroupKind) (
9475
9576// resourcesToCheck is a map of resources and corresponding kinds of things that we want handled in this plugin
9677var resourcesToCheck = map [schema.GroupResource ]schema.GroupKind {
97- kapi .Resource ("pods" ): kapi .Kind ("Pod" ),
98- kapi .Resource ("podtemplates" ): kapi .Kind ("PodTemplate" ),
99- kapi .Resource ("replicationcontrollers" ): kapi .Kind ("ReplicationController" ),
100- batch .Resource ("jobs" ): batch .Kind ("Job" ),
101- batch .Resource ("jobtemplates" ): batch .Kind ("JobTemplate" ),
102-
103- batch .Resource ("cronjobs" ): batch .Kind ("CronJob" ),
104- extensions .Resource ("deployments" ): extensions .Kind ("Deployment" ),
105- extensions .Resource ("replicasets" ): extensions .Kind ("ReplicaSet" ),
106- apps .Resource ("deployments" ): apps .Kind ("Deployment" ),
107- apps .Resource ("replicasets" ): apps .Kind ("ReplicaSet" ),
108- apps .Resource ("statefulsets" ): apps .Kind ("StatefulSet" ),
109-
110- legacy .Resource ("deploymentconfigs" ): legacy .Kind ("DeploymentConfig" ),
111- legacy .Resource ("podsecuritypolicysubjectreviews" ): legacy .Kind ("PodSecurityPolicySubjectReview" ),
112- legacy .Resource ("podsecuritypolicyselfsubjectreviews" ): legacy .Kind ("PodSecurityPolicySelfSubjectReview" ),
113- legacy .Resource ("podsecuritypolicyreviews" ): legacy .Kind ("PodSecurityPolicyReview" ),
114-
115- oapps .Resource ("deploymentconfigs" ): oapps .Kind ("DeploymentConfig" ),
116- security .Resource ("podsecuritypolicysubjectreviews" ): security .Kind ("PodSecurityPolicySubjectReview" ),
117- security .Resource ("podsecuritypolicyselfsubjectreviews" ): security .Kind ("PodSecurityPolicySelfSubjectReview" ),
118- security .Resource ("podsecuritypolicyreviews" ): security .Kind ("PodSecurityPolicyReview" ),
78+ coreapi .Resource ("pods" ): coreapi .Kind ("Pod" ),
11979}
12080
12181func readConfig (reader io.Reader ) (* podnodeconstraints.PodNodeConstraintsConfig , error ) {
@@ -151,28 +111,15 @@ func (o *podNodeConstraints) Validate(attr admission.Attributes) error {
151111 return nil
152112 }
153113 // Only check Create operation on pods
154- if attr .GetResource ().GroupResource () == kapi .Resource ("pods" ) && attr .GetOperation () != admission .Create {
114+ if attr .GetResource ().GroupResource () == coreapi .Resource ("pods" ) && attr .GetOperation () != admission .Create {
155115 return nil
156116 }
157- ps , err := o .getPodSpec (attr )
158- if err != nil {
159- return err
160- }
161117
162- return o .validatePodSpec (attr , ps )
163- }
164-
165- // extract the PodSpec from the pod templates for each object we care about
166- func (o * podNodeConstraints ) getPodSpec (attr admission.Attributes ) (kapi.PodSpec , error ) {
167- spec , _ , err := imagereferencemutators .GetPodSpec (attr .GetObject ())
168- if err != nil {
169- return kapi.PodSpec {}, kapierrors .NewInternalError (err )
170- }
171- return * spec , nil
118+ return o .validatePodSpec (attr , attr .GetObject ().(* coreapi.Pod ).Spec )
172119}
173120
174121// validate PodSpec if NodeName or NodeSelector are specified
175- func (o * podNodeConstraints ) validatePodSpec (attr admission.Attributes , ps kapi .PodSpec ) error {
122+ func (o * podNodeConstraints ) validatePodSpec (attr admission.Attributes , ps coreapi .PodSpec ) error {
176123 // a node creating a mirror pod that targets itself is allowed
177124 // see the NodeRestriction plugin for further details
178125 if o .isNodeSelfTargetWithMirrorPod (attr , ps .NodeName ) {
@@ -228,10 +175,10 @@ func (o *podNodeConstraints) checkPodsBindAccess(attr admission.Attributes) (boo
228175 Namespace : attr .GetNamespace (),
229176 Resource : "pods" ,
230177 Subresource : "binding" ,
231- APIGroup : kapi .GroupName ,
178+ APIGroup : coreapi .GroupName ,
232179 ResourceRequest : true ,
233180 }
234- if attr .GetResource ().GroupResource () == kapi .Resource ("pods" ) {
181+ if attr .GetResource ().GroupResource () == coreapi .Resource ("pods" ) {
235182 authzAttr .Name = attr .GetName ()
236183 }
237184 authorized , _ , err := o .authorizer .Authorize (authzAttr )
@@ -244,14 +191,14 @@ func (o *podNodeConstraints) isNodeSelfTargetWithMirrorPod(attr admission.Attrib
244191 return false
245192 }
246193 // this check specifically requires the object to be pod (unlike the other checks where we want any pod spec)
247- pod , ok := attr .GetObject ().(* kapi .Pod )
194+ pod , ok := attr .GetObject ().(* coreapi .Pod )
248195 if ! ok {
249196 return false
250197 }
251198 // note that anyone can create a mirror pod, but they are not privileged in any way
252199 // they are actually highly constrained since they cannot reference secrets
253200 // nodes can only create and delete them, and they will delete any "orphaned" mirror pods
254- if _ , isMirrorPod := pod .Annotations [kapi .MirrorPodAnnotationKey ]; ! isMirrorPod {
201+ if _ , isMirrorPod := pod .Annotations [coreapi .MirrorPodAnnotationKey ]; ! isMirrorPod {
255202 return false
256203 }
257204 // we are targeting a node with a mirror pod
0 commit comments