Skip to content

Commit 8bacaef

Browse files
authored
Merge pull request #20 from STRRL/setup-crdb-health-check-and-dependencies
feat: setup health checker on crdb and redis
2 parents e8f4c29 + 60fb356 commit 8bacaef

2 files changed

Lines changed: 66 additions & 16 deletions

File tree

pkg/cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Then, with a single command, you create and start all the services from your con
3636

3737
composeFile := compose.NewCompose(
3838
compose.WithWorkers(cfg.Component.Decentralized),
39+
compose.SetDependsOnCRDB(),
3940
compose.SetNodeVersion(version),
4041
compose.SetNodeVolume(),
4142
compose.SetRestartPolicy(),

pkg/compose/compose.go

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package compose
33
import (
44
"fmt"
55
"strings"
6+
"time"
67

78
"github.com/rss3-network/node/config"
89
)
@@ -12,41 +13,69 @@ type Compose struct {
1213
Volumes map[string]*string
1314
}
1415

16+
type Healthcheck struct {
17+
Test []string `yaml:"test,omitempty"`
18+
Interval time.Duration `yaml:"interval,omitempty"`
19+
Timeout time.Duration `yaml:"timeout,omitempty"`
20+
Retries int `yaml:"retries,omitempty"`
21+
}
22+
23+
type DependsOn struct {
24+
Condition string `yaml:"condition,omitempty"`
25+
}
26+
1527
type Service struct {
16-
Command string `yaml:"command,omitempty"`
17-
ContainerName string `yaml:"container_name,omitempty"`
18-
Environment map[string]string `yaml:"environment,omitempty"`
19-
Expose []string `yaml:"expose,omitempty"`
20-
Image string `yaml:"image"`
21-
Restart string `yaml:"restart,omitempty"`
22-
Ports []string `yaml:"ports,omitempty"`
23-
Volumes []string `yaml:"volumes,omitempty"`
28+
Command string `yaml:"command,omitempty"`
29+
ContainerName string `yaml:"container_name,omitempty"`
30+
Environment map[string]string `yaml:"environment,omitempty"`
31+
Expose []string `yaml:"expose,omitempty"`
32+
Image string `yaml:"image"`
33+
Restart string `yaml:"restart,omitempty"`
34+
Ports []string `yaml:"ports,omitempty"`
35+
Volumes []string `yaml:"volumes,omitempty"`
36+
Healthcheck Healthcheck `yaml:"healthcheck,omitempty"`
37+
DependsOn map[string]DependsOn `yaml:"depends_on,omitempty"`
2438
}
2539

2640
type Option func(*Compose)
2741

42+
// use a prefix to avoid conflict with other containers
43+
const dockerComposeContainerNamePrefix = "rss3_node"
44+
2845
func NewCompose(options ...Option) *Compose {
29-
// use a prefix to avoid conflict with other containers
30-
prefix := "rss3_node"
3146
cockroachdbVolume := "cockroachdb"
3247

3348
compose := &Compose{
3449
Services: map[string]Service{
35-
fmt.Sprintf("%s_redis", prefix): {
36-
ContainerName: fmt.Sprintf("%s_redis", prefix),
50+
fmt.Sprintf("%s_redis", dockerComposeContainerNamePrefix): {
51+
ContainerName: fmt.Sprintf("%s_redis", dockerComposeContainerNamePrefix),
3752
Expose: []string{"6397"},
3853
Image: "redis:7-alpine",
54+
Healthcheck: Healthcheck{
55+
Test: []string{"CMD", "redis-cli", "ping"},
56+
Interval: 5 * time.Second,
57+
Timeout: 10 * time.Second,
58+
Retries: 3,
59+
},
3960
},
40-
fmt.Sprintf("%s_cockroachdb", prefix): {
61+
fmt.Sprintf("%s_cockroachdb", dockerComposeContainerNamePrefix): {
4162
Command: "start-single-node --cluster-name=node --insecure",
42-
ContainerName: fmt.Sprintf("%s_cockroachdb", prefix),
63+
ContainerName: fmt.Sprintf("%s_cockroachdb", dockerComposeContainerNamePrefix),
4364
Expose: []string{"26257", "8080"},
4465
Image: "cockroachdb/cockroach:v23.2.5",
4566
Volumes: []string{fmt.Sprintf("%s:/cockroach/cockroach-data", cockroachdbVolume)},
67+
// we use similar healthcheck as the official cockroachdb operator
68+
// ref: https://github.com/cockroachdb/cockroach-operator/blob/28d139cb0c19d3c7984b2b2da1b25c5ba388d814/pkg/resource/testdata/TestStatefulSetBuilder/default_secure.golden#L76-L83
69+
Healthcheck: Healthcheck{
70+
Test: []string{"CMD", "curl", "-f", "http://localhost:8080/health?ready=1"},
71+
Interval: 5 * time.Second,
72+
Timeout: 1 * time.Second,
73+
Retries: 3,
74+
},
4675
},
47-
fmt.Sprintf("%s_core", prefix): {
76+
fmt.Sprintf("%s_core", dockerComposeContainerNamePrefix): {
4877
Command: "--module=core",
49-
ContainerName: fmt.Sprintf("%s_core", prefix),
78+
ContainerName: fmt.Sprintf("%s_core", dockerComposeContainerNamePrefix),
5079
Ports: []string{"8080:80"},
5180
Image: "rss3/node",
5281
},
@@ -119,3 +148,23 @@ func WithWorkers(workers []*config.Module) Option {
119148
c.Services = services
120149
}
121150
}
151+
152+
// SetDependsOnCRDB would set all the rss3 node service to depend on the cockroachdb service
153+
func SetDependsOnCRDB() Option {
154+
return func(c *Compose) {
155+
services := c.Services
156+
for k, v := range services {
157+
if strings.Contains(v.Image, "rss3/node") {
158+
v.DependsOn = map[string]DependsOn{
159+
fmt.Sprintf("%s_cockroachdb", dockerComposeContainerNamePrefix): {
160+
Condition: "service_healthy",
161+
},
162+
fmt.Sprintf("%s_redis", dockerComposeContainerNamePrefix): {
163+
Condition: "service_healthy",
164+
},
165+
}
166+
c.Services[k] = v
167+
}
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)