Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.

Commit 08f6048

Browse files
committed
k8s deployment updates
1 parent 20988e1 commit 08f6048

File tree

7 files changed

+75
-30
lines changed

7 files changed

+75
-30
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

build.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
set -exu
4+
5+
# if [[ `git status --porcelain` ]]; then
6+
# echo has git changes;
7+
# echo exit 1;
8+
# fi
9+
10+
GO111MODULE=on GOOS=linux CGO_ENABLED=0 GOARCH=amd64 \
11+
go build \
12+
-ldflags "-s -w -X 'main.version=$(git name-rev --tags --name-only $(git rev-parse HEAD))' -X 'main.commit=$(git rev-parse --short HEAD)' -X 'main.date=$(date)'" \
13+
-o golangci-${1} \
14+
./cmd/golangci-${1}/main.go

internal/api/session/factory.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package session
22

33
import (
4-
"strings"
54
"time"
65

76
"github.com/garyburd/redigo/redis"
@@ -16,7 +15,12 @@ type Factory struct {
1615
}
1716

1817
func NewFactory(redisPool *redis.Pool, cfg config.Config, maxAge time.Duration) (*Factory, error) {
19-
store, err := redistore.NewRediStoreWithPool(redisPool, []byte(cfg.GetString("SESSION_SECRET")))
18+
sessSecret := cfg.GetString("SESSION_SECRET")
19+
if sessSecret == "" {
20+
return nil, errors.New("SESSION_SECRET isn't set")
21+
}
22+
23+
store, err := redistore.NewRediStoreWithPool(redisPool, []byte(sessSecret))
2024
if err != nil {
2125
return nil, errors.Wrap(err, "can't create redis session store")
2226
}
@@ -34,13 +38,7 @@ func NewFactory(redisPool *redis.Pool, cfg config.Config, maxAge time.Duration)
3438
}
3539

3640
func (f *Factory) updateOptions() {
37-
// https for dev/prod, http for testing
38-
d := strings.TrimPrefix(f.cfg.GetString("WEB_ROOT"), "https://")
39-
d = strings.TrimPrefix(d, "http://")
40-
if strings.HasPrefix(d, "127.0.0.1") { // test mocking
41-
d = "" // prevent warnings
42-
}
43-
f.store.Options.Domain = d
41+
f.store.Options.Domain = f.cfg.GetString("COOKIE_DOMAIN")
4442
// TODO: set httponly and secure for non-testing
4543
}
4644

internal/shared/db/gormdb/gormdb.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gormdb
22

33
import (
44
"database/sql"
5+
"fmt"
56
"strings"
67

78
"github.com/golangci/golangci-api/internal/shared/config"
@@ -13,12 +14,21 @@ import (
1314

1415
func GetDBConnString(cfg config.Config) (string, error) {
1516
dbURL := cfg.GetString("DATABASE_URL")
16-
if dbURL == "" {
17-
return "", errors.New("no DATABASE_URL in config")
17+
if dbURL != "" {
18+
dbURL = strings.Replace(dbURL, "postgresql", "postgres", 1)
19+
return dbURL, nil
1820
}
1921

20-
dbURL = strings.Replace(dbURL, "postgresql", "postgres", 1)
21-
return dbURL, nil
22+
host := cfg.GetString("DATABASE_HOST")
23+
username := cfg.GetString("DATABASE_USERNAME")
24+
password := cfg.GetString("DATABASE_PASSWORD")
25+
name := cfg.GetString("DATABASE_NAME")
26+
if host == "" || username == "" || password == "" || name == "" {
27+
return "", errors.New("no DATABASE_URL or DATABASE_{HOST,USERNAME,PASSWORD,NAME} in config")
28+
}
29+
30+
//TODO: enable SSL, but it's not critical
31+
return fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", username, password, host, name), nil
2232
}
2333

2434
func GetDB(cfg config.Config, log logutil.Log, connString string) (*gorm.DB, error) {
@@ -30,13 +40,17 @@ func GetDB(cfg config.Config, log logutil.Log, connString string) (*gorm.DB, err
3040
}
3141
}
3242
adapter := strings.Split(connString, "://")[0]
43+
isDebug := cfg.GetBool("DEBUG_DB", false)
44+
if isDebug {
45+
log.Infof("Connecting to database %s", connString)
46+
}
3347

3448
db, err := gorm.Open(adapter, connString)
3549
if err != nil {
3650
return nil, errors.Wrap(err, "can't open db connection")
3751
}
3852

39-
if cfg.GetBool("DEBUG_DB", false) {
53+
if isDebug {
4054
db = db.Debug()
4155
}
4256

internal/shared/db/migrations/runner.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ import (
1111
)
1212

1313
type Runner struct {
14-
distLock *redsync.Mutex
15-
log logutil.Log
16-
dbConnString string
17-
projectRoot string
14+
distLock *redsync.Mutex
15+
log logutil.Log
16+
dbConnString string
17+
migrationsPath string
1818
}
1919

20-
func NewRunner(distLock *redsync.Mutex, log logutil.Log, dbConnString, projectRoot string) *Runner {
20+
func NewRunner(distLock *redsync.Mutex, log logutil.Log, dbConnString, migrationsPath string) *Runner {
2121
return &Runner{
22-
distLock: distLock,
23-
log: log,
24-
dbConnString: dbConnString,
25-
projectRoot: projectRoot,
22+
distLock: distLock,
23+
log: log,
24+
dbConnString: dbConnString,
25+
migrationsPath: migrationsPath,
2626
}
2727
}
2828

@@ -33,7 +33,7 @@ func (r Runner) Run() error {
3333
}
3434
defer r.distLock.Unlock()
3535

36-
migrationsDir := fmt.Sprintf("file://%s/migrations", r.projectRoot)
36+
migrationsDir := fmt.Sprintf("file://%s", r.migrationsPath)
3737
m, err := migrate.New(migrationsDir, r.dbConnString)
3838
if err != nil {
3939
return errors.Wrap(err, "can't initialize migrations")

internal/shared/db/redis/pool.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis
22

33
import (
4+
"errors"
45
"fmt"
56
"time"
67

@@ -9,9 +10,9 @@ import (
910
)
1011

1112
func GetPool(cfg config.Config) (*redis.Pool, error) {
12-
redisURL := cfg.GetString("REDIS_URL")
13-
if redisURL == "" {
14-
return nil, fmt.Errorf("config REDIS_URL isn't set")
13+
redisURL, err := GetURL(cfg)
14+
if err != nil {
15+
return nil, err
1516
}
1617

1718
return &redis.Pool{
@@ -26,3 +27,17 @@ func GetPool(cfg config.Config) (*redis.Pool, error) {
2627
},
2728
}, nil
2829
}
30+
31+
func GetURL(cfg config.Config) (string, error) {
32+
if redisURL := cfg.GetString("REDIS_URL"); redisURL != "" {
33+
return redisURL, nil
34+
}
35+
36+
host := cfg.GetString("REDIS_HOST")
37+
password := cfg.GetString("REDIS_PASSWORD")
38+
if host == "" || password == "" {
39+
return "", errors.New("no REDIS_URL or REDIS_{HOST,PASSWORD} in config")
40+
}
41+
42+
return fmt.Sprintf("redis://h:%s@%s", password, host), nil
43+
}

pkg/api/app.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99
"time"
1010

11-
"github.com/golangci/golangci-api/internal/shared/fsutil"
1211
"github.com/golangci/golangci-api/pkg/api/policy"
1312
"github.com/golangci/golangci-api/pkg/worker/analyze/analyzesqueue"
1413
"github.com/golangci/golangci-api/pkg/worker/analyze/analyzesqueue/pullanalyzesqueue"
@@ -183,7 +182,11 @@ func (a *App) buildDeps() {
183182
}
184183

185184
if a.cache == nil {
186-
a.cache = cache.NewRedis(a.cfg.GetString("REDIS_URL") + "/1")
185+
redisURL, err := redis.GetURL(a.cfg)
186+
if err != nil {
187+
a.log.Fatalf("Can't get redis URL: %s", err)
188+
}
189+
a.cache = cache.NewRedis(redisURL + "/1")
187190
}
188191

189192
if a.authorizer == nil {
@@ -351,7 +354,7 @@ func (a *App) buildMigrationsRunner() {
351354
a.log.Fatalf("Can't get DB conn string: %s", err)
352355
}
353356
a.migrationsRunner = migrations.NewRunner(a.distLockFactory.NewMutex("migrations"), a.trackedLog,
354-
dbConnString, fsutil.GetProjectRoot())
357+
dbConnString, a.cfg.GetString("MIGRATIONS_PATH"))
355358
}
356359

357360
func NewApp(modifiers ...Modifier) *App {

0 commit comments

Comments
 (0)