@@ -3,8 +3,9 @@ package cmds
33import (
44 "encoding/base64"
55 "fmt"
6+ "time"
67
7- "github.com/rancher/terraform-controller/pkg/age "
8+ "github.com/docker/go-units "
89 v1 "github.com/rancher/terraform-controller/pkg/apis/terraformcontroller.cattle.io/v1"
910 "github.com/rancher/terraform-controller/pkg/gz"
1011 "github.com/sirupsen/logrus"
@@ -29,11 +30,18 @@ func ExecutionCommand() cli.Command {
2930 Action : executionList ,
3031 },
3132 {
32- Name : "delete" ,
33- Aliases : []string {"del" },
34- Usage : "Delete executions for a specific state." ,
35- ArgsUsage : "[EXECUTION NAME]" ,
36- Action : executionDelete ,
33+ Name : "prune" ,
34+ Aliases : []string {"pr" },
35+ Usage : "Takes a state and will delete all executions aged > <days> , pass --days to change how many days to leave" ,
36+ ArgsUsage : "[STATE NAME]" ,
37+ Action : executionPrune ,
38+ Flags : []cli.Flag {
39+ cli.IntFlag {
40+ Name : "days" ,
41+ Usage : "How many days of executions you want to keep around, --days 2 will leave the last 48hrs of executions" ,
42+ Value : 7 ,
43+ },
44+ },
3745 },
3846 {
3947 Name : "logs" ,
@@ -42,6 +50,13 @@ func ExecutionCommand() cli.Command {
4250 ArgsUsage : "[EXECUTION NAME]" ,
4351 Action : logs ,
4452 },
53+ {
54+ Name : "delete" ,
55+ Aliases : []string {"del" },
56+ Usage : "Delete an execution" ,
57+ ArgsUsage : "[EXECUTION NAME]" ,
58+ Action : delete ,
59+ },
4560 {
4661 Name : "approve" ,
4762 Aliases : []string {"a" },
@@ -90,10 +105,19 @@ func logs(c *cli.Context) error {
90105 return nil
91106}
92107
93- func executionDelete (c * cli.Context ) error {
108+ func executionPrune (c * cli.Context ) error {
94109 namespace := c .GlobalString ("namespace" )
95110 kubeConfig := c .GlobalString ("kubeconfig" )
96111
112+ days := c .Int ("days" )
113+
114+ if days > 0 {
115+ days = days * - 1
116+ }
117+
118+ now := time .Now ()
119+ from := now .AddDate (0 , 0 , days )
120+
97121 controllers , err := getControllers (kubeConfig , namespace )
98122 if err != nil {
99123 return err
@@ -114,8 +138,11 @@ func executionDelete(c *cli.Context) error {
114138 }
115139
116140 for _ , exec := range execs .Items {
117- err := controllers .executions .Delete (namespace , exec .Name , & metav1.DeleteOptions {})
141+ if exec .CreationTimestamp .After (from ) {
142+ continue
143+ }
118144 logrus .Info ("deleting " + exec .Name )
145+ err := controllers .executions .Delete (namespace , exec .Name , & metav1.DeleteOptions {})
119146 if err != nil {
120147 return err
121148 }
@@ -124,6 +151,36 @@ func executionDelete(c *cli.Context) error {
124151 return nil
125152}
126153
154+ func delete (c * cli.Context ) error {
155+ namespace := c .GlobalString ("namespace" )
156+ kubeConfig := c .GlobalString ("kubeconfig" )
157+
158+ if len (c .Args ()) != 1 {
159+ return InvalidArgs {}
160+ }
161+
162+ name := c .Args ().First ()
163+
164+ controllers , err := getControllers (kubeConfig , namespace )
165+ if err != nil {
166+ return err
167+ }
168+
169+ _ , err = controllers .executions .Get (namespace , name , metav1.GetOptions {})
170+ if err != nil {
171+ return err
172+ }
173+
174+ err = controllers .executions .Delete (namespace , name , & metav1.DeleteOptions {})
175+
176+ if err != nil {
177+ return err
178+ }
179+
180+ fmt .Printf ("Deleted %s\n " , name )
181+ return nil
182+ }
183+
127184func executionList (c * cli.Context ) error {
128185 namespace := c .GlobalString ("namespace" )
129186 kubeConfig := c .GlobalString ("kubeconfig" )
@@ -209,11 +266,12 @@ func runsListToTableStrings(executions *v1.ExecutionList) [][]string {
209266 approved = "False"
210267 }
211268
269+ age := units .HumanDuration (time .Now ().UTC ().Sub (execution .ObjectMeta .CreationTimestamp .Time ))
212270 values = append (values , []string {
213271 execution .Name ,
214272 execution .Labels ["state" ],
215273 approved ,
216- age . Age ( execution . ObjectMeta . CreationTimestamp . Time ) ,
274+ age ,
217275 })
218276 }
219277
0 commit comments