2015-01-09 23:53:06 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2015-01-09 23:53:06 +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 kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2015-03-25 21:51:58 +00:00
|
|
|
"time"
|
2015-01-09 23:53:06 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-15 00:04:13 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/errors"
|
2015-10-09 22:04:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2015-08-13 19:01:50 +00:00
|
|
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/wait"
|
2015-01-09 23:53:06 +00:00
|
|
|
)
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
// ScalePrecondition describes a condition that must be true for the scale to take place
|
2015-01-09 23:53:06 +00:00
|
|
|
// If CurrentSize == -1, it is ignored.
|
|
|
|
// If CurrentResourceVersion is the empty string, it is ignored.
|
|
|
|
// Otherwise they must equal the values in the replication controller for it to be valid.
|
2015-05-21 21:10:25 +00:00
|
|
|
type ScalePrecondition struct {
|
2015-01-09 23:53:06 +00:00
|
|
|
Size int
|
|
|
|
ResourceVersion string
|
|
|
|
}
|
|
|
|
|
2015-03-01 02:40:57 +00:00
|
|
|
// A PreconditionError is returned when a replication controller fails to match
|
2015-05-21 21:10:25 +00:00
|
|
|
// the scale preconditions passed to kubectl.
|
2015-01-09 23:53:06 +00:00
|
|
|
type PreconditionError struct {
|
|
|
|
Precondition string
|
|
|
|
ExpectedValue string
|
|
|
|
ActualValue string
|
|
|
|
}
|
|
|
|
|
2015-03-01 02:40:57 +00:00
|
|
|
func (pe PreconditionError) Error() string {
|
2015-01-09 23:53:06 +00:00
|
|
|
return fmt.Sprintf("Expected %s to be %s, was %s", pe.Precondition, pe.ExpectedValue, pe.ActualValue)
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
type ControllerScaleErrorType int
|
2015-03-01 02:40:57 +00:00
|
|
|
|
|
|
|
const (
|
2015-05-21 21:10:25 +00:00
|
|
|
ControllerScaleGetFailure ControllerScaleErrorType = iota
|
|
|
|
ControllerScaleUpdateFailure
|
2015-09-15 00:04:13 +00:00
|
|
|
ControllerScaleUpdateInvalidFailure
|
2015-03-01 02:40:57 +00:00
|
|
|
)
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
// A ControllerScaleError is returned when a scale request passes
|
|
|
|
// preconditions but fails to actually scale the controller.
|
|
|
|
type ControllerScaleError struct {
|
|
|
|
FailureType ControllerScaleErrorType
|
2015-03-01 02:40:57 +00:00
|
|
|
ResourceVersion string
|
|
|
|
ActualError error
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
func (c ControllerScaleError) Error() string {
|
2015-03-01 02:40:57 +00:00
|
|
|
return fmt.Sprintf(
|
2015-05-21 21:10:25 +00:00
|
|
|
"Scaling the controller failed with: %s; Current resource version %s",
|
2015-03-01 02:40:57 +00:00
|
|
|
c.ActualError, c.ResourceVersion)
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:32:59 +00:00
|
|
|
// ValidateReplicationController ensures that the preconditions match. Returns nil if they are valid, an error otherwise
|
|
|
|
func (precondition *ScalePrecondition) ValidateReplicationController(controller *api.ReplicationController) error {
|
2015-01-09 23:53:06 +00:00
|
|
|
if precondition.Size != -1 && controller.Spec.Replicas != precondition.Size {
|
2015-03-01 02:40:57 +00:00
|
|
|
return PreconditionError{"replicas", strconv.Itoa(precondition.Size), strconv.Itoa(controller.Spec.Replicas)}
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
if precondition.ResourceVersion != "" && controller.ResourceVersion != precondition.ResourceVersion {
|
2015-03-01 02:40:57 +00:00
|
|
|
return PreconditionError{"resource version", precondition.ResourceVersion, controller.ResourceVersion}
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:32:59 +00:00
|
|
|
// ValidateJob ensures that the preconditions match. Returns nil if they are valid, an error otherwise
|
2015-10-09 22:49:10 +00:00
|
|
|
func (precondition *ScalePrecondition) ValidateJob(job *extensions.Job) error {
|
2015-09-16 15:32:59 +00:00
|
|
|
if precondition.Size != -1 && job.Spec.Parallelism == nil {
|
|
|
|
return PreconditionError{"parallelism", strconv.Itoa(precondition.Size), "nil"}
|
|
|
|
}
|
|
|
|
if precondition.Size != -1 && *job.Spec.Parallelism != precondition.Size {
|
|
|
|
return PreconditionError{"parallelism", strconv.Itoa(precondition.Size), strconv.Itoa(*job.Spec.Parallelism)}
|
|
|
|
}
|
|
|
|
if precondition.ResourceVersion != "" && job.ResourceVersion != precondition.ResourceVersion {
|
|
|
|
return PreconditionError{"resource version", precondition.ResourceVersion, job.ResourceVersion}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
type Scaler interface {
|
|
|
|
// Scale scales the named resource after checking preconditions. It optionally
|
2015-03-25 21:51:58 +00:00
|
|
|
// retries in the event of resource version mismatch (if retry is not nil),
|
|
|
|
// and optionally waits until the status of the resource matches newSize (if wait is not nil)
|
2015-05-21 21:10:25 +00:00
|
|
|
Scale(namespace, name string, newSize uint, preconditions *ScalePrecondition, retry, wait *RetryParams) error
|
|
|
|
// ScaleSimple does a simple one-shot attempt at scaling - not useful on it's own, but
|
|
|
|
// a necessary building block for Scale
|
2015-09-16 15:32:59 +00:00
|
|
|
ScaleSimple(namespace, name string, preconditions *ScalePrecondition, newSize uint) error
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 15:32:59 +00:00
|
|
|
func ScalerFor(kind string, c client.Interface) (Scaler, error) {
|
2015-01-09 23:53:06 +00:00
|
|
|
switch kind {
|
|
|
|
case "ReplicationController":
|
2015-05-21 21:10:25 +00:00
|
|
|
return &ReplicationControllerScaler{c}, nil
|
2015-09-16 15:32:59 +00:00
|
|
|
case "Job":
|
|
|
|
return &JobScaler{c}, nil
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
2015-05-21 21:10:25 +00:00
|
|
|
return nil, fmt.Errorf("no scaler has been implemented for %q", kind)
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
type ReplicationControllerScaler struct {
|
2015-09-16 15:32:59 +00:00
|
|
|
c client.Interface
|
|
|
|
}
|
|
|
|
type JobScaler struct {
|
|
|
|
c client.Interface
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
// RetryParams encapsulates the retry parameters used by kubectl's scaler.
|
2015-03-25 21:51:58 +00:00
|
|
|
type RetryParams struct {
|
2015-05-11 15:31:08 +00:00
|
|
|
Interval, Timeout time.Duration
|
2015-03-25 21:51:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 14:48:50 +00:00
|
|
|
func NewRetryParams(interval, timeout time.Duration) *RetryParams {
|
|
|
|
return &RetryParams{interval, timeout}
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
// ScaleCondition is a closure around Scale that facilitates retries via util.wait
|
|
|
|
func ScaleCondition(r Scaler, precondition *ScalePrecondition, namespace, name string, count uint) wait.ConditionFunc {
|
2015-03-01 02:40:57 +00:00
|
|
|
return func() (bool, error) {
|
2015-09-16 15:32:59 +00:00
|
|
|
err := r.ScaleSimple(namespace, name, precondition, count)
|
2015-05-21 21:10:25 +00:00
|
|
|
switch e, _ := err.(ControllerScaleError); err.(type) {
|
2015-03-01 02:40:57 +00:00
|
|
|
case nil:
|
|
|
|
return true, nil
|
2015-05-21 21:10:25 +00:00
|
|
|
case ControllerScaleError:
|
2015-09-15 00:04:13 +00:00
|
|
|
// if it's invalid we shouldn't keep waiting
|
|
|
|
if e.FailureType == ControllerScaleUpdateInvalidFailure {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-05-21 21:10:25 +00:00
|
|
|
if e.FailureType == ControllerScaleUpdateFailure {
|
2015-03-01 02:40:57 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 15:32:59 +00:00
|
|
|
func (scaler *ReplicationControllerScaler) ScaleSimple(namespace, name string, preconditions *ScalePrecondition, newSize uint) error {
|
|
|
|
controller, err := scaler.c.ReplicationControllers(namespace).Get(name)
|
2015-01-09 23:53:06 +00:00
|
|
|
if err != nil {
|
2015-09-16 15:32:59 +00:00
|
|
|
return ControllerScaleError{ControllerScaleGetFailure, "Unknown", err}
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
if preconditions != nil {
|
2015-09-16 15:32:59 +00:00
|
|
|
if err := preconditions.ValidateReplicationController(controller); err != nil {
|
|
|
|
return err
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
controller.Spec.Replicas = int(newSize)
|
|
|
|
// TODO: do retry on 409 errors here?
|
2015-09-16 15:32:59 +00:00
|
|
|
if _, err := scaler.c.ReplicationControllers(namespace).Update(controller); err != nil {
|
2015-09-15 00:04:13 +00:00
|
|
|
if errors.IsInvalid(err) {
|
2015-09-16 15:32:59 +00:00
|
|
|
return ControllerScaleError{ControllerScaleUpdateInvalidFailure, controller.ResourceVersion, err}
|
2015-09-15 00:04:13 +00:00
|
|
|
}
|
2015-09-16 15:32:59 +00:00
|
|
|
return ControllerScaleError{ControllerScaleUpdateFailure, controller.ResourceVersion, err}
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
|
|
|
// TODO: do a better job of printing objects here.
|
2015-09-16 15:32:59 +00:00
|
|
|
return nil
|
2015-01-09 23:53:06 +00:00
|
|
|
}
|
2015-03-25 21:51:58 +00:00
|
|
|
|
2015-05-21 21:10:25 +00:00
|
|
|
// Scale updates a ReplicationController to a new size, with optional precondition check (if preconditions is not nil),
|
2015-03-25 21:51:58 +00:00
|
|
|
// optional retries (if retry is not nil), and then optionally waits for it's replica count to reach the new value
|
|
|
|
// (if wait is not nil).
|
2015-05-21 21:10:25 +00:00
|
|
|
func (scaler *ReplicationControllerScaler) Scale(namespace, name string, newSize uint, preconditions *ScalePrecondition, retry, waitForReplicas *RetryParams) error {
|
2015-03-25 21:51:58 +00:00
|
|
|
if preconditions == nil {
|
2015-05-21 21:10:25 +00:00
|
|
|
preconditions = &ScalePrecondition{-1, ""}
|
2015-03-25 21:51:58 +00:00
|
|
|
}
|
|
|
|
if retry == nil {
|
|
|
|
// Make it try only once, immediately
|
2015-05-11 15:31:08 +00:00
|
|
|
retry = &RetryParams{Interval: time.Millisecond, Timeout: time.Millisecond}
|
2015-03-25 21:51:58 +00:00
|
|
|
}
|
2015-05-21 21:10:25 +00:00
|
|
|
cond := ScaleCondition(scaler, preconditions, namespace, name, newSize)
|
2015-05-11 15:31:08 +00:00
|
|
|
if err := wait.Poll(retry.Interval, retry.Timeout, cond); err != nil {
|
2015-03-25 21:51:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if waitForReplicas != nil {
|
2015-09-16 15:32:59 +00:00
|
|
|
rc, err := scaler.c.ReplicationControllers(namespace).Get(name)
|
2015-06-18 19:00:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-11 15:31:08 +00:00
|
|
|
return wait.Poll(waitForReplicas.Interval, waitForReplicas.Timeout,
|
2015-09-16 15:32:59 +00:00
|
|
|
client.ControllerHasDesiredReplicas(scaler.c, rc))
|
2015-03-25 21:51:58 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-15 18:28:59 +00:00
|
|
|
|
2015-09-16 15:32:59 +00:00
|
|
|
// ScaleSimple is responsible for updating job's parallelism.
|
|
|
|
func (scaler *JobScaler) ScaleSimple(namespace, name string, preconditions *ScalePrecondition, newSize uint) error {
|
|
|
|
job, err := scaler.c.Experimental().Jobs(namespace).Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return ControllerScaleError{ControllerScaleGetFailure, "Unknown", err}
|
|
|
|
}
|
|
|
|
if preconditions != nil {
|
|
|
|
if err := preconditions.ValidateJob(job); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parallelism := int(newSize)
|
|
|
|
job.Spec.Parallelism = ¶llelism
|
|
|
|
if _, err := scaler.c.Experimental().Jobs(namespace).Update(job); err != nil {
|
|
|
|
if errors.IsInvalid(err) {
|
|
|
|
return ControllerScaleError{ControllerScaleUpdateInvalidFailure, job.ResourceVersion, err}
|
|
|
|
}
|
|
|
|
return ControllerScaleError{ControllerScaleUpdateFailure, job.ResourceVersion, err}
|
2015-04-15 18:28:59 +00:00
|
|
|
|
2015-09-16 15:32:59 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-04-15 20:50:08 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 15:32:59 +00:00
|
|
|
// Scale updates a Job to a new size, with optional precondition check (if preconditions is not nil),
|
|
|
|
// optional retries (if retry is not nil), and then optionally waits for parallelism to reach desired
|
|
|
|
// number, which can be less than requested based on job's current progress.
|
|
|
|
func (scaler *JobScaler) Scale(namespace, name string, newSize uint, preconditions *ScalePrecondition, retry, waitForReplicas *RetryParams) error {
|
|
|
|
if preconditions == nil {
|
|
|
|
preconditions = &ScalePrecondition{-1, ""}
|
|
|
|
}
|
|
|
|
if retry == nil {
|
|
|
|
// Make it try only once, immediately
|
|
|
|
retry = &RetryParams{Interval: time.Millisecond, Timeout: time.Millisecond}
|
|
|
|
}
|
|
|
|
cond := ScaleCondition(scaler, preconditions, namespace, name, newSize)
|
|
|
|
if err := wait.Poll(retry.Interval, retry.Timeout, cond); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if waitForReplicas != nil {
|
|
|
|
job, err := scaler.c.Experimental().Jobs(namespace).Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return wait.Poll(waitForReplicas.Interval, waitForReplicas.Timeout,
|
|
|
|
client.JobHasDesiredParallelism(scaler.c, job))
|
|
|
|
}
|
|
|
|
return nil
|
2015-04-15 18:28:59 +00:00
|
|
|
}
|