-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
cluster_inspector.go
107 lines (90 loc) · 2.81 KB
/
cluster_inspector.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
/*
Copyright 2018 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tunnel
import (
"fmt"
"net"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/state"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/util"
)
type clusterInspector struct {
machineAPI libmachine.API
configLoader config.Loader
machineName string
}
func (m *clusterInspector) getStateAndHost() (HostState, *host.Host, error) {
h, err := machine.LoadHost(m.machineAPI, m.machineName)
if err != nil {
err = errors.Wrapf(err, "error loading docker-machine host for: %s", m.machineName)
return Unknown, nil, err
}
var s state.State
s, err = h.Driver.GetState()
if err != nil {
err = errors.Wrapf(err, "error getting host status for %s", m.machineName)
return Unknown, nil, err
}
if s == state.Running {
return Running, h, nil
}
return Stopped, h, nil
}
func (m *clusterInspector) getStateAndRoute() (HostState, *Route, error) {
hostState, h, err := m.getStateAndHost()
defer m.machineAPI.Close()
if err != nil {
return hostState, nil, err
}
var c *config.ClusterConfig
c, err = m.configLoader.LoadConfigFromFile(m.machineName)
if err != nil {
err = errors.Wrapf(err, "error loading config for %s", m.machineName)
return hostState, nil, err
}
var route *Route
route, err = getRoute(h, *c)
if err != nil {
err = errors.Wrapf(err, "error getting route info for %s", m.machineName)
return hostState, nil, err
}
return hostState, route, nil
}
func getRoute(host *host.Host, clusterConfig config.ClusterConfig) (*Route, error) {
hostDriverIP, err := host.Driver.GetIP()
if err != nil {
return nil, errors.Wrapf(err, "error getting host IP for %s", host.Name)
}
_, ipNet, err := net.ParseCIDR(clusterConfig.KubernetesConfig.ServiceCIDR)
if err != nil {
return nil, fmt.Errorf("error parsing service CIDR: %s", err)
}
ip := net.ParseIP(hostDriverIP)
if ip == nil {
return nil, fmt.Errorf("invalid IP for host %s", hostDriverIP)
}
dnsIP, err := util.GetDNSIP(ipNet.String())
if err != nil {
return nil, err
}
return &Route{
Gateway: ip,
DestCIDR: ipNet,
ClusterDomain: clusterConfig.KubernetesConfig.DNSDomain,
ClusterDNSIP: dnsIP,
}, nil
}