Browse code

Merge pull request #2336 from derekwaynecarr/prune_cmd

Merged by openshift-bot

OpenShift Bot authored on 2015/05/20 01:39:57
Showing 4 changed files
... ...
@@ -9,6 +9,7 @@ import (
9 9
 	"github.com/openshift/origin/pkg/cmd/admin/node"
10 10
 	"github.com/openshift/origin/pkg/cmd/admin/policy"
11 11
 	"github.com/openshift/origin/pkg/cmd/admin/project"
12
+	"github.com/openshift/origin/pkg/cmd/admin/prune"
12 13
 	"github.com/openshift/origin/pkg/cmd/cli/cmd"
13 14
 	"github.com/openshift/origin/pkg/cmd/experimental/buildchain"
14 15
 	exipfailover "github.com/openshift/origin/pkg/cmd/experimental/ipfailover"
... ...
@@ -49,6 +50,7 @@ func NewCommandAdmin(name, fullName string, out io.Writer) *cobra.Command {
49 49
 	cmds.AddCommand(buildchain.NewCmdBuildChain(f, fullName, "build-chain"))
50 50
 	cmds.AddCommand(node.NewCommandManageNode(f, node.ManageNodeCommandName, fullName+" "+node.ManageNodeCommandName, out))
51 51
 	cmds.AddCommand(cmd.NewCmdConfig(fullName, "config"))
52
+	cmds.AddCommand(prune.NewCommandPrune(prune.PruneRecommendedName, fullName+" "+prune.PruneRecommendedName, f, out))
52 53
 
53 54
 	// TODO: these probably belong in a sub command
54 55
 	cmds.AddCommand(admin.NewCommandCreateKubeConfig(admin.CreateKubeConfigCommandName, fullName+" "+admin.CreateKubeConfigCommandName, out))
55 56
new file mode 100644
... ...
@@ -0,0 +1,121 @@
0
+package prune
1
+
2
+import (
3
+	"fmt"
4
+	"io"
5
+	"os"
6
+	"text/tabwriter"
7
+	"time"
8
+
9
+	"github.com/golang/glog"
10
+	"github.com/spf13/cobra"
11
+
12
+	kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
13
+	"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
14
+	cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
15
+	"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
16
+
17
+	buildapi "github.com/openshift/origin/pkg/build/api"
18
+	"github.com/openshift/origin/pkg/build/prune"
19
+	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
20
+)
21
+
22
+const buildsLongDesc = `
23
+`
24
+
25
+const PruneBuildsRecommendedName = "builds"
26
+
27
+type pruneBuildsConfig struct {
28
+	DryRun          bool
29
+	KeepYoungerThan time.Duration
30
+	Orphans         bool
31
+	KeepComplete    int
32
+	KeepFailed      int
33
+}
34
+
35
+func NewCmdPruneBuilds(f *clientcmd.Factory, parentName, name string, out io.Writer) *cobra.Command {
36
+	cfg := &pruneBuildsConfig{
37
+		DryRun:          true,
38
+		KeepYoungerThan: 60 * time.Minute,
39
+		Orphans:         false,
40
+		KeepComplete:    5,
41
+		KeepFailed:      1,
42
+	}
43
+
44
+	cmd := &cobra.Command{
45
+		Use:   name,
46
+		Short: "Prune builds",
47
+		Long:  fmt.Sprintf(buildsLongDesc, parentName, name),
48
+
49
+		Run: func(cmd *cobra.Command, args []string) {
50
+			if len(args) > 0 {
51
+				glog.Fatalf("No arguments are allowed to this command")
52
+			}
53
+
54
+			osClient, _, err := f.Clients()
55
+			if err != nil {
56
+				cmdutil.CheckErr(err)
57
+			}
58
+
59
+			buildConfigList, err := osClient.BuildConfigs(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
60
+			if err != nil {
61
+				cmdutil.CheckErr(err)
62
+			}
63
+
64
+			buildList, err := osClient.Builds(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
65
+			if err != nil {
66
+				cmdutil.CheckErr(err)
67
+			}
68
+
69
+			buildConfigs := []*buildapi.BuildConfig{}
70
+			for i := range buildConfigList.Items {
71
+				buildConfigs = append(buildConfigs, &buildConfigList.Items[i])
72
+			}
73
+
74
+			builds := []*buildapi.Build{}
75
+			for i := range buildList.Items {
76
+				builds = append(builds, &buildList.Items[i])
77
+			}
78
+
79
+			var buildPruneFunc prune.PruneFunc
80
+
81
+			w := tabwriter.NewWriter(out, 10, 4, 3, ' ', 0)
82
+			defer w.Flush()
83
+
84
+			describingPruneBuildFunc := func(build *buildapi.Build) error {
85
+				fmt.Fprintf(w, "%s\t%s\n", build.Namespace, build.Name)
86
+				return nil
87
+			}
88
+
89
+			switch cfg.DryRun {
90
+			case false:
91
+				buildPruneFunc = func(build *buildapi.Build) error {
92
+					describingPruneBuildFunc(build)
93
+					err := osClient.Builds(build.Namespace).Delete(build.Name)
94
+					if err != nil {
95
+						return err
96
+					}
97
+					return nil
98
+				}
99
+			default:
100
+				fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made.")
101
+				buildPruneFunc = describingPruneBuildFunc
102
+			}
103
+
104
+			fmt.Fprintln(w, "NAMESPACE\tNAME")
105
+			pruneTask := prune.NewPruneTasker(buildConfigs, builds, cfg.KeepYoungerThan, cfg.Orphans, cfg.KeepComplete, cfg.KeepFailed, buildPruneFunc)
106
+			err = pruneTask.PruneTask()
107
+			if err != nil {
108
+				cmdutil.CheckErr(err)
109
+			}
110
+		},
111
+	}
112
+
113
+	cmd.Flags().BoolVar(&cfg.DryRun, "dry-run", cfg.DryRun, "Perform a build pruning dry-run, displaying what would be deleted but not actually deleting anything.")
114
+	cmd.Flags().BoolVar(&cfg.Orphans, "orphans", cfg.Orphans, "Prune all builds whose associated build config no longer exists and whose status is complete, failed, error, or canceled.")
115
+	cmd.Flags().DurationVar(&cfg.KeepYoungerThan, "keep-younger-than", cfg.KeepYoungerThan, "Specify the minimum age of a build for it to be considered a candidate for pruning.")
116
+	cmd.Flags().IntVar(&cfg.KeepComplete, "keep-complete", cfg.KeepComplete, "Per build configuration, specify the number of builds whose status is complete that will be preserved.")
117
+	cmd.Flags().IntVar(&cfg.KeepFailed, "keep-failed", cfg.KeepFailed, "Per build configuration, specify the number of builds whose status is failed, error, or canceled that will be preserved.")
118
+
119
+	return cmd
120
+}
0 121
new file mode 100644
... ...
@@ -0,0 +1,120 @@
0
+package prune
1
+
2
+import (
3
+	"fmt"
4
+	"io"
5
+	"os"
6
+	"text/tabwriter"
7
+	"time"
8
+
9
+	"github.com/golang/glog"
10
+	"github.com/spf13/cobra"
11
+
12
+	kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
13
+	"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
14
+	cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
15
+	"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
16
+
17
+	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
18
+	deployapi "github.com/openshift/origin/pkg/deploy/api"
19
+	"github.com/openshift/origin/pkg/deploy/prune"
20
+)
21
+
22
+const deploymentsLongDesc = `
23
+`
24
+
25
+const PruneDeploymentsRecommendedName = "deployments"
26
+
27
+type pruneDeploymentConfig struct {
28
+	DryRun          bool
29
+	KeepYoungerThan time.Duration
30
+	Orphans         bool
31
+	KeepComplete    int
32
+	KeepFailed      int
33
+}
34
+
35
+func NewCmdPruneDeployments(f *clientcmd.Factory, parentName, name string, out io.Writer) *cobra.Command {
36
+	cfg := &pruneDeploymentConfig{
37
+		DryRun:          true,
38
+		KeepYoungerThan: 60 * time.Minute,
39
+		KeepComplete:    5,
40
+		KeepFailed:      1,
41
+	}
42
+
43
+	cmd := &cobra.Command{
44
+		Use:   name,
45
+		Short: "Prune deployments",
46
+		Long:  fmt.Sprintf(deploymentsLongDesc, parentName, name),
47
+
48
+		Run: func(cmd *cobra.Command, args []string) {
49
+			if len(args) > 0 {
50
+				glog.Fatalf("No arguments are allowed to this command")
51
+			}
52
+
53
+			osClient, kclient, err := f.Clients()
54
+			if err != nil {
55
+				cmdutil.CheckErr(err)
56
+			}
57
+
58
+			deploymentConfigList, err := osClient.DeploymentConfigs(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
59
+			if err != nil {
60
+				cmdutil.CheckErr(err)
61
+			}
62
+
63
+			deploymentList, err := kclient.ReplicationControllers(kapi.NamespaceAll).List(labels.Everything())
64
+			if err != nil {
65
+				cmdutil.CheckErr(err)
66
+			}
67
+
68
+			deploymentConfigs := []*deployapi.DeploymentConfig{}
69
+			for i := range deploymentConfigList.Items {
70
+				deploymentConfigs = append(deploymentConfigs, &deploymentConfigList.Items[i])
71
+			}
72
+
73
+			deployments := []*kapi.ReplicationController{}
74
+			for i := range deploymentList.Items {
75
+				deployments = append(deployments, &deploymentList.Items[i])
76
+			}
77
+
78
+			var deploymentPruneFunc prune.PruneFunc
79
+
80
+			w := tabwriter.NewWriter(out, 10, 4, 3, ' ', 0)
81
+			defer w.Flush()
82
+
83
+			describingPruneDeploymentFunc := func(deployment *kapi.ReplicationController) error {
84
+				fmt.Fprintf(w, "%s\t%s\n", deployment.Namespace, deployment.Name)
85
+				return nil
86
+			}
87
+
88
+			switch cfg.DryRun {
89
+			case false:
90
+				deploymentPruneFunc = func(deployment *kapi.ReplicationController) error {
91
+					describingPruneDeploymentFunc(deployment)
92
+					err := kclient.ReplicationControllers(deployment.Namespace).Delete(deployment.Name)
93
+					if err != nil {
94
+						return err
95
+					}
96
+					return nil
97
+				}
98
+			default:
99
+				fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made.")
100
+				deploymentPruneFunc = describingPruneDeploymentFunc
101
+			}
102
+
103
+			fmt.Fprintln(w, "NAMESPACE\tNAME")
104
+			pruneTask := prune.NewPruneTasker(deploymentConfigs, deployments, cfg.KeepYoungerThan, cfg.Orphans, cfg.KeepComplete, cfg.KeepFailed, deploymentPruneFunc)
105
+			err = pruneTask.PruneTask()
106
+			if err != nil {
107
+				cmdutil.CheckErr(err)
108
+			}
109
+		},
110
+	}
111
+
112
+	cmd.Flags().BoolVar(&cfg.DryRun, "dry-run", cfg.DryRun, "Perform a deployment pruning dry-run, displaying what would be deleted but not actually deleting anything.")
113
+	cmd.Flags().BoolVar(&cfg.Orphans, "orphans", cfg.Orphans, "Prune all deployments where the associated deployment config no longer exists, the status is complete or failed, and the replica size is 0.")
114
+	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.")
115
+	cmd.Flags().IntVar(&cfg.KeepComplete, "keep-complete", cfg.KeepComplete, "Per deployment config, specify the number of deployments whose status is complete that will be preserved whose replica size is 0.")
116
+	cmd.Flags().IntVar(&cfg.KeepFailed, "keep-failed", cfg.KeepFailed, "Per deployment config, specify the number of deployments whose status is failed that will be preserved whose replica size is 0.")
117
+
118
+	return cmd
119
+}
0 120
new file mode 100644
... ...
@@ -0,0 +1,29 @@
0
+package prune
1
+
2
+import (
3
+	"io"
4
+
5
+	"github.com/spf13/cobra"
6
+
7
+	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
8
+)
9
+
10
+const PruneRecommendedName = "prune"
11
+
12
+func NewCommandPrune(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
13
+	// Parent command to which all subcommands are added.
14
+	cmds := &cobra.Command{
15
+		Use:   name,
16
+		Short: "Prune resources",
17
+		Long:  `Prune resources`,
18
+		Run:   runHelp,
19
+	}
20
+
21
+	cmds.AddCommand(NewCmdPruneBuilds(f, fullName, PruneBuildsRecommendedName, out))
22
+	cmds.AddCommand(NewCmdPruneDeployments(f, fullName, PruneDeploymentsRecommendedName, out))
23
+	return cmds
24
+}
25
+
26
+func runHelp(cmd *cobra.Command, args []string) {
27
+	cmd.Help()
28
+}