forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployments.go
119 lines (97 loc) · 4.06 KB
/
deployments.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package prune
import (
"fmt"
"io"
"os"
"text/tabwriter"
"time"
"github.com/golang/glog"
"github.com/spf13/cobra"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/fields"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/labels"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
deployapi "github.com/openshift/origin/pkg/deploy/api"
"github.com/openshift/origin/pkg/deploy/prune"
)
const deploymentsLongDesc = `%s %s - Remove older completed and failed deployments`
const PruneDeploymentsRecommendedName = "deployments"
type pruneDeploymentConfig struct {
Confirm bool
KeepYoungerThan time.Duration
Orphans bool
KeepComplete int
KeepFailed int
}
func NewCmdPruneDeployments(f *clientcmd.Factory, parentName, name string, out io.Writer) *cobra.Command {
cfg := &pruneDeploymentConfig{
Confirm: false,
KeepYoungerThan: 60 * time.Minute,
KeepComplete: 5,
KeepFailed: 1,
}
cmd := &cobra.Command{
Use: name,
Short: "Remove completed and failed deployments",
Long: fmt.Sprintf(deploymentsLongDesc, parentName, name),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
glog.Fatalf("No arguments are allowed to this command")
}
osClient, kclient, err := f.Clients()
if err != nil {
cmdutil.CheckErr(err)
}
deploymentConfigList, err := osClient.DeploymentConfigs(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
if err != nil {
cmdutil.CheckErr(err)
}
deploymentList, err := kclient.ReplicationControllers(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
if err != nil {
cmdutil.CheckErr(err)
}
deploymentConfigs := []*deployapi.DeploymentConfig{}
for i := range deploymentConfigList.Items {
deploymentConfigs = append(deploymentConfigs, &deploymentConfigList.Items[i])
}
deployments := []*kapi.ReplicationController{}
for i := range deploymentList.Items {
deployments = append(deployments, &deploymentList.Items[i])
}
var deploymentPruneFunc prune.PruneFunc
w := tabwriter.NewWriter(out, 10, 4, 3, ' ', 0)
defer w.Flush()
describingPruneDeploymentFunc := func(deployment *kapi.ReplicationController) error {
fmt.Fprintf(w, "%s\t%s\n", deployment.Namespace, deployment.Name)
return nil
}
switch cfg.Confirm {
case true:
deploymentPruneFunc = func(deployment *kapi.ReplicationController) error {
describingPruneDeploymentFunc(deployment)
err := kclient.ReplicationControllers(deployment.Namespace).Delete(deployment.Name)
if err != nil {
return err
}
return nil
}
default:
fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made. Add --confirm to remove deployments")
deploymentPruneFunc = describingPruneDeploymentFunc
}
fmt.Fprintln(w, "NAMESPACE\tNAME")
pruneTask := prune.NewPruneTasker(deploymentConfigs, deployments, cfg.KeepYoungerThan, cfg.Orphans, cfg.KeepComplete, cfg.KeepFailed, deploymentPruneFunc)
err = pruneTask.PruneTask()
if err != nil {
cmdutil.CheckErr(err)
}
},
}
cmd.Flags().BoolVar(&cfg.Confirm, "confirm", cfg.Confirm, "Specify that deployment pruning should proceed. Defaults to false, displaying what would be deleted but not actually deleting anything.")
cmd.Flags().BoolVar(&cfg.Orphans, "orphans", cfg.Orphans, "Prune all deployments where the associated DeploymentConfig no longer exists, the status is complete or failed, and the replica size is 0.")
cmd.Flags().DurationVar(&cfg.KeepYoungerThan, "keep-younger-than", cfg.KeepYoungerThan, "Specify the minimum age of a deployment for it to be considered a candidate for pruning.")
cmd.Flags().IntVar(&cfg.KeepComplete, "keep-complete", cfg.KeepComplete, "Per DeploymentConfig, specify the number of deployments whose status is complete that will be preserved whose replica size is 0.")
cmd.Flags().IntVar(&cfg.KeepFailed, "keep-failed", cfg.KeepFailed, "Per DeploymentConfig, specify the number of deployments whose status is failed that will be preserved whose replica size is 0.")
return cmd
}