Skip to content

Commit

Permalink
Merge pull request #11249 from soltysh/issue9502
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot authored Oct 9, 2016
2 parents 6c5d083 + af137c1 commit 3d24bd1
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 101 deletions.
65 changes: 36 additions & 29 deletions pkg/cmd/admin/prune/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ type PruneBuildsOptions struct {
KeepYoungerThan time.Duration
KeepComplete int
KeepFailed int
Namespace string

Pruner prune.Pruner
Client client.Interface
Out io.Writer
OSClient client.Interface
Out io.Writer
}

// NewCmdPruneBuilds implements the OpenShift cli prune builds command.
Expand Down Expand Up @@ -84,15 +84,42 @@ func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
return kcmdutil.UsageError(cmd, "no arguments are allowed to this command")
}

o.Namespace = kapi.NamespaceAll
if cmd.Flags().Lookup("namespace").Changed {
var err error
o.Namespace, _, err = f.DefaultNamespace()
if err != nil {
return err
}
}
o.Out = out

osClient, _, err := f.Clients()
if err != nil {
return err
}
o.Client = osClient
o.OSClient = osClient

return nil
}

// Validate ensures that a PruneBuildsOptions is valid and can be used to execute pruning.
func (o PruneBuildsOptions) Validate() error {
if o.KeepYoungerThan < 0 {
return fmt.Errorf("--keep-younger-than must be greater than or equal to 0")
}
if o.KeepComplete < 0 {
return fmt.Errorf("--keep-complete must be greater than or equal to 0")
}
if o.KeepFailed < 0 {
return fmt.Errorf("--keep-failed must be greater than or equal to 0")
}
return nil
}

buildConfigList, err := osClient.BuildConfigs(kapi.NamespaceAll).List(kapi.ListOptions{})
// Run contains all the necessary functionality for the OpenShift cli prune builds command.
func (o PruneBuildsOptions) Run() error {
buildConfigList, err := o.OSClient.BuildConfigs(o.Namespace).List(kapi.ListOptions{})
if err != nil {
return err
}
Expand All @@ -101,7 +128,7 @@ func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
buildConfigs = append(buildConfigs, &buildConfigList.Items[i])
}

buildList, err := osClient.Builds(kapi.NamespaceAll).List(kapi.ListOptions{})
buildList, err := o.OSClient.Builds(o.Namespace).List(kapi.ListOptions{})
if err != nil {
return err
}
Expand All @@ -118,40 +145,20 @@ func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
BuildConfigs: buildConfigs,
Builds: builds,
}
pruner := prune.NewPruner(options)

o.Pruner = prune.NewPruner(options)

return nil
}

// Validate ensures that a PruneBuildsOptions is valid and can be used to execute pruning.
func (o PruneBuildsOptions) Validate() error {
if o.KeepYoungerThan < 0 {
return fmt.Errorf("--keep-younger-than must be greater than or equal to 0")
}
if o.KeepComplete < 0 {
return fmt.Errorf("--keep-complete must be greater than or equal to 0")
}
if o.KeepFailed < 0 {
return fmt.Errorf("--keep-failed must be greater than or equal to 0")
}
return nil
}

// Run contains all the necessary functionality for the OpenShift cli prune builds command.
func (o PruneBuildsOptions) Run() error {
w := tabwriter.NewWriter(o.Out, 10, 4, 3, ' ', 0)
defer w.Flush()

buildDeleter := &describingBuildDeleter{w: w}

if o.Confirm {
buildDeleter.delegate = prune.NewBuildDeleter(o.Client)
buildDeleter.delegate = prune.NewBuildDeleter(o.OSClient)
} else {
fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made. Add --confirm to remove builds")
}

return o.Pruner.Prune(buildDeleter)
return pruner.Prune(buildDeleter)
}

// describingBuildDeleter prints information about each build it removes.
Expand Down
31 changes: 31 additions & 0 deletions pkg/cmd/admin/prune/builds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package prune

import (
"io/ioutil"
"testing"

"github.com/openshift/origin/pkg/client/testclient"
)

func TestBuildPruneNamespaced(t *testing.T) {
osFake := testclient.NewSimpleFake()
opts := &PruneBuildsOptions{
Namespace: "foo",

OSClient: osFake,
Out: ioutil.Discard,
}

if err := opts.Run(); err != nil {
t.Errorf("Unexpected error: %v", err)
}

if len(osFake.Actions()) == 0 {
t.Errorf("Missing get build actions")
}
for _, a := range osFake.Actions() {
if a.GetNamespace() != "foo" {
t.Errorf("Unexpected namespace while pruning %s: %s", a.GetResource(), a.GetNamespace())
}
}
}
68 changes: 39 additions & 29 deletions pkg/cmd/admin/prune/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
kclient "k8s.io/kubernetes/pkg/client/unversioned"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"

"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
deployapi "github.com/openshift/origin/pkg/deploy/api"
"github.com/openshift/origin/pkg/deploy/prune"
Expand Down Expand Up @@ -41,10 +42,11 @@ type PruneDeploymentsOptions struct {
KeepYoungerThan time.Duration
KeepComplete int
KeepFailed int
Namespace string

Pruner prune.Pruner
Client kclient.Interface
Out io.Writer
OSClient client.Interface
KClient kclient.Interface
Out io.Writer
}

// NewCmdPruneDeployments implements the OpenShift cli prune deployments command.
Expand Down Expand Up @@ -85,15 +87,43 @@ func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Comm
return kcmdutil.UsageError(cmd, "no arguments are allowed to this command")
}

o.Namespace = kapi.NamespaceAll
if cmd.Flags().Lookup("namespace").Changed {
var err error
o.Namespace, _, err = f.DefaultNamespace()
if err != nil {
return err
}
}
o.Out = out

osClient, kClient, err := f.Clients()
if err != nil {
return err
}
o.Client = kClient
o.OSClient = osClient
o.KClient = kClient

return nil
}

// Validate ensures that a PruneDeploymentsOptions is valid and can be used to execute pruning.
func (o PruneDeploymentsOptions) Validate() error {
if o.KeepYoungerThan < 0 {
return fmt.Errorf("--keep-younger-than must be greater than or equal to 0")
}
if o.KeepComplete < 0 {
return fmt.Errorf("--keep-complete must be greater than or equal to 0")
}
if o.KeepFailed < 0 {
return fmt.Errorf("--keep-failed must be greater than or equal to 0")
}
return nil
}

deploymentConfigList, err := osClient.DeploymentConfigs(kapi.NamespaceAll).List(kapi.ListOptions{})
// Run contains all the necessary functionality for the OpenShift cli prune deployments command.
func (o PruneDeploymentsOptions) Run() error {
deploymentConfigList, err := o.OSClient.DeploymentConfigs(o.Namespace).List(kapi.ListOptions{})
if err != nil {
return err
}
Expand All @@ -102,7 +132,7 @@ func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Comm
deploymentConfigs = append(deploymentConfigs, &deploymentConfigList.Items[i])
}

deploymentList, err := kClient.ReplicationControllers(kapi.NamespaceAll).List(kapi.ListOptions{})
deploymentList, err := o.KClient.ReplicationControllers(o.Namespace).List(kapi.ListOptions{})
if err != nil {
return err
}
Expand All @@ -119,40 +149,20 @@ func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Comm
DeploymentConfigs: deploymentConfigs,
Deployments: deployments,
}
pruner := prune.NewPruner(options)

o.Pruner = prune.NewPruner(options)

return nil
}

// Validate ensures that a PruneDeploymentsOptions is valid and can be used to execute pruning.
func (o PruneDeploymentsOptions) Validate() error {
if o.KeepYoungerThan < 0 {
return fmt.Errorf("--keep-younger-than must be greater than or equal to 0")
}
if o.KeepComplete < 0 {
return fmt.Errorf("--keep-complete must be greater than or equal to 0")
}
if o.KeepFailed < 0 {
return fmt.Errorf("--keep-failed must be greater than or equal to 0")
}
return nil
}

// Run contains all the necessary functionality for the OpenShift cli prune deployments command.
func (o PruneDeploymentsOptions) Run() error {
w := tabwriter.NewWriter(o.Out, 10, 4, 3, ' ', 0)
defer w.Flush()

deploymentDeleter := &describingDeploymentDeleter{w: w}

if o.Confirm {
deploymentDeleter.delegate = prune.NewDeploymentDeleter(o.Client, o.Client)
deploymentDeleter.delegate = prune.NewDeploymentDeleter(o.KClient, o.KClient)
} else {
fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made. Add --confirm to remove deployments")
}

return o.Pruner.Prune(deploymentDeleter)
return pruner.Prune(deploymentDeleter)
}

// describingDeploymentDeleter prints information about each deployment it removes.
Expand Down
40 changes: 40 additions & 0 deletions pkg/cmd/admin/prune/deployments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package prune

import (
"io/ioutil"
"testing"

ktestclient "k8s.io/kubernetes/pkg/client/unversioned/testclient"

"github.com/openshift/origin/pkg/client/testclient"
)

func TestDeploymentPruneNamespaced(t *testing.T) {
kFake := ktestclient.NewSimpleFake()
osFake := testclient.NewSimpleFake()
opts := &PruneDeploymentsOptions{
Namespace: "foo",

OSClient: osFake,
KClient: kFake,
Out: ioutil.Discard,
}

if err := opts.Run(); err != nil {
t.Errorf("Unexpected error: %v", err)
}

if len(osFake.Actions()) == 0 || len(kFake.Actions()) == 0 {
t.Errorf("Missing get deployments actions")
}
for _, a := range osFake.Actions() {
if a.GetNamespace() != "foo" {
t.Errorf("Unexpected namespace while pruning %s: %s", a.GetResource(), a.GetNamespace())
}
}
for _, a := range kFake.Actions() {
if a.GetNamespace() != "foo" {
t.Errorf("Unexpected namespace while pruning %s: %s", a.GetResource(), a.GetNamespace())
}
}
}
Loading

0 comments on commit 3d24bd1

Please sign in to comment.