Skip to content

Commit

Permalink
make admission plugins configurable based on external criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed May 26, 2016
1 parent 44de311 commit a27d6ed
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion pkg/admission/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ limitations under the License.
package admission

import (
"bytes"
"io"
"io/ioutil"
"os"
"reflect"
"sort"
"sync"

"github.com/golang/glog"

clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
)

Expand All @@ -36,8 +40,16 @@ type Factory func(client clientset.Interface, config io.Reader) (Interface, erro
var (
pluginsMutex sync.Mutex
plugins = make(map[string]Factory)

// PluginEnabledFn checks whether a plugin is enabled. By default, if you ask about it, it's enabled.
PluginEnabledFn = func(name string, config io.Reader) bool {
return true
}
)

// PluginEnabledFunc is a function type that can provide an external check on whether an admission plugin may be enabled
type PluginEnabledFunc func(name string, config io.Reader) bool

// GetPlugins enumerates the names of all registered plugins.
func GetPlugins() []string {
pluginsMutex.Lock()
Expand Down Expand Up @@ -74,10 +86,33 @@ func getPlugin(name string, client clientset.Interface, config io.Reader) (Inter
if !found {
return nil, false, nil
}
ret, err := f(client, config)

config1, config2, err := splitStream(config)
if err != nil {
return nil, true, err
}
if !PluginEnabledFn(name, config1) {
return nil, true, nil
}

ret, err := f(client, config2)
return ret, true, err
}

// splitStream reads the stream bytes and constructs two copies of it.
func splitStream(config io.Reader) (io.Reader, io.Reader, error) {
if config == nil || reflect.ValueOf(config).IsNil() {
return nil, nil, nil
}

configBytes, err := ioutil.ReadAll(config)
if err != nil {
return nil, nil, err
}

return bytes.NewBuffer(configBytes), bytes.NewBuffer(configBytes), nil
}

// InitPlugin creates an instance of the named interface.
func InitPlugin(name string, client clientset.Interface, configFilePath string) Interface {
var (
Expand Down

0 comments on commit a27d6ed

Please sign in to comment.