-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
180 lines (154 loc) · 4.79 KB
/
config.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
package kubernetes
import (
"errors"
"fmt"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
)
type configCommand struct {
*commands.BaseCommand
resolver.CachingKubernetes
completion.Kubernetes
pathOptions *clientcmd.PathOptions
}
// ConfigCommand creates the "connection config" command
func ConfigCommand() commands.Command {
return &configCommand{
BaseCommand: commands.New(
"config",
"Output Kubernetes cluster kubeconfig",
`upctl kubernetes config 0fa980c4-0e4f-460b-9869-11b7bd62b831 --output human`,
`upctl kubernetes config 0fa980c4-0e4f-460b-9869-11b7bd62b831 --output yaml --write $KUBECONFIG`,
`upctl kubernetes config 0fa980c4-0e4f-460b-9869-11b7bd62b831 --output yaml --write ./my_kubeconfig.yaml`,
),
pathOptions: clientcmd.NewDefaultPathOptions(),
}
}
// InitCommand implements Command.InitCommand
func (c *configCommand) InitCommand() {
flagSet := &pflag.FlagSet{}
flagSet.StringVar(
&c.pathOptions.LoadingRules.ExplicitPath,
"write",
"",
"Absolute path for writing output. If the file exists, the config will be merged.")
c.AddFlags(flagSet)
}
// Execute implements commands.MultipleArgumentCommand
func (c *configCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.All()
msg := fmt.Sprintf("Getting kubeconfig for Kubernetes cluster %s", uuid)
exec.PushProgressStarted(msg)
resp, err := svc.GetKubernetesKubeconfig(exec.Context(), &request.GetKubernetesKubeconfigRequest{
UUID: uuid,
})
if err != nil {
return commands.HandleError(exec, msg, err)
}
config, err := clientcmd.Load([]byte(resp))
if err != nil {
return commands.HandleError(exec, msg, err)
}
exec.PushProgressSuccess(msg)
if c.Cobra().Flag("write").Changed {
return c.write(exec, uuid, config)
}
return c.output(exec, config, resp, msg)
}
func (c *configCommand) output(exec commands.Executor, config *api.Config, resp string, msg string) (output.Output, error) {
clusters := make([]output.TableRow, 0)
for _, v := range config.Clusters {
clusters = append(clusters, output.TableRow{
v.Server,
v.InsecureSkipTLSVerify,
})
}
contexts := make([]output.TableRow, 0)
for _, v := range config.Contexts {
contexts = append(contexts, output.TableRow{
v.Cluster,
v.AuthInfo,
})
}
var value interface{}
err := yaml.Unmarshal([]byte(resp), &value)
if err != nil {
return commands.HandleError(exec, msg, err)
}
return output.MarshaledWithHumanOutput{
Value: value,
Output: output.Combined{
output.CombinedSection{
Contents: output.Details{
Sections: []output.DetailSection{
{
Title: "Overview:",
Rows: []output.DetailRow{
{Title: "Current context:", Value: config.CurrentContext},
},
},
},
},
},
output.CombinedSection{
Title: "Clusters",
Contents: output.Table{
Columns: []output.TableColumn{
{Key: "server", Header: "Server"},
{Key: "insecure_skip_tls_verify", Header: "Insecure skip TLS verify"},
},
Rows: clusters,
HideHeader: false,
},
},
output.CombinedSection{
Title: "Contexts",
Contents: output.Table{
Columns: []output.TableColumn{
{Key: "cluster", Header: "Cluster"},
{Key: "authinfo", Header: "Authinfo"},
},
Rows: contexts,
HideHeader: false,
},
},
},
}, nil
}
func (c *configCommand) write(exec commands.Executor, uuid string, config *api.Config) (output.Output, error) {
msg := fmt.Sprintf("Writing kubeconfig for Kubernetes cluster %s to destination %s", uuid, c.pathOptions.GetDefaultFilename())
exec.PushProgressStarted(msg)
if c.Cobra().Flag("write").Value.String() == c.Cobra().Flag("write").DefValue {
return commands.HandleError(exec, msg, errors.New("invalid write path"))
}
startingConfig, err := c.pathOptions.GetStartingConfig()
if err != nil {
return commands.HandleError(exec, msg, err)
}
err = clientcmd.ModifyConfig(c.pathOptions, mergeConfig(startingConfig, config), false)
if err != nil {
return commands.HandleError(exec, msg, err)
}
exec.PushProgressSuccess(msg)
return output.None{}, nil
}
func mergeConfig(startingConfig, newConfig *api.Config) api.Config {
startingConfig.CurrentContext = newConfig.CurrentContext
for k, v := range newConfig.Clusters {
startingConfig.Clusters[k] = v
}
for k, v := range newConfig.AuthInfos {
startingConfig.AuthInfos[k] = v
}
for k, v := range newConfig.Contexts {
startingConfig.Contexts[k] = v
}
return *startingConfig
}