forked from openshift/origin
/
start_allinone.go
244 lines (193 loc) · 7.72 KB
/
start_allinone.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package start
import (
"errors"
"fmt"
"net"
"net/http"
_ "net/http/pprof"
"strings"
"github.com/coreos/go-systemd/daemon"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/openshift/origin/pkg/cmd/server/admin"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
_ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/admit"
_ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/limitranger"
_ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/namespace/exists"
_ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/resourcedefaults"
_ "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/admission/resourcequota"
)
type AllInOneOptions struct {
MasterArgs *MasterArgs
NodeArgs *NodeArgs
WriteConfigOnly bool
MasterConfigFile string
NodeConfigFile string
}
const longAllInOneCommandDesc = `
Start an OpenShift all-in-one server
This command helps you launch an OpenShift all-in-one server, which allows
you to run all of the components of an OpenShift system on a server with Docker. Running
$ openshift start
will start OpenShift listening on all interfaces, launch an etcd server to store persistent
data, and launch the Kubernetes system components. The server will run in the foreground until
you terminate the process. This command delegates to "openshift start master" and
"openshift start node".
Note: starting OpenShift without passing the --master address will attempt to find the IP
address that will be visible inside running Docker containers. This is not always successful,
so if you have problems tell OpenShift what public address it will be via --master=<ip>.
You may also pass --etcd=<address> to connect to an external etcd server.
You may also pass --kubeconfig=<path> to connect to an external Kubernetes cluster.
`
// NewCommandStartMaster provides a CLI handler for 'start' command
func NewCommandStartAllInOne() (*cobra.Command, *AllInOneOptions) {
options := &AllInOneOptions{}
cmd := &cobra.Command{
Use: "start",
Short: "Launch OpenShift All-In-One",
Long: longAllInOneCommandDesc,
Run: func(c *cobra.Command, args []string) {
if err := options.Complete(); err != nil {
fmt.Println(err.Error())
c.Help()
return
}
if err := options.Validate(args); err != nil {
fmt.Println(err.Error())
c.Help()
return
}
startProfiler()
if err := options.StartAllInOne(); err != nil {
glog.Fatal(err)
}
},
}
flags := cmd.Flags()
flags.BoolVar(&options.WriteConfigOnly, "write-config", false, "Indicates that the command should build the configuration from command-line arguments, write it to the locations specified by --master-config and --node-config, and exit.")
flags.StringVar(&options.MasterConfigFile, "master-config", "", "Location of the master configuration file to run from, or write to (when used with --write-config). When running from configuration files, all other command-line arguments are ignored.")
flags.StringVar(&options.NodeConfigFile, "node-config", "", "Location of the node configuration file to run from, or write to (when used with --write-config). When running from configuration files, all other command-line arguments are ignored.")
masterArgs, nodeArgs, listenArg, imageFormatArgs, kubeConnectionArgs, certArgs := GetAllInOneArgs()
options.MasterArgs, options.NodeArgs = masterArgs, nodeArgs
// by default, all-in-ones all disabled docker. Set it here so that if we allow it to be bound later, bindings take precendence
options.NodeArgs.AllowDisabledDocker = true
BindMasterArgs(masterArgs, flags, "")
BindNodeArgs(nodeArgs, flags, "")
BindListenArg(listenArg, flags, "")
BindPolicyArgs(options.MasterArgs.PolicyArgs, flags, "")
BindImageFormatArgs(imageFormatArgs, flags, "")
BindKubeConnectionArgs(kubeConnectionArgs, flags, "")
BindCertArgs(certArgs, flags, "")
startMaster, _ := NewCommandStartMaster()
startNode, _ := NewCommandStartNode()
cmd.AddCommand(startMaster)
cmd.AddCommand(startNode)
return cmd, options
}
// GetAllInOneArgs makes sure that the node and master args that should be shared, are shared
func GetAllInOneArgs() (*MasterArgs, *NodeArgs, *ListenArg, *ImageFormatArgs, *KubeConnectionArgs, *CertArgs) {
masterArgs := NewDefaultMasterArgs()
nodeArgs := NewDefaultNodeArgs()
listenArg := NewDefaultListenArg()
masterArgs.ListenArg = listenArg
nodeArgs.ListenArg = listenArg
imageFormatArgs := NewDefaultImageFormatArgs()
masterArgs.ImageFormatArgs = imageFormatArgs
nodeArgs.ImageFormatArgs = imageFormatArgs
kubeConnectionArgs := NewDefaultKubeConnectionArgs()
masterArgs.KubeConnectionArgs = kubeConnectionArgs
nodeArgs.KubeConnectionArgs = kubeConnectionArgs
certArgs := NewDefaultCertArgs()
masterArgs.CertArgs = certArgs
nodeArgs.CertArgs = certArgs
kubeConnectionArgs.CertArgs = certArgs
return masterArgs, nodeArgs, listenArg, imageFormatArgs, kubeConnectionArgs, certArgs
}
func (o AllInOneOptions) Validate(args []string) error {
if len(args) != 0 {
return errors.New("no arguments are supported for start")
}
if o.WriteConfigOnly {
if len(o.MasterConfigFile) == 0 {
return errors.New("--master-config is required if --write-config is true")
}
if len(o.NodeConfigFile) == 0 {
return errors.New("--node-config is required if --write-config is true")
}
}
if err := o.MasterArgs.Validate(); err != nil {
return err
}
return nil
}
func (o AllInOneOptions) Complete() error {
nodeList := util.NewStringSet(strings.ToLower(o.NodeArgs.NodeName))
// take everything toLower
for _, s := range o.MasterArgs.NodeList {
nodeList.Insert(strings.ToLower(s))
}
o.MasterArgs.NodeList = nodeList.List()
o.NodeArgs.NodeName = strings.ToLower(o.NodeArgs.NodeName)
return nil
}
// StartAllInOne:
// 1. Creates the signer certificate if needed
// 2. Calls RunMaster
// 3. Calls RunNode
// 4. If only writing configs, it exits
// 5. Waits forever
func (o AllInOneOptions) StartAllInOne() error {
if !o.WriteConfigOnly {
glog.Infof("Starting an OpenShift all-in-one")
}
// if either one of these wants to mint certs, make sure the signer is present BEFORE they start up to make sure they always share
if o.MasterArgs.CertArgs.CreateCerts || o.NodeArgs.CertArgs.CreateCerts {
signerOptions := &admin.CreateSignerCertOptions{
CertFile: admin.DefaultCertFilename(o.NodeArgs.CertArgs.CertDir, "ca"),
KeyFile: admin.DefaultKeyFilename(o.NodeArgs.CertArgs.CertDir, "ca"),
SerialFile: admin.DefaultSerialFilename(o.NodeArgs.CertArgs.CertDir, "ca"),
Name: admin.DefaultSignerName(),
}
if err := signerOptions.Validate(nil); err != nil {
return err
}
if _, err := signerOptions.CreateSignerCert(); err != nil {
return err
}
}
masterOptions := MasterOptions{o.MasterArgs, o.WriteConfigOnly, o.MasterConfigFile}
masterAddr, err := masterOptions.MasterArgs.GetMasterAddress()
if err != nil {
return nil
}
// in the all-in-one, default kubernetes URL to the master's address
o.NodeArgs.DefaultKubernetesURL = *masterAddr
// in the all-in-one, default ClusterDNS to the master's address
if host, _, err := net.SplitHostPort(masterAddr.Host); err == nil {
if ip := net.ParseIP(host); ip != nil {
o.NodeArgs.ClusterDNS = ip
}
}
nodeOptions := NodeOptions{o.NodeArgs, o.WriteConfigOnly, o.NodeConfigFile}
if err := masterOptions.RunMaster(); err != nil {
return err
}
if err := nodeOptions.RunNode(); err != nil {
return err
}
if o.WriteConfigOnly {
return nil
}
daemon.SdNotify("READY=1")
select {}
return nil
}
func startProfiler() {
if cmdutil.Env("OPENSHIFT_PROFILE", "") == "web" {
go func() {
glog.Infof("Starting profiling endpoint at http://127.0.0.1:6060/debug/pprof/")
glog.Fatal(http.ListenAndServe("127.0.0.1:6060", nil))
}()
}
}