Skip to content

Commit

Permalink
Removed env vars for instance connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
retpolanne authored and drgarcia1986 committed Oct 29, 2018
1 parent cb1e6c5 commit 22dd788
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 123 deletions.
15 changes: 10 additions & 5 deletions FAQ.md
Expand Up @@ -420,12 +420,17 @@ applications:
credentialFile: credentials.json
```

You can either provide the instance connection name or provide the following variables to your application. You can provide many connection names by passing a comma-separated list of instances.
You can provide many connection names by passing a comma-separated list of instances.

You can either provide the instance connection name or provide the format of an environment variable.

```yaml
cloudsql-proxy:
instances: $(GCP_CLOUDSQL_INSTANCE_NAME)
```

And create the environment variable.

$ teresa app env-set DB_PROJECT=<gcp-project> --app <app-name>
$ teresa app env-set DB_ZONE=<gcp-zone> --app <app-name>
$ teresa app env-set DB_NAME=<db-name> --app <app-name>
# You can also set the whole instance string, which will override the other variables
$ teresa app env-set GCP_CLOUDSQL_INSTANCE_NAME=<instance-name> --app <app-name>

After deploying the app the instance will be available on `localhost` using port
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/deploy/deploy.go
Expand Up @@ -140,7 +140,7 @@ func (ops *DeployOperations) runReleaseCmd(a *app.App, deployId, slugURL string,
}

func (ops *DeployOperations) createOrUpdateDeploy(a *app.App, confFiles *DeployConfigFiles, w io.Writer, slugURL, description, deployId string) error {
csp, err := spec.NewCloudSQLProxy(ops.opts.CloudSQLProxyImage, confFiles.TeresaYaml, a)
csp, err := spec.NewCloudSQLProxy(ops.opts.CloudSQLProxyImage, confFiles.TeresaYaml)
if err != nil {
return errors.Wrap(err, "failed to create the deploy")
}
Expand Down
17 changes: 1 addition & 16 deletions pkg/server/spec/cloudsqlproxy.go
Expand Up @@ -19,7 +19,7 @@ type CloudSQLProxy struct {
Image string `yaml:"-"`
}

func NewCloudSQLProxy(img string, t *TeresaYaml, a *app.App) (*CloudSQLProxy, error) {
func NewCloudSQLProxy(img string, t *TeresaYaml) (*CloudSQLProxy, error) {
if t == nil {
return nil, nil
}
Expand All @@ -32,7 +32,6 @@ func NewCloudSQLProxy(img string, t *TeresaYaml, a *app.App) (*CloudSQLProxy, er
return nil, errors.Wrap(err, "failed to build cloudsql proxy")
}
csp.Image = img
csp.Instances = newCloudSQLProxyContainerInstanceString(csp, a)
return csp, nil
}

Expand Down Expand Up @@ -62,17 +61,3 @@ func newCloudSQLProxyContainerArgs(csp *CloudSQLProxy) []string {
func newCloudSQLProxyContainerMountPath(csp *CloudSQLProxy) string {
return path.Join("/secrets/cloudsql", path.Base(csp.CredentialFile))
}

func newCloudSQLProxyContainerInstanceString(csp *CloudSQLProxy, a *app.App) string {
if csp.Instances == "" {
for _, envVar := range a.EnvVars {
switch envVar.Key {
case "GCP_CLOUDSQL_INSTANCE_NAME":
csp.Instances = envVar.Value
return csp.Instances
}
}
csp.Instances = fmt.Sprintf("$(DB_PROJECT):$(DB_ZONE):$(DB_NAME)=tcp:3306")
}
return csp.Instances
}
104 changes: 3 additions & 101 deletions pkg/server/spec/cloudsqlproxy_test.go
Expand Up @@ -26,57 +26,7 @@ func TestNewCloudSQLProxy(t *testing.T) {
},
}

csp, err := NewCloudSQLProxy(img, ty, &app.App{})
if err != nil {
t.Fatal("got unexpected error:", err)
}
if !reflect.DeepEqual(csp, want) {
t.Errorf("got %v; want %v", csp, want)
}
}

func TestNewCloudSQLProxyFromEnvVar(t *testing.T) {
a := &app.App{
EnvVars: []*app.EnvVar{
&app.EnvVar{Key: "DB_PROJECT", Value: "project"},
&app.EnvVar{Key: "DB_ZONE", Value: "zone"},
&app.EnvVar{Key: "DB_NAME", Value: "name"},
},
}
img := "image"

want := &CloudSQLProxy{
Instances: "$(DB_PROJECT):$(DB_ZONE):$(DB_NAME)=tcp:3306",
CredentialFile: "file",
Image: img,
}
fn := func(v interface{}) error {
v.(*CloudSQLProxy).CredentialFile = want.CredentialFile
return nil
}
ty := &TeresaYaml{
SideCars: map[string]RawData{
"cloudsql-proxy": {fn},
},
}

csp, err := NewCloudSQLProxy(img, ty, a)
if err != nil {
t.Fatal("got unexpected error:", err)
}
if !reflect.DeepEqual(csp, want) {
t.Errorf("got %v; want %v", csp, want)
}
a = &app.App{
EnvVars: []*app.EnvVar{
&app.EnvVar{Key: "GCP_CLOUDSQL_INSTANCE_NAME", Value: "project:zone:name=tcp:3306"},
&app.EnvVar{Key: "DB_PROJECT", Value: "a_project"},
&app.EnvVar{Key: "DB_ZONE", Value: "a_zone"},
&app.EnvVar{Key: "DB_NAME", Value: "a_name"},
},
}
want.Instances = "project:zone:name=tcp:3306"
csp, err = NewCloudSQLProxy(img, ty, a)
csp, err := NewCloudSQLProxy(img, ty)
if err != nil {
t.Fatal("got unexpected error:", err)
}
Expand All @@ -95,7 +45,7 @@ func TestNewCloudSQLProxyError(t *testing.T) {
},
}

if _, err := NewCloudSQLProxy("test", ty, &app.App{}); err == nil {
if _, err := NewCloudSQLProxy("test", ty); err == nil {
t.Error("got nil; want error")
}
}
Expand All @@ -107,7 +57,7 @@ func TestNewCloudSQLProxyNil(t *testing.T) {
},
}

csp, err := NewCloudSQLProxy("test", ty, &app.App{})
csp, err := NewCloudSQLProxy("test", ty)
if err != nil {
t.Fatal("got unexpected error:", err)
}
Expand All @@ -116,54 +66,6 @@ func TestNewCloudSQLProxyNil(t *testing.T) {
}
}

func TestNewCloudSQLProxyContainerFromEnvVar(t *testing.T) {
a := &app.App{
EnvVars: []*app.EnvVar{
&app.EnvVar{Key: "DB_PROJECT", Value: "project"},
&app.EnvVar{Key: "DB_ZONE", Value: "zone"},
&app.EnvVar{Key: "DB_NAME", Value: "name"},
},
}
csp := &CloudSQLProxy{
Instances: "$(DB_PROJECT):$(DB_ZONE):$(DB_NAME)=tcp:3306",
CredentialFile: "file",
Image: "image",
}
want := &Container{
Name: "cloudsql-proxy",
ContainerLimits: &ContainerLimits{
CPU: cloudSQLProxyDefaultCPULimit,
Memory: cloudSQLProxyDefaultMemoryLimit,
},
Image: csp.Image,
Command: []string{"/cloud_sql_proxy"},
Args: []string{
"-instances=$(DB_PROJECT):$(DB_ZONE):$(DB_NAME)=tcp:3306",
"-credential_file=/secrets/cloudsql/file",
},
VolumeMounts: []*VolumeMounts{
{
Name: AppSecretName,
MountPath: "/secrets/cloudsql/file",
SubPath: "file",
ReadOnly: true,
},
},
Env: map[string]string{
"DB_PROJECT": "project",
"DB_ZONE": "zone",
"DB_NAME": "name",
},
Ports: []Port{},
Secrets: []string{},
}

cn := NewCloudSQLProxyContainer(csp, a)
if !reflect.DeepEqual(cn, want) {
t.Errorf("got %v; want %v", cn, want)
}
}

func TestNewCloudSQLProxyContainer(t *testing.T) {
a := &app.App{
EnvVars: []*app.EnvVar{&app.EnvVar{Key: "key1", Value: "value1"}},
Expand Down

0 comments on commit 22dd788

Please sign in to comment.