Skip to content

Commit

Permalink
cli,server: static configuration profiles
Browse files Browse the repository at this point in the history
This change introduces a mechanism through which an operator can
select a "configuration profile" via the command-line flag
`--config-profile` or env var `COCKROACH_CONFIG_PROFILE`. The SQL
initialization defined by the profile is applied during server
start-up.

The profiles are (currently) hardcoded inside CockroachDB.

The following profiles are predefined:

- `default`: no configuration.

- `multitenant+noapp`: no pre-defined `application` tenant, but with a
  predefined application tenant template that is used whenever a new
  tenant is defined. This config profile is meant for use for C2C
  replication target clusters.

- `multitenant+app+sharedservice`: shared-process multitenancy with
  pre-defined `application` tenant, based off the same configuration as
  `multitenant+noapp`.

- `replication-source`: alias for `multitenant+app+sharedservice`
- `replication-target`: alias for `multitenant+noapp`

Release note: None
  • Loading branch information
knz committed Apr 2, 2023
1 parent f5588db commit 62781e4
Show file tree
Hide file tree
Showing 20 changed files with 620 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ ALL_TESTS = [
"//pkg/config/zonepb:zonepb_test",
"//pkg/config:config_disallowed_imports_test",
"//pkg/config:config_test",
"//pkg/configprofiles:configprofiles_test",
"//pkg/geo/geogen:geogen_test",
"//pkg/geo/geogfn:geogfn_test",
"//pkg/geo/geographiclib:geographiclib_test",
Expand Down Expand Up @@ -1108,6 +1109,8 @@ GO_TARGETS = [
"//pkg/config/zonepb:zonepb_test",
"//pkg/config:config",
"//pkg/config:config_test",
"//pkg/configprofiles:configprofiles",
"//pkg/configprofiles:configprofiles_test",
"//pkg/docs:docs",
"//pkg/featureflag:featureflag",
"//pkg/gen/genbzl:genbzl",
Expand Down Expand Up @@ -2612,6 +2615,7 @@ GET_X_DATA_TARGETS = [
"//pkg/compose/compare/compare:get_x_data",
"//pkg/config:get_x_data",
"//pkg/config/zonepb:get_x_data",
"//pkg/configprofiles:get_x_data",
"//pkg/docs:get_x_data",
"//pkg/featureflag:get_x_data",
"//pkg/gen/genbzl:get_x_data",
Expand Down
1 change: 1 addition & 0 deletions pkg/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ go_library(
"//pkg/cli/cliflags",
"//pkg/roachpb",
"//pkg/security/username",
"//pkg/server/autoconfig/acprovider",
"//pkg/settings/cluster",
"//pkg/util",
"//pkg/util/envutil",
Expand Down
5 changes: 5 additions & 0 deletions pkg/base/test_server_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/autoconfig/acprovider"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/retry"
Expand Down Expand Up @@ -168,6 +169,10 @@ type TestServerArgs struct {
// ObsServiceAddr is the address to which events will be exported over OTLP.
// If empty, exporting events is inhibited.
ObsServiceAddr string

// AutoConfigProvider provides auto-configuration tasks to apply on
// the cluster during server initialization.
AutoConfigProvider acprovider.Provider
}

// TestClusterArgs contains the parameters one can set when creating a test
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ go_library(
"//pkg/cloud/userfile",
"//pkg/clusterversion",
"//pkg/config",
"//pkg/configprofiles",
"//pkg/docs",
"//pkg/geo/geos",
"//pkg/gossip",
Expand All @@ -143,6 +144,7 @@ go_library(
"//pkg/security/securitytest",
"//pkg/security/username",
"//pkg/server",
"//pkg/server/autoconfig/acprovider",
"//pkg/server/pgurl",
"//pkg/server/profiler",
"//pkg/server/serverpb",
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,12 @@ Disable the creation of a default dataset in the demo shell.
This makes 'cockroach demo' faster to start.`,
}

ConfigProfile = FlagInfo{
Name: "config-profile",
EnvVar: "COCKROACH_CONFIG_PROFILE",
Description: `Select a configuration profile to apply.`,
}

GeoLibsDir = FlagInfo{
Name: "spatial-libs",
Description: `
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/security/clientsecopts"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/server/autoconfig/acprovider"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/storage"
Expand Down Expand Up @@ -639,6 +640,7 @@ func setDemoContextDefaults() {
demoCtx.Multitenant = true
demoCtx.DisableServerController = false
demoCtx.DefaultEnableRangefeeds = true
demoCtx.AutoConfigProvider = acprovider.NoTaskProvider{}

demoCtx.pidFile = ""
demoCtx.disableEnterpriseFeatures = false
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/democluster/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_library(
"//pkg/security/certnames",
"//pkg/security/username",
"//pkg/server",
"//pkg/server/autoconfig/acprovider",
"//pkg/server/pgurl",
"//pkg/server/serverpb",
"//pkg/server/status",
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/democluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/cli/clicfg"
"github.com/cockroachdb/cockroach/pkg/server/autoconfig/acprovider"
"github.com/cockroachdb/cockroach/pkg/workload"
)

Expand Down Expand Up @@ -110,6 +111,10 @@ type Context struct {
// DisableServerController is true if we want to avoid the server
// controller to instantiate tenant secondary servers.
DisableServerController bool

// AutoConfigProvider provides auto-configuration tasks to apply on
// the cluster during server initialization.
AutoConfigProvider acprovider.Provider
}

// IsInteractive returns true if the demo cluster configuration
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/democluster/demo_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ func (demoCtx *Context) testServerArgsForTransientCluster(
StoreSpecs: []base.StoreSpec{storeSpec},
SQLMemoryPoolSize: demoCtx.SQLPoolMemorySize,
CacheSize: demoCtx.CacheSize,
AutoConfigProvider: demoCtx.AutoConfigProvider,
NoAutoInitializeCluster: true,
EnableDemoLoginEndpoint: true,
// Demo clusters by default will create their own tenants, so we
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/cli/clienturl"
"github.com/cockroachdb/cockroach/pkg/cli/cliflagcfg"
"github.com/cockroachdb/cockroach/pkg/cli/cliflags"
"github.com/cockroachdb/cockroach/pkg/configprofiles"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/security/username"
Expand Down Expand Up @@ -393,6 +394,10 @@ func init() {
// planning?
if cmd != connectInitCmd && cmd != connectJoinCmd {
cliflagcfg.StringFlag(f, &serverCfg.Attrs, cliflags.Attrs)
// Cluster initialization. We only do this for a regular start command;
// SQL-only servers get their initialization payload from their tenant
// configuration.
cliflagcfg.VarFlag(f, configprofiles.NewProfileSetter(&serverCfg.AutoConfigProvider), cliflags.ConfigProfile)
}
}

Expand Down Expand Up @@ -828,6 +833,7 @@ func init() {
cliflagcfg.IntFlag(f, &demoCtx.HTTPPort, cliflags.DemoHTTPPort)
cliflagcfg.StringFlag(f, &demoCtx.ListeningURLFile, cliflags.ListeningURLFile)
cliflagcfg.StringFlag(f, &demoCtx.pidFile, cliflags.PIDFile)
cliflagcfg.VarFlag(f, configprofiles.NewProfileSetter(&demoCtx.AutoConfigProvider), cliflags.ConfigProfile)
}

{
Expand Down
49 changes: 49 additions & 0 deletions pkg/configprofiles/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
load("//build/bazelutil/unused_checker:unused.bzl", "get_x_data")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "configprofiles",
srcs = [
"doc.go",
"profiles.go",
"provider.go",
"setter.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/configprofiles",
visibility = ["//visibility:public"],
deps = [
"//pkg/clusterversion",
"//pkg/server/autoconfig/acprovider",
"//pkg/server/autoconfig/autoconfigpb",
"//pkg/util/log",
"//pkg/util/syncutil",
"@com_github_cockroachdb_errors//:errors",
"@com_github_spf13_pflag//:pflag",
],
)

go_test(
name = "configprofiles_test",
srcs = [
"main_test.go",
"profiles_test.go",
],
args = ["-test.timeout=295s"],
embed = [":configprofiles"],
deps = [
"//pkg/base",
"//pkg/build",
"//pkg/ccl",
"//pkg/security/securityassets",
"//pkg/security/securitytest",
"//pkg/server",
"//pkg/server/autoconfig/autoconfigpb",
"//pkg/testutils/serverutils",
"//pkg/testutils/sqlutils",
"//pkg/testutils/testcluster",
"//pkg/util/leaktest",
"//pkg/util/log",
],
)

get_x_data(name = "get_x_data")
16 changes: 16 additions & 0 deletions pkg/configprofiles/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2023 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

// Package configprofiles contain static configuration profiles embedded
// inside the CockroachDB binary.
//
// Configuration profiles are ways to initialize CockroachDB clusters
// differently (upon cluster creation) depending on purpose.
package configprofiles
38 changes: 38 additions & 0 deletions pkg/configprofiles/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2015 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package configprofiles

import (
"os"
"testing"

"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/security/securityassets"
"github.com/cockroachdb/cockroach/pkg/security/securitytest"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
)

func init() {
securityassets.SetLoader(securitytest.EmbeddedAssets)
}

func TestMain(m *testing.M) {
// CLI tests are sensitive to the server version, but test binaries don't have
// a version injected. Pretend to be a very up-to-date version.
defer build.TestingOverrideVersion("v999.0.0")()
serverutils.InitTestServerFactory(server.TestServerFactory)
serverutils.InitTestClusterFactory(testcluster.TestClusterFactory)
os.Exit(m.Run())
}

//go:generate ../util/leaktest/add-leaktest.sh *_test.go
Loading

0 comments on commit 62781e4

Please sign in to comment.