Skip to content

Commit

Permalink
team: embed TEAMS.yaml
Browse files Browse the repository at this point in the history
The `internal/team` code looks for `TEAMS.yaml` in the repo. This
means that `roachtest` must be run in the tree (and if the branches
don't match, obscure errors could happen in principle).

This commit moves to embedding the data using `go:embed`. Because
`go:embed` doesn't allow embedding of files outside the package, we
also have to add a generation rule to copy the file.

Fixes: #111661
Release note: None
  • Loading branch information
RaduBerinde committed Nov 4, 2023
1 parent 0496181 commit b26b154
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
1 change: 1 addition & 0 deletions build/bazelutil/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pkg/util/log/channels.go://go:generate go run gen/main.go logpb/log.proto loggin
pkg/util/log/channels.go://go:generate go run gen/main.go logpb/log.proto severity.go severity/severity_generated.go
pkg/util/log/sinks.go://go:generate mockgen -package=log -destination=mocks_generated_test.go --mock_names=TestingLogSink=MockLogSink . TestingLogSink
pkg/util/timeutil/zoneinfo.go://go:generate go run gen/main.go
pkg/internal/team/team.go://go:generate cp ../../../TEAMS.yaml TEAMS.yaml
"

EXISTING_CRDB_TEST_BUILD_CONSTRAINTS="
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/misc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ MISC_SRCS = [
"//pkg/ccl/backupccl:restore_memory_monitoring_generated_test.go",
"//pkg/ccl/backupccl:restore_mid_schema_change_generated_test.go",
"//pkg/ccl/kvccl/kvtenantccl/upgradeinterlockccl:generated_test.go",
"//pkg/internal/team:TEAMS.yaml",
"//pkg/kv/kvpb:batch_generated.go",
"//pkg/kv/kvserver/concurrency:keylocks_interval_btree.go",
"//pkg/kv/kvserver/concurrency:keylocks_interval_btree_test.go",
Expand Down
1 change: 1 addition & 0 deletions pkg/internal/team/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEAMS.yaml
20 changes: 15 additions & 5 deletions pkg/internal/team/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,34 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "team",
srcs = ["team.go"],
data = [
":gen-teams-yaml", # keep
],
embedsrcs = ["TEAMS.yaml"],
importpath = "github.com/cockroachdb/cockroach/pkg/internal/team",
visibility = ["//pkg:__subpackages__"],
deps = [
"//pkg/build/bazel",
"//pkg/internal/reporoot",
"@com_github_cockroachdb_errors//:errors",
"@in_gopkg_yaml_v2//:yaml_v2",
],
)

genrule(
name = "gen-teams-yaml",
srcs = ["//:TEAMS.yaml"],
outs = ["TEAMS.yaml"],
cmd = "cat $(SRCS) > $@",
visibility = [
":__pkg__",
"//pkg/gen:__pkg__",
],
)

go_test(
name = "team_test",
size = "small",
srcs = ["team_test.go"],
args = ["-test.timeout=55s"],
data = [
"//:TEAMS.yaml",
],
embed = [":team"],
deps = ["@com_github_stretchr_testify//require"],
)
32 changes: 8 additions & 24 deletions pkg/internal/team/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
package team

import (
_ "embed"
"io"
"os"
"path/filepath"
"strings"

"github.com/cockroachdb/cockroach/pkg/build/bazel"
"github.com/cockroachdb/cockroach/pkg/internal/reporoot"
"github.com/cockroachdb/errors"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -83,28 +81,14 @@ func (m Map) GetAliasesForPurpose(alias Alias, purpose Purpose) ([]Alias, bool)
return sl, true
}

//go:generate cp ../../../TEAMS.yaml TEAMS.yaml

//go:embed TEAMS.yaml
var teamsYaml string

// DefaultLoadTeams loads teams from the repo root's TEAMS.yaml.
func DefaultLoadTeams() (Map, error) {
var path string
if os.Getenv("BAZEL_TEST") != "" {
runfiles, err := bazel.RunfilesPath()
if err != nil {
return nil, err
}
path = filepath.Join(runfiles, "TEAMS.yaml")
} else {
root := reporoot.GetFor(".", "TEAMS.yaml")
if root == "" {
return nil, errors.New("TEAMS.yaml not found")
}
path = filepath.Join(root, "TEAMS.yaml")
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
return LoadTeams(f)
return LoadTeams(strings.NewReader(teamsYaml))
}

// Purpose determines which alias to return for a given team via
Expand Down

0 comments on commit b26b154

Please sign in to comment.