forked from openshift/origin
/
reaper.go
77 lines (68 loc) · 2.88 KB
/
reaper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package reaper
import (
"fmt"
"time"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/deploy/util"
)
// ReaperFor returns the appropriate Reaper client depending on the provided
// kind of resource (Replication controllers, pods, services, and deploymentConfigs
// supported)
func ReaperFor(kind string, osc *client.Client, kc *kclient.Client) (kubectl.Reaper, error) {
if kind != "DeploymentConfig" {
return kubectl.ReaperFor(kind, kc)
}
return &DeploymentConfigReaper{osc: osc, kc: kc, pollInterval: kubectl.Interval, timeout: kubectl.Timeout}, nil
}
// DeploymentConfigReaper implements the Reaper interface for deploymentConfigs
type DeploymentConfigReaper struct {
osc client.Interface
kc kclient.Interface
pollInterval, timeout time.Duration
}
// Stop scales a replication controller via its deployment configuration down to
// zero replicas, waits for all of them to get deleted and then deletes both the
// replication controller and its deployment configuration.
func (reaper *DeploymentConfigReaper) Stop(namespace, name string, gracePeriod *kapi.DeleteOptions) (string, error) {
// If the config is already deleted, it may still have associated
// deployments which didn't get cleaned up during prior calls to Stop. If
// the config can't be found, still make an attempt to clean up the
// deployments.
//
// It's important to delete the config first to avoid an undesirable side
// effect which can cause the deployment to be re-triggered upon the
// config's deletion. See https://github.com/openshift/origin/issues/2721
// for more details.
err := reaper.osc.DeploymentConfigs(namespace).Delete(name)
configNotFound := kerrors.IsNotFound(err)
if err != nil && !configNotFound {
return "", err
}
// Clean up deployments related to the config.
rcList, err := reaper.kc.ReplicationControllers(namespace).List(labels.Everything())
if err != nil {
return "", err
}
rcReaper, err := kubectl.ReaperFor("ReplicationController", reaper.kc)
if err != nil {
return "", err
}
// If there is neither a config nor any deployments, we can return NotFound.
deployments := util.ConfigSelector(name, rcList.Items)
if configNotFound && len(deployments) == 0 {
return "", kerrors.NewNotFound("DeploymentConfig", name)
}
for _, rc := range deployments {
if _, err = rcReaper.Stop(rc.Namespace, rc.Name, gracePeriod); err != nil {
// Better not error out here...
glog.Infof("Cannot delete ReplicationController %s/%s: %v", rc.Namespace, rc.Name, err)
}
}
return fmt.Sprintf("%s stopped", name), nil
}