Skip to content

Commit 23f1dca

Browse files
craig[bot]herkolategan
andcommitted
Merge #152908
152908: roachprod: add codec util r=golgeek a=herkolategan This change introduces a new codec utility to facilitate serializing and deserializing `interface{}`. This is the first step in being able to serialize mixedversion test plans. Since the mixedversion framework utilizes dynamic types (interfaces{}), for `steps`, we need a way to be able to handle those types when serializing or deserializing plans. See the `README.md` in the `codec` package for a more detailed description of how the codec package works, and what it is used for. As part of this PR and as an example we use the new codec to implement serialization for a list of `ClusterSettingOption`. Cluster settings are used by the mixedversion steps, which we intend to serialize eventually. This PR also supplies a `typegen` command that generates registration code for the concrete types we wish to serialize as part of a dynamic type. Informs: #149451, #151461 Epic: None Release note: None Co-authored-by: Herko Lategan <[email protected]>
2 parents 44500c4 + 33ba59b commit 23f1dca

File tree

13 files changed

+749
-0
lines changed

13 files changed

+749
-0
lines changed

pkg/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ ALL_TESTS = [
327327
"//pkg/roachprod/opentelemetry:opentelemetry_test",
328328
"//pkg/roachprod/prometheus:prometheus_test",
329329
"//pkg/roachprod/promhelperclient:promhelperclient_test",
330+
"//pkg/roachprod/roachprodutil/codec:codec_test",
330331
"//pkg/roachprod/ssh:ssh_test",
331332
"//pkg/roachprod/vm/aws:aws_test",
332333
"//pkg/roachprod/vm/azure:azure_test",
@@ -1716,6 +1717,10 @@ GO_TARGETS = [
17161717
"//pkg/roachprod/prometheus:prometheus_test",
17171718
"//pkg/roachprod/promhelperclient:promhelperclient",
17181719
"//pkg/roachprod/promhelperclient:promhelperclient_test",
1720+
"//pkg/roachprod/roachprodutil/codec/cmd/typegen:typegen",
1721+
"//pkg/roachprod/roachprodutil/codec/cmd/typegen:typegen_lib",
1722+
"//pkg/roachprod/roachprodutil/codec:codec",
1723+
"//pkg/roachprod/roachprodutil/codec:codec_test",
17191724
"//pkg/roachprod/roachprodutil:roachprodutil",
17201725
"//pkg/roachprod/ssh:ssh",
17211726
"//pkg/roachprod/ssh:ssh_test",

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
@@ -10,8 +10,10 @@ import (
1010
"os"
1111

1212
"github.com/cockroachdb/cockroach/pkg/roachprod/config"
13+
"github.com/cockroachdb/cockroach/pkg/roachprod/roachprodutil/codec"
1314
"github.com/cockroachdb/cockroach/pkg/roachprod/vm/gce"
1415
"github.com/cockroachdb/errors"
16+
"gopkg.in/yaml.v3"
1517
)
1618

1719
// ClusterSettings contains various knobs that affect operations on a cluster.
@@ -41,10 +43,12 @@ type secureFlagsOpt struct {
4143

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

4750
// ClusterSettingsOption adds cluster settings via SET CLUSTER SETTING.
51+
// typegen:reg
4852
type ClusterSettingsOption map[string]string
4953

5054
func (o ClusterSettingsOption) apply(settings *ClusterSettings) {
@@ -54,20 +58,23 @@ func (o ClusterSettingsOption) apply(settings *ClusterSettings) {
5458
}
5559

5660
// TagOption is used to pass a process tag.
61+
// typegen:reg
5762
type TagOption string
5863

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

6368
// BinaryOption is used to pass a process tag.
69+
// typegen:reg
6470
type BinaryOption string
6571

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

7076
// PGUrlCertsDirOption is used to pass certs dir for secure connections.
77+
// typegen:reg
7178
type PGUrlCertsDirOption string
7279

7380
func (o PGUrlCertsDirOption) apply(settings *ClusterSettings) {
@@ -76,6 +83,7 @@ func (o PGUrlCertsDirOption) apply(settings *ClusterSettings) {
7683

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

8189
func (o ComplexSecureOption) apply(settings *ClusterSettings) {
@@ -127,6 +135,7 @@ func (o ComplexSecureOption) overrideBasedOnClusterSettings(c *SyncedCluster) er
127135

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

132141
func (o SimpleSecureOption) apply(settings *ClusterSettings) {
@@ -151,13 +160,15 @@ type SecureOption interface {
151160
}
152161

153162
// UseTreeDistOption is passed to use treedist copy algorithm.
163+
// typegen:reg
154164
type UseTreeDistOption bool
155165

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

160170
// EnvOption is used to pass environment variables to the cockroach process.
171+
// typegen:reg
161172
type EnvOption []string
162173

163174
var _ EnvOption
@@ -167,6 +178,7 @@ func (o EnvOption) apply(settings *ClusterSettings) {
167178
}
168179

169180
// NumRacksOption is used to pass the number of racks to partition the nodes into.
181+
// typegen:reg
170182
type NumRacksOption int
171183

172184
var _ NumRacksOption
@@ -176,6 +188,7 @@ func (o NumRacksOption) apply(settings *ClusterSettings) {
176188
}
177189

178190
// DebugDirOption is used to stash debug information.
191+
// typegen:reg
179192
type DebugDirOption string
180193

181194
var _ DebugDirOption
@@ -202,3 +215,20 @@ func MakeClusterSettings(opts ...ClusterSettingOption) ClusterSettings {
202215
}
203216
return clusterSettings
204217
}
218+
219+
// ClusterSettingOptionList is a list of ClusterSettingOption that can be
220+
// serialized to YAML. It uses codec.ListWrapper to handle the dynamic types.
221+
type ClusterSettingOptionList []ClusterSettingOption
222+
223+
func (o ClusterSettingOptionList) MarshalYAML() (any, error) {
224+
return codec.WrapList(o), nil
225+
}
226+
227+
func (o *ClusterSettingOptionList) UnmarshalYAML(value *yaml.Node) error {
228+
var lw codec.ListWrapper[ClusterSettingOption]
229+
if err := value.Decode(&lw); err != nil {
230+
return err
231+
}
232+
*o = lw.Get()
233+
return nil
234+
}
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.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "codec",
5+
srcs = ["types.go"],
6+
importpath = "github.com/cockroachdb/cockroach/pkg/roachprod/roachprodutil/codec",
7+
visibility = ["//visibility:public"],
8+
deps = ["@in_gopkg_yaml_v3//:yaml_v3"],
9+
)
10+
11+
go_test(
12+
name = "codec_test",
13+
srcs = [
14+
"types_registry_test.go",
15+
"types_test.go",
16+
],
17+
embed = [":codec"],
18+
deps = [
19+
"@com_github_stretchr_testify//require",
20+
"@in_gopkg_yaml_v3//:yaml_v3",
21+
],
22+
)

0 commit comments

Comments
 (0)