forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.go
172 lines (137 loc) · 4.14 KB
/
remote.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
package eskipfile
import (
"errors"
"io"
"os"
"strings"
"time"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/net"
"github.com/zalando/skipper/routing"
log "github.com/sirupsen/logrus"
)
type remoteEskipFile struct {
preloaded bool
remotePath string
localPath string
eskipFileClient *WatchClient
threshold int
verbose bool
http *net.Client
}
type RemoteWatchOptions struct {
// URL of the route file
RemoteFile string
// Verbose mode for the dataClient
Verbose bool
// Amount of route changes that will trigger logs after route updates
Threshold int
// It does an initial download and parsing of remote routes, and makes RemoteWatch to return an error
FailOnStartup bool
// HTTPTimeout is the generic timeout for any phase of a single HTTP request to RemoteFile.
HTTPTimeout time.Duration
}
// RemoteWatch creates a route configuration client with (remote) file watching. Watch doesn't follow file system nodes,
// it always reads (or re-downloads) from the file identified by the initially provided file name.
func RemoteWatch(o *RemoteWatchOptions) (routing.DataClient, error) {
if !isFileRemote(o.RemoteFile) {
return Watch(o.RemoteFile), nil
}
tempFilename, err := os.CreateTemp("", "routes")
if err != nil {
return nil, err
}
dataClient := &remoteEskipFile{
remotePath: o.RemoteFile,
localPath: tempFilename.Name(),
threshold: o.Threshold,
verbose: o.Verbose,
http: net.NewClient(net.Options{Timeout: o.HTTPTimeout}),
}
if o.FailOnStartup {
err = dataClient.DownloadRemoteFile()
if err != nil {
return nil, err
}
} else {
f, err := os.OpenFile(tempFilename.Name(), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err == nil {
err = f.Close()
}
if err != nil {
return nil, err
}
dataClient.preloaded = true
}
dataClient.eskipFileClient = Watch(tempFilename.Name())
return dataClient, nil
}
// LoadAll returns the parsed route definitions found in the file.
func (client *remoteEskipFile) LoadAll() ([]*eskip.Route, error) {
var err error = nil
if client.preloaded {
client.preloaded = false
} else {
err = client.DownloadRemoteFile()
}
if err != nil {
log.Errorf("LoadAll from remote %s failed. Continue using the last loaded routes", client.remotePath)
return nil, err
}
if client.verbose {
log.Infof("New routes file %s was downloaded", client.remotePath)
}
return client.eskipFileClient.LoadAll()
}
// LoadUpdate returns differential updates when a remote file has changed.
func (client *remoteEskipFile) LoadUpdate() ([]*eskip.Route, []string, error) {
err := client.DownloadRemoteFile()
if err != nil {
log.Errorf("LoadUpdate from remote %s failed. Trying to LoadAll", client.remotePath)
return nil, nil, err
}
newRoutes, deletedRoutes, err := client.eskipFileClient.LoadUpdate()
if err == nil {
if client.verbose {
log.Infof("New routes were loaded. New: %d; deleted: %d", len(newRoutes), len(deletedRoutes))
if client.threshold > 0 {
if len(newRoutes)+len(deletedRoutes) > client.threshold {
log.Warnf("Significant amount of routes was updated. New: %d; deleted: %d", len(newRoutes), len(deletedRoutes))
}
}
}
} else {
log.Errorf("RemoteEskipFile LoadUpdate %s failed. Skipper continues to serve the last successfully updated routes. Error: %s",
client.remotePath, err)
}
return newRoutes, deletedRoutes, err
}
func isFileRemote(remotePath string) bool {
return strings.HasPrefix(remotePath, "http://") || strings.HasPrefix(remotePath, "https://")
}
func (client *remoteEskipFile) DownloadRemoteFile() error {
data, err := client.getRemoteData()
if err != nil {
return err
}
defer data.Close()
out, err := os.OpenFile(client.localPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
if _, err = io.Copy(out, data); err != nil {
_ = out.Close()
return err
}
return out.Close()
}
func (client *remoteEskipFile) getRemoteData() (io.ReadCloser, error) {
resp, err := client.http.Get(client.remotePath)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New("download file failed")
}
return resp.Body, nil
}