2015-01-22 21:52:40 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-01-22 21:52:40 +00:00
|
|
|
|
|
|
|
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 limitrange
|
|
|
|
|
|
|
|
import (
|
2015-08-31 12:11:11 +00:00
|
|
|
"fmt"
|
|
|
|
|
2017-01-19 14:50:16 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-01-24 14:35:22 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/uuid"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
2017-01-17 10:38:25 +00:00
|
|
|
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
2017-01-31 16:47:19 +00:00
|
|
|
"k8s.io/apiserver/pkg/storage"
|
2017-01-13 19:56:52 +00:00
|
|
|
"k8s.io/apiserver/pkg/storage/names"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/validation"
|
2015-01-22 21:52:40 +00:00
|
|
|
)
|
|
|
|
|
2015-08-11 07:05:40 +00:00
|
|
|
type limitrangeStrategy struct {
|
|
|
|
runtime.ObjectTyper
|
2017-01-13 19:56:52 +00:00
|
|
|
names.NameGenerator
|
2015-01-22 21:52:40 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 07:05:40 +00:00
|
|
|
// Strategy is the default logic that applies when creating and updating
|
|
|
|
// LimitRange objects via the REST API.
|
2017-01-13 19:56:52 +00:00
|
|
|
var Strategy = limitrangeStrategy{api.Scheme, names.SimpleNameGenerator}
|
2015-01-22 21:52:40 +00:00
|
|
|
|
2015-08-11 07:05:40 +00:00
|
|
|
func (limitrangeStrategy) NamespaceScoped() bool {
|
|
|
|
return true
|
|
|
|
}
|
2015-01-22 21:52:40 +00:00
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (limitrangeStrategy) PrepareForCreate(ctx genericapirequest.Context, obj runtime.Object) {
|
2015-08-11 07:05:40 +00:00
|
|
|
limitRange := obj.(*api.LimitRange)
|
2015-01-22 21:52:40 +00:00
|
|
|
if len(limitRange.Name) == 0 {
|
2016-07-26 15:13:18 +00:00
|
|
|
limitRange.Name = string(uuid.NewUUID())
|
2015-01-22 21:52:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (limitrangeStrategy) PrepareForUpdate(ctx genericapirequest.Context, obj, old runtime.Object) {
|
2015-01-22 21:52:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (limitrangeStrategy) Validate(ctx genericapirequest.Context, obj runtime.Object) field.ErrorList {
|
2015-08-11 07:05:40 +00:00
|
|
|
limitRange := obj.(*api.LimitRange)
|
|
|
|
return validation.ValidateLimitRange(limitRange)
|
2015-01-22 21:52:40 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 05:13:16 +00:00
|
|
|
// Canonicalize normalizes the object after validation.
|
|
|
|
func (limitrangeStrategy) Canonicalize(obj runtime.Object) {
|
|
|
|
}
|
|
|
|
|
2015-08-11 07:05:40 +00:00
|
|
|
func (limitrangeStrategy) AllowCreateOnUpdate() bool {
|
|
|
|
return true
|
2015-01-22 21:52:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (limitrangeStrategy) ValidateUpdate(ctx genericapirequest.Context, obj, old runtime.Object) field.ErrorList {
|
2015-08-11 07:05:40 +00:00
|
|
|
limitRange := obj.(*api.LimitRange)
|
|
|
|
return validation.ValidateLimitRange(limitRange)
|
2015-01-22 21:52:40 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 07:05:40 +00:00
|
|
|
func (limitrangeStrategy) AllowUnconditionalUpdate() bool {
|
|
|
|
return true
|
2015-01-22 21:52:40 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 12:11:11 +00:00
|
|
|
func LimitRangeToSelectableFields(limitRange *api.LimitRange) fields.Set {
|
2016-08-15 18:41:25 +00:00
|
|
|
return nil
|
2015-01-22 21:52:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (limitrangeStrategy) Export(genericapirequest.Context, runtime.Object, bool) error {
|
2015-12-01 23:45:29 +00:00
|
|
|
// Copied from OpenShift exporter
|
|
|
|
// TODO: this needs to be fixed
|
2016-08-08 20:15:33 +00:00
|
|
|
// limitrange.Strategy.PrepareForCreate(ctx, obj)
|
2015-12-01 23:45:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:19:55 +00:00
|
|
|
// GetAttrs returns labels and fields of a given object for filtering purposes.
|
|
|
|
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
|
|
|
|
lr, ok := obj.(*api.LimitRange)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("given object is not a limit range.")
|
|
|
|
}
|
|
|
|
return labels.Set(lr.ObjectMeta.Labels), LimitRangeToSelectableFields(lr), nil
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:41:21 +00:00
|
|
|
func MatchLimitRange(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
|
|
|
|
return storage.SelectionPredicate{
|
2016-11-16 09:19:55 +00:00
|
|
|
Label: label,
|
|
|
|
Field: field,
|
|
|
|
GetAttrs: GetAttrs,
|
2015-08-31 12:11:11 +00:00
|
|
|
}
|
2015-01-22 21:52:40 +00:00
|
|
|
}
|