Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using mysql as a new storage engine for etcd. #18601

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions server/embed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,23 @@ type Config struct {
// - https://bugs.chromium.org/p/project-zero/issues/detail?id=1447#c2
// - https://github.com/transmission/transmission/pull/468
// - https://github.com/etcd-io/etcd/issues/9353
//
// 1. If client connection is secure via HTTPS, allow any hostnames.
// 2. If client connection is not secure and "HostWhitelist" is not empty,
// only allow HTTP requests whose Host field is listed in whitelist.
//
// Note that the client origin policy is enforced whether authentication
// is enabled or not, for tighter controls.
//
// By default, "HostWhitelist" is "*", which allows any hostnames.
// Note that when specifying hostnames, loopback addresses are not added
// automatically. To allow loopback interfaces, leave it empty or set it "*",
// or add them to whitelist manually (e.g. "localhost", "127.0.0.1", etc.).
//
// CVE-2018-5702 reference:
// - https://bugs.chromium.org/p/project-zero/issues/detail?id=1447#c2
// - https://github.com/transmission/transmission/pull/468
// - https://github.com/etcd-io/etcd/issues/9353
HostWhitelist map[string]struct{}

// UserHandlers is for registering users handlers and only used for
Expand Down Expand Up @@ -462,6 +479,11 @@ type Config struct {

// ServerFeatureGate is a server level feature gate
ServerFeatureGate featuregate.FeatureGate

// BackendType specifies the type of backend storage to use
BackendType string `json:"backend-type"`
// MySQLDSN is the Data Source Name for the MySQL backend
MySQLDSN string `json:"mysql-dsn"`
}

// configYAML holds the config suitable for yaml parsing
Expand Down Expand Up @@ -586,6 +608,9 @@ func NewConfig() *Config {

AutoCompactionMode: DefaultAutoCompactionMode,
ServerFeatureGate: features.NewDefaultServerFeatureGate(DefaultName, nil),

BackendType: "mysql",
MySQLDSN: "root:password@tcp(localhost:3306)/etcd",
}
cfg.InitialCluster = cfg.InitialClusterFromName(cfg.Name)
return cfg
Expand Down
2 changes: 2 additions & 0 deletions server/etcdmain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ type config struct {
configFile string
printVersion bool
ignored []string
BackendType string `json:"backend-type"`
MySQLDSN string `json:"mysql-dsn"`
}

// configFlags has the set of flags used for command line parsing a Config
Expand Down
1 change: 1 addition & 0 deletions server/etcdmain/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"go.etcd.io/etcd/server/v3/embed"
"go.etcd.io/etcd/server/v3/etcdserver/api/v2discovery"
"go.etcd.io/etcd/server/v3/etcdserver/errors"
_ "github.com/go-sql-driver/mysql"
)

type dirType string
Expand Down
2 changes: 2 additions & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ require (
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
Expand Down Expand Up @@ -32,6 +34,8 @@ github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down
26 changes: 25 additions & 1 deletion server/storage/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ type backend struct {
lg *zap.Logger
}

type BackendType string

const (
BackendTypeBoltDB BackendType = "boltdb"
BackendTypeMySQL BackendType = "mysql"
)

type BackendConfig struct {
// Path is the file path to the backend file.
Path string
Expand All @@ -149,6 +156,9 @@ type BackendConfig struct {

// Hooks are getting executed during lifecycle of Backend's transactions.
Hooks Hooks

BackendType BackendType
MySQLDSN string // MySQL Data Source Name
}

type BackendConfigOption func(*BackendConfig)
Expand All @@ -159,11 +169,25 @@ func DefaultBackendConfig(lg *zap.Logger) BackendConfig {
BatchLimit: defaultBatchLimit,
MmapSize: InitialMmapSize,
Logger: lg,
BackendType: BackendTypeMySQL, // Change this to MySQL
MySQLDSN: "root:password@tcp(localhost:3306)/etcd", // Default MySQL DSN
}
}

func New(bcfg BackendConfig) Backend {
return newBackend(bcfg)
switch bcfg.BackendType {
case BackendTypeBoltDB:
return newBackend(bcfg)
case BackendTypeMySQL:
be, err := newMySQLBackend(bcfg)
if err != nil {
bcfg.Logger.Panic("failed to create MySQL backend", zap.Error(err))
}
return be
default:
bcfg.Logger.Panic("unknown backend type", zap.String("type", string(bcfg.BackendType)))
return nil
}
}

func WithMmapSize(size uint64) BackendConfigOption {
Expand Down
Loading
Loading