Skip to content

Commit

Permalink
PR comments and better prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
enocom committed Nov 7, 2022
1 parent 74a2c1d commit 7054b95
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 45 deletions.
23 changes: 12 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,13 @@ Configuration using environment variables
`

const envPrefix = "CLOUD_SQL"
const envPrefix = "CSQL_PROXY"

func instanceConnectionNames(args []string) []string {
// If args is already populated from a CLI invocation, ignore any instance
// connection name env vars and return the CLI args.
if len(args) > 0 {
return args
}
func instanceFromEnv(args []string) []string {
// This supports naming the first instance first with:
// INSTANCE_CONNECTION_NAME
// or if that's not defined, with:
// INSTANCE_CONNECTION_NAME_0
inst := os.Getenv(fmt.Sprintf("%s_INSTANCE_CONNECTION_NAME", envPrefix))
if inst == "" {
inst = os.Getenv(fmt.Sprintf("%s_INSTANCE_CONNECTION_NAME_0", envPrefix))
Expand Down Expand Up @@ -293,7 +292,10 @@ func NewCommand(opts ...Option) *Command {
}

cmd.Args = func(cmd *cobra.Command, args []string) error {
args = instanceConnectionNames(args)
// If args is not already populated, try to read from the environment.
if len(args) == 0 {
args = instanceFromEnv(args)
}
// Handle logger separately from config
if c.conf.StructuredLogs {
c.logger, c.cleanup = log.NewStructuredLogger()
Expand Down Expand Up @@ -392,9 +394,8 @@ is the target account.`)
_ = v.BindPFlags(pflags)

pflags.VisitAll(func(f *pflag.Flag) {
// When the flag has not been set, but there is a Viper value, set the
// flag to the Viper value. This removes the need to manually assign
// Viper values into the proxy.Config for each value.
// Override any unset flags with Viper values to use the pflags
// object as a single source of truth.
if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
pflags.Set(f.Name, fmt.Sprintf("%v", val))
Expand Down
76 changes: 42 additions & 34 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ func withDefaults(c *proxy.Config) *proxy.Config {
return c
}

// pointer returns a pointer to v
// pointer returns the address of v and makes it easy to take the address of a
// predeclared identifier. Compare:
//
// t := true
// pt := &t
//
// vs
//
// pt := pointer(true)
func pointer[T any](v T) *T {
return &v
}
Expand Down Expand Up @@ -335,79 +343,79 @@ func TestNewCommandWithEnvironmentConfigPrivateFields(t *testing.T) {
}{
{
desc: "using the disable traces envvar",
envName: "CLOUD_SQL_DISABLE_TRACES",
envName: "CSQL_PROXY_DISABLE_TRACES",
envValue: "true",
isValid: func(cmd *Command) bool {
return cmd.disableTraces == true
},
},
{
desc: "using the telemetry sample rate envvar",
envName: "CLOUD_SQL_TELEMETRY_SAMPLE_RATE",
envName: "CSQL_PROXY_TELEMETRY_SAMPLE_RATE",
envValue: "500",
isValid: func(cmd *Command) bool {
return cmd.telemetryTracingSampleRate == 500
},
},
{
desc: "using the disable metrics envvar",
envName: "CLOUD_SQL_DISABLE_METRICS",
envName: "CSQL_PROXY_DISABLE_METRICS",
envValue: "true",
isValid: func(cmd *Command) bool {
return cmd.disableMetrics == true
},
},
{
desc: "using the telemetry project envvar",
envName: "CLOUD_SQL_TELEMETRY_PROJECT",
envName: "CSQL_PROXY_TELEMETRY_PROJECT",
envValue: "mycoolproject",
isValid: func(cmd *Command) bool {
return cmd.telemetryProject == "mycoolproject"
},
},
{
desc: "using the telemetry prefix envvar",
envName: "CLOUD_SQL_TELEMETRY_PREFIX",
envName: "CSQL_PROXY_TELEMETRY_PREFIX",
envValue: "myprefix",
isValid: func(cmd *Command) bool {
return cmd.telemetryPrefix == "myprefix"
},
},
{
desc: "using the prometheus envvar",
envName: "CLOUD_SQL_PROMETHEUS",
envName: "CSQL_PROXY_PROMETHEUS",
envValue: "true",
isValid: func(cmd *Command) bool {
return cmd.prometheus == true
},
},
{
desc: "using the prometheus namespace envvar",
envName: "CLOUD_SQL_PROMETHEUS_NAMESPACE",
envName: "CSQL_PROXY_PROMETHEUS_NAMESPACE",
envValue: "myns",
isValid: func(cmd *Command) bool {
return cmd.prometheusNamespace == "myns"
},
},
{
desc: "using the health check envvar",
envName: "CLOUD_SQL_HEALTH_CHECK",
envName: "CSQL_PROXY_HEALTH_CHECK",
envValue: "true",
isValid: func(cmd *Command) bool {
return cmd.healthCheck == true
},
},
{
desc: "using the http address envvar",
envName: "CLOUD_SQL_HTTP_ADDRESS",
envName: "CSQL_PROXY_HTTP_ADDRESS",
envValue: "0.0.0.0",
isValid: func(cmd *Command) bool {
return cmd.httpAddress == "0.0.0.0"
},
},
{
desc: "using the http port envvar",
envName: "CLOUD_SQL_HTTP_PORT",
envName: "CSQL_PROXY_HTTP_PORT",
envValue: "5555",
isValid: func(cmd *Command) bool {
return cmd.httpPort == "5555"
Expand Down Expand Up @@ -441,7 +449,7 @@ func TestNewCommandWithEnvironmentConfigInstanceConnectionName(t *testing.T) {
{
desc: "with one instance connection name",
env: map[string]string{
"CLOUD_SQL_INSTANCE_CONNECTION_NAME": "proj:reg:inst",
"CSQL_PROXY_INSTANCE_CONNECTION_NAME": "proj:reg:inst",
},
want: withDefaults(&proxy.Config{Instances: []proxy.InstanceConnConfig{
{Name: "proj:reg:inst"},
Expand All @@ -450,8 +458,8 @@ func TestNewCommandWithEnvironmentConfigInstanceConnectionName(t *testing.T) {
{
desc: "with multiple instance connection names",
env: map[string]string{
"CLOUD_SQL_INSTANCE_CONNECTION_NAME_0": "proj:reg:inst0",
"CLOUD_SQL_INSTANCE_CONNECTION_NAME_1": "proj:reg:inst1",
"CSQL_PROXY_INSTANCE_CONNECTION_NAME_0": "proj:reg:inst0",
"CSQL_PROXY_INSTANCE_CONNECTION_NAME_1": "proj:reg:inst1",
},
want: withDefaults(&proxy.Config{Instances: []proxy.InstanceConnConfig{
{Name: "proj:reg:inst0"},
Expand All @@ -462,7 +470,7 @@ func TestNewCommandWithEnvironmentConfigInstanceConnectionName(t *testing.T) {
desc: "with query params",

env: map[string]string{
"CLOUD_SQL_INSTANCE_CONNECTION_NAME_0": "proj:reg:inst0?auto-iam-authn=true",
"CSQL_PROXY_INSTANCE_CONNECTION_NAME_0": "proj:reg:inst0?auto-iam-authn=true",
},
want: withDefaults(&proxy.Config{Instances: []proxy.InstanceConnConfig{
{Name: "proj:reg:inst0", IAMAuthN: pointer(true)},
Expand All @@ -471,8 +479,8 @@ func TestNewCommandWithEnvironmentConfigInstanceConnectionName(t *testing.T) {
{
desc: "when the index skips a number",
env: map[string]string{
"CLOUD_SQL_INSTANCE_CONNECTION_NAME_0": "proj:reg:inst0",
"CLOUD_SQL_INSTANCE_CONNECTION_NAME_2": "proj:reg:inst1",
"CSQL_PROXY_INSTANCE_CONNECTION_NAME_0": "proj:reg:inst0",
"CSQL_PROXY_INSTANCE_CONNECTION_NAME_2": "proj:reg:inst1",
},
want: withDefaults(&proxy.Config{Instances: []proxy.InstanceConnConfig{
{Name: "proj:reg:inst0"},
Expand All @@ -481,7 +489,7 @@ func TestNewCommandWithEnvironmentConfigInstanceConnectionName(t *testing.T) {
{
desc: "when there are CLI args provided",
env: map[string]string{
"CLOUD_SQL_INSTANCE_CONNECTION_NAME": "proj:reg:inst0",
"CSQL_PROXY_INSTANCE_CONNECTION_NAME": "proj:reg:inst0",
},
args: []string{"myotherproj:myreg:myinst"},
want: withDefaults(&proxy.Config{Instances: []proxy.InstanceConnConfig{
Expand All @@ -491,7 +499,7 @@ func TestNewCommandWithEnvironmentConfigInstanceConnectionName(t *testing.T) {
{
desc: "when only an index instance connection name is defined",
env: map[string]string{
"CLOUD_SQL_INSTANCE_CONNECTION_NAME_0": "proj:reg:inst0",
"CSQL_PROXY_INSTANCE_CONNECTION_NAME_0": "proj:reg:inst0",
},
want: withDefaults(&proxy.Config{Instances: []proxy.InstanceConnConfig{
{Name: "proj:reg:inst0"},
Expand Down Expand Up @@ -532,119 +540,119 @@ func TestNewCommandWithEnvironmentConfig(t *testing.T) {
}{
{
desc: "using the address envvar",
envName: "CLOUD_SQL_ADDRESS",
envName: "CSQL_PROXY_ADDRESS",
envValue: "0.0.0.0",
want: withDefaults(&proxy.Config{
Addr: "0.0.0.0",
}),
},
{
desc: "using the port envvar",
envName: "CLOUD_SQL_PORT",
envName: "CSQL_PROXY_PORT",
envValue: "6000",
want: withDefaults(&proxy.Config{
Port: 6000,
}),
},
{
desc: "using the token envvar",
envName: "CLOUD_SQL_TOKEN",
envName: "CSQL_PROXY_TOKEN",
envValue: "MYCOOLTOKEN",
want: withDefaults(&proxy.Config{
Token: "MYCOOLTOKEN",
}),
},
{
desc: "using the credentiale file envvar",
envName: "CLOUD_SQL_CREDENTIALS_FILE",
envName: "CSQL_PROXY_CREDENTIALS_FILE",
envValue: "/path/to/file",
want: withDefaults(&proxy.Config{
CredentialsFile: "/path/to/file",
}),
},
{
desc: "using the JSON credentials",
envName: "CLOUD_SQL_JSON_CREDENTIALS",
envName: "CSQL_PROXY_JSON_CREDENTIALS",
envValue: `{"json":"goes-here"}`,
want: withDefaults(&proxy.Config{
CredentialsJSON: `{"json":"goes-here"}`,
}),
},
{
desc: "using the gcloud auth envvar",
envName: "CLOUD_SQL_GCLOUD_AUTH",
envName: "CSQL_PROXY_GCLOUD_AUTH",
envValue: "true",
want: withDefaults(&proxy.Config{
GcloudAuth: true,
}),
},
{
desc: "using the api-endpoint envvar",
envName: "CLOUD_SQL_SQLADMIN_API_ENDPOINT",
envName: "CSQL_PROXY_SQLADMIN_API_ENDPOINT",
envValue: "https://test.googleapis.com/",
want: withDefaults(&proxy.Config{
APIEndpointURL: "https://test.googleapis.com/",
}),
},
{
desc: "using the unix socket envvar",
envName: "CLOUD_SQL_UNIX_SOCKET",
envName: "CSQL_PROXY_UNIX_SOCKET",
envValue: "/path/to/dir/",
want: withDefaults(&proxy.Config{
UnixSocket: "/path/to/dir/",
}),
},
{
desc: "using the iam authn login envvar",
envName: "CLOUD_SQL_AUTO_IAM_AUTHN",
envName: "CSQL_PROXY_AUTO_IAM_AUTHN",
envValue: "true",
want: withDefaults(&proxy.Config{
IAMAuthN: true,
}),
},
{
desc: "enabling structured logging",
envName: "CLOUD_SQL_STRUCTURED_LOGS",
envName: "CSQL_PROXY_STRUCTURED_LOGS",
envValue: "true",
want: withDefaults(&proxy.Config{
StructuredLogs: true,
}),
},
{
desc: "using the max connections envvar",
envName: "CLOUD_SQL_MAX_CONNECTIONS",
envName: "CSQL_PROXY_MAX_CONNECTIONS",
envValue: "1",
want: withDefaults(&proxy.Config{
MaxConnections: 1,
}),
},
{
desc: "using wait after signterm envvar",
envName: "CLOUD_SQL_MAX_SIGTERM_DELAY",
envName: "CSQL_PROXY_MAX_SIGTERM_DELAY",
envValue: "10s",
want: withDefaults(&proxy.Config{
WaitOnClose: 10 * time.Second,
}),
},
{
desc: "using the private-ip envvar",
envName: "CLOUD_SQL_PRIVATE_IP",
envName: "CSQL_PROXY_PRIVATE_IP",
envValue: "true",
want: withDefaults(&proxy.Config{
PrivateIP: true,
}),
},
{
desc: "using the quota project envvar",
envName: "CLOUD_SQL_QUOTA_PROJECT",
envName: "CSQL_PROXY_QUOTA_PROJECT",
envValue: "proj",
want: withDefaults(&proxy.Config{
QuotaProject: "proj",
}),
},
{
desc: "using the imopersonate service accounn envvar",
envName: "CLOUD_SQL_IMPERSONATE_SERVICE_ACCOUNT",
envName: "CSQL_PROXY_IMPERSONATE_SERVICE_ACCOUNT",
envValue: "sv1@developer.gserviceaccount.com,sv2@developer.gserviceaccount.com,sv3@developer.gserviceaccount.com",
want: withDefaults(&proxy.Config{
ImpersonateTarget: "sv1@developer.gserviceaccount.com",
Expand Down

0 comments on commit 7054b95

Please sign in to comment.