-
Notifications
You must be signed in to change notification settings - Fork 18
/
smart_proxy.go
298 lines (244 loc) · 8.07 KB
/
smart_proxy.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
Copyright © 2020, 2021 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Entry point to the insights results smart proxy REST API service.
// This file contains functions needed to start the service from command line.
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"net/url"
"os"
"strings"
"time"
"github.com/RedHatInsights/insights-content-service/groups"
"github.com/RedHatInsights/insights-operator-utils/logger"
"github.com/RedHatInsights/insights-operator-utils/metrics"
"github.com/rs/zerolog/log"
"github.com/RedHatInsights/insights-results-smart-proxy/amsclient"
"github.com/RedHatInsights/insights-results-smart-proxy/conf"
"github.com/RedHatInsights/insights-results-smart-proxy/server"
"github.com/RedHatInsights/insights-results-smart-proxy/services"
proxy_content "github.com/RedHatInsights/insights-results-smart-proxy/content"
)
// ExitCode represents numeric value returned to parent process when the
// current process finishes
type ExitCode int
const (
// ExitStatusOK means that the service have finished with success
ExitStatusOK = iota
// ExitStatusServerError means that the HTTP server cannot be initialized
ExitStatusServerError
defaultConfigFileName = "config"
)
const helpMessageTemplate = `
Smart Proxy service for insights results
Usage:
%+v [command]
The commands are:
<EMPTY> starts smart-proxy
start-service starts smart-proxy
help prints help
print-help prints help
print-config prints current configuration set by files & env variables
print-env prints env variables
print-version-info prints version info
`
// serverInstance represents instance of REST API server
var serverInstance *server.HTTPServer
// printHelp function displays help on the standard output.
func printHelp() ExitCode {
fmt.Printf(helpMessageTemplate, os.Args[0])
return ExitStatusOK
}
// printConfig function displays loaded configuration on the standard output.
func printConfig() ExitCode {
configBytes, err := json.MarshalIndent(conf.Config, "", " ")
if err != nil {
log.Error().Err(err)
return 1
}
// convert configuration to string and displays it to standard output
fmt.Println(string(configBytes))
return ExitStatusOK
}
// printEnv function prints all environment variables to standard output.
func printEnv() ExitCode {
for _, keyVal := range os.Environ() {
fmt.Println(keyVal)
}
return ExitStatusOK
}
// startService function starts service and returns error code.
func startServer() ExitCode {
_ = conf.GetSetupConfiguration()
serverCfg := conf.GetServerConfiguration()
metricsCfg := conf.GetMetricsConfiguration()
servicesCfg := conf.GetServicesConfiguration()
amsConfig := conf.GetAMSClientConfiguration()
redisConf := conf.GetRedisConfiguration()
groupsChannel := make(chan []groups.Group)
errorFoundChannel := make(chan bool)
errorChannel := make(chan error)
if metricsCfg.Namespace != "" {
metrics.AddAPIMetricsWithNamespace(metricsCfg.Namespace)
}
amsClient, err := amsclient.NewAMSClient(amsConfig)
if err != nil {
log.Error().Err(err).Msg("Cannot init the AMSClient, using old approach")
amsClient = nil
} else {
log.Info().Msg("AMSClient successfully created")
}
redisClient, err := services.NewRedisClient(redisConf)
if err != nil {
redisClient = nil
} else {
// PING Redis server
if err = redisClient.HealthCheck(); err != nil {
log.Error().Err(err).Msg("failed to ping Redis server")
redisClient = nil
} else {
log.Info().Msg("Redis client created, Redis server is responding")
}
}
serverInstance = server.New(serverCfg, servicesCfg, amsClient, redisClient, groupsChannel, errorFoundChannel, errorChannel)
// fill-in additional info used by /info endpoint handler
fillInInfoParams(serverInstance.InfoParams)
proxy_content.SetContentDirectoryTimeout(servicesCfg.ContentDirectoryTimeout)
go updateGroupInfo(servicesCfg, groupsChannel, errorFoundChannel, errorChannel)
go proxy_content.RunUpdateContentLoop(servicesCfg)
err = serverInstance.Start()
if err != nil {
log.Error().Err(err).Msg("HTTP(s) start error")
return ExitStatusServerError
}
return ExitStatusOK
}
// fillInInfoParams function fills-in additional info used by /info endpoint
// handler
func fillInInfoParams(params map[string]string) {
params["BuildVersion"] = BuildVersion
params["BuildTime"] = BuildTime
params["BuildBranch"] = BuildBranch
params["BuildCommit"] = BuildCommit
params["UtilsVersion"] = UtilsVersion
}
// updateGroupInfo function is run in a goroutine. It runs forever, waiting for 1 of 2 events: a Ticker or a channel
// * If ticker comes first, the groups configuration is updated, doing a request to the content-service
// * If the channel comes first, the latest valid groups configuration is send through the channel
func updateGroupInfo(servicesConf services.Configuration,
groupsChannel chan []groups.Group,
errorFoundChannel chan bool,
errorChannel chan error) {
var currentGroups []groups.Group
var currentError error
var currentErrorFound bool
retrievedGroups, err := services.GetGroups(servicesConf)
if err != nil {
log.Error().Err(err).Msg("Error retrieving groups")
currentErrorFound = true
var e *url.Error
if errors.As(err, &e) {
currentError = &server.ContentServiceUnavailableError{}
} else {
currentError = err
}
} else {
currentErrorFound = false
currentGroups = retrievedGroups
}
uptimeTicker := time.NewTicker(servicesConf.GroupsPollingTime)
log.Info().Msgf("Updating groups configuration each %f seconds", servicesConf.GroupsPollingTime.Seconds())
for {
select {
case <-uptimeTicker.C:
retrievedGroups, err = services.GetGroups(servicesConf)
currentGroups, currentErrorFound, currentError = handleGroupError(err, currentGroups, retrievedGroups)
case errorFoundChannel <- currentErrorFound:
case errorChannel <- currentError:
case groupsChannel <- currentGroups:
}
}
}
// handleGroupError handles error after retrieving groups info in updateGroupInfo
func handleGroupError(err error,
currentGroups []groups.Group,
newGroups []groups.Group) ([]groups.Group, bool, error) {
if err != nil {
log.Error().Err(err).Msg("Error retrieving groups")
var e *url.Error
if errors.As(err, &e) {
return currentGroups, true, &server.ContentServiceUnavailableError{}
}
return currentGroups, true, err
}
return newGroups, false, nil
}
// handleCommand select the function to be called depending on command argument
func handleCommand(command string) ExitCode {
switch command {
case "start-service":
return startServer()
case "print-version":
printVersionInfo()
return ExitStatusOK
case "print-help":
printHelp()
return ExitStatusOK
case "print-config":
printConfig()
return ExitStatusOK
case "print-env":
printEnv()
return ExitStatusOK
}
return ExitStatusOK
}
// main represents entry point to CLI client.
func main() {
err := conf.LoadConfiguration(defaultConfigFileName)
if err != nil {
panic(err)
}
err = logger.InitZerolog(
conf.GetLoggingConfiguration(),
conf.GetCloudWatchConfiguration(),
conf.GetSentryLoggingConfiguration(),
conf.GetKafkaZerologConfiguration(),
)
if err != nil {
panic(err)
}
var (
showHelp bool
showVersion bool
)
flag.BoolVar(&showHelp, "help", false, "Show the help")
flag.BoolVar(&showVersion, "version", false, "Show the version and exit")
flag.Parse()
if showHelp {
os.Exit(int(printHelp()))
}
if showVersion {
printVersionInfo()
os.Exit(ExitStatusOK)
}
args := flag.Args()
command := "start-service"
if len(args) >= 1 {
command = strings.ToLower(strings.TrimSpace(args[0]))
}
os.Exit(int(handleCommand(command)))
}