forked from grafana/tanka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow.go
186 lines (150 loc) · 4.83 KB
/
workflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"log"
"os"
"github.com/go-clix/cli"
"github.com/posener/complete"
"github.com/grafana/tanka/pkg/process"
"github.com/grafana/tanka/pkg/tanka"
"github.com/grafana/tanka/pkg/term"
)
// special exit codes for tk diff
const (
// no changes
ExitStatusClean = 0
// differences between the local config and the cluster
ExitStatusDiff = 16
)
func applyCmd() *cli.Command {
cmd := &cli.Command{
Use: "apply <path>",
Short: "apply the configuration to the cluster",
Args: workflowArgs,
}
var opts tanka.ApplyOpts
cmd.Flags().BoolVar(&opts.Force, "force", false, "force applying (kubectl apply --force)")
cmd.Flags().BoolVar(&opts.Validate, "validate", true, "validation of resources (kubectl --validate=false)")
cmd.Flags().BoolVar(&opts.AutoApprove, "dangerous-auto-approve", false, "skip interactive approval. Only for automation!")
vars := workflowFlags(cmd.Flags())
getJsonnetOpts := jsonnetFlags(cmd.Flags())
cmd.Run = func(cmd *cli.Command, args []string) error {
filters, err := process.StrExps(vars.targets...)
if err != nil {
return err
}
opts.Filters = filters
opts.JsonnetOpts = getJsonnetOpts()
return tanka.Apply(args[0], opts)
}
return cmd
}
func pruneCmd() *cli.Command {
cmd := &cli.Command{
Use: "prune <path>",
Short: "delete resources removed from Jsonnet",
Args: workflowArgs,
}
var opts tanka.PruneOpts
cmd.Flags().BoolVar(&opts.Force, "force", false, "force deleting (kubectl delete --force)")
cmd.Flags().BoolVar(&opts.AutoApprove, "dangerous-auto-approve", false, "skip interactive approval. Only for automation!")
getJsonnetOpts := jsonnetFlags(cmd.Flags())
cmd.Run = func(cmd *cli.Command, args []string) error {
opts.JsonnetOpts = getJsonnetOpts()
return tanka.Prune(args[0], opts)
}
return cmd
}
func deleteCmd() *cli.Command {
cmd := &cli.Command{
Use: "delete <path>",
Short: "delete the environment from cluster",
Args: workflowArgs,
}
var opts tanka.DeleteOpts
cmd.Flags().BoolVar(&opts.Force, "force", false, "force deleting (kubectl delete --force)")
cmd.Flags().BoolVar(&opts.Validate, "validate", true, "validation of resources (kubectl --validate=false)")
cmd.Flags().BoolVar(&opts.AutoApprove, "dangerous-auto-approve", false, "skip interactive approval. Only for automation!")
vars := workflowFlags(cmd.Flags())
getJsonnetOpts := jsonnetFlags(cmd.Flags())
cmd.Run = func(cmd *cli.Command, args []string) error {
filters, err := process.StrExps(vars.targets...)
if err != nil {
return err
}
opts.Filters = filters
opts.JsonnetOpts = getJsonnetOpts()
return tanka.Delete(args[0], opts)
}
return cmd
}
func diffCmd() *cli.Command {
cmd := &cli.Command{
Use: "diff <path>",
Short: "differences between the configuration and the cluster",
Args: workflowArgs,
Predictors: complete.Flags{
"diff-strategy": cli.PredictSet("native", "subset"),
},
}
var opts tanka.DiffOpts
cmd.Flags().StringVar(&opts.Strategy, "diff-strategy", "", "force the diff-strategy to use. Automatically chosen if not set.")
cmd.Flags().BoolVarP(&opts.Summarize, "summarize", "s", false, "print summary of the differences, not the actual contents")
cmd.Flags().BoolVarP(&opts.WithPrune, "with-prune", "p", false, "include objects deleted from the configuration in the differences")
vars := workflowFlags(cmd.Flags())
getJsonnetOpts := jsonnetFlags(cmd.Flags())
cmd.Run = func(cmd *cli.Command, args []string) error {
filters, err := process.StrExps(vars.targets...)
if err != nil {
return err
}
opts.Filters = filters
opts.JsonnetOpts = getJsonnetOpts()
changes, err := tanka.Diff(args[0], opts)
if err != nil {
return err
}
if changes == nil {
log.Println("No differences.")
os.Exit(ExitStatusClean)
}
r := term.Colordiff(*changes)
if err := fPageln(r); err != nil {
return err
}
os.Exit(ExitStatusDiff)
return nil
}
return cmd
}
func showCmd() *cli.Command {
cmd := &cli.Command{
Use: "show <path>",
Short: "jsonnet as yaml",
Args: workflowArgs,
}
allowRedirect := cmd.Flags().Bool("dangerous-allow-redirect", false, "allow redirecting output to a file or a pipe.")
vars := workflowFlags(cmd.Flags())
getJsonnetOpts := jsonnetFlags(cmd.Flags())
cmd.Run = func(cmd *cli.Command, args []string) error {
if !interactive && !*allowRedirect {
fmt.Fprintln(os.Stderr, `Redirection of the output of tk show is discouraged and disabled by default.
If you want to export .yaml files for use with other tools, try 'tk export'.
Otherwise run tk show --dangerous-allow-redirect to bypass this check.`)
return nil
}
filters, err := process.StrExps(vars.targets...)
if err != nil {
return err
}
pretty, err := tanka.Show(args[0], tanka.Opts{
JsonnetOpts: getJsonnetOpts(),
Filters: filters,
})
if err != nil {
return err
}
return pageln(pretty.String())
}
return cmd
}