-
Notifications
You must be signed in to change notification settings - Fork 40.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30900 from jsafrane/pvc-admission
Automatic merge from submit-queue Add admission controller for default storage class. The admission controller adds a default class to PVCs that do not require any specific class. This way, users (=PVC authors) do not need to care about storage classes, administrator can configure a default one and all these PVCs that do not care about class will get the default one. The marker of default class is annotation "volume.beta.kubernetes.io/storage-class", which must be set to "true" to work. All other values (or missing annotation) makes the class non-default. Based on @thockin's code, added tests and made it not to reject a PVC when no class is marked as default. . @kubernetes/sig-storage
- Loading branch information
Showing
21 changed files
with
423 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package admission | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/golang/glog" | ||
|
||
admission "k8s.io/kubernetes/pkg/admission" | ||
api "k8s.io/kubernetes/pkg/api" | ||
"k8s.io/kubernetes/pkg/api/errors" | ||
"k8s.io/kubernetes/pkg/apis/extensions" | ||
"k8s.io/kubernetes/pkg/client/cache" | ||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" | ||
"k8s.io/kubernetes/pkg/runtime" | ||
"k8s.io/kubernetes/pkg/watch" | ||
) | ||
|
||
const ( | ||
PluginName = "DefaultStorageClass" | ||
) | ||
|
||
func init() { | ||
admission.RegisterPlugin(PluginName, func(client clientset.Interface, config io.Reader) (admission.Interface, error) { | ||
plugin := newPlugin(client) | ||
plugin.Run() | ||
return plugin, nil | ||
}) | ||
} | ||
|
||
// claimDefaulterPlugin holds state for and implements the admission plugin. | ||
type claimDefaulterPlugin struct { | ||
*admission.Handler | ||
client clientset.Interface | ||
|
||
reflector *cache.Reflector | ||
stopChan chan struct{} | ||
store cache.Store | ||
} | ||
|
||
var _ admission.Interface = &claimDefaulterPlugin{} | ||
|
||
// newPlugin creates a new admission plugin. | ||
func newPlugin(kclient clientset.Interface) *claimDefaulterPlugin { | ||
store := cache.NewStore(cache.MetaNamespaceKeyFunc) | ||
reflector := cache.NewReflector( | ||
&cache.ListWatch{ | ||
ListFunc: func(options api.ListOptions) (runtime.Object, error) { | ||
return kclient.Extensions().StorageClasses().List(options) | ||
}, | ||
WatchFunc: func(options api.ListOptions) (watch.Interface, error) { | ||
return kclient.Extensions().StorageClasses().Watch(options) | ||
}, | ||
}, | ||
&extensions.StorageClass{}, | ||
store, | ||
0, | ||
) | ||
|
||
return &claimDefaulterPlugin{ | ||
Handler: admission.NewHandler(admission.Create), | ||
client: kclient, | ||
store: store, | ||
reflector: reflector, | ||
} | ||
} | ||
|
||
func (a *claimDefaulterPlugin) Run() { | ||
if a.stopChan == nil { | ||
a.stopChan = make(chan struct{}) | ||
} | ||
a.reflector.RunUntil(a.stopChan) | ||
} | ||
func (a *claimDefaulterPlugin) Stop() { | ||
if a.stopChan != nil { | ||
close(a.stopChan) | ||
a.stopChan = nil | ||
} | ||
} | ||
|
||
// This is a stand-in until we have a real field. This string should be a const somewhere. | ||
const classAnnotation = "volume.beta.kubernetes.io/storage-class" | ||
|
||
// This indicates that a particular StorageClass nominates itself as the system default. | ||
const isDefaultAnnotation = "storageclass.beta.kubernetes.io/is-default-class" | ||
|
||
// Admit sets the default value of a PersistentVolumeClaim's storage class, in case the user did | ||
// not provide a value. | ||
// | ||
// 1. Find available StorageClasses. | ||
// 2. Figure which is the default | ||
// 3. Write to the PVClaim | ||
func (c *claimDefaulterPlugin) Admit(a admission.Attributes) error { | ||
if a.GetResource().GroupResource() != api.Resource("persistentvolumeclaims") { | ||
return nil | ||
} | ||
|
||
if len(a.GetSubresource()) != 0 { | ||
return nil | ||
} | ||
|
||
pvc, ok := a.GetObject().(*api.PersistentVolumeClaim) | ||
// if we can't convert then we don't handle this object so just return | ||
if !ok { | ||
return nil | ||
} | ||
|
||
_, found := pvc.Annotations[classAnnotation] | ||
if found { | ||
// The user asked for a class. | ||
return nil | ||
} | ||
|
||
glog.V(4).Infof("no storage class for claim %s (generate: %s)", pvc.Name, pvc.GenerateName) | ||
|
||
def, err := getDefaultClass(c.store) | ||
if err != nil { | ||
return admission.NewForbidden(a, err) | ||
} | ||
if def == nil { | ||
// No default class selected, do nothing about the PVC. | ||
return nil | ||
} | ||
|
||
glog.V(4).Infof("defaulting storage class for claim %s (generate: %s) to %s", pvc.Name, pvc.GenerateName, def.Name) | ||
if pvc.ObjectMeta.Annotations == nil { | ||
pvc.ObjectMeta.Annotations = map[string]string{} | ||
} | ||
pvc.Annotations[classAnnotation] = def.Name | ||
return nil | ||
} | ||
|
||
// getDefaultClass returns the default StorageClass from the store, or nil. | ||
func getDefaultClass(store cache.Store) (*extensions.StorageClass, error) { | ||
defaultClasses := []*extensions.StorageClass{} | ||
for _, c := range store.List() { | ||
class, ok := c.(*extensions.StorageClass) | ||
if !ok { | ||
return nil, errors.NewInternalError(fmt.Errorf("error converting stored object to StorageClass: %v", c)) | ||
} | ||
if class.Annotations[isDefaultAnnotation] == "true" { | ||
defaultClasses = append(defaultClasses, class) | ||
glog.V(4).Infof("getDefaultClass added: %s", class.Name) | ||
} | ||
} | ||
|
||
if len(defaultClasses) == 0 { | ||
return nil, nil | ||
} | ||
if len(defaultClasses) > 1 { | ||
glog.V(4).Infof("getDefaultClass %s defaults found", len(defaultClasses)) | ||
return nil, errors.NewInternalError(fmt.Errorf("%d default StorageClasses were found", len(defaultClasses))) | ||
} | ||
return defaultClasses[0], nil | ||
} |
Oops, something went wrong.