forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.go
283 lines (256 loc) · 8.46 KB
/
helper.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package openshift
import (
"fmt"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kclientcmd "k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
_ "github.com/openshift/origin/pkg/cmd/server/apis/config/install"
configapilatest "github.com/openshift/origin/pkg/cmd/server/apis/config/latest"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/oc/clusterup/docker/dockerhelper"
"github.com/openshift/origin/pkg/oc/clusterup/docker/errors"
"github.com/openshift/origin/pkg/oc/clusterup/docker/run"
)
const (
defaultNodeName = "localhost"
DefaultDNSPort = 8053
DefaultSvcCIDR = "172.30.0.0/16"
cmdDetermineNodeHost = "for name in %s; do ls /var/lib/origin/openshift.local.config/node-$name &> /dev/null && echo $name && break; done"
// TODO: Figure out why cluster up relies on this name
ContainerName = "origin"
Namespace = "openshift"
)
var (
BasePorts = []int{4001, 7001, 8443, 10250, DefaultDNSPort}
RouterPorts = []int{80, 443}
AllPorts = append(RouterPorts, BasePorts...)
SocatPidFile = filepath.Join(homedir.HomeDir(), kclientcmd.RecommendedHomeDir, "socat-8443.pid")
)
// Helper contains methods and utilities to help with OpenShift startup
type Helper struct {
dockerHelper *dockerhelper.Helper
runHelper *run.RunHelper
image string
containerName string
serverIP string
}
// NewHelper creates a new OpenShift helper
func NewHelper(dockerHelper *dockerhelper.Helper, image, containerName string) *Helper {
return &Helper{
dockerHelper: dockerHelper,
runHelper: run.NewRunHelper(dockerHelper),
image: image,
containerName: containerName,
}
}
func (h *Helper) TestPorts(ports []int) error {
_, portData, _, _, err := h.runHelper.New().Image(h.image).
DiscardContainer().
Privileged().
HostNetwork().
HostPid().
Entrypoint("/bin/bash").
Command("-c", "cat /proc/net/tcp && ( [ -e /proc/net/tcp6 ] && cat /proc/net/tcp6 || true)").
Output()
if err != nil {
return errors.NewError("Cannot get TCP port information from Kubernetes host").WithCause(err)
}
return checkPortsInUse(portData, ports)
}
func testIPDial(ip string) error {
// Attempt to connect to test container
testHost := fmt.Sprintf("%s:8443", ip)
glog.V(4).Infof("Attempting to dial %s", testHost)
if err := cmdutil.WaitForSuccessfulDial(false, "tcp", testHost, 200*time.Millisecond, 1*time.Second, 10); err != nil {
glog.V(2).Infof("Dial error: %v", err)
return err
}
glog.V(4).Infof("Successfully dialed %s", testHost)
return nil
}
func (h *Helper) TestIP(ip string) error {
// Start test server on host
id, err := h.runHelper.New().Image(h.image).
Privileged().
HostNetwork().
Entrypoint("socat").
Command("TCP-LISTEN:8443,crlf,reuseaddr,fork", "SYSTEM:\"echo 'hello world'\"").Start()
if err != nil {
return errors.NewError("cannot start simple server on Docker host").WithCause(err)
}
defer func() {
errors.LogError(h.dockerHelper.StopAndRemoveContainer(id))
}()
return testIPDial(ip)
}
func (h *Helper) TestForwardedIP(ip string) error {
// Start test server on host
id, err := h.runHelper.New().Image(h.image).
PortForward(8443, 8443).
Entrypoint("socat").
Command("TCP-LISTEN:8443,crlf,reuseaddr,fork", "SYSTEM:\"echo 'hello world'\"").Start()
if err != nil {
return errors.NewError("cannot start simple server on Docker host").WithCause(err)
}
defer func() {
errors.LogError(h.dockerHelper.StopAndRemoveContainer(id))
}()
return testIPDial(ip)
}
func (h *Helper) DetermineNodeHost(hostConfigDir string, names ...string) (string, error) {
_, result, _, _, err := h.runHelper.New().Image(h.image).
DiscardContainer().
Privileged().
HostNetwork().
Entrypoint("/bin/bash").
Bind(fmt.Sprintf("%s:/var/lib/origin/openshift.local.config", hostConfigDir)).
Command("-c", fmt.Sprintf(cmdDetermineNodeHost, strings.Join(names, " "))).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(result), nil
}
// ServerIP retrieves the Server ip through the openshift start command
func (h *Helper) ServerIP() (string, error) {
if len(h.serverIP) > 0 {
return h.serverIP, nil
}
_, result, _, _, err := h.runHelper.New().Image(h.image).
DiscardContainer().
Privileged().
HostNetwork().
Command("start", "--print-ip").Output()
if err != nil {
return "", err
}
h.serverIP = strings.TrimSpace(result)
return h.serverIP, nil
}
// OtherIPs tries to find other IPs besides the argument IP for the Docker host
func (h *Helper) OtherIPs(excludeIP string) ([]string, error) {
_, result, _, _, err := h.runHelper.New().Image(h.image).
DiscardContainer().
Privileged().
HostNetwork().
Entrypoint("hostname").
Command("-I").Output()
if err != nil {
return nil, err
}
candidates := strings.Split(result, " ")
resultIPs := []string{}
for _, ip := range candidates {
if len(strings.TrimSpace(ip)) == 0 {
continue
}
if ip != excludeIP && !strings.Contains(ip, ":") { // for now, ignore IPv6
resultIPs = append(resultIPs, ip)
}
}
return resultIPs, nil
}
// CheckNodes determines if there is more than one node that corresponds to the
// current machine and removes the one that doesn't match the default node name
func (h *Helper) CheckNodes(kclient kclientset.Interface) error {
nodes, err := kclient.Core().Nodes().List(metav1.ListOptions{})
if err != nil {
return errors.NewError("cannot retrieve nodes").WithCause(err)
}
if len(nodes.Items) > 1 {
glog.V(2).Infof("Found more than one node, will attempt to remove duplicate nodes")
nodesToRemove := []string{}
// First, find default node
defaultNodeMachineId := ""
for i := 0; i < len(nodes.Items); i++ {
if nodes.Items[i].Name == defaultNodeName {
defaultNodeMachineId = nodes.Items[i].Status.NodeInfo.MachineID
glog.V(5).Infof("machine id for default node is: %s", defaultNodeMachineId)
break
}
}
for i := 0; i < len(nodes.Items); i++ {
if nodes.Items[i].Name != defaultNodeName &&
nodes.Items[i].Status.NodeInfo.MachineID == defaultNodeMachineId {
glog.V(5).Infof("Found non-default node with duplicate machine id: %s", nodes.Items[i].Name)
nodesToRemove = append(nodesToRemove, nodes.Items[i].Name)
}
}
for i := 0; i < len(nodesToRemove); i++ {
glog.V(2).Infof("Deleting extra node %s", nodesToRemove[i])
err = kclient.Core().Nodes().Delete(nodesToRemove[i], nil)
if err != nil {
return errors.NewError("cannot delete duplicate node %s", nodesToRemove[i]).WithCause(err)
}
}
}
return nil
}
func (h *Helper) OriginLog() string {
log := h.dockerHelper.ContainerLog(h.containerName, 10)
if len(log) > 0 {
return fmt.Sprintf("Last 10 lines of %q container log:\n%s\n", h.containerName, log)
}
return fmt.Sprintf("No log available from %q container\n", h.containerName)
}
func (h *Helper) Master(ip string) string {
return fmt.Sprintf("https://%s:8443", ip)
}
func (h *Helper) GetConfigFromLocalDir(configDir string) (*configapi.MasterConfig, string, error) {
configPath := filepath.Join(configDir, "master-config.yaml")
glog.V(1).Infof("Reading master config from %s", configPath)
cfg, err := configapilatest.ReadMasterConfig(configPath)
if err != nil {
glog.V(2).Infof("Could not read master config: %v", err)
return nil, "", err
}
return cfg, configPath, nil
}
func checkPortsInUse(data string, ports []int) error {
used := getUsedPorts(data)
conflicts := []int{}
for _, port := range ports {
if _, inUse := used[port]; inUse {
conflicts = append(conflicts, port)
}
}
if len(conflicts) > 0 {
return ErrPortsNotAvailable(conflicts)
}
return nil
}
func getUsedPorts(data string) map[int]struct{} {
ports := map[int]struct{}{}
lines := strings.Split(data, "\n")
for _, line := range lines {
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
// discard lines that don't contain connection data
if !strings.Contains(parts[0], ":") {
continue
}
glog.V(5).Infof("Determining port in use from: %s", line)
localAddress := strings.Split(parts[1], ":")
if len(localAddress) < 2 {
continue
}
state := parts[3]
if state != "0A" { // only look at connections that are listening
continue
}
port, err := strconv.ParseInt(localAddress[1], 16, 0)
if err == nil {
ports[int(port)] = struct{}{}
}
}
glog.V(2).Infof("Used ports in container: %#v", ports)
return ports
}