-
Notifications
You must be signed in to change notification settings - Fork 582
/
util.go
213 lines (183 loc) · 6.45 KB
/
util.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cmd
import (
"context"
"encoding/json"
"fmt"
"github.com/rancher/rke/cluster"
"github.com/rancher/rke/hosts"
"github.com/rancher/rke/k8s"
"github.com/rancher/rke/pki"
v3 "github.com/rancher/rke/types"
"github.com/rancher/rke/util"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func UtilCommand() cli.Command {
utilCfgFlags := []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "Specify an alternate cluster YAML file",
Value: pki.ClusterConfig,
EnvVar: "RKE_CONFIG",
},
}
utilFlags := append(utilCfgFlags, commonFlags...)
return cli.Command{
Name: "util",
Usage: "Various utilities to retrieve cluster related files and troubleshoot",
Subcommands: cli.Commands{
cli.Command{
Name: "get-state-file",
Usage: "Retrieve state file from cluster",
Action: getStateFile,
Flags: utilFlags,
},
cli.Command{
Name: "get-kubeconfig",
Usage: "Retrieve kubeconfig file from cluster state",
Action: getKubeconfigFile,
Flags: utilFlags,
},
},
}
}
func getKubeconfigFile(ctx *cli.Context) error {
logrus.Infof("Creating new kubeconfig file")
// Check if we can successfully connect to the cluster using the existing kubeconfig file
clusterFile, clusterFilePath, err := resolveClusterFile(ctx)
if err != nil {
return fmt.Errorf("failed to resolve cluster file: %v", err)
}
// setting up the flags
flags := cluster.GetExternalFlags(false, false, false, false, "", clusterFilePath)
rkeConfig, err := cluster.ParseConfig(clusterFile)
if err != nil {
return fmt.Errorf("failed to parse cluster file: %v", err)
}
rkeConfig, err = setOptionsFromCLI(ctx, rkeConfig)
if err != nil {
return err
}
clusterState, err := cluster.ReadStateFile(context.Background(), cluster.GetStateFilePath(flags.ClusterFilePath, flags.ConfigDir))
if err != nil {
return err
}
// Creating temp cluster to check if snapshot archive contains state file and retrieve it
tempCluster, err := cluster.InitClusterObject(context.Background(), rkeConfig, flags, "")
if err != nil {
return err
}
// Move current kubeconfig file
err = util.CopyFileWithPrefix(tempCluster.LocalKubeConfigPath, "kube_config")
if err != nil {
return err
}
kubeCluster, _ := tempCluster.GetClusterState(context.Background(), clusterState)
return cluster.RebuildKubeconfig(context.Background(), kubeCluster)
}
func getStateFile(ctx *cli.Context) error {
logrus.Infof("Retrieving state file from cluster")
// Check if we can successfully connect to the cluster using the existing kubeconfig file
localKubeConfig := pki.GetLocalKubeConfig(ctx.String("config"), "")
clusterFile, clusterFilePath, err := resolveClusterFile(ctx)
if err != nil {
return fmt.Errorf("failed to resolve cluster file: %v", err)
}
// setting up the flags
flags := cluster.GetExternalFlags(false, false, false, false, "", clusterFilePath)
// not going to use a k8s dialer here.. this is a CLI command
serverVersion, err := cluster.GetK8sVersion(localKubeConfig, nil)
if err != nil {
logrus.Infof("Unable to connect to server using kubeconfig, trying to get state from Control Plane node(s), error: %v", err)
// We need to retrieve the state file using Docker on the node(s)
rkeConfig, err := cluster.ParseConfig(clusterFile)
if err != nil {
return fmt.Errorf("failed to parse cluster file: %v", err)
}
rkeConfig, err = setOptionsFromCLI(ctx, rkeConfig)
if err != nil {
return err
}
_, _, _, _, _, err = RetrieveClusterStateConfigMap(context.Background(), rkeConfig, hosts.DialersOptions{}, flags, map[string]interface{}{})
if err != nil {
return err
}
return nil
}
logrus.Infof("Successfully connected to server using kubeconfig, retrieved server version [%s]", serverVersion)
// Retrieve full-cluster-state configmap
k8sClient, err := k8s.NewClient(localKubeConfig, nil)
if err != nil {
return err
}
cfgMap, err := k8s.GetConfigMap(k8sClient, cluster.FullStateConfigMapName)
if err != nil {
return err
}
clusterData := cfgMap.Data[cluster.FullStateConfigMapName]
rkeFullState := &cluster.FullState{}
if err = json.Unmarshal([]byte(clusterData), rkeFullState); err != nil {
return err
}
// Move current state file
stateFilePath := cluster.GetStateFilePath(flags.ClusterFilePath, flags.ConfigDir)
err = util.ReplaceFileWithBackup(stateFilePath, "rkestate")
if err != nil {
return err
}
// Write new state file
err = rkeFullState.WriteStateFile(context.Background(), stateFilePath)
if err != nil {
return err
}
return nil
}
func RetrieveClusterStateConfigMap(
ctx context.Context,
rkeConfig *v3.RancherKubernetesEngineConfig,
dialersOptions hosts.DialersOptions,
flags cluster.ExternalFlags,
data map[string]interface{}) (string, string, string, string, map[string]pki.CertificatePKI, error) {
var APIURL, caCrt, clientCert, clientKey string
rkeFullState := &cluster.FullState{}
// Creating temp cluster to check if snapshot archive contains state file and retrieve it
tempCluster, err := cluster.InitClusterObject(ctx, rkeConfig, flags, "")
if err != nil {
return APIURL, caCrt, clientCert, clientKey, nil, err
}
if err := tempCluster.SetupDialers(ctx, dialersOptions); err != nil {
return APIURL, caCrt, clientCert, clientKey, nil, err
}
if err := tempCluster.TunnelHosts(ctx, flags); err != nil {
return APIURL, caCrt, clientCert, clientKey, nil, err
}
// Get ConfigMap containing cluster state from Control Plane Hosts
stateFile, err := tempCluster.GetStateFileFromConfigMap(ctx)
if err != nil {
return APIURL, caCrt, clientCert, clientKey, nil, err
}
rkeFullState, err = cluster.StringToFullState(ctx, stateFile)
if err != nil {
return APIURL, caCrt, clientCert, clientKey, nil, err
}
// Move current state file
stateFilePath := cluster.GetStateFilePath(flags.ClusterFilePath, flags.ConfigDir)
err = util.ReplaceFileWithBackup(stateFilePath, "rkestate")
if err != nil {
return APIURL, caCrt, clientCert, clientKey, nil, err
}
err = rkeFullState.WriteStateFile(context.Background(), stateFilePath)
if err != nil {
return APIURL, caCrt, clientCert, clientKey, nil, err
}
// Move current kubeconfig file
err = util.CopyFileWithPrefix(tempCluster.LocalKubeConfigPath, "kube_config")
if err != nil {
return APIURL, caCrt, clientCert, clientKey, nil, err
}
kubeCluster, _ := tempCluster.GetClusterState(ctx, rkeFullState)
if err := cluster.RebuildKubeconfig(ctx, kubeCluster); err != nil {
return APIURL, caCrt, clientCert, clientKey, nil, nil
}
return APIURL, caCrt, clientCert, clientKey, nil, nil
}