Skip to content

Commit 996ef5f

Browse files
committed
feat: use alloydb omni instead of crdb
Signed-off-by: STRRL <[email protected]>
1 parent b4ae08a commit 996ef5f

File tree

2 files changed

+84
-17
lines changed

2 files changed

+84
-17
lines changed

pkg/cmd/cmd.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ Then, with a single command, you create and start all the services from your con
4545
}
4646
}
4747

48+
patchFileSetDatabaseConnectionURI(file, "postgres://postgres:password@rss3_node_alloydb:5432/postgres")
49+
4850
composeFile := compose.NewCompose(
4951
compose.WithWorkers(cfg.Component.Decentralized),
50-
compose.SetDependsOnCRDB(),
52+
compose.SetDependsOnAlloyDB(),
5153
compose.SetNodeVersion(version),
5254
compose.SetNodeVolume(),
5355
compose.SetRestartPolicy(),
@@ -84,6 +86,69 @@ func randomString(n int) string {
8486
return string(b)
8587
}
8688

89+
func patchFileSetDatabaseConnectionURI(file string, newConnectionURI string) error {
90+
discovered, err := discoverConfigFile(file)
91+
if err != nil {
92+
return fmt.Errorf("patch config file with new database connection uri, discover config file, %w", err)
93+
}
94+
95+
f, err := os.Open(discovered)
96+
if err != nil {
97+
return fmt.Errorf("patch config file with new database connection uri, open config file, %w", err)
98+
}
99+
100+
// we do not unmashal DSL node config.File directly because it does not have yaml struct tags and it does not play well with yaml encoder
101+
// as a workaround, we unmarshal the file into yaml.Node then manually patch the access token
102+
var root yaml.Node
103+
if err = yaml.NewDecoder(f).Decode(&root); err != nil {
104+
return fmt.Errorf("patch config file with new database connection uri, decode config file, %w", err)
105+
}
106+
107+
if len(root.Content) > 0 {
108+
databaseNode, err := findYamlNode("database", root.Content[0])
109+
if err != nil {
110+
return fmt.Errorf("patch config file with new database connection uri, find database node, %w", err)
111+
}
112+
113+
if databaseNode == nil {
114+
return fmt.Errorf("patch config file with new database connection uri, database node not found")
115+
}
116+
117+
uriNode, err := findYamlNode("uri", databaseNode)
118+
if err != nil {
119+
return fmt.Errorf("patch config file with new database connection uri, find uri node, %w", err)
120+
}
121+
122+
if uriNode == nil {
123+
return fmt.Errorf("patch config file with new database connection uri, uri node not found")
124+
}
125+
126+
uriNode.Kind = yaml.ScalarNode
127+
uriNode.Tag = "!!str"
128+
uriNode.Value = newConnectionURI
129+
130+
}
131+
132+
// dump patched yaml node to file
133+
if err = f.Close(); err != nil {
134+
return fmt.Errorf("patch config file with generated access token, close config file, %w", err)
135+
}
136+
137+
f, err = os.OpenFile(discovered, os.O_WRONLY|os.O_TRUNC, 0644)
138+
if err != nil {
139+
return fmt.Errorf("patch config file with generated access token, open config file, %w", err)
140+
}
141+
142+
err = yaml.NewEncoder(f).Encode(&root)
143+
if err != nil {
144+
return fmt.Errorf("patch config file with generated access token, encode config file, %w", err)
145+
}
146+
147+
f.Close()
148+
149+
return nil
150+
}
151+
87152
func patchConfigFileWithAccessToken(file string, accessToken string) error {
88153
discovered, err := discoverConfigFile(file)
89154
if err != nil {

pkg/compose/compose.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Option func(*Compose)
4343
const dockerComposeContainerNamePrefix = "rss3_node"
4444

4545
func NewCompose(options ...Option) *Compose {
46-
cockroachdbVolume := "cockroachdb"
46+
alloydbVolume := "alloydb"
4747

4848
compose := &Compose{
4949
Services: map[string]Service{
@@ -58,19 +58,21 @@ func NewCompose(options ...Option) *Compose {
5858
Retries: 3,
5959
},
6060
},
61-
fmt.Sprintf("%s_cockroachdb", dockerComposeContainerNamePrefix): {
62-
Command: "start-single-node --cluster-name=node --insecure",
63-
ContainerName: fmt.Sprintf("%s_cockroachdb", dockerComposeContainerNamePrefix),
64-
Expose: []string{"26257", "8080"},
65-
Image: "cockroachdb/cockroach:v23.2.5",
66-
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
61+
fmt.Sprintf("%s_alloydb", dockerComposeContainerNamePrefix): {
62+
ContainerName: fmt.Sprintf("%s_alloydb", dockerComposeContainerNamePrefix),
63+
Expose: []string{"5432"},
64+
Image: "google/alloydbomni:latest",
65+
Volumes: []string{fmt.Sprintf("%s:/alloydb/alloydb-data", alloydbVolume)},
66+
Environment: map[string]string{
67+
"DATA_DIR": "/var/lib/postgresql/data",
68+
"HOST_PORT": "5432",
69+
"POSTGRES_PASSWORD": "password",
70+
},
6971
Healthcheck: Healthcheck{
70-
Test: []string{"CMD", "curl", "-f", "http://localhost:8080/health?ready=1"},
72+
Test: []string{"CMD-SHELL", "pg_isready -U postgres"},
7173
Interval: 5 * time.Second,
72-
Timeout: 1 * time.Second,
73-
Retries: 3,
74+
Timeout: 5 * time.Second,
75+
Retries: 5,
7476
},
7577
},
7678
fmt.Sprintf("%s_core", dockerComposeContainerNamePrefix): {
@@ -91,7 +93,7 @@ func NewCompose(options ...Option) *Compose {
9193
},
9294
},
9395
Volumes: map[string]*string{
94-
cockroachdbVolume: nil,
96+
alloydbVolume: nil,
9597
},
9698
}
9799

@@ -159,14 +161,14 @@ func WithWorkers(workers []*config.Module) Option {
159161
}
160162
}
161163

162-
// SetDependsOnCRDB would set all the rss3 node service to depend on the cockroachdb service
163-
func SetDependsOnCRDB() Option {
164+
// SetDependsOnAlloyDB would set all the rss3 node service to depend on the AlloyDB service
165+
func SetDependsOnAlloyDB() Option {
164166
return func(c *Compose) {
165167
services := c.Services
166168
for k, v := range services {
167169
if strings.Contains(v.Image, "rss3/node") {
168170
v.DependsOn = map[string]DependsOn{
169-
fmt.Sprintf("%s_cockroachdb", dockerComposeContainerNamePrefix): {
171+
fmt.Sprintf("%s_alloydb", dockerComposeContainerNamePrefix): {
170172
Condition: "service_healthy",
171173
},
172174
fmt.Sprintf("%s_redis", dockerComposeContainerNamePrefix): {

0 commit comments

Comments
 (0)