-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathremote.go
214 lines (169 loc) · 5.03 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
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
package eskipfile
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/net"
"github.com/zalando/skipper/routing"
log "github.com/sirupsen/logrus"
)
var errContentNotChanged = errors.New("content in cache did not change, 304 response status code")
type remoteEskipFile struct {
once sync.Once
preloaded bool
remotePath string
localPath string
eskipFileClient *WatchClient
threshold int
verbose bool
http *net.Client
etag string
}
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{
once: sync.Once{},
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 {
dataClient.http.Close()
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 {
dataClient.http.Close()
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 {
log.Errorf("RemoteEskipFile LoadUpdate %s failed. Skipper continues to serve the last successfully updated routes. Error: %s",
client.remotePath, err)
return newRoutes, deletedRoutes, err
}
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))
}
}
}
return newRoutes, deletedRoutes, err
}
func (client *remoteEskipFile) Close() {
client.once.Do(func() {
client.http.Close()
client.eskipFileClient.Close()
})
}
func isFileRemote(remotePath string) bool {
return strings.HasPrefix(remotePath, "http://") || strings.HasPrefix(remotePath, "https://")
}
func (client *remoteEskipFile) DownloadRemoteFile() error {
resBody, err := client.getRemoteData()
if err != nil {
if errors.Is(err, errContentNotChanged) {
return nil
}
return err
}
defer resBody.Close()
outFile, err := os.OpenFile(client.localPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer outFile.Close()
if _, err = io.Copy(outFile, resBody); err != nil {
_ = outFile.Close()
return err
}
return outFile.Close()
}
func (client *remoteEskipFile) getRemoteData() (io.ReadCloser, error) {
req, err := http.NewRequest("GET", client.remotePath, nil)
if err != nil {
return nil, err
}
if client.etag != "" {
req.Header.Set("If-None-Match", client.etag)
}
resp, err := client.http.Do(req)
if err != nil {
return nil, err
}
if client.etag != "" && resp.StatusCode == 304 {
resp.Body.Close()
return nil, errContentNotChanged
}
if resp.StatusCode != 200 {
resp.Body.Close()
return nil, fmt.Errorf("failed to download remote file %s, status code: %d", client.remotePath, resp.StatusCode)
}
client.etag = resp.Header.Get("ETag")
return resp.Body, err
}