Skip to content

Commit

Permalink
ADD: more db retry
Browse files Browse the repository at this point in the history
  • Loading branch information
mc256 committed Nov 15, 2022
1 parent 445fcd7 commit 76819ee
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 74 deletions.
11 changes: 1 addition & 10 deletions cmd/ctr-starlight/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ https://github.com/mc256/starlight
Value: "default",
DefaultText: "default",
EnvVars: []string{"CONTAINERD_NAMESPACE"},
Usage: "namespace to use with commands",
Usage: "namespace to use with commands (if using kubernetes, please specify `k8s.io`)",
Required: false,
},
&cli.StringFlag{
Expand All @@ -90,15 +90,6 @@ https://github.com/mc256/starlight
DefaultText: "info",
Required: false,
},
&cli.StringFlag{
Name: "namespace",
Aliases: []string{"n"},
EnvVars: []string{"CONTAINERD_NAMESPACE"},
Usage: "namespace to use with commands (if using kubernetes, please specify `k8s.io`)",
Value: "default",
DefaultText: "default",
Required: false,
},
}
app.Commands = append([]*cli.Command{
cmdVersion.Command(), // 1. confirm the version of starlight-daemon
Expand Down
4 changes: 3 additions & 1 deletion demo/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
temp.sh
!convert*

*.class
*.class

k8s
15 changes: 2 additions & 13 deletions demo/chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@ starlightProxy:
# For example: "postgres://postgres:postgres@postgres:5432/starlight?sslmode=disable"
# if left blank, the proxy will use the default connection string assuming the PostgresDB is enabled in this chart.
dbConnection: ""

persistence:
enabled: true
existingClaim: ""
storageClass: ""
accessModes: [ReadWriteOnce]
size: 2Gi
logLevel: "info"
defaultRegistry: "http://container-registry.default.svc.cluster.local:5000"

# ---------------------------------------------------------------------
# Metadata database - PostgreSQL
Expand All @@ -48,12 +43,6 @@ adminer:
enabled: true
pullPolicy: IfNotPresent
tag: "latest"
persistence:
enabled: true
existingClaim: ""
storageClass: ""
accessModes: [ReadWriteOnce]
size: 2Gi

# ---------------------------------------------------------------------
# Vanilla Container Registry
Expand Down
40 changes: 4 additions & 36 deletions docs/helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## TL;DR

```shell
helm upgrade --install -f starlight/values.yaml starlight oci://ghcr.io/mc256/starlight/starlight-proxy-chart --version 0.1.2
helm upgrade --install -f starlight/values.yaml starlight oci://ghcr.io/mc256/starlight/starlight-proxy-chart --version 0.2.3
```

## Prerequisites
Expand Down Expand Up @@ -34,14 +34,14 @@ kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storagec
To install the chart with the app name `my-starlight-proxy`:

```shell
helm install my-starlight-proxy oci://ghcr.io/mc256/starlight/starlight-proxy-chart --version 0.1.2
helm install my-starlight-proxy oci://ghcr.io/mc256/starlight/starlight-proxy-chart --version 0.2.3
```


You may want to set a few parameter for example the domain name for the ingress, for example set the domain name for ingress to `mydomain.local`:

```shell
helm install my-starlight-proxy oci://ghcr.io/mc256/starlight/starlight-proxy-chart --version 0.1.2 \
helm install my-starlight-proxy oci://ghcr.io/mc256/starlight/starlight-proxy-chart --version 0.2.3 \
--set "ingress.hosts={mydomain.local}"
```

Expand All @@ -59,37 +59,5 @@ helm delete my-starlight-proxy


## Parameters

### Common Parameters

| Name | Description | Value|
| --- | --- | --- |
| ingress.hosts | domain name for the ingress | [starlight.lan] |
| registryAddress | customize registry address if choose not to use the container registry in this chart | null |
| registry.enable | enable container registry in this deployment | true |

### Starlight Proxy

| Name | Description | Value|
| --- | --- | --- |
| starlightProxy.tag | tage of the image | "latest" |
| starlightProxy.pullPolicy | pull image policy | IfNotPresent |
| starlightProxy.persistence.enabled | tage of the image | "latest" |
| starlightProxy.persistence.existingClaim | if specified, use existing PV | "" |
| starlightProxy.persistence.storageClass | storage class, if not specified, used default storage class | "" |
| starlightProxy.persistence.size | storage size | 2Gi |

### Registry

| Name | Description | Value|
| --- | --- | --- |
| registry.enable | enable registry | true |
| registry.repository | container image | "registry"|
| registry.pullPolicy | pull image policy | IfNotPresent |
| registry.tag | tage of the image | "latest" |
| registry.persistence.enabled | tage of the image | "latest" |
| registry.persistence.existingClaim | if specified, use existing PV | "" |
| registry.persistence.storageClass | storage class, if not specified, used default storage class | "" |
| registry.persistence.size | storage size | 2Gi |

Please the comments in the [`values.yaml`]() file

2 changes: 1 addition & 1 deletion proxy/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func InitDatabase() (context.Context, *Configuration, *Server) {
config: cfg,
cache: make(map[string]*common.LayerCache),
}
if db, err := NewDatabase(cfg.PostgresConnectionString); err != nil {
if db, err := NewDatabase(ctx, cfg.PostgresConnectionString); err != nil {
log.G(ctx).Errorf("failed to connect to database: %v\n", err)
} else {
server.db = db
Expand Down
27 changes: 22 additions & 5 deletions proxy/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
package proxy

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"github.com/containerd/containerd/log"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/lib/pq"
Expand All @@ -30,12 +32,27 @@ func (d *Database) Close() {
_ = d.db.Close()
}

func NewDatabase(conStr string) (*Database, error) {
d, err := sql.Open("postgres", conStr)
if err != nil {
return nil, err
func NewDatabase(ctx context.Context, conStr string) (*Database, error) {
var (
d *sql.DB
err error
)
i := 0
for {
i += 1
d, err = sql.Open("postgres", conStr)
if err == nil {
return &Database{db: d}, nil
} else if err != nil {
if i > 10 {
return nil, err
}
log.G(ctx).
WithError(err).
Errorf("failed to connect to database, retrying in 5 seconds (%d/10)", i)
time.Sleep(5 * time.Second)
}
}
return &Database{db: d}, nil
}

func (d *Database) InitDatabase() error {
Expand Down
7 changes: 5 additions & 2 deletions proxy/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package proxy

import (
"context"
"encoding/json"
"fmt"
"github.com/mc256/starlight/client/fs"
Expand All @@ -20,16 +21,18 @@ var (

func TestMain(m *testing.M) {
cfg, _, _, _ := LoadConfig("")
ctx := context.Background()
var err error
db, err = NewDatabase(cfg.PostgresConnectionString)
db, err = NewDatabase(ctx, cfg.PostgresConnectionString)
if err != nil {
fmt.Printf("failed to connect to database %v\n", err)
}
m.Run()
}

func TestDatabase_Init(t *testing.T) {
db, err := NewDatabase("postgres://postgres:[email protected]:5432/postgres?sslmode=disable")
ctx := context.Background()
db, err := NewDatabase(ctx, "postgres://postgres:[email protected]:5432/postgres?sslmode=disable")
if err != nil {
t.Error(err)
return
Expand Down
2 changes: 1 addition & 1 deletion proxy/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestExtractor_SaveToC(t *testing.T) {
},
config: cfg,
}
if db, err := NewDatabase(cfg.PostgresConnectionString); err != nil {
if db, err := NewDatabase(ctx, cfg.PostgresConnectionString); err != nil {
log.G(ctx).Errorf("failed to connect to database: %v\n", err)
} else {
server.db = db
Expand Down
22 changes: 17 additions & 5 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,29 @@ func NewServer(ctx context.Context, wg *sync.WaitGroup, cfg *Configuration) (*Se
}

// connect database
if db, err := NewDatabase(cfg.PostgresConnectionString); err != nil {
if db, err := NewDatabase(ctx, cfg.PostgresConnectionString); err != nil {
log.G(ctx).Errorf("failed to connect to database: %v\n", err)
} else {
server.db = db
}

// init database
err := server.db.InitDatabase()
if err != nil {
log.G(ctx).Errorf("failed to init database: %v\n", err)
return nil, err
i := 0
for {
i += 1
err := server.db.InitDatabase()
if err != nil {
if i > 10 {
log.G(ctx).Errorf("failed to init database: %v\n", err)
return nil, err
}
log.G(ctx).
WithError(err).
Errorf("failed to connect to database, retrying in 5 seconds (%d/10)", i)
time.Sleep(5 * time.Second)
} else {
break
}
}
log.G(ctx).Debug("database initialized")

Expand Down

0 comments on commit 76819ee

Please sign in to comment.