-
Notifications
You must be signed in to change notification settings - Fork 507
/
cmd_uninstall.go
120 lines (106 loc) · 3.53 KB
/
cmd_uninstall.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
package cli
import (
"context"
"github.com/spf13/cobra"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/v2/pkg/client/cache"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/cliutil"
"github.com/telepresenceio/telepresence/v2/pkg/client/errcat"
)
type uninstallInfo struct {
agent bool
allAgents bool
everything bool
namespace string
}
func uninstallCommand() *cobra.Command {
ui := &uninstallInfo{}
cmd := &cobra.Command{
Use: "uninstall [flags] { --agent <agents...> |--all-agents | --everything }",
Args: ui.args,
Short: "Uninstall telepresence agents and manager",
RunE: ui.run,
}
flags := cmd.Flags()
flags.BoolVarP(&ui.agent, "agent", "d", false, "uninstall intercept agent on specific deployments")
flags.BoolVarP(&ui.allAgents, "all-agents", "a", false, "uninstall intercept agent on all deployments")
flags.BoolVarP(&ui.everything, "everything", "e", false, "uninstall agents and the traffic manager")
flags.StringVarP(&ui.namespace, "namespace", "n", "", "If present, the namespace scope for this CLI request")
return cmd
}
func (u *uninstallInfo) args(cmd *cobra.Command, args []string) error {
if u.agent && u.allAgents || u.agent && u.everything || u.allAgents && u.everything {
return errcat.User.New("--agent, --all-agents, or --everything are mutually exclusive")
}
if !(u.agent || u.allAgents || u.everything) {
return errcat.User.New("please specify --agent, --all-agents, or --everything")
}
switch {
case u.agent && len(args) == 0:
return errcat.User.New("at least one argument (the name of an agent) is expected")
case !u.agent && len(args) != 0:
return errcat.User.New("unexpected argument(s)")
}
return nil
}
// uninstall
func (u *uninstallInfo) run(cmd *cobra.Command, args []string) error {
doQuit := false
err := withConnector(cmd, true, nil, func(ctx context.Context, cs *connectorState) error {
ur := &connector.UninstallRequest{
UninstallType: 0,
Namespace: u.namespace,
}
switch {
case u.agent:
ur.UninstallType = connector.UninstallRequest_NAMED_AGENTS
ur.Agents = args
case u.allAgents:
ur.UninstallType = connector.UninstallRequest_ALL_AGENTS
default:
ur.UninstallType = connector.UninstallRequest_EVERYTHING
}
r, err := cs.userD.Uninstall(ctx, ur)
if err != nil {
return err
}
if r.ErrorText != "" {
ec := errcat.Unknown
if r.ErrorCategory != 0 {
ec = errcat.Category(r.ErrorCategory)
}
return ec.New(r.ErrorText)
}
if ur.UninstallType == connector.UninstallRequest_EVERYTHING {
// No need to keep daemons once everything is uninstalled
doQuit = true
return removeClusterFromUserCache(ctx, cs.ConnectInfo)
}
return nil
})
if err == nil && doQuit {
err = cliutil.Disconnect(cmd.Context(), true, true)
}
return err
}
func removeClusterFromUserCache(ctx context.Context, connInfo *connector.ConnectInfo) (err error) {
// Login token is affined to the traffic-manager that just got removed. The user-info
// in turn, is info obtained using that token so both are removed here as a
// consequence of removing the manager.
if err := cliutil.EnsureLoggedOut(ctx); err != nil {
return err
}
// Delete the ingress info for the cluster if it exists.
ingresses, err := cache.LoadIngressesFromUserCache(ctx)
if err != nil {
return err
}
key := connInfo.ClusterServer + "/" + connInfo.ClusterContext
if _, ok := ingresses[key]; ok {
delete(ingresses, key)
if err = cache.SaveIngressesToUserCache(ctx, ingresses); err != nil {
return err
}
}
return nil
}