-
Notifications
You must be signed in to change notification settings - Fork 4
/
clouddriver.go
214 lines (179 loc) · 5.99 KB
/
clouddriver.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 main
import (
"fmt"
"log"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/homedepot/go-clouddriver/internal"
"github.com/homedepot/go-clouddriver/internal/api"
"github.com/homedepot/go-clouddriver/internal/arcade"
"github.com/homedepot/go-clouddriver/internal/artifact"
"github.com/homedepot/go-clouddriver/internal/fiat"
"github.com/homedepot/go-clouddriver/internal/front50"
"github.com/homedepot/go-clouddriver/internal/kubernetes"
"github.com/homedepot/go-clouddriver/internal/sql"
ginprometheus "github.com/zsais/go-gin-prometheus"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
const (
arcadeShortExpirationSeconds = 60
mysqlDefaultStringSize = 256
)
var (
r = gin.New()
)
func main() {
if err := r.Run(":7002"); err != nil {
log.Fatal(err)
}
}
func setupArcadeClient() (client arcade.Client) {
url := os.Getenv("ARCADE_URL")
if url != "" {
client = arcade.NewClient(url)
} else {
client = arcade.NewDefaultClient()
}
arcadeAPIKey := os.Getenv("ARCADE_API_KEY")
if arcadeAPIKey == "" {
log.Println("[CLOUDDRIVER] WARNING: ARCADE_API_KEY not set")
}
client.WithAPIKey(arcadeAPIKey)
client.WithShortExpiration(arcadeShortExpirationSeconds)
return client
}
func setupFiatClient() fiat.Client {
url := os.Getenv("FIAT_URL")
if url != "" {
return fiat.NewClient(url)
}
return fiat.NewDefaultClient()
}
func setupFront50Client() front50.Client {
url := os.Getenv("FRONT50_URL")
if url != "" {
return front50.NewClient(url)
}
return front50.NewDefaultClient()
}
func init() {
// Setup metrics.
p := ginprometheus.NewPrometheus("clouddriver")
p.MetricsPath = "/metrics"
p.Use(r)
// Preserve low cardinality for the request counter.
// See https://github.com/zsais/go-gin-prometheus#preserving-a-low-cardinality-for-the-request-counter.
p.ReqCntURLLabelMappingFn = reqCntURLLabelMappingFn
gin.ForceConsoleColor()
// Ignore logging of certain endpoints.
r.Use(gin.LoggerWithConfig(gin.LoggerConfig{SkipPaths: []string{"/health"}}))
r.Use(gin.Recovery())
sqlClient := sql.NewClient(dialector())
if err := sqlClient.Connect(); err != nil {
log.Fatal(err)
}
artifactCredentialsController := getArtifactsCredentialsController()
fiatClient := setupFiatClient()
front50Client := setupFront50Client()
kubeController := kubernetes.NewController()
arcadeClient := setupArcadeClient()
ic := &internal.Controller{
ArcadeClient: arcadeClient,
ArtifactCredentialsController: artifactCredentialsController,
SQLClient: sqlClient,
FiatClient: fiatClient,
Front50Client: front50Client,
KubernetesController: kubeController,
}
server := api.NewServer(r)
server.WithController(ic)
if os.Getenv("VERBOSE_REQUEST_LOGGING") == "true" {
server.WithVerboseRequestLogging()
}
if os.Getenv("KUBERNETES_USE_DISK_CACHE") == "true" {
kubernetes.UseDiskCache()
}
server.Setup()
}
func reqCntURLLabelMappingFn(c *gin.Context) string {
// Setting the url to the path will remove query params, which sometimes have a GUID.
url := c.Request.URL.Path
for _, p := range c.Params {
// The following replaces certain path params with a generic name.
switch p.Key {
case "account":
// Leave account information if this is the Manifests API.
if !strings.HasPrefix(url, "/manifests") {
url = strings.Replace(url, p.Value, ":"+p.Key, 1)
}
case "application":
// Leave application information if this is the Applications API.
if !strings.HasPrefix(url, "/applications") {
url = strings.Replace(url, p.Value, ":"+p.Key, 1)
}
case "location":
url = strings.Replace(url, p.Value, ":"+p.Key, 1)
case "name":
url = strings.Replace(url, p.Value, ":"+p.Key, 1)
case "kind":
url = strings.Replace(url, p.Value, ":"+p.Key, 1)
case "cluster":
url = strings.Replace(url, p.Value, ":"+p.Key, 1)
case "target":
url = strings.Replace(url, p.Value, ":"+p.Key, 1)
case "id":
url = strings.Replace(url, p.Value, ":"+p.Key, 1)
}
}
return url
}
// getArtifactsCredentialsController gets an artifacts credentials controller
// either using the default directory (/opt/spinnaker/artifacts/config)
// or, if it exists, using the value of the ARTIFACTS_CREDENTIALS_CONFIG_DIR
// environment variable.
func getArtifactsCredentialsController() artifact.CredentialsController {
var (
artifactCredentialsController artifact.CredentialsController
err error
)
artifactsCredentialsConfigDir := os.Getenv("ARTIFACTS_CREDENTIALS_CONFIG_DIR")
if artifactsCredentialsConfigDir == "" {
// Use default directory /opt/spinnaker/artifacts/config.
artifactCredentialsController, err = artifact.NewDefaultCredentialsController()
} else {
artifactCredentialsController, err = artifact.NewCredentialsController(artifactsCredentialsConfigDir)
}
if err != nil {
log.Println("[CLOUDDRIVER] error setting up artifact credentials controller:", err.Error())
}
return artifactCredentialsController
}
// dialector defines the SQL dialector.
//
// Defaults to sqlite if env vars DB_HOST, DB_NAME, DB_PASS, and DB_USER
// are not all defined. Otherwise it creates a MySQL dialctor with a DSN in the format
// `<USER>:<PASS>@tcp(<HOST>)/<NAME>?timeout=30s&charset=utf8&parseTime=True&loc=UTC`.
//
// See https://gorm.io/docs/connecting_to_the_database.html for more info.
func dialector() gorm.Dialector {
var dialector gorm.Dialector
host := os.Getenv("DB_HOST")
name := os.Getenv("DB_NAME")
pass := os.Getenv("DB_PASS")
user := os.Getenv("DB_USER")
// Default to SQLite, else define a MySQL connection.
if host == "" || name == "" || pass == "" || user == "" {
log.Println("[CLOUDDRIVER] DB_HOST, DB_NAME, DB_PASS, or DB_USER not defined; defaulting to local SQLite DB")
dialector = sqlite.Open("clouddriver.db")
} else {
dialector = mysql.New(mysql.Config{
DSN: fmt.Sprintf("%s:%s@tcp(%s)/%s?timeout=30s&charset=utf8&parseTime=True&loc=UTC",
user, pass, host, name),
DefaultStringSize: mysqlDefaultStringSize,
})
}
return dialector
}