2014-06-06 23:40:48 +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.
|
|
|
|
*/
|
2014-06-12 20:09:41 +00:00
|
|
|
|
2014-08-11 07:34:59 +00:00
|
|
|
package controller
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
import (
|
2014-06-25 23:23:15 +00:00
|
|
|
"fmt"
|
2015-03-30 17:51:33 +00:00
|
|
|
"strconv"
|
2014-06-25 23:23:15 +00:00
|
|
|
|
2014-06-12 20:17:34 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-09-01 05:10:49 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
|
2015-03-06 23:29:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
2014-06-17 01:17:08 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2015-03-16 04:36:26 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
|
2014-09-06 02:22:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2015-03-22 21:40:47 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2015-03-06 06:12:52 +00:00
|
|
|
// rcStrategy implements verification logic for Replication Controllers.
|
|
|
|
type rcStrategy struct {
|
|
|
|
runtime.ObjectTyper
|
|
|
|
api.NameGenerator
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strategy is the default logic that applies when creating and updating Replication Controller objects.
|
|
|
|
var Strategy = rcStrategy{api.Scheme, api.SimpleNameGenerator}
|
|
|
|
|
|
|
|
// NamespaceScoped returns true because all Replication Controllers need to be within a namespace.
|
|
|
|
func (rcStrategy) NamespaceScoped() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-03-25 21:45:07 +00:00
|
|
|
// PrepareForCreate clears the status of a replication controller before creation.
|
|
|
|
func (rcStrategy) PrepareForCreate(obj runtime.Object) {
|
2015-03-06 06:12:52 +00:00
|
|
|
controller := obj.(*api.ReplicationController)
|
|
|
|
controller.Status = api.ReplicationControllerStatus{}
|
|
|
|
}
|
|
|
|
|
2015-03-26 00:03:30 +00:00
|
|
|
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
|
|
|
|
func (rcStrategy) PrepareForUpdate(obj, old runtime.Object) {
|
|
|
|
// TODO: once RC has a status sub-resource we can enable this.
|
|
|
|
//newController := obj.(*api.ReplicationController)
|
|
|
|
//oldController := old.(*api.ReplicationController)
|
|
|
|
//newController.Status = oldController.Status
|
|
|
|
}
|
|
|
|
|
2015-03-06 06:12:52 +00:00
|
|
|
// Validate validates a new replication controller.
|
2015-03-30 17:51:33 +00:00
|
|
|
func (rcStrategy) Validate(ctx api.Context, obj runtime.Object) fielderrors.ValidationErrorList {
|
2015-03-06 06:12:52 +00:00
|
|
|
controller := obj.(*api.ReplicationController)
|
|
|
|
return validation.ValidateReplicationController(controller)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllowCreateOnUpdate is false for replication controllers; this means a POST is
|
|
|
|
// needed to create one.
|
|
|
|
func (rcStrategy) AllowCreateOnUpdate() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateUpdate is the default update validation for an end user.
|
2015-03-30 17:51:33 +00:00
|
|
|
func (rcStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) fielderrors.ValidationErrorList {
|
2015-03-06 06:12:52 +00:00
|
|
|
return validation.ValidateReplicationControllerUpdate(old.(*api.ReplicationController), obj.(*api.ReplicationController))
|
|
|
|
}
|
|
|
|
|
2015-03-25 02:57:04 +00:00
|
|
|
// ControllerToSelectableFields returns a label set that represents the object.
|
|
|
|
func ControllerToSelectableFields(controller *api.ReplicationController) labels.Set {
|
|
|
|
return labels.Set{
|
|
|
|
"name": controller.Name,
|
|
|
|
"status.replicas": strconv.Itoa(controller.Status.Replicas),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 04:36:26 +00:00
|
|
|
// MatchController is the filter used by the generic etcd backend to route
|
|
|
|
// watch events from etcd to clients of the apiserver only interested in specific
|
|
|
|
// labels/fields.
|
|
|
|
func MatchController(label labels.Selector, field fields.Selector) generic.Matcher {
|
|
|
|
return generic.MatcherFunc(
|
|
|
|
func(obj runtime.Object) (bool, error) {
|
|
|
|
controllerObj, ok := obj.(*api.ReplicationController)
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("Given object is not a replication controller.")
|
|
|
|
}
|
2015-03-25 02:57:04 +00:00
|
|
|
fields := ControllerToSelectableFields(controllerObj)
|
|
|
|
return label.Matches(labels.Set(controllerObj.Labels)) && field.Matches(fields), nil
|
2015-03-16 04:36:26 +00:00
|
|
|
})
|
2014-06-26 19:43:08 +00:00
|
|
|
}
|