-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathgateway_reset.go
More file actions
158 lines (138 loc) · 4.58 KB
/
gateway_reset.go
File metadata and controls
158 lines (138 loc) · 4.58 KB
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
package cmd
import (
"fmt"
"os"
"github.com/kong/go-database-reconciler/pkg/state"
"github.com/kong/go-database-reconciler/pkg/utils"
"github.com/spf13/cobra"
)
var (
resetCmdForce bool
resetWorkspace string
resetAllWorkspaces bool
resetJSONOutput bool
)
func executeReset(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
if resetAllWorkspaces && resetWorkspace != "" {
return fmt.Errorf("workspace cannot be specified with --all-workspace flag")
}
if !resetCmdForce {
ok, err := utils.Confirm("This will delete all configuration from Kong's database." +
"\n> Are you sure? ")
if err != nil {
return err
}
if !ok {
return nil
}
}
mode := getMode(nil)
if mode == modeKonnect {
_ = sendAnalytics("reset", "", mode)
return resetKonnectV2(ctx)
}
rootClient, err := utils.GetKongClient(rootConfig)
if err != nil {
return err
}
kongVersion, err := fetchKongVersion(ctx, rootConfig.ForWorkspace(resetWorkspace))
if err != nil {
return fmt.Errorf("reading Kong version: %w", err)
}
parsedKongVersion, err := utils.ParseKongVersion(kongVersion)
if err != nil {
return fmt.Errorf("parsing Kong version: %w", err)
}
_ = sendAnalytics("reset", kongVersion, mode)
if utils.Kong340Version.LTE(parsedKongVersion) {
dumpConfig.IsConsumerGroupScopedPluginSupported = true
}
var workspaces []string
// Kong OSS or default workspace
if !resetAllWorkspaces && resetWorkspace == "" {
workspaces = append(workspaces, "")
}
// Kong Enterprise
if resetAllWorkspaces {
workspaces, err = listWorkspaces(ctx, rootClient)
if err != nil {
return err
}
}
if resetWorkspace != "" {
exists, err := workspaceExists(ctx, rootConfig, resetWorkspace)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("workspace '%v' does not exist in Kong", resetWorkspace)
}
workspaces = append(workspaces, resetWorkspace)
}
for _, workspace := range workspaces {
wsClient, err := utils.GetKongClient(rootConfig.ForWorkspace(workspace))
if err != nil {
return err
}
currentState, err := fetchCurrentState(ctx, wsClient, dumpConfig)
if err != nil {
return err
}
targetState, err := state.NewKongState()
if err != nil {
return err
}
_, err = performDiff(ctx, currentState, targetState, false, 10, 0, wsClient, false, resetJSONOutput, false)
if err != nil {
return err
}
}
return nil
}
// newResetCmd represents the reset command
func newResetCmd(deprecated bool) *cobra.Command {
short := "Reset deletes all entities in Kong"
execute := executeReset
if deprecated {
short = "[deprecated] use 'deck gateway reset' instead"
execute = func(cmd *cobra.Command, args []string) error {
fmt.Fprintf(os.Stderr, "Info: 'deck reset' functionality has moved to 'deck gateway reset' and will be removed\n"+
"in a future MAJOR version of deck. Migration to 'deck gateway reset' is recommended.\n")
return executeReset(cmd, args)
}
}
resetCmd := &cobra.Command{
Use: "reset",
Short: short,
Long: `The reset command deletes all entities in Kong's database.string.
Use this command with extreme care as it's equivalent to running
"kong migrations reset" on your Kong instance.
By default, this command will ask for confirmation.`,
Args: validateNoArgs,
RunE: execute,
}
resetCmd.Flags().BoolVarP(&resetCmdForce, "force", "f",
false, "Skip interactive confirmation prompt before reset.")
resetCmd.Flags().BoolVar(&dumpConfig.SkipConsumers, "skip-consumers",
false, "do not reset consumers, consumer-groups or "+
"any plugins associated with consumers.")
resetCmd.Flags().StringVarP(&resetWorkspace, "workspace", "w",
"", "reset configuration of a specific workspace"+
"(Kong Enterprise only).")
resetCmd.Flags().BoolVar(&resetAllWorkspaces, "all-workspaces",
false, "reset configuration of all workspaces (Kong Enterprise only).")
resetCmd.Flags().BoolVar(&noMaskValues, "no-mask-deck-env-vars-value",
false, "do not mask DECK_ environment variable values at diff output.")
resetCmd.Flags().StringSliceVar(&dumpConfig.SelectorTags,
"select-tag", []string{},
"only entities matching tags specified via this flag are deleted.\n"+
"When this setting has multiple tag values, entities must match every tag.")
resetCmd.Flags().BoolVar(&dumpConfig.RBACResourcesOnly, "rbac-resources-only",
false, "reset only the RBAC resources (Kong Enterprise only).")
resetCmd.Flags().BoolVar(&dumpConfig.SkipCACerts, "skip-ca-certificates",
false, "do not reset CA certificates.")
resetCmd.Flags().BoolVar(&resetJSONOutput, "json-output",
false, "generate command execution report in a JSON format")
return resetCmd
}