Skip to content

Commit

Permalink
init codespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tyng94 committed Mar 3, 2024
1 parent 8672308 commit 102c096
Show file tree
Hide file tree
Showing 8 changed files with 486 additions and 326 deletions.
42 changes: 42 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "Registrywatcher Dev Container - Go",
"dockerComposeFile": "docker-compose.yml",
"service": "devcontainer",
"workspaceFolder": "/workspaces/registrywatcher",
"forwardPorts": [5432],
"portsAttributes": {
"5432": {"label": "PostgreSQL port", "onAutoForward": "silent"}
},
"customizations": {
"vscode": {
"extensions": [
"golang.go",
"ms-azuretools.vscode-docker",
"GitHub.copilot",
"waderyan.gitblame",
"DavidAnson.vscode-markdownlint",
"mtxr.sqltools",
"mtxr.sqltools-driver-pg"
],
"settings": {
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
}
},
"sqltools.connections": [
{
"name": "Local database",
"driver": "PostgreSQL",
"server": "localhost",
"port": 5432,
"database": "lauth",
"username": "postgres",
"password": "Password123"
}
]
}
}
}
}
25 changes: 25 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '3.8'
services:
backend:
image: mcr.microsoft.com/devcontainers/go:0-1.17-bullseye
volumes:
- ../..:/workspaces:cached
network_mode: service:db
command: sleep infinity
environment:
DATABASE_URL: postgresql://registry-watcher:registry-watcher@db/registry-watcher?sslmode=disable
VAULT_TOKEN: ${VAULT_TOKEN}
NOMAD_TOKEN: ${NOMAD_TOKEN}

db:
image: postgres:9.6-alpine
restart: unless-stopped
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: registry-watcher
POSTGRES_DB: registry-watcher
POSTGRES_PASSWORD: registry-watcher

volumes:
postgres-data:
36 changes: 20 additions & 16 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"github.com/dsaidgovsg/registrywatcher/log"
"github.com/dsaidgovsg/registrywatcher/utils"
"github.com/spf13/viper"
"gorm.io/gorm"
)

type Clients struct {
NomadClient *NomadClient
DockerRegistryClient *DockerRegistryClient
PostgresClient *PostgresClient
PostgresClient *gorm.DB
DockerhubApi *DockerhubApi
DockerTags sync.Map
DigestMap sync.Map
Expand Down Expand Up @@ -81,18 +82,19 @@ func (client *Clients) GetCachedTagDigest(repoName string) (string, error) {

// fetches the CACHED pinned tag
func (client *Clients) GetFormattedPinnedTag(repoName string) (string, error) {
pinnedTag, err := client.PostgresClient.GetPinnedTag(repoName)
if err != nil {
return "", err
}
// pinnedTag, err := client.PostgresClient.GetPinnedTag(repoName)
// if err != nil {
// return "", err
// }
pinnedTag := ""
if pinnedTag == "" {
tags, err := client.GetCachedTags(repoName)
if err != nil {
return "", err
}
pinnedTag, err = utils.GetLatestReleaseTag(tags)
}
return pinnedTag, err
return pinnedTag, nil
}

func (client *Clients) DeployPinnedTag(conf *viper.Viper, repoName string) {
Expand Down Expand Up @@ -265,21 +267,23 @@ func (client *Clients) updateCaches(repoName string) {
// this function compares cached values with the actual values,
// so only update the cache before returning non-error cases
func (client *Clients) ShouldDeploy(repoName string) (bool, error) {
autoDeploy, err := client.PostgresClient.GetAutoDeployFlag(repoName)
if err != nil {
log.LogAppErr(fmt.Sprintf("Couldn't fetch whether to deploy flag while checking whether to deploy for %s", repoName), err)
return false, err
}
// autoDeploy, err := client.PostgresClient.GetAutoDeployFlag(repoName)
// if err != nil {
// log.LogAppErr(fmt.Sprintf("Couldn't fetch whether to deploy flag while checking whether to deploy for %s", repoName), err)
// return false, err
// }
autoDeploy := false
if !autoDeploy {
client.updateCaches(repoName)
return false, nil
}

pinnedTag, err := client.PostgresClient.GetPinnedTag(repoName)
if err != nil {
log.LogAppErr(fmt.Sprintf("Couldn't fetch pinned tag while checking whether to deploy for %s", repoName), err)
return false, err
}
// pinnedTag, err := client.PostgresClient.GetPinnedTag(repoName)
// if err != nil {
// log.LogAppErr(fmt.Sprintf("Couldn't fetch pinned tag while checking whether to deploy for %s", repoName), err)
// return false, err
// }
pinnedTag := ""
isDigestChanged, err := client.isTagDigestChanged(repoName)
if err != nil {
log.LogAppErr(fmt.Sprintf("Couldn't check tag digest changed while checking whether to deploy for %s", repoName), err)
Expand Down
90 changes: 47 additions & 43 deletions client/postgres_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package client

import (
"database/sql"
"math"
"os"
"time"

"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

type PostgresClient struct {
Expand All @@ -19,47 +18,52 @@ type PostgresClient struct {

// Initialize creates tables if they do not exist
// cloud agnostic function
func InitializePostgresClient(conf *viper.Viper) (*PostgresClient, error) {
client := PostgresClient{
conf: conf,
}
var dburl string
if len(os.Getenv("DATABASE_URL")) > 0 {
dburl = os.Getenv("DATABASE_URL")
} else {
dburl = conf.GetString("database_url")
}
createSchema := conf.GetBool("create_database_schema")

var err error
if client.db, err = sqlx.Open("postgres", dburl); err != nil {
return &client, errors.Wrap(err, "unable to open postgres db")
}

if createSchema {
// Since this happens at initialization we
// could encounter racy conditions waiting for pg
// to become available. Wait for it a bit
if err = client.db.Ping(); err != nil {
// Try 3 more times
// 5, 10, 20
for i := 0; i < 3 && err != nil; i++ {
time.Sleep(time.Duration(5*math.Pow(2, float64(i))) * time.Second)
err = client.db.Ping()
}
if err != nil {
return &client, errors.Wrap(err, "error trying to connect to postgres db, retries exhausted")
}
}

if err = client.createTables(); err != nil {
return &client, errors.Wrap(err, "problem executing create tables sql")
}
if err = client.initRows(); err != nil {
return &client, errors.Wrap(err, "problem executing init row sql")
}
func InitializePostgresClient(conf *viper.Viper) (*gorm.DB, error) {
// client := PostgresClient{
// conf: conf,
// }
// var dburl string
// if len(os.Getenv("DATABASE_URL")) > 0 {
// dburl = os.Getenv("DATABASE_URL")
// } else {
// dburl = conf.GetString("database_url")
// }
// createSchema := conf.GetBool("create_database_schema")

// var err error
// if client.db, err = sqlx.Open("postgres", dburl); err != nil {
// return &client, errors.Wrap(err, "unable to open postgres db")
// }

// if createSchema {
// // Since this happens at initialization we
// // could encounter racy conditions waiting for pg
// // to become available. Wait for it a bit
// if err = client.db.Ping(); err != nil {
// // Try 3 more times
// // 5, 10, 20
// for i := 0; i < 3 && err != nil; i++ {
// time.Sleep(time.Duration(5*math.Pow(2, float64(i))) * time.Second)
// err = client.db.Ping()
// }
// if err != nil {
// return &client, errors.Wrap(err, "error trying to connect to postgres db, retries exhausted")
// }
// }

// if err = client.createTables(); err != nil {
// return &client, errors.Wrap(err, "problem executing create tables sql")
// }
// if err = client.initRows(); err != nil {
// return &client, errors.Wrap(err, "problem executing init row sql")
// }
// }
dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
return &client, nil
return db, nil
}

func (client *PostgresClient) createTables() error {
Expand Down
19 changes: 14 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ require (
github.com/nlopes/slack v0.6.0
github.com/pkg/errors v0.9.1
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.21.0
gorm.io/driver/postgres v1.5.6
gorm.io/gorm v1.25.7
)

require (
Expand All @@ -27,6 +29,12 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.1-0.20201016140508-a07e7d50bbee // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.3 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
Expand All @@ -48,10 +56,11 @@ require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/goleak v1.1.12 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 102c096

Please sign in to comment.