forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
175 lines (142 loc) · 4.64 KB
/
root.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
package main
import (
goflag "flag"
"fmt"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/kops/pkg/client/simple"
"k8s.io/kops/pkg/client/simple/vfsclientset"
"k8s.io/kops/upup/pkg/api"
"k8s.io/kops/upup/pkg/kutil"
"k8s.io/kops/util/pkg/vfs"
"os"
)
type RootCmd struct {
configFile string
clientset simple.Clientset
stateLocation string
clusterName string
cobraCommand *cobra.Command
}
var rootCommand = RootCmd{
cobraCommand: &cobra.Command{
Use: "kops",
Short: "kops is kubernetes ops",
Long: `kops is kubernetes ops.
It allows you to create, destroy, upgrade and maintain clusters.`,
},
}
func Execute() {
goflag.Set("logtostderr", "true")
goflag.CommandLine.Parse([]string{})
if err := rootCommand.cobraCommand.Execute(); err != nil {
exitWithError(err)
}
}
func init() {
cobra.OnInitialize(initConfig)
cmd := rootCommand.cobraCommand
cmd.PersistentFlags().AddGoFlagSet(goflag.CommandLine)
cmd.PersistentFlags().StringVar(&rootCommand.configFile, "config", "", "config file (default is $HOME/.kops.yaml)")
defaultStateStore := os.Getenv("KOPS_STATE_STORE")
cmd.PersistentFlags().StringVarP(&rootCommand.stateLocation, "state", "", defaultStateStore, "Location of state storage")
cmd.PersistentFlags().StringVarP(&rootCommand.clusterName, "name", "", "", "Name of cluster")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if rootCommand.configFile != "" {
// enable ability to specify config file via flag
viper.SetConfigFile(rootCommand.configFile)
}
viper.SetConfigName(".kops") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
func (c *RootCmd) AddCommand(cmd *cobra.Command) {
c.cobraCommand.AddCommand(cmd)
}
// ProcessArgs will parse the positional args. It assumes one of these formats:
// * <no arguments at all>
// * <clustername> (and --name not specified)
// Everything else is an error.
func (c *RootCmd) ProcessArgs(args []string) error {
if len(args) == 0 {
return nil
}
if len(args) == 1 {
// Assume <clustername>
if c.clusterName != "" {
return fmt.Errorf("Cannot specify cluster via --name and positional argument")
}
c.clusterName = args[0]
return nil
}
return fmt.Errorf("expected a single <clustername> to be passed as an argument")
}
func (c *RootCmd) ClusterName() string {
if c.clusterName != "" {
return c.clusterName
}
config, err := readKubectlClusterConfig()
if err != nil {
glog.Warningf("error reading kubecfg: %v", err)
} else if config != nil && config.Name != "" {
fmt.Fprintf(os.Stderr, "Using cluster from kubectl context: %s\n\n", config.Name)
c.clusterName = config.Name
}
return c.clusterName
}
func readKubectlClusterConfig() (*kutil.KubectlClusterWithName, error) {
kubectl := &kutil.Kubectl{}
context, err := kubectl.GetCurrentContext()
if err != nil {
return nil, fmt.Errorf("error getting current context from kubectl: %v", err)
}
glog.V(4).Infof("context = %q", context)
config, err := kubectl.GetConfig(true)
if err != nil {
return nil, fmt.Errorf("error getting current config from kubectl: %v", err)
}
// Minify should have done this
if len(config.Clusters) != 1 {
return nil, fmt.Errorf("expected exactly one cluster in kubectl config, found %d", len(config.Clusters))
}
return config.Clusters[0], nil
}
func (c *RootCmd) Clientset() (simple.Clientset, error) {
basePath, err := vfs.Context.BuildVfsPath(c.stateLocation)
if err != nil {
return nil, fmt.Errorf("error building state store path for %q: %v", c.stateLocation, err)
}
if !vfs.IsClusterReadable(basePath) {
return nil, fmt.Errorf("State store %q is not cloud-reachable - please use an S3 bucket", c.stateLocation)
}
clientset := vfsclientset.NewVFSClientset(basePath)
return clientset, nil
}
func (c *RootCmd) Cluster() (*api.Cluster, error) {
clientset, err := c.Clientset()
if err != nil {
return nil, err
}
clusterName := c.ClusterName()
if clusterName == "" {
return nil, fmt.Errorf("--name is required")
}
cluster, err := clientset.Clusters().Get(clusterName)
if err != nil {
return nil, fmt.Errorf("error reading cluster configuration: %v", err)
}
if cluster == nil {
return nil, fmt.Errorf("cluster %q not found", clusterName)
}
if clusterName != cluster.Name {
return nil, fmt.Errorf("cluster name did not match expected name: %v vs %v", clusterName, cluster.Name)
}
return cluster, nil
}