Skip to content

Commit 88541de

Browse files
authored
Merge pull request #1407 from cortexproject/1342-cassandra-config-yaml
Allow Cassandra config to be passed as YAML.
2 parents dee54bf + acd24cf commit 88541de

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

pkg/chunk/cassandra/fixtures.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ func Fixtures() ([]testutils.Fixture, error) {
4141
}
4242

4343
cfg := Config{
44-
addresses: addresses,
45-
keyspace: "test",
46-
consistency: "QUORUM",
47-
replicationFactor: 1,
44+
Addresses: addresses,
45+
Keyspace: "test",
46+
Consistency: "QUORUM",
47+
ReplicationFactor: 1,
4848
}
4949

5050
// Get a SchemaConfig with the defaults.

pkg/chunk/cassandra/storage_client.go

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,40 @@ const (
2020

2121
// Config for a StorageClient
2222
type Config struct {
23-
addresses string
24-
port int
25-
keyspace string
26-
consistency string
27-
replicationFactor int
28-
disableInitialHostLookup bool
29-
ssl bool
30-
hostVerification bool
31-
caPath string
32-
auth bool
33-
username string
34-
password string
35-
timeout time.Duration
23+
Addresses string `yaml:"addresses,omitempty"`
24+
Port int `yaml:"port,omitempty"`
25+
Keyspace string `yaml:"keyspace,omitempty"`
26+
Consistency string `yaml:"consistency,omitempty"`
27+
ReplicationFactor int `yaml:"replication_factor,omitempty"`
28+
DisableInitialHostLookup bool `yaml:"disable_initial_host_lookup,omitempty"`
29+
SSL bool `yaml:"SSL,omitempty"`
30+
HostVerification bool `yaml:"host_verification,omitempty"`
31+
CAPath string `yaml:"CA_path,omitempty"`
32+
Auth bool `yaml:"auth,omitempty"`
33+
Username string `yaml:"username,omitempty"`
34+
Password string `yaml:"password,omitempty"`
35+
Timeout time.Duration `yaml:"timeout,omitempty"`
3636
}
3737

3838
// RegisterFlags adds the flags required to config this to the given FlagSet
3939
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
40-
f.StringVar(&cfg.addresses, "cassandra.addresses", "", "Comma-separated hostnames or ips of Cassandra instances.")
41-
f.IntVar(&cfg.port, "cassandra.port", 9042, "Port that Cassandra is running on")
42-
f.StringVar(&cfg.keyspace, "cassandra.keyspace", "", "Keyspace to use in Cassandra.")
43-
f.StringVar(&cfg.consistency, "cassandra.consistency", "QUORUM", "Consistency level for Cassandra.")
44-
f.IntVar(&cfg.replicationFactor, "cassandra.replication-factor", 1, "Replication factor to use in Cassandra.")
45-
f.BoolVar(&cfg.disableInitialHostLookup, "cassandra.disable-initial-host-lookup", false, "Instruct the cassandra driver to not attempt to get host info from the system.peers table.")
46-
f.BoolVar(&cfg.ssl, "cassandra.ssl", false, "Use SSL when connecting to cassandra instances.")
47-
f.BoolVar(&cfg.hostVerification, "cassandra.host-verification", true, "Require SSL certificate validation.")
48-
f.StringVar(&cfg.caPath, "cassandra.ca-path", "", "Path to certificate file to verify the peer.")
49-
f.BoolVar(&cfg.auth, "cassandra.auth", false, "Enable password authentication when connecting to cassandra.")
50-
f.StringVar(&cfg.username, "cassandra.username", "", "Username to use when connecting to cassandra.")
51-
f.StringVar(&cfg.password, "cassandra.password", "", "Password to use when connecting to cassandra.")
52-
f.DurationVar(&cfg.timeout, "cassandra.timeout", 600*time.Millisecond, "Timeout when connecting to cassandra.")
40+
f.StringVar(&cfg.Addresses, "cassandra.addresses", "", "Comma-separated hostnames or IPs of Cassandra instances.")
41+
f.IntVar(&cfg.Port, "cassandra.port", 9042, "Port that Cassandra is running on")
42+
f.StringVar(&cfg.Keyspace, "cassandra.keyspace", "", "Keyspace to use in Cassandra.")
43+
f.StringVar(&cfg.Consistency, "cassandra.consistency", "QUORUM", "Consistency level for Cassandra.")
44+
f.IntVar(&cfg.ReplicationFactor, "cassandra.replication-factor", 1, "Replication factor to use in Cassandra.")
45+
f.BoolVar(&cfg.DisableInitialHostLookup, "cassandra.disable-initial-host-lookup", false, "Instruct the cassandra driver to not attempt to get host info from the system.peers table.")
46+
f.BoolVar(&cfg.SSL, "cassandra.ssl", false, "Use SSL when connecting to cassandra instances.")
47+
f.BoolVar(&cfg.HostVerification, "cassandra.host-verification", true, "Require SSL certificate validation.")
48+
f.StringVar(&cfg.CAPath, "cassandra.ca-path", "", "Path to certificate file to verify the peer.")
49+
f.BoolVar(&cfg.Auth, "cassandra.auth", false, "Enable password authentication when connecting to cassandra.")
50+
f.StringVar(&cfg.Username, "cassandra.username", "", "Username to use when connecting to cassandra.")
51+
f.StringVar(&cfg.Password, "cassandra.password", "", "Password to use when connecting to cassandra.")
52+
f.DurationVar(&cfg.Timeout, "cassandra.timeout", 600*time.Millisecond, "Timeout when connecting to cassandra.")
5353
}
5454

5555
func (cfg *Config) session() (*gocql.Session, error) {
56-
consistency, err := gocql.ParseConsistencyWrapper(cfg.consistency)
56+
consistency, err := gocql.ParseConsistencyWrapper(cfg.Consistency)
5757
if err != nil {
5858
return nil, errors.WithStack(err)
5959
}
@@ -62,40 +62,40 @@ func (cfg *Config) session() (*gocql.Session, error) {
6262
return nil, errors.WithStack(err)
6363
}
6464

65-
cluster := gocql.NewCluster(strings.Split(cfg.addresses, ",")...)
66-
cluster.Port = cfg.port
67-
cluster.Keyspace = cfg.keyspace
65+
cluster := gocql.NewCluster(strings.Split(cfg.Addresses, ",")...)
66+
cluster.Port = cfg.Port
67+
cluster.Keyspace = cfg.Keyspace
6868
cluster.Consistency = consistency
6969
cluster.BatchObserver = observer{}
7070
cluster.QueryObserver = observer{}
71-
cluster.Timeout = cfg.timeout
71+
cluster.Timeout = cfg.Timeout
7272
cfg.setClusterConfig(cluster)
7373

7474
return cluster.CreateSession()
7575
}
7676

7777
// apply config settings to a cassandra ClusterConfig
7878
func (cfg *Config) setClusterConfig(cluster *gocql.ClusterConfig) {
79-
cluster.DisableInitialHostLookup = cfg.disableInitialHostLookup
79+
cluster.DisableInitialHostLookup = cfg.DisableInitialHostLookup
8080

81-
if cfg.ssl {
81+
if cfg.SSL {
8282
cluster.SslOpts = &gocql.SslOptions{
83-
CaPath: cfg.caPath,
84-
EnableHostVerification: cfg.hostVerification,
83+
CaPath: cfg.CAPath,
84+
EnableHostVerification: cfg.HostVerification,
8585
}
8686
}
87-
if cfg.auth {
87+
if cfg.Auth {
8888
cluster.Authenticator = gocql.PasswordAuthenticator{
89-
Username: cfg.username,
90-
Password: cfg.password,
89+
Username: cfg.Username,
90+
Password: cfg.Password,
9191
}
9292
}
9393
}
9494

9595
// createKeyspace will create the desired keyspace if it doesn't exist.
9696
func (cfg *Config) createKeyspace() error {
97-
cluster := gocql.NewCluster(strings.Split(cfg.addresses, ",")...)
98-
cluster.Port = cfg.port
97+
cluster := gocql.NewCluster(strings.Split(cfg.Addresses, ",")...)
98+
cluster.Port = cfg.Port
9999
cluster.Keyspace = "system"
100100
cluster.Timeout = 20 * time.Second
101101

@@ -113,7 +113,7 @@ func (cfg *Config) createKeyspace() error {
113113
'class' : 'SimpleStrategy',
114114
'replication_factor' : %d
115115
}`,
116-
cfg.keyspace, cfg.replicationFactor)).Exec()
116+
cfg.Keyspace, cfg.ReplicationFactor)).Exec()
117117
return errors.WithStack(err)
118118
}
119119

pkg/chunk/cassandra/table_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewTableClient(ctx context.Context, cfg Config) (chunk.TableClient, error)
2828
}
2929

3030
func (c *tableClient) ListTables(ctx context.Context) ([]string, error) {
31-
md, err := c.session.KeyspaceMetadata(c.cfg.keyspace)
31+
md, err := c.session.KeyspaceMetadata(c.cfg.Keyspace)
3232
if err != nil {
3333
return nil, errors.WithStack(err)
3434
}

0 commit comments

Comments
 (0)