/
detachNode.go
193 lines (154 loc) · 5.45 KB
/
detachNode.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
187
188
189
190
191
192
193
package cmd
import (
"fmt"
"time"
"github.com/platform9/pf9ctl/pkg/client"
"github.com/platform9/pf9ctl/pkg/cmdexec"
"github.com/platform9/pf9ctl/pkg/color"
"github.com/platform9/pf9ctl/pkg/config"
"github.com/platform9/pf9ctl/pkg/objects"
"github.com/platform9/pf9ctl/pkg/pmk"
"github.com/platform9/pf9ctl/pkg/qbert"
"github.com/platform9/pf9ctl/pkg/util"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var (
nodeIPs []string
)
var detachNodeCmd = &cobra.Command{
Use: "detach-node [flags]",
Short: "Detaches a node from a Kubernetes cluster",
Long: "Detach nodes from their clusters. If no nodes are passed it will detach the node on which the command was run.",
Args: func(detachNodeCmd *cobra.Command, args []string) error {
return nil
},
Run: detachNodeRun,
}
func init() {
detachNodeCmd.Flags().StringSliceVarP(&nodeIPs, "node-ip", "n", []string{}, "node ip address")
detachNodeCmd.Flags().StringVar(&attachconfig.MFA, "mfa", "", "MFA token")
rootCmd.AddCommand(detachNodeCmd)
}
func detachNodeRun(cmd *cobra.Command, args []string) {
if len(nodeIPs) == 0 {
nodeIPs = append(nodeIPs, pmk.GetIp().String())
}
detachedMode := cmd.Flags().Changed("no-prompt")
if cmdexec.CheckRemote(nc) {
if !config.ValidateNodeConfig(&nc, !detachedMode) {
zap.S().Fatal("Invalid remote node config (Username/Password/IP), use 'single quotes' to pass password")
}
}
cfg := &objects.Config{WaitPeriod: time.Duration(60), AllowInsecure: false, MfaToken: attachconfig.MFA}
var err error
if detachedMode {
err = config.LoadConfig(util.Pf9DBLoc, cfg, nc)
} else {
err = config.LoadConfigInteractive(util.Pf9DBLoc, cfg, nc)
}
if err != nil {
zap.S().Fatalf("Unable to load the context: %s\n", err.Error())
}
fmt.Println(color.Green("✓ ") + "Loaded Config Successfully")
zap.S().Debug("Loaded Config Successfully")
var executor cmdexec.Executor
if executor, err = cmdexec.GetExecutor(cfg.ProxyURL, nc); err != nil {
zap.S().Fatalf("Unable to create executor: %s\n", err.Error())
}
var c client.Client
if c, err = client.NewClient(cfg.Fqdn, executor, cfg.AllowInsecure, false); err != nil {
zap.S().Fatalf("Unable to create client: %s\n", err.Error())
}
defer c.Segment.Close()
auth, err := c.Keystone.GetAuth(cfg.Username, cfg.Password, cfg.Tenant, cfg.MfaToken)
if err != nil {
zap.S().Debug("Failed to get keystone %s", err.Error())
}
projectId := auth.ProjectID
token := auth.Token
projectNodes := c.Qbert.GetAllNodes(token, projectId)
nodeUuids := c.Resmgr.GetHostId(token, nodeIPs)
if err != nil {
zap.S().Fatalf("%v", err)
return
}
detachNodes, err := getNodesFromUuids(nodeUuids, projectNodes)
if err != nil {
zap.S().Fatalf(err.Error())
}
fmt.Println("Starting detaching process")
if err := c.Segment.SendEvent("Starting detach-node", auth, "", ""); err != nil {
zap.S().Debugf("Unable to send Segment event for detach node. Error: %s", err.Error())
}
for i := range detachNodes {
isMaster, err := c.Qbert.GetNodeInfo(token, projectId, nodeUuids[0])
if err != nil {
zap.S().Debugf("Failed to get node info for host %s: %s", nodeUuids[0], err.Error())
continue
}
clusterNodes := getAllClusterNodes(projectNodes, []string{isMaster.ClusterUuid})
if len(clusterNodes) == 1 || isMaster.IsMaster == 1 {
fmt.Printf("Node %v is either the master node or the last node in the cluster\n", isMaster.Uuid)
}
err1 := c.Qbert.DetachNode(detachNodes[i].ClusterUuid, projectId, token, detachNodes[i].Uuid)
if err1 != nil {
if err := c.Segment.SendEvent("Detaching-node", auth, "Failed to detach node", ""); err != nil {
zap.S().Debugf("Unable to send Segment event for detach node. Error: %s", err.Error())
}
zap.S().Info("Encountered an error while detaching the", detachNodes[i].PrimaryIp, " node from a Kubernetes cluster : ", err1)
} else {
if err := c.Segment.SendEvent("Detaching-node", detachNodes[i].PrimaryIp, "Node detached", ""); err != nil {
zap.S().Debugf("Unable to send Segment event for detach node. Error: %s", err.Error())
}
zap.S().Infof("Node [%v] detached from cluster", detachNodes[i].Uuid)
}
}
}
//returns the nodes whos ip's were passed in the flag (or the node installed on the machine if no ip was passed)
func getNodesFromUuids(nodeUuids []string, allNodes []qbert.Node) ([]qbert.Node, error) {
var nodesUuid []qbert.Node
for i := range allNodes {
for j := range nodeUuids {
if nodeUuids[j] == allNodes[i].Uuid {
if allNodes[i].ClusterUuid == "" {
return nodesUuid, fmt.Errorf("The node %v is not connected to any clusters", allNodes[i].PrimaryIp)
} else {
nodesUuid = append(nodesUuid, allNodes[i])
break
}
}
}
}
return nodesUuid, nil
}
//returns a list of all clusters the nodes are attached to
func getClusters(allNodes []qbert.Node) []string {
var clusters []string
for i := range allNodes {
clusterExists := false
for j := range clusters {
if clusters[j] == allNodes[i].ClusterUuid {
clusterExists = true
break
}
}
if !clusterExists {
clusters = append(clusters, allNodes[i].ClusterUuid)
}
}
return clusters
}
//returns all nodes attached to a specific clusters, used to detach all nodes from clusters
func getAllClusterNodes(allNodes []qbert.Node, clusters []string) []qbert.Node {
var clusterNodes []qbert.Node
for i := range allNodes {
for j := range clusters {
if allNodes[i].ClusterUuid == clusters[j] {
clusterNodes = append(clusterNodes, allNodes[i])
break
}
}
}
return clusterNodes
}