-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
129 lines (111 loc) · 3.54 KB
/
registry.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
package fleet
import (
"fmt"
"net"
"net/http"
"strings"
"time"
"github.com/coreos/fleet/client"
"github.com/coreos/fleet/etcd"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/pkg"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/ssh"
)
// Flags used for Fleet API connectivity
var Flags = struct {
Debug bool
Version bool
Endpoint string
EtcdKeyPrefix string
EtcdKeyFile string
EtcdCertFile string
EtcdCAFile string
UseAPI bool
KnownHostsFile string
StrictHostKeyChecking bool
Tunnel string
RequestTimeout float64
SSHTimeout float64
}{}
const (
oldVersionWarning = `####################################################################
WARNING: fleetctl (%s) is older than the latest registered
version of fleet found in the cluster (%s). You are strongly
recommended to upgrade fleetctl to prevent incompatibility issues.
####################################################################
`
)
// global API client used by commands
var cAPI client.API
// used to cache MachineStates
var machineStates map[string]*machine.MachineState
var requestTimeout = time.Duration(10) * time.Second
func getTunnelFlag() string {
tun := Flags.Tunnel
if tun != "" && !strings.Contains(tun, ":") {
tun += ":22"
}
return tun
}
func getChecker() *ssh.HostKeyChecker {
if !Flags.StrictHostKeyChecking {
return nil
}
keyFile := ssh.NewHostKeyFile(Flags.KnownHostsFile)
return ssh.NewHostKeyChecker(keyFile)
}
func getFakeClient() (*registry.FakeRegistry, error) {
return registry.NewFakeRegistry(), nil
}
func getRegistryClient() (client.API, error) {
var dial func(string, string) (net.Conn, error)
sshTimeout := time.Duration(Flags.SSHTimeout*1000) * time.Millisecond
tun := getTunnelFlag()
if tun != "" {
sshClient, err := ssh.NewSSHClient("core", tun, getChecker(), false, sshTimeout)
if err != nil {
return nil, fmt.Errorf("failed initializing SSH client: %v", err)
}
dial = func(network, addr string) (net.Conn, error) {
tcpaddr, err := net.ResolveTCPAddr(network, addr)
if err != nil {
return nil, err
}
return sshClient.DialTCP(network, nil, tcpaddr)
}
}
tlsConfig, err := pkg.ReadTLSConfigFiles(Flags.EtcdCAFile, Flags.EtcdCertFile, Flags.EtcdKeyFile)
if err != nil {
return nil, err
}
trans := &http.Transport{
Dial: dial,
TLSClientConfig: tlsConfig,
}
timeout := time.Duration(Flags.RequestTimeout*1000) * time.Millisecond
machines := []string{Flags.Endpoint}
eClient, err := etcd.NewClient(machines, trans, timeout)
if err != nil {
return nil, err
}
reg := registry.NewEtcdRegistry(eClient, Flags.EtcdKeyPrefix)
// if msg, ok := checkVersion(reg); !ok {
// stderr(msg)
// }
return &client.RegistryClient{Registry: reg}, nil
}
// checkVersion makes a best-effort attempt to verify that fleetctl is at least as new as the
// latest fleet version found registered in the cluster. If any errors are encountered or fleetctl
// is >= the latest version found, it returns true. If it is < the latest found version, it returns
// false and a scary warning to the user.
// func checkVersion(reg registry.Registry) (string, bool) {
// fv := version.SemVersion
// lv, err := reg.LatestVersion()
// if err != nil {
// fmt.Printf("error attempting to check latest fleet version in Registry: %v", err)
// } else if lv != nil && fv.LessThan(*lv) {
// return fmt.Sprintf(oldVersionWarning, fv.String(), lv.String()), false
// }
// return "", true
// }