Skip to content
This repository was archived by the owner on Mar 25, 2024. It is now read-only.

Commit e094f73

Browse files
luthermonsonCraig Jellick
authored andcommitted
added execution delete/prune, module update and updated state run to account for new fields
1 parent a52c9b4 commit e094f73

File tree

3 files changed

+114
-29
lines changed

3 files changed

+114
-29
lines changed

pkg/cli/cmds/executions.go

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package cmds
33
import (
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+
127184
func 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

pkg/cli/cmds/modules.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,41 @@ func ModuleCommand() cli.Command {
3939
ArgsUsage: "[NAME]",
4040
Action: deleteModule,
4141
},
42+
{
43+
Name: "update",
44+
Usage: "Force the handler to update the git content.",
45+
ArgsUsage: "[NAME]",
46+
Action: updateModule,
47+
},
4248
},
4349
}
4450
}
4551

52+
func updateModule(c *cli.Context) error {
53+
kubeConfig := c.GlobalString("kubeconfig")
54+
namespace := c.GlobalString("namespace")
55+
56+
controllers, err := getControllers(kubeConfig, namespace)
57+
if err != nil {
58+
return err
59+
}
60+
61+
args := c.Args()
62+
if len(args) != 1 {
63+
return InvalidArgs{}
64+
}
65+
66+
moduleName := c.Args()[0]
67+
68+
module, err := controllers.modules.Get(namespace, moduleName, metav1.GetOptions{})
69+
if err != nil {
70+
return err
71+
}
72+
73+
v1.ModuleConditionGitUpdated.False(module)
74+
_, err = controllers.modules.Update(module)
75+
return err
76+
}
4677
func moduleList(c *cli.Context) error {
4778
kubeConfig := c.GlobalString("kubeconfig")
4879
namespace := c.GlobalString("namespace")

pkg/cli/cmds/states.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ func stateUnlock(c *cli.Context) error {
182182

183183
for _, secret := range secrets.Items {
184184
if secret.Data["lockInfo"] != nil {
185-
copy := secret.DeepCopy()
186-
copy.Data["lockInfo"] = nil
187-
_, err := controllers.secrets.Update(copy)
185+
copyObj := secret.DeepCopy()
186+
copyObj.Data["lockInfo"] = nil
187+
_, err := controllers.secrets.Update(copyObj)
188188
if err != nil {
189189
return err
190190
}
@@ -236,14 +236,16 @@ func runState(c *cli.Context) error {
236236

237237
state, err := getState(namespace, kubeConfig, name)
238238
if err != nil {
239+
logrus.Debug(err)
239240
return err
240241
}
241242

242-
// Not really an ideal or safe operation.
243-
// Need to create something on the execution type to lock
244-
state.Spec.Version += 1
243+
copyObj := state.DeepCopy()
244+
copyObj.Spec.Version += 1
245+
copyObj.Status.LastRunHash = ""
246+
v1.StateConditionJobDeployed.False(copyObj)
245247

246-
_, err = saveState(kubeConfig, namespace, state)
248+
_, err = saveState(kubeConfig, namespace, copyObj)
247249
return err
248250
}
249251

@@ -268,19 +270,13 @@ func deleteState(c *cli.Context) error {
268270
}
269271

270272
if state.DeletionTimestamp != nil {
271-
jobName := "job-" + state.Status.ExecutionName
272-
job, err := controllers.jobs.Get(namespace, jobName, metav1.GetOptions{})
273-
if err == nil && job.Status.Failed > 0 {
274-
err := controllers.jobs.Delete(namespace, jobName, &metav1.DeleteOptions{})
275-
if err != nil {
276-
return err
277-
}
278-
//trying to delete but the job failed, give the change handler a chance to delete again.
279-
state.Status.ExecutionName = ""
280-
_, err = controllers.states.Update(state)
281-
if err != nil {
282-
return err
283-
}
273+
state.Status.ExecutionName = ""
274+
state.Spec.Version += 1
275+
state.Status.LastRunHash = ""
276+
v1.StateConditionJobDeployed.False(state)
277+
_, err = controllers.states.Update(state)
278+
if err != nil {
279+
return err
284280
}
285281
}
286282

0 commit comments

Comments
 (0)