Skip to content

Bug 1371511 - add namespace awareness to oadm prune commands #11249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link

@miminar miminar Oct 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert also there's at least one action in Actions list.

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() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

if a.GetNamespace() != "foo" {
t.Errorf("Unexpected namespace while pruning %s: %s", a.GetResource(), a.GetNamespace())
}
}
}
Loading