| ... | ... |
@@ -18,6 +18,7 @@ import ( |
| 18 | 18 |
"github.com/openshift/origin/pkg/cmd/util/clientcmd" |
| 19 | 19 |
deployapi "github.com/openshift/origin/pkg/deploy/api" |
| 20 | 20 |
"github.com/openshift/origin/pkg/deploy/prune" |
| 21 |
+ deployutil "github.com/openshift/origin/pkg/deploy/util" |
|
| 21 | 22 |
) |
| 22 | 23 |
|
| 23 | 24 |
const deploymentsLongDesc = `%s %s - Remove older completed and failed deployments` |
| ... | ... |
@@ -89,11 +90,21 @@ func NewCmdPruneDeployments(f *clientcmd.Factory, parentName, name string, out i |
| 89 | 89 |
case true: |
| 90 | 90 |
deploymentPruneFunc = func(deployment *kapi.ReplicationController) error {
|
| 91 | 91 |
describingPruneDeploymentFunc(deployment) |
| 92 |
- err := kclient.ReplicationControllers(deployment.Namespace).Delete(deployment.Name) |
|
| 93 |
- if err != nil {
|
|
| 94 |
- return err |
|
| 92 |
+ // If the deployment is failed we need to remove its deployer pods, too. |
|
| 93 |
+ if deployutil.DeploymentStatusFor(deployment) == deployapi.DeploymentStatusFailed {
|
|
| 94 |
+ dpSelector := deployutil.DeployerPodSelector(deployment.Name) |
|
| 95 |
+ deployers, err := kclient.Pods(deployment.Namespace).List(dpSelector, fields.Everything()) |
|
| 96 |
+ if err != nil {
|
|
| 97 |
+ fmt.Fprintf(os.Stderr, "Cannot list deployer pods for %q: %v\n", deployment.Name, err) |
|
| 98 |
+ } else {
|
|
| 99 |
+ for _, pod := range deployers.Items {
|
|
| 100 |
+ if err := kclient.Pods(pod.Namespace).Delete(pod.Name, nil); err != nil {
|
|
| 101 |
+ fmt.Fprintf(os.Stderr, "Cannot remove deployer pod %q: %v\n", pod.Name, err) |
|
| 102 |
+ } |
|
| 103 |
+ } |
|
| 104 |
+ } |
|
| 95 | 105 |
} |
| 96 |
- return nil |
|
| 106 |
+ return kclient.ReplicationControllers(deployment.Namespace).Delete(deployment.Name) |
|
| 97 | 107 |
} |
| 98 | 108 |
default: |
| 99 | 109 |
fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made. Add --confirm to remove deployments") |
| ... | ... |
@@ -111,8 +111,8 @@ func (o *perDeploymentConfigResolver) Resolve() ([]*kapi.ReplicationController, |
| 111 | 111 |
failedDeployments = append(failedDeployments, deployment) |
| 112 | 112 |
} |
| 113 | 113 |
} |
| 114 |
- sort.Sort(sortableReplicationControllers(completeDeployments)) |
|
| 115 |
- sort.Sort(sortableReplicationControllers(failedDeployments)) |
|
| 114 |
+ sort.Sort(deployutil.ByMostRecent(completeDeployments)) |
|
| 115 |
+ sort.Sort(deployutil.ByMostRecent(failedDeployments)) |
|
| 116 | 116 |
|
| 117 | 117 |
if o.keepComplete >= 0 && o.keepComplete < len(completeDeployments) {
|
| 118 | 118 |
results = append(results, completeDeployments[o.keepComplete:]...) |
| ... | ... |
@@ -11,6 +11,7 @@ import ( |
| 11 | 11 |
"k8s.io/kubernetes/pkg/util/sets" |
| 12 | 12 |
|
| 13 | 13 |
deployapi "github.com/openshift/origin/pkg/deploy/api" |
| 14 |
+ deployutil "github.com/openshift/origin/pkg/deploy/util" |
|
| 14 | 15 |
) |
| 15 | 16 |
|
| 16 | 17 |
type mockResolver struct {
|
| ... | ... |
@@ -150,8 +151,8 @@ func TestPerDeploymentConfigResolver(t *testing.T) {
|
| 150 | 150 |
failedDeployments = append(failedDeployments, deployment) |
| 151 | 151 |
} |
| 152 | 152 |
} |
| 153 |
- sort.Sort(sortableReplicationControllers(completedDeployments)) |
|
| 154 |
- sort.Sort(sortableReplicationControllers(failedDeployments)) |
|
| 153 |
+ sort.Sort(deployutil.ByMostRecent(completedDeployments)) |
|
| 154 |
+ sort.Sort(deployutil.ByMostRecent(failedDeployments)) |
|
| 155 | 155 |
purgeCompleted := []*kapi.ReplicationController{}
|
| 156 | 156 |
purgeFailed := []*kapi.ReplicationController{}
|
| 157 | 157 |
if keep >= 0 && keep < len(completedDeployments) {
|
| 158 | 158 |
deleted file mode 100644 |
| ... | ... |
@@ -1,20 +0,0 @@ |
| 1 |
-package prune |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- kapi "k8s.io/kubernetes/pkg/api" |
|
| 5 |
-) |
|
| 6 |
- |
|
| 7 |
-// sortableReplicationControllers supports sorting ReplicationController items by most recently created |
|
| 8 |
-type sortableReplicationControllers []*kapi.ReplicationController |
|
| 9 |
- |
|
| 10 |
-func (s sortableReplicationControllers) Len() int {
|
|
| 11 |
- return len(s) |
|
| 12 |
-} |
|
| 13 |
- |
|
| 14 |
-func (s sortableReplicationControllers) Less(i, j int) bool {
|
|
| 15 |
- return !s[i].CreationTimestamp.Before(s[j].CreationTimestamp) |
|
| 16 |
-} |
|
| 17 |
- |
|
| 18 |
-func (s sortableReplicationControllers) Swap(i, j int) {
|
|
| 19 |
- s[i], s[j] = s[j], s[i] |
|
| 20 |
-} |
| 21 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,37 +0,0 @@ |
| 1 |
-package prune |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "sort" |
|
| 5 |
- "testing" |
|
| 6 |
- "time" |
|
| 7 |
- |
|
| 8 |
- kapi "k8s.io/kubernetes/pkg/api" |
|
| 9 |
- "k8s.io/kubernetes/pkg/api/unversioned" |
|
| 10 |
-) |
|
| 11 |
- |
|
| 12 |
-// TestSort verifies that builds are sorted by most recently created |
|
| 13 |
-func TestSort(t *testing.T) {
|
|
| 14 |
- present := unversioned.Now() |
|
| 15 |
- past := unversioned.NewTime(present.Time.Add(-1 * time.Minute)) |
|
| 16 |
- controllers := []*kapi.ReplicationController{
|
|
| 17 |
- {
|
|
| 18 |
- ObjectMeta: kapi.ObjectMeta{
|
|
| 19 |
- Name: "past", |
|
| 20 |
- CreationTimestamp: past, |
|
| 21 |
- }, |
|
| 22 |
- }, |
|
| 23 |
- {
|
|
| 24 |
- ObjectMeta: kapi.ObjectMeta{
|
|
| 25 |
- Name: "present", |
|
| 26 |
- CreationTimestamp: present, |
|
| 27 |
- }, |
|
| 28 |
- }, |
|
| 29 |
- } |
|
| 30 |
- sort.Sort(sortableReplicationControllers(controllers)) |
|
| 31 |
- if controllers[0].Name != "present" {
|
|
| 32 |
- t.Errorf("Unexpected sort order")
|
|
| 33 |
- } |
|
| 34 |
- if controllers[1].Name != "past" {
|
|
| 35 |
- t.Errorf("Unexpected sort order")
|
|
| 36 |
- } |
|
| 37 |
-} |
| ... | ... |
@@ -289,3 +289,12 @@ func (d ByLatestVersionDesc) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
|
| 289 | 289 |
func (d ByLatestVersionDesc) Less(i, j int) bool {
|
| 290 | 290 |
return DeploymentVersionFor(&d[j]) < DeploymentVersionFor(&d[i]) |
| 291 | 291 |
} |
| 292 |
+ |
|
| 293 |
+// ByMostRecent sorts deployments by most recently created. |
|
| 294 |
+type ByMostRecent []*api.ReplicationController |
|
| 295 |
+ |
|
| 296 |
+func (s ByMostRecent) Len() int { return len(s) }
|
|
| 297 |
+func (s ByMostRecent) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
| 298 |
+func (s ByMostRecent) Less(i, j int) bool {
|
|
| 299 |
+ return !s[i].CreationTimestamp.Before(s[j].CreationTimestamp) |
|
| 300 |
+} |
| ... | ... |
@@ -4,10 +4,13 @@ import ( |
| 4 | 4 |
"sort" |
| 5 | 5 |
"strconv" |
| 6 | 6 |
"testing" |
| 7 |
+ "time" |
|
| 8 |
+ |
|
| 9 |
+ kapi "k8s.io/kubernetes/pkg/api" |
|
| 10 |
+ "k8s.io/kubernetes/pkg/api/unversioned" |
|
| 7 | 11 |
|
| 8 | 12 |
deployapi "github.com/openshift/origin/pkg/deploy/api" |
| 9 | 13 |
deploytest "github.com/openshift/origin/pkg/deploy/api/test" |
| 10 |
- kapi "k8s.io/kubernetes/pkg/api" |
|
| 11 | 14 |
) |
| 12 | 15 |
|
| 13 | 16 |
func podTemplateA() *kapi.PodTemplateSpec {
|
| ... | ... |
@@ -152,3 +155,30 @@ func TestDeploymentsByLatestVersion_sorting(t *testing.T) {
|
| 152 | 152 |
} |
| 153 | 153 |
} |
| 154 | 154 |
} |
| 155 |
+ |
|
| 156 |
+// TestSort verifies that builds are sorted by most recently created |
|
| 157 |
+func TestSort(t *testing.T) {
|
|
| 158 |
+ present := unversioned.Now() |
|
| 159 |
+ past := unversioned.NewTime(present.Time.Add(-1 * time.Minute)) |
|
| 160 |
+ controllers := []*kapi.ReplicationController{
|
|
| 161 |
+ {
|
|
| 162 |
+ ObjectMeta: kapi.ObjectMeta{
|
|
| 163 |
+ Name: "past", |
|
| 164 |
+ CreationTimestamp: past, |
|
| 165 |
+ }, |
|
| 166 |
+ }, |
|
| 167 |
+ {
|
|
| 168 |
+ ObjectMeta: kapi.ObjectMeta{
|
|
| 169 |
+ Name: "present", |
|
| 170 |
+ CreationTimestamp: present, |
|
| 171 |
+ }, |
|
| 172 |
+ }, |
|
| 173 |
+ } |
|
| 174 |
+ sort.Sort(ByMostRecent(controllers)) |
|
| 175 |
+ if controllers[0].Name != "present" {
|
|
| 176 |
+ t.Errorf("Unexpected sort order")
|
|
| 177 |
+ } |
|
| 178 |
+ if controllers[1].Name != "past" {
|
|
| 179 |
+ t.Errorf("Unexpected sort order")
|
|
| 180 |
+ } |
|
| 181 |
+} |