2014-03-18 09:00:41 -07:00
|
|
|
package datasource
|
|
|
|
|
2014-04-22 15:36:07 -07:00
|
|
|
import (
|
2014-05-15 18:40:06 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-04-22 15:36:07 -07:00
|
|
|
"io/ioutil"
|
2014-05-14 22:20:40 -07:00
|
|
|
"log"
|
|
|
|
"math"
|
2014-05-15 18:40:06 -04:00
|
|
|
"net"
|
2014-04-22 15:36:07 -07:00
|
|
|
"net/http"
|
2014-05-15 18:40:06 -04:00
|
|
|
neturl "net/url"
|
|
|
|
"strings"
|
2014-05-14 22:20:40 -07:00
|
|
|
"time"
|
2014-04-22 15:36:07 -07:00
|
|
|
)
|
|
|
|
|
2014-05-15 18:40:06 -04:00
|
|
|
const (
|
|
|
|
HTTP_2xx = 2
|
|
|
|
HTTP_4xx = 4
|
|
|
|
|
|
|
|
maxTimeout = time.Second * 5
|
|
|
|
maxRetries = 15
|
|
|
|
)
|
2014-05-15 13:04:37 -04:00
|
|
|
|
2014-03-18 09:00:41 -07:00
|
|
|
type Datasource interface {
|
|
|
|
Fetch() ([]byte, error)
|
2014-05-14 22:20:40 -07:00
|
|
|
Type() string
|
2014-03-18 09:00:41 -07:00
|
|
|
}
|
2014-04-22 15:36:07 -07:00
|
|
|
|
2014-05-15 18:40:06 -04:00
|
|
|
// HTTP client timeout
|
|
|
|
// This one is low since exponential backoff will kick off too.
|
|
|
|
var timeout = time.Duration(2) * time.Second
|
|
|
|
|
|
|
|
func dialTimeout(network, addr string) (net.Conn, error) {
|
|
|
|
deadline := time.Now().Add(timeout)
|
|
|
|
c, err := net.DialTimeout(network, addr, timeout)
|
2014-04-22 15:36:07 -07:00
|
|
|
if err != nil {
|
2014-05-15 18:40:06 -04:00
|
|
|
return nil, err
|
2014-04-22 15:36:07 -07:00
|
|
|
}
|
2014-05-15 18:40:06 -04:00
|
|
|
c.SetDeadline(deadline)
|
|
|
|
return c, nil
|
|
|
|
}
|
2014-04-22 15:36:07 -07:00
|
|
|
|
2014-05-15 23:38:50 -04:00
|
|
|
// Fetches user-data url with support for exponential backoff and maximum retries
|
2014-05-15 18:40:06 -04:00
|
|
|
func fetchURL(rawurl string) ([]byte, error) {
|
|
|
|
if rawurl == "" {
|
|
|
|
return nil, errors.New("user-data URL is empty. Skipping.")
|
|
|
|
}
|
|
|
|
|
|
|
|
url, err := neturl.Parse(rawurl)
|
2014-04-22 15:36:07 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-05-15 23:38:50 -04:00
|
|
|
// Unfortunately, url.Parse is too generic to throw errors if a URL does not
|
|
|
|
// have a valid HTTP scheme. So, we have to do this extra validation
|
2014-05-15 18:40:06 -04:00
|
|
|
if !strings.HasPrefix(url.Scheme, "http") {
|
2014-05-15 23:38:50 -04:00
|
|
|
return nil, fmt.Errorf("user-data URL %s does not have a valid HTTP scheme. Skipping.", rawurl)
|
2014-05-15 18:40:06 -04:00
|
|
|
}
|
2014-05-14 22:20:40 -07:00
|
|
|
|
2014-05-15 18:40:06 -04:00
|
|
|
userdataURL := url.String()
|
|
|
|
|
2014-05-15 23:38:50 -04:00
|
|
|
// We need to create our own client in order to add timeout support.
|
2014-05-15 18:40:06 -04:00
|
|
|
// TODO(c4milo) Replace it once Go 1.3 is officially used by CoreOS
|
2014-05-15 23:38:50 -04:00
|
|
|
// More info: https://code.google.com/p/go/source/detail?r=ada6f2d5f99f
|
2014-05-15 18:40:06 -04:00
|
|
|
transport := &http.Transport{
|
|
|
|
Dial: dialTimeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
2014-05-15 23:40:18 -04:00
|
|
|
for retry := 1; retry <= maxRetries; retry++ {
|
2014-05-15 18:40:06 -04:00
|
|
|
log.Printf("Fetching user-data from %s. Attempt #%d", userdataURL, retry)
|
|
|
|
|
|
|
|
resp, err := client.Get(userdataURL)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
status := resp.StatusCode / 100
|
|
|
|
|
|
|
|
if status == HTTP_2xx {
|
|
|
|
return ioutil.ReadAll(resp.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == HTTP_4xx {
|
|
|
|
return nil, fmt.Errorf("user-data not found. HTTP status code: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("user-data not found. HTTP status code: %d", resp.StatusCode)
|
|
|
|
} else {
|
|
|
|
log.Printf("unable to fetch user-data: %s", err.Error())
|
2014-05-14 22:20:40 -07:00
|
|
|
}
|
2014-05-15 18:40:06 -04:00
|
|
|
|
|
|
|
duration := time.Millisecond * time.Duration((math.Pow(float64(2), float64(retry)) * 100))
|
2014-05-15 13:04:37 -04:00
|
|
|
if duration > maxTimeout {
|
|
|
|
duration = maxTimeout
|
|
|
|
}
|
|
|
|
|
2014-05-14 22:20:40 -07:00
|
|
|
time.Sleep(duration)
|
|
|
|
}
|
2014-05-15 18:40:06 -04:00
|
|
|
|
|
|
|
return nil, fmt.Errorf("unable to fetch user-data. Maximum retries reached: %d", maxRetries)
|
2014-05-14 22:20:40 -07:00
|
|
|
}
|