Skip to content

Commit e9d50d0

Browse files
committed
Merge remote-tracking branch 'upstream/main' into fix-redis-exposed-port
2 parents 843a0d8 + 8666833 commit e9d50d0

File tree

2 files changed

+76
-16
lines changed

2 files changed

+76
-16
lines changed

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: 75 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,44 +13,82 @@ 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{"6379"},
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
},
82+
fmt.Sprintf("%s_monitor", prefix): {
83+
Command: "--module=monitor",
84+
ContainerName: fmt.Sprintf("%s_monitor", prefix),
85+
Image: "rss3/node",
86+
},
87+
fmt.Sprintf("%s_broadcaster", prefix): {
88+
Command: "--module=broadcaster",
89+
ContainerName: fmt.Sprintf("%s_broadcaster", prefix),
90+
Image: "rss3/node",
91+
},
5392
},
5493
Volumes: map[string]*string{
5594
cockroachdbVolume: nil,
@@ -119,3 +158,23 @@ func WithWorkers(workers []*config.Module) Option {
119158
c.Services = services
120159
}
121160
}
161+
162+
// SetDependsOnCRDB would set all the rss3 node service to depend on the cockroachdb service
163+
func SetDependsOnCRDB() Option {
164+
return func(c *Compose) {
165+
services := c.Services
166+
for k, v := range services {
167+
if strings.Contains(v.Image, "rss3/node") {
168+
v.DependsOn = map[string]DependsOn{
169+
fmt.Sprintf("%s_cockroachdb", dockerComposeContainerNamePrefix): {
170+
Condition: "service_healthy",
171+
},
172+
fmt.Sprintf("%s_redis", dockerComposeContainerNamePrefix): {
173+
Condition: "service_healthy",
174+
},
175+
}
176+
c.Services[k] = v
177+
}
178+
}
179+
}
180+
}

0 commit comments

Comments
 (0)