Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit f0cfb8d

Browse files
Merge pull request #1 from xxxibgdrgnmm/refactor-repository
feat: refactor repository factory, add readme
2 parents 153a283 + 2b8bf3d commit f0cfb8d

File tree

20 files changed

+136
-38
lines changed

20 files changed

+136
-38
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
reverse-registry

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM cgr.dev/chainguard/go AS builder
2+
COPY . /app
3+
RUN cd /app && CGO_ENABLED=0 GOOS=linux go build -o reverse-registry .
4+
5+
FROM cgr.dev/chainguard/glibc-dynamic
6+
COPY --from=builder /app/reverse-registry /usr/bin/
7+
COPY ./config/config.local.yaml /etc/reverse-registry/config.local.yaml
8+
CMD ["/usr/bin/go-digester", "server", "--config=/etc/reverse-registry/config.local.yaml"]

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Reverse registry
2+
3+
4+
This is a simple registry redirect service that redirect our.domain.com/* to cgr.dev/chainguard/*. It also run a background process to periodically hash image index to sha256 checksum and save to a local database (default in-mem sqlite is used. For production purpose, mysql is the recommended choice)
5+
6+
Deploying
7+
8+
[![Run on Google Cloud](https://deploy.cloud.run/button.svg)](https://deploy.cloud.run)

app.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "reverse-registry",
3+
"env": {
4+
"WORKER_FETCH_INTERVAL": {
5+
"description": "specify interval to fetch manifest from cgr",
6+
"value": "30s",
7+
"required": true
8+
}
9+
},
10+
"options": {
11+
"allow-unauthenticated": false,
12+
"memory": "512Mi",
13+
"cpu": "1",
14+
"port": 443,
15+
"http2": false,
16+
"concurrency": 80,
17+
"max-instances": 1
18+
},
19+
"build": {
20+
"skip": false,
21+
"buildpacks": {
22+
"builder": "gcr.io/buildpacks/builder:v1"
23+
}
24+
}
25+
}

app/app.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ func RunFetcher(conf config.Config) error {
5858
if err != nil {
5959
return err
6060
}
61+
d, err := time.ParseDuration(conf.WorkerFetchInterval)
62+
if err != nil {
63+
return err
64+
}
6165
fetcher := digestfetcher.New(digestfetcher.Options{
6266
Storage: storage,
6367
Registry: registryClient,
6468
Log: log,
65-
FetchInterval: conf.WorkerFetchInterval * time.Second,
69+
FetchInterval: d,
6670
})
6771
return fetcher.Fetch(conf.Images)
6872
}

cmd/root.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/spf13/cobra"
2929
"github.com/spf13/viper"
3030
"github.com/xxxibgdrgnmm/reverse-registry/config"
31+
"github.com/xxxibgdrgnmm/reverse-registry/constant"
3132
)
3233

3334
var cfgFile string
@@ -92,5 +93,13 @@ func initConfig() {
9293
if err = viper.Unmarshal(&c); err != nil {
9394
panic(fmt.Sprintf("can not marshal config %v", err))
9495
}
96+
wF := os.Getenv(constant.WorkerFetchIntervalEnv)
97+
mP := os.Getenv(constant.MySQLPassWordEnv)
98+
if wF != "" {
99+
c.WorkerFetchInterval = wF
100+
}
101+
if mP != "" {
102+
c.DBConfig.Password = mP
103+
}
95104
}
96105
}

config/config.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package config
22

3-
import "time"
4-
53
type MysqlConfig struct {
64
Host string `mapstructure:"host"`
75
User string `mapstructure:"user"`
@@ -10,9 +8,10 @@ type MysqlConfig struct {
108
}
119

1210
type Config struct {
13-
DBConfig MysqlConfig `mapstructure:"dbConfig"`
14-
Images []Image `mapstructure:"images"`
15-
WorkerFetchInterval time.Duration `mapstructure:"workerFetchInterval"`
11+
DB string `mapstructure:"db"`
12+
DBConfig MysqlConfig `mapstructure:"dbConfig"`
13+
Images []Image `mapstructure:"images"`
14+
WorkerFetchInterval string `mapstructure:"workerFetchInterval"`
1615
}
1716

1817
type Image struct {

config/config.local.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
db: sqlite
12
dbConfig:
23
host: localhost
34
user: root

constant/const.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package constant
2+
3+
const (
4+
WorkerFetchIntervalEnv = "WORKER_FETCH_INTERVAL"
5+
MySQLPassWordEnv = "MYSQL_PASSWORD"
6+
)

driver/mysql_driver.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ func NewMySQLDB(host string, user string, password string, dbName string) (*gorm
1515
Logger: logger.Default.LogMode(logger.Silent),
1616
})
1717
if err != nil {
18-
msg := fmt.Sprintf("cannot connect to database. host: %s, user: %s, db: %s", host, user, dbName)
19-
fmt.Println(msg)
20-
2118
return nil, err
2219
}
2320

0 commit comments

Comments
 (0)