1
1
package admission
2
2
3
3
import (
4
+ "bytes"
5
+ "io"
6
+ "io/ioutil"
4
7
"net"
5
8
"reflect"
6
9
"strings"
@@ -16,6 +19,7 @@ import (
16
19
17
20
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
18
21
configapi "github.com/openshift/origin/pkg/cmd/server/api"
22
+ configlatest "github.com/openshift/origin/pkg/cmd/server/api/latest"
19
23
"github.com/openshift/origin/pkg/cmd/util/pluginconfig"
20
24
imageadmission "github.com/openshift/origin/pkg/image/admission"
21
25
imagepolicy "github.com/openshift/origin/pkg/image/admission/imagepolicy/api"
@@ -351,3 +355,50 @@ func dedupe(input []string) []string {
351
355
}
352
356
return result
353
357
}
358
+
359
+ func init () {
360
+ // add a filter that will remove DefaultAdmissionConfig
361
+ admission .FactoryFilterFn = filterEnableAdmissionConfigs
362
+ }
363
+
364
+ func filterEnableAdmissionConfigs (delegate admission.Factory ) admission.Factory {
365
+ return func (config io.Reader ) (admission.Interface , error ) {
366
+ config1 , config2 , err := splitStream (config )
367
+ if err != nil {
368
+ return nil , err
369
+ }
370
+ // if the config isn't a DefaultAdmissionConfig, then assume we're enabled (we were called after all)
371
+ // if the config *is* a DefaultAdmissionConfig and it explicitly said
372
+ obj , err := configlatest .ReadYAML (config1 )
373
+ // if we can't read it, let the plugin deal with it
374
+ if err != nil {
375
+ return delegate (config2 )
376
+ }
377
+ // if nothing was there, let the plugin deal with it
378
+ if obj == nil {
379
+ return delegate (config2 )
380
+ }
381
+ // if it wasn't a DefaultAdmissionConfig object, let the plugin deal with it
382
+ if _ , ok := obj .(* configapi.DefaultAdmissionConfig ); ! ok {
383
+ return delegate (config2 )
384
+ }
385
+
386
+ // if it was a DefaultAdmissionConfig, then it must have said "enabled" and it wasn't really meant for the
387
+ // admission plugin
388
+ return delegate (nil )
389
+ }
390
+ }
391
+
392
+ // splitStream reads the stream bytes and constructs two copies of it.
393
+ func splitStream (config io.Reader ) (io.Reader , io.Reader , error ) {
394
+ if config == nil || reflect .ValueOf (config ).IsNil () {
395
+ return nil , nil , nil
396
+ }
397
+
398
+ configBytes , err := ioutil .ReadAll (config )
399
+ if err != nil {
400
+ return nil , nil , err
401
+ }
402
+
403
+ return bytes .NewBuffer (configBytes ), bytes .NewBuffer (configBytes ), nil
404
+ }
0 commit comments