forked from rancher/channelserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
198 lines (163 loc) · 4.53 KB
/
config.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
package config
import (
"context"
"fmt"
"net/url"
"sync"
"time"
"github.com/google/go-github/v29/github"
"github.com/rancher/channelserver/pkg/model"
"github.com/sirupsen/logrus"
)
type Config struct {
sync.Mutex
url string
redirect *url.URL
gh *github.Client
channelsConfig *model.ChannelsConfig
releasesConfig *model.ReleasesConfig
appDefaultsConfig *model.AppDefaultsConfig
}
type Wait interface {
Wait(ctx context.Context) bool
}
type Source interface {
URL() string
}
type DurationWait struct {
Duration time.Duration
}
func (d *DurationWait) Wait(ctx context.Context) bool {
select {
case <-ctx.Done():
return false
case <-time.After(d.Duration):
return true
}
}
type StringSource string
func (s StringSource) URL() string {
return string(s)
}
func NewConfig(ctx context.Context, subKey string, wait Wait, channelServerVersion string, appName string, urls []Source) *Config {
c := &Config{
channelsConfig: &model.ChannelsConfig{},
releasesConfig: &model.ReleasesConfig{},
appDefaultsConfig: &model.AppDefaultsConfig{},
}
_, _ = c.loadConfig(ctx, subKey, channelServerVersion, appName, urls...)
go func() {
for wait.Wait(ctx) {
if index, err := c.loadConfig(ctx, subKey, channelServerVersion, appName, urls...); err != nil {
logrus.Errorf("failed to reload configuration from %s: %v", urls[index].URL(), err)
} else {
urls = urls[:index+1]
logrus.Infof("Loaded configuration from %s in %v", urls[index].URL(), urls)
}
}
}()
return c
}
func (c *Config) loadConfig(ctx context.Context, subKey string, channelServerVersion string, appName string, urls ...Source) (int, error) {
content, index, err := getURLs(ctx, urls...)
if err != nil {
return index, fmt.Errorf("failed to get content from url %s: %v", urls[index].URL(), err)
}
config, err := GetChannelsConfig(ctx, content, subKey)
if err != nil {
return index, fmt.Errorf("failed to get channel config: %v", err)
}
releases, err := GetReleasesConfig(content, channelServerVersion, subKey)
if err != nil {
return index, fmt.Errorf("failed to get release config: %v", err)
}
appDefaultsConfig, err := GetAppDefaultsConfig(content, subKey, appName)
if err != nil {
return index, fmt.Errorf("failed to get app default config: %v", err)
}
return index, c.setConfig(ctx, channelServerVersion, config, releases, appDefaultsConfig)
}
func (c *Config) ghClient(config *model.ChannelsConfig) (*github.Client, error) {
if config.GitHub == nil {
return nil, nil
}
if c.gh == nil || c.url != config.GitHub.APIURL {
if config.GitHub.APIURL == "" {
return github.NewClient(nil), nil
}
return github.NewEnterpriseClient(config.GitHub.APIURL, config.GitHub.APIURL, nil)
}
return c.gh, nil
}
func (c *Config) setConfig(ctx context.Context, channelServerVersion string, config *model.ChannelsConfig, releases *model.ReleasesConfig, appDefaultsConfig *model.AppDefaultsConfig) error {
gh, err := c.ghClient(config)
if err != nil {
return err
}
redirect, err := url.Parse(config.RedirectBase)
if err != nil {
return err
}
var ghReleases []string
if gh != nil {
ghReleases, err = GetGHReleases(ctx, gh, config.GitHub.Owner, config.GitHub.Repo)
if err != nil {
return err
}
}
if err := resolveChannels(ghReleases, config); err != nil {
return err
}
c.Lock()
defer c.Unlock()
c.gh = gh
c.channelsConfig = config
c.redirect = redirect
c.releasesConfig = releases
c.appDefaultsConfig = appDefaultsConfig
if config.GitHub != nil {
c.url = config.GitHub.APIURL
}
return nil
}
func resolveChannels(releases []string, config *model.ChannelsConfig) error {
for i, channel := range config.Channels {
if channel.Latest != "" {
continue
}
if channel.LatestRegexp == "" {
continue
}
release, err := Latest(releases, channel.LatestRegexp, channel.ExcludeRegexp)
if err != nil {
return err
}
config.Channels[i].Latest = release
}
return nil
}
func (c *Config) ChannelsConfig() *model.ChannelsConfig {
c.Lock()
defer c.Unlock()
return c.channelsConfig
}
func (c *Config) ReleasesConfig() *model.ReleasesConfig {
c.Lock()
defer c.Unlock()
return c.releasesConfig
}
func (c *Config) AppDefaultsConfig() *model.AppDefaultsConfig {
c.Lock()
defer c.Unlock()
return c.appDefaultsConfig
}
func (c *Config) Redirect(id string) (string, error) {
for _, channel := range c.channelsConfig.Channels {
if channel.Name == id && channel.Latest != "" {
return c.redirect.ResolveReference(&url.URL{
Path: channel.Latest,
}).String(), nil
}
}
return "", nil
}