Skip to content

Commit 33ba59b

Browse files
committed
roachtest: serializable cluster options
Previously, a list of cluster options was not serializable to YAML (due to the functional and dynamic nature of how a cluster option is implemented). This is required to eventually support serializing mixedversion plans, since some of the steps in the plan keep a list of cluster options that need to be applied to a cluster. This change uses the codec package to enable serializing a list of cluster options. Informs: #149451, #151461 Epic: None Release note: None
1 parent 8368b3a commit 33ba59b

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

pkg/gen/misc.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ MISC_SRCS = [
1111
"//pkg/kv/kvserver/concurrency:keylocks_interval_btree_test.go",
1212
"//pkg/kv/kvserver/spanlatch:latch_interval_btree.go",
1313
"//pkg/kv/kvserver/spanlatch:latch_interval_btree_test.go",
14+
"//pkg/roachprod/install:types_generated.go",
1415
"//pkg/roachprod/vm/aws:terraform/main.tf",
1516
"//pkg/spanconfig/spanconfigstore:entry_interval_btree.go",
1617
"//pkg/spanconfig/spanconfigstore:entry_interval_btree_test.go",

pkg/roachprod/install/BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go_library(
1616
"services.go",
1717
"session.go",
1818
"staging.go",
19+
":typegen-cluster-settings", # keep
1920
],
2021
embedsrcs = [
2122
"files/cockroachdb-logging.yaml",
@@ -34,6 +35,7 @@ go_library(
3435
"//pkg/roachprod/errors",
3536
"//pkg/roachprod/logger",
3637
"//pkg/roachprod/roachprodutil",
38+
"//pkg/roachprod/roachprodutil/codec",
3739
"//pkg/roachprod/ssh",
3840
"//pkg/roachprod/ui",
3941
"//pkg/roachprod/vm",
@@ -49,6 +51,7 @@ go_library(
4951
"@com_github_alessio_shellescape//:shellescape",
5052
"@com_github_cockroachdb_errors//:errors",
5153
"@com_github_cockroachdb_version//:version",
54+
"@in_gopkg_yaml_v3//:yaml_v3",
5255
"@org_golang_x_exp//maps",
5356
"@org_golang_x_sync//errgroup",
5457
"@org_golang_x_sys//unix",
@@ -58,6 +61,7 @@ go_library(
5861
go_test(
5962
name = "install_test",
6063
srcs = [
64+
"cluster_settings_test.go",
6165
"cluster_synced_test.go",
6266
"cockroach_test.go",
6367
"expander_test.go",
@@ -84,5 +88,22 @@ go_test(
8488
"@com_github_cockroachdb_datadriven//:datadriven",
8589
"@com_github_cockroachdb_errors//:errors",
8690
"@com_github_stretchr_testify//require",
91+
"@in_gopkg_yaml_v3//:yaml_v3",
92+
],
93+
)
94+
95+
genrule(
96+
name = "typegen-cluster-settings",
97+
srcs = ["cluster_settings.go"],
98+
outs = ["types_generated.go"],
99+
cmd = """$(location //pkg/roachprod/roachprodutil/codec/cmd/typegen:typegen) \
100+
-output $@ $(location cluster_settings.go)
101+
""",
102+
tools = [
103+
"//pkg/roachprod/roachprodutil/codec/cmd/typegen",
104+
],
105+
visibility = [
106+
":__pkg__",
107+
"//pkg/gen:__pkg__",
87108
],
88109
)

pkg/roachprod/install/cluster_settings.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"fmt"
1010

1111
"github.com/cockroachdb/cockroach/pkg/roachprod/config"
12+
"github.com/cockroachdb/cockroach/pkg/roachprod/roachprodutil/codec"
1213
"github.com/cockroachdb/cockroach/pkg/roachprod/vm/gce"
1314
"github.com/cockroachdb/errors"
15+
"gopkg.in/yaml.v3"
1416
)
1517

1618
// ClusterSettings contains various knobs that affect operations on a cluster.
@@ -40,10 +42,12 @@ type secureFlagsOpt struct {
4042

4143
// ClusterSettingOption is the interface satisfied by options to MakeClusterSettings.
4244
type ClusterSettingOption interface {
45+
codec.DynamicType
4346
apply(settings *ClusterSettings)
4447
}
4548

4649
// ClusterSettingsOption adds cluster settings via SET CLUSTER SETTING.
50+
// typegen:reg
4751
type ClusterSettingsOption map[string]string
4852

4953
func (o ClusterSettingsOption) apply(settings *ClusterSettings) {
@@ -53,20 +57,23 @@ func (o ClusterSettingsOption) apply(settings *ClusterSettings) {
5357
}
5458

5559
// TagOption is used to pass a process tag.
60+
// typegen:reg
5661
type TagOption string
5762

5863
func (o TagOption) apply(settings *ClusterSettings) {
5964
settings.Tag = string(o)
6065
}
6166

6267
// BinaryOption is used to pass a process tag.
68+
// typegen:reg
6369
type BinaryOption string
6470

6571
func (o BinaryOption) apply(settings *ClusterSettings) {
6672
settings.Binary = string(o)
6773
}
6874

6975
// PGUrlCertsDirOption is used to pass certs dir for secure connections.
76+
// typegen:reg
7077
type PGUrlCertsDirOption string
7178

7279
func (o PGUrlCertsDirOption) apply(settings *ClusterSettings) {
@@ -75,6 +82,7 @@ func (o PGUrlCertsDirOption) apply(settings *ClusterSettings) {
7582

7683
// ComplexSecureOption is a complex type for secure options that keeps track of
7784
// the user's intent regarding security.
85+
// typegen:reg
7886
type ComplexSecureOption secureFlagsOpt
7987

8088
func (o ComplexSecureOption) apply(settings *ClusterSettings) {
@@ -126,6 +134,7 @@ func (o ComplexSecureOption) overrideBasedOnClusterSettings(c *SyncedCluster) er
126134

127135
// SimpleSecureOption is a simple type that simplifies setting the secure flags
128136
// in the cluster settings without keeping track of --secure or --insecure options.
137+
// typegen:reg
129138
type SimpleSecureOption bool
130139

131140
func (o SimpleSecureOption) apply(settings *ClusterSettings) {
@@ -150,13 +159,15 @@ type SecureOption interface {
150159
}
151160

152161
// UseTreeDistOption is passed to use treedist copy algorithm.
162+
// typegen:reg
153163
type UseTreeDistOption bool
154164

155165
func (o UseTreeDistOption) apply(settings *ClusterSettings) {
156166
settings.UseTreeDist = bool(o)
157167
}
158168

159169
// EnvOption is used to pass environment variables to the cockroach process.
170+
// typegen:reg
160171
type EnvOption []string
161172

162173
var _ EnvOption
@@ -166,6 +177,7 @@ func (o EnvOption) apply(settings *ClusterSettings) {
166177
}
167178

168179
// NumRacksOption is used to pass the number of racks to partition the nodes into.
180+
// typegen:reg
169181
type NumRacksOption int
170182

171183
var _ NumRacksOption
@@ -175,6 +187,7 @@ func (o NumRacksOption) apply(settings *ClusterSettings) {
175187
}
176188

177189
// DebugDirOption is used to stash debug information.
190+
// typegen:reg
178191
type DebugDirOption string
179192

180193
var _ DebugDirOption
@@ -201,3 +214,20 @@ func MakeClusterSettings(opts ...ClusterSettingOption) ClusterSettings {
201214
}
202215
return clusterSettings
203216
}
217+
218+
// ClusterSettingOptionList is a list of ClusterSettingOption that can be
219+
// serialized to YAML. It uses codec.ListWrapper to handle the dynamic types.
220+
type ClusterSettingOptionList []ClusterSettingOption
221+
222+
func (o ClusterSettingOptionList) MarshalYAML() (any, error) {
223+
return codec.WrapList(o), nil
224+
}
225+
226+
func (o *ClusterSettingOptionList) UnmarshalYAML(value *yaml.Node) error {
227+
var lw codec.ListWrapper[ClusterSettingOption]
228+
if err := value.Decode(&lw); err != nil {
229+
return err
230+
}
231+
*o = lw.Get()
232+
return nil
233+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2025 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package install
7+
8+
import (
9+
"bytes"
10+
"testing"
11+
12+
"github.com/stretchr/testify/require"
13+
"gopkg.in/yaml.v3"
14+
)
15+
16+
func TestClusterSettingOptionListCodec(t *testing.T) {
17+
opts := ClusterSettingOptionList{
18+
NumRacksOption(1),
19+
DebugDirOption("foo"),
20+
}
21+
data, err := yaml.Marshal(opts)
22+
require.NoError(t, err)
23+
24+
var decOpts ClusterSettingOptionList
25+
dec := yaml.NewDecoder(bytes.NewReader(data))
26+
dec.KnownFields(true)
27+
err = dec.Decode(&decOpts)
28+
require.NoError(t, err)
29+
30+
require.Equal(t, opts, decOpts)
31+
require.Equal(t, MakeClusterSettings(opts...), MakeClusterSettings(decOpts...))
32+
}

pkg/roachprod/install/types_generated.go

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)