forked from makethunder/terraform-provider-fleet
/
provider.go
216 lines (179 loc) · 5.62 KB
/
provider.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
package main
import (
"net"
"net/http"
"net/url"
"strings"
"time"
"fmt"
"log"
"github.com/coreos/fleet/client"
"github.com/coreos/fleet/pkg"
"github.com/coreos/fleet/ssh"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/coreos/fleet/version"
"github.com/coreos/fleet/registry"
etcd "github.com/coreos/fleet/Godeps/_workspace/src/github.com/coreos/etcd/client"
)
const oldVersionWarning = `####################################################################
WARNING: The linked against fleet go lib (%s) is older than the latest
registered version of fleet found in the cluster (%s). You are strongly
recommended to upgrade the linked fleet go lib and rebuild to prevent
incompatibility issues.
####################################################################
`
const defaultTimeout = time.Second*10
// retry wraps a function with retry logic. Only errors containing "timed out"
// will be retried. (authentication errors and other stuff should fail
// immediately)
func retry(f func() (interface{}, error), maxRetries int) (interface{}, error) {
var result interface{}
var err error
for retries := 0; retries < maxRetries; retries++ {
result, err = f()
if err == nil || !strings.Contains(err.Error(), "timed out") {
break
}
}
return result, err
}
func checkVersion(cReg registry.ClusterRegistry) (string, bool) {
fv := version.SemVersion
lv, err := cReg.LatestDaemonVersion()
if err != nil {
log.Fatal("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
}
func getHTTPClient(driverEndpoint string) (client.API, error) {
log.Printf("Using API connection for requests")
endpoint, err := url.Parse(driverEndpoint)
if err != nil {
return nil, err
}
trans := pkg.LoggingHTTPTransport{
Transport: http.Transport{},
}
httpClient := http.Client{
Transport: &trans,
}
return client.NewHTTPClient(&httpClient, *endpoint)
}
func getETCDClient(driverEndpoint string, etcdKeyPrefix string) (client.API, error) {
log.Printf("Using ETCD connection for requests")
trans := &http.Transport{}
eCfg := etcd.Config{
Endpoints: strings.Split(driverEndpoint, ","),
Transport: trans,
}
eClient, err := etcd.New(eCfg)
if err != nil {
return nil, err
}
keysAPI := etcd.NewKeysAPI(eClient)
reg := registry.NewEtcdRegistry(keysAPI, etcdKeyPrefix, defaultTimeout)
if msg, ok := checkVersion(reg); !ok {
log.Printf(msg)
}
return &client.RegistryClient{Registry: reg}, nil
}
func getTunnelClient(driverEndpoint string, tunnelEndpoint string, maxRetries int) (client.API, error) {
log.Printf("Using Fleet Tunnel connection for requests")
getSSHClient := func() (interface{}, error) {
return ssh.NewSSHClient("core", driverEndpoint, nil, false, defaultTimeout)
}
result, err := retry(getSSHClient, maxRetries)
if err != nil {
return nil, err
}
sshClient := result.(*ssh.SSHForwardingClient)
dial := func(string, string) (net.Conn, error) {
cmd := fmt.Sprintf("fleetctl fd-forward %s", tunnelEndpoint)
return ssh.DialCommand(sshClient, cmd)
}
// This is needed to fake out the client - it isn't used
// since we're overloading the dial method on the transport
// but the client complains if it isn't set
fakeHTTPEndpoint, err := url.Parse("http://domain-sock")
if err != nil {
return nil, err
}
trans := pkg.LoggingHTTPTransport{
Transport: http.Transport{
Dial: dial,
},
}
httpClient := http.Client{
Transport: &trans,
}
return client.NewHTTPClient(&httpClient, *fakeHTTPEndpoint)
}
// getAPI returns an API to Fleet.
func getAPI(driver string, driverEndpoint string, maxRetries int, etcdKeyPrefix string, tunnel string) (client.API, error) {
switch strings.ToLower(driver) {
case "api":
return getHTTPClient(driverEndpoint)
case "etcd":
return getETCDClient(driverEndpoint, etcdKeyPrefix)
case "tunnel":
if len(driverEndpoint) > 0 {
return getTunnelClient(driverEndpoint, tunnel, maxRetries)
}
fallthrough
case "null":
fallthrough
default:
return nullAPI{}, nil
}
}
// Provider returns the ResourceProvider implemented by this package. Serve
// this with the Terraform plugin helper and you are golden.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"driver": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "tunnel",
Description: "Driver to use to connect to Fleet. Can be tunnel or api.",
},
"endpoint": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Endpoint for Fleet.",
},
"connection_retries": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 12,
},
"etcd_key_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "/_coreos.com/fleet/",
Description: "EtcdKeyPrefix to use for fleet",
},
"tunnel": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "/var/run/fleet.sock",
Description: "Tunnel address to use. Defaults to /var/run/fleet.sock",
},
},
ResourcesMap: map[string]*schema.Resource{
"fleet_unit": resourceUnit(),
},
ConfigureFunc: providerConfigure,
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
retries := d.Get("connection_retries").(int)
driver := d.Get("driver").(string)
endpoint := d.Get("endpoint").(string)
etcKeyPrefix := d.Get("etcd_key_prefix").(string)
tunnel := d.Get("tunnel").(string)
return getAPI(driver, endpoint, retries, etcKeyPrefix, tunnel)
}