2015-01-22 17:46:38 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2015-01-22 17:46:38 +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"
|
|
|
|
"time"
|
|
|
|
|
2015-04-28 12:21:57 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-01-22 17:46:38 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-03-07 06:30:56 +00:00
|
|
|
shortInterval = time.Millisecond * 100
|
|
|
|
interval = time.Second * 3
|
|
|
|
timeout = time.Minute * 5
|
2015-01-22 17:46:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Reaper handles terminating an object as gracefully as possible.
|
|
|
|
type Reaper interface {
|
2015-04-28 12:21:57 +00:00
|
|
|
Stop(namespace, name string, gracePeriod *api.DeleteOptions) (string, error)
|
2015-01-22 17:46:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 04:15:15 +00:00
|
|
|
type NoSuchReaperError struct {
|
|
|
|
kind string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NoSuchReaperError) Error() string {
|
|
|
|
return fmt.Sprintf("no reaper has been implemented for %q", n.kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsNoSuchReaperError(err error) bool {
|
|
|
|
_, ok := err.(*NoSuchReaperError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-01-22 17:46:38 +00:00
|
|
|
func ReaperFor(kind string, c client.Interface) (Reaper, error) {
|
|
|
|
switch kind {
|
|
|
|
case "ReplicationController":
|
|
|
|
return &ReplicationControllerReaper{c, interval, timeout}, nil
|
|
|
|
case "Pod":
|
|
|
|
return &PodReaper{c}, nil
|
|
|
|
case "Service":
|
|
|
|
return &ServiceReaper{c}, nil
|
|
|
|
}
|
2015-04-23 04:15:15 +00:00
|
|
|
return nil, &NoSuchReaperError{kind}
|
2015-01-22 17:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ReplicationControllerReaper struct {
|
|
|
|
client.Interface
|
|
|
|
pollInterval, timeout time.Duration
|
|
|
|
}
|
|
|
|
type PodReaper struct {
|
|
|
|
client.Interface
|
|
|
|
}
|
|
|
|
type ServiceReaper struct {
|
|
|
|
client.Interface
|
|
|
|
}
|
|
|
|
|
|
|
|
type objInterface interface {
|
|
|
|
Delete(name string) error
|
|
|
|
Get(name string) (meta.Interface, error)
|
|
|
|
}
|
|
|
|
|
2015-04-28 12:21:57 +00:00
|
|
|
func (reaper *ReplicationControllerReaper) Stop(namespace, name string, gracePeriod *api.DeleteOptions) (string, error) {
|
2015-01-22 17:46:38 +00:00
|
|
|
rc := reaper.ReplicationControllers(namespace)
|
2015-04-15 20:50:08 +00:00
|
|
|
resizer, err := ResizerFor("ReplicationController", NewResizerClient(*reaper))
|
2015-03-07 06:30:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-03-25 21:51:58 +00:00
|
|
|
retry := &RetryParams{shortInterval, reaper.timeout}
|
|
|
|
waitForReplicas := &RetryParams{reaper.pollInterval, reaper.timeout}
|
|
|
|
err = resizer.Resize(namespace, name, 0, nil, retry, waitForReplicas)
|
2015-01-22 17:46:38 +00:00
|
|
|
if err := rc.Delete(name); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s stopped", name), nil
|
|
|
|
}
|
|
|
|
|
2015-04-28 12:21:57 +00:00
|
|
|
func (reaper *PodReaper) Stop(namespace, name string, gracePeriod *api.DeleteOptions) (string, error) {
|
2015-01-22 17:46:38 +00:00
|
|
|
pods := reaper.Pods(namespace)
|
|
|
|
_, err := pods.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-04-28 12:21:57 +00:00
|
|
|
if err := pods.Delete(name, gracePeriod); err != nil {
|
2015-01-22 17:46:38 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2015-04-28 12:21:57 +00:00
|
|
|
|
2015-01-22 17:46:38 +00:00
|
|
|
return fmt.Sprintf("%s stopped", name), nil
|
|
|
|
}
|
|
|
|
|
2015-04-28 12:21:57 +00:00
|
|
|
func (reaper *ServiceReaper) Stop(namespace, name string, gracePeriod *api.DeleteOptions) (string, error) {
|
2015-01-22 17:46:38 +00:00
|
|
|
services := reaper.Services(namespace)
|
|
|
|
_, err := services.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := services.Delete(name); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s stopped", name), nil
|
|
|
|
}
|