k3s/pkg/registry/resourcequota/rest.go

97 lines
3.5 KiB
Go
Raw Normal View History

2015-01-23 17:38:30 +00:00
/*
Copyright 2014 Google Inc. All rights reserved.
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 resourcequota
import (
"fmt"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
2015-03-06 23:29:03 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
2015-01-23 17:38:30 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
2015-01-23 17:38:30 +00:00
)
// resourcequotaStrategy implements behavior for ResourceQuota objects
type resourcequotaStrategy struct {
runtime.ObjectTyper
api.NameGenerator
2015-01-23 17:38:30 +00:00
}
// Strategy is the default logic that applies when creating and updating ResourceQuota
// objects via the REST API.
var Strategy = resourcequotaStrategy{api.Scheme, api.SimpleNameGenerator}
2015-01-23 17:38:30 +00:00
// NamespaceScoped is true for resourcequotas.
func (resourcequotaStrategy) NamespaceScoped() bool {
return true
2015-01-23 17:38:30 +00:00
}
// PrepareForCreate clears fields that are not allowed to be set by end users on creation.
func (resourcequotaStrategy) PrepareForCreate(obj runtime.Object) {
resourcequota := obj.(*api.ResourceQuota)
resourcequota.Status = api.ResourceQuotaStatus{}
2015-01-23 17:38:30 +00:00
}
// Validate validates a new resourcequota.
func (resourcequotaStrategy) Validate(obj runtime.Object) fielderrors.ValidationErrorList {
resourcequota := obj.(*api.ResourceQuota)
return validation.ValidateResourceQuota(resourcequota)
2015-01-23 17:38:30 +00:00
}
// AllowCreateOnUpdate is false for resourcequotas.
func (resourcequotaStrategy) AllowCreateOnUpdate() bool {
return false
2015-01-23 17:38:30 +00:00
}
// ValidateUpdate is the default update validation for an end user.
func (resourcequotaStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList {
return validation.ValidateResourceQuotaUpdate(obj.(*api.ResourceQuota), old.(*api.ResourceQuota))
2015-01-23 17:38:30 +00:00
}
type resourcequotaStatusStrategy struct {
resourcequotaStrategy
2015-01-23 17:38:30 +00:00
}
var StatusStrategy = resourcequotaStatusStrategy{Strategy}
func (resourcequotaStatusStrategy) ValidateUpdate(obj, old runtime.Object) fielderrors.ValidationErrorList {
return validation.ValidateResourceQuotaStatusUpdate(obj.(*api.ResourceQuota), old.(*api.ResourceQuota))
2015-01-23 17:38:30 +00:00
}
// MatchResourceQuota returns a generic matcher for a given label and field selector.
func MatchResourceQuota(label labels.Selector, field fields.Selector) generic.Matcher {
return generic.MatcherFunc(func(obj runtime.Object) (bool, error) {
resourcequotaObj, ok := obj.(*api.ResourceQuota)
if !ok {
return false, fmt.Errorf("not a resourcequota")
}
fields := ResourceQuotaToSelectableFields(resourcequotaObj)
return label.Matches(labels.Set(resourcequotaObj.Labels)) && field.Matches(fields), nil
})
2015-01-23 17:38:30 +00:00
}
// ResourceQuotaToSelectableFields returns a label set that represents the object
// TODO: fields are not labels, and the validation rules for them do not apply.
func ResourceQuotaToSelectableFields(resourcequota *api.ResourceQuota) labels.Set {
return labels.Set{
"name": resourcequota.Name,
}
2015-01-23 17:38:30 +00:00
}