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

Commit c7fc034

Browse files
committed
vendor
1 parent f85a61a commit c7fc034

File tree

20 files changed

+601
-432
lines changed

20 files changed

+601
-432
lines changed

example/20-execution.yaml renamed to example/20-state.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: terraformcontroller.cattle.io/v1
2-
kind: Execution
2+
kind: State
33
metadata:
4-
name: my-execution
4+
name: my-state
55
spec:
66
moduleName: my-module
77
destroyOnDelete: true

pkg/cli/cmds/executions.go

Lines changed: 58 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,210 +1,144 @@
11
package cmds
22

33
import (
4-
"github.com/rancher/terraform-controller/pkg/apis/terraformcontroller.cattle.io/v1"
5-
"github.com/rancher/terraform-controller/pkg/terraform/execution"
4+
v1 "github.com/rancher/terraform-controller/pkg/apis/terraformcontroller.cattle.io/v1"
65
"github.com/urfave/cli"
76
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
87
)
98

10-
var simpleExecutionTableHeader = []string{"NAME", "RUNNER NAME", "STATUS"}
9+
var simpleRunTableHeaders = []string{"RUN NAME", "EXECUTION NAME", "APPROVAL"}
1110

12-
func ExecutionCommand() cli.Command {
11+
func RunCommand() cli.Command {
1312
return cli.Command{
14-
Name: "executions",
15-
Aliases: []string{"execution"},
16-
Usage: "Operations on TF Operator modules",
13+
Name: "runs",
14+
Aliases: []string{"run"},
15+
Usage: "Manage executions",
1716
Action: executionList,
1817
Subcommands: []cli.Command{
1918
{
2019
Name: "ls",
21-
Usage: "List Executions",
20+
Usage: "List runs",
2221
ArgsUsage: "None",
2322
Action: executionList,
2423
},
2524
{
26-
Name: "create",
27-
Usage: "Create new executions pointing to a module",
28-
ArgsUsage: "[EXECUTION NAME] [MODULE NAME]",
29-
Action: createExecution,
30-
Flags: []cli.Flag{
31-
cli.StringFlag{
32-
Name: "image",
33-
Usage: "Set the image for the execution environment [registry]:org/repo:tag",
34-
Value: execution.DefaultExecutorImage,
35-
},
36-
cli.BoolFlag{
37-
Name: "destroy-on-delete",
38-
Usage: "If this execution is deleted a TF destroy is also run",
39-
},
40-
cli.BoolFlag{
41-
Name: "autoconfirm",
42-
Usage: "Autoapply TF updates",
43-
},
44-
cli.StringSliceFlag{
45-
Name: "secret",
46-
Usage: "Name of Kubernetes secret to use during execution run(Must be in same namespace and pre-created)",
47-
},
48-
cli.StringSliceFlag{
49-
Name: "configmap",
50-
Usage: "Name of Kubernetes configmap to use during execution run(Must be in same namespace and pre-created)",
51-
},
52-
},
25+
Name: "approve",
26+
Usage: "Approves a run",
27+
ArgsUsage: "[RUN NAME]",
28+
Action: approveExecution,
5329
},
5430
{
55-
Name: "delete",
56-
Usage: "Delete execution",
57-
ArgsUsage: "[EXECUTION NAME]",
58-
Action: deleteExecution,
59-
},
60-
{
61-
Name: "run",
62-
Usage: "Run the execution",
63-
Action: runExecution,
64-
ArgsUsage: "[EXECUTION NAME]",
31+
Name: "deny",
32+
Usage: "Deny approval for a run",
33+
ArgsUsage: "[RUN NAME]",
34+
Action: denyExecution,
6535
},
6636
},
6737
}
6838
}
6939

7040
func executionList(c *cli.Context) error {
71-
kubeConfig := c.GlobalString("kubeconfig")
7241
namespace := c.GlobalString("namespace")
42+
kubeConfig := c.GlobalString("kubeconfig")
7343

74-
executions, err := getExecutionList(namespace, kubeConfig)
44+
runs, err := getRunList(namespace, kubeConfig)
7545
if err != nil {
7646
return err
7747
}
7848

79-
NewTableWriter(getSimpleExecutionTableHeader(), executionListToTableStrings(executions)).Write()
49+
NewTableWriter(getSimpleRunTableHeader(), runsListToTableStrings(runs)).Write()
8050

8151
return nil
8252
}
8353

84-
func createExecution(c *cli.Context) error {
85-
kubeConfig := c.GlobalString("kubeconfig")
54+
func approveExecution(c *cli.Context) error {
8655
namespace := c.GlobalString("namespace")
87-
88-
if len(c.Args()) != 2 {
89-
return InvalidArgs{}
90-
}
91-
92-
executionName := c.Args()[0]
93-
moduleName := c.Args()[1]
94-
95-
execution := &v1.Execution{
96-
Spec: v1.ExecutionSpec{
97-
ModuleName: moduleName,
98-
Image: c.String("image"),
99-
DestroyOnDelete: c.Bool("destroy-on-delete"),
100-
AutoConfirm: c.Bool("autoconfirm"),
101-
Variables: v1.Variables{
102-
SecretNames: c.StringSlice("secret"),
103-
EnvConfigName: c.StringSlice("configmap"),
104-
},
105-
},
106-
}
107-
108-
execution.Name = executionName
109-
110-
return doExecutionCreate(namespace, kubeConfig, execution)
111-
}
112-
113-
func runExecution(c *cli.Context) error {
11456
kubeConfig := c.GlobalString("kubeconfig")
115-
namespace := c.GlobalString("namespace")
11657

11758
if len(c.Args()) != 1 {
11859
return InvalidArgs{}
11960
}
12061

12162
name := c.Args()[0]
12263

123-
execution, err := getExecution(namespace, kubeConfig, name)
64+
run, err := getExecution(namespace, kubeConfig, name)
12465
if err != nil {
12566
return err
12667
}
12768

128-
// Not really an ideal or safe operation.
129-
// Need to create something on the execution type to lock
130-
execution.Spec.Version += 1
69+
run.Annotations["approved"] = "yes"
13170

132-
_, err = saveExecution(kubeConfig, namespace, execution)
71+
_, err = saveExecution(kubeConfig, namespace, run)
13372
return err
13473
}
13574

136-
func deleteExecution(c *cli.Context) error {
137-
kubeConfig := c.GlobalString("kubeconfig")
75+
func denyExecution(c *cli.Context) error {
13876
namespace := c.GlobalString("namespace")
77+
kubeConfig := c.GlobalString("kubeconfig")
13978

14079
if len(c.Args()) != 1 {
14180
return InvalidArgs{}
14281
}
14382

144-
executionName := c.Args()[0]
145-
146-
return doExecutionDelete(namespace, kubeConfig, executionName)
147-
}
148-
149-
func doExecutionDelete(namespace, kubeConfig, executionName string) error {
150-
controllers, err := getControllers(kubeConfig, namespace)
151-
152-
if err != nil {
153-
return err
154-
}
155-
return controllers.executions.Delete(namespace, executionName, &metav1.DeleteOptions{})
156-
}
83+
name := c.Args()[0]
15784

158-
func doExecutionCreate(namespace, kubeConfig string, execution *v1.Execution) error {
159-
controllers, err := getControllers(kubeConfig, namespace)
85+
run, err := getExecution(namespace, kubeConfig, name)
16086
if err != nil {
16187
return err
16288
}
16389

164-
execution.Namespace = namespace
90+
run.Annotations["approved"] = "no"
16591

166-
_, err = controllers.executions.Create(execution)
92+
_, err = saveExecution(kubeConfig, namespace, run)
16793
return err
16894
}
16995

170-
func getExecution(namespace, kubeConfig, executionName string) (*v1.Execution, error) {
96+
func getSimpleRunTableHeader() []string {
97+
return simpleRunTableHeaders
98+
}
99+
100+
func getRunList(namespace, kubeConfig string) (*v1.ExecutionList, error) {
171101
controllers, err := getControllers(kubeConfig, namespace)
172102
if err != nil {
173103
return nil, err
174104
}
175-
return controllers.executions.Get(namespace, executionName, metav1.GetOptions{})
105+
106+
return controllers.executions.List(namespace, metav1.ListOptions{})
176107
}
177108

178-
func saveExecution(kubeConfig, namespace string, execution *v1.Execution) (*v1.Execution, error) {
179-
controllers, err := getControllers(kubeConfig, namespace)
180-
if err != nil {
181-
return nil, err
109+
func runsListToTableStrings(runs *v1.ExecutionList) [][]string {
110+
var values [][]string
111+
112+
for _, run := range runs.Items {
113+
approved := "True"
114+
if approvalStatus, ok := run.Annotations["approved"]; ok && approvalStatus == "" || approvalStatus == "no" {
115+
approved = "False"
116+
}
117+
118+
values = append(values, []string{
119+
run.Name,
120+
run.ObjectMeta.OwnerReferences[0].Name,
121+
approved,
122+
})
182123
}
183-
return controllers.executions.Update(execution)
124+
125+
return values
184126
}
185127

186-
func getExecutionList(namespace, kubeConfig string) (*v1.ExecutionList, error) {
128+
func saveExecution(kubeConfig, namespace string, run *v1.Execution) (*v1.Execution, error) {
187129
controllers, err := getControllers(kubeConfig, namespace)
188130
if err != nil {
189131
return nil, err
190132
}
191-
return controllers.executions.List(namespace, metav1.ListOptions{})
192-
}
193133

194-
func getSimpleExecutionTableHeader() []string {
195-
return simpleExecutionTableHeader
134+
return controllers.executions.Update(run)
196135
}
197136

198-
func executionListToTableStrings(executions *v1.ExecutionList) [][]string {
199-
var values [][]string
200-
201-
for _, execution := range executions.Items {
202-
values = append(values, []string{
203-
execution.Name,
204-
execution.Status.ExecutionRunName,
205-
execution.Status.Conditions[len(execution.Status.Conditions)-1].Type,
206-
})
137+
func getExecution(namespace, kubeConfig, name string) (*v1.Execution, error) {
138+
controllers, err := getControllers(kubeConfig, namespace)
139+
if err != nil {
140+
return nil, err
207141
}
208142

209-
return values
143+
return controllers.executions.Get(namespace, name, metav1.GetOptions{})
210144
}

0 commit comments

Comments
 (0)