Skip to content

Commit 068bf9b

Browse files
committed
fix: use only internal containers paths (#156)
1 parent e8312ae commit 068bf9b

File tree

8 files changed

+22
-78
lines changed

8 files changed

+22
-78
lines changed

pkg/retrieval/engine/postgres/logical/dump.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"fmt"
1010
"io"
1111
"os"
12-
"path/filepath"
1312
"strconv"
1413
"strings"
1514
"time"
@@ -363,7 +362,7 @@ func (d *DumpJob) buildContainerConfig(password string) *container.Config {
363362

364363
func (d *DumpJob) buildHostConfig(ctx context.Context) (*container.HostConfig, error) {
365364
hostConfig := &container.HostConfig{
366-
Mounts: d.getMountVolumes(),
365+
Mounts: d.dumper.GetMounts(),
367366
NetworkMode: d.getContainerNetworkMode(),
368367
}
369368

@@ -374,21 +373,6 @@ func (d *DumpJob) buildHostConfig(ctx context.Context) (*container.HostConfig, e
374373
return hostConfig, nil
375374
}
376375

377-
// getMountVolumes returns a list of mount volumes.
378-
func (d *DumpJob) getMountVolumes() []mount.Mount {
379-
mounts := d.dumper.GetMounts()
380-
381-
if d.DumpOptions.DumpLocation != "" {
382-
mounts = append(mounts, mount.Mount{
383-
Type: mount.TypeBind,
384-
Source: filepath.Dir(d.DumpOptions.DumpLocation),
385-
Target: filepath.Dir(d.DumpOptions.DumpLocation),
386-
})
387-
}
388-
389-
return mounts
390-
}
391-
392376
func (d *DumpJob) getContainerNetworkMode() container.NetworkMode {
393377
networkMode := networkModeDefault
394378

pkg/retrieval/engine/postgres/logical/dump_rds.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
const (
26-
sslRootCert = "/tmp/cert/ssl-combined-ca-bundle.pem"
26+
sslRootCert = "/cert/ssl-combined-ca-bundle.pem"
2727

2828
// AWS service names.
2929
serviceIAM = "iam"
@@ -98,6 +98,7 @@ func (r *rdsDumper) GetMounts() []mount.Mount {
9898
mounts := []mount.Mount{}
9999

100100
if r.rdsCfg != nil && r.rdsCfg.SSLRootCert != "" {
101+
// TODO (akartasov): Remove mounting after the image contains a built-in certificate.
101102
mounts = append(mounts, mount.Mount{
102103
Type: mount.TypeBind,
103104
Source: r.rdsCfg.SSLRootCert,

pkg/retrieval/engine/postgres/logical/restore.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/docker/docker/api/types"
1616
"github.com/docker/docker/api/types/container"
17-
"github.com/docker/docker/api/types/mount"
1817
"github.com/docker/docker/api/types/network"
1918
"github.com/docker/docker/client"
2019
"github.com/pkg/errors"
@@ -51,7 +50,7 @@ type RestoreJob struct {
5150

5251
// RestoreOptions defines a logical restore options.
5352
type RestoreOptions struct {
54-
DumpFile string `yaml:"dumpLocation"`
53+
DumpLocation string `yaml:"dumpLocation"`
5554
DockerImage string `yaml:"dockerImage"`
5655
DBName string `yaml:"dbname"`
5756
ForceInit bool `yaml:"forceInit"`
@@ -214,15 +213,7 @@ func (r *RestoreJob) buildContainerConfig(password string) *container.Config {
214213
}
215214

216215
func (r *RestoreJob) buildHostConfig(ctx context.Context) (*container.HostConfig, error) {
217-
hostConfig := &container.HostConfig{
218-
Mounts: []mount.Mount{
219-
{
220-
Type: mount.TypeBind,
221-
Source: r.RestoreOptions.DumpFile,
222-
Target: r.RestoreOptions.DumpFile,
223-
},
224-
},
225-
}
216+
hostConfig := &container.HostConfig{}
226217

227218
if err := tools.AddVolumesToHostConfig(ctx, r.dockerClient, hostConfig, r.globalCfg.DataDir); err != nil {
228219
return nil, err
@@ -253,7 +244,7 @@ func (r *RestoreJob) markDatabase(ctx context.Context, contID string) error {
253244
}
254245

255246
func (r *RestoreJob) retrieveDataStateAt(ctx context.Context, contID string) (string, error) {
256-
restoreMetaCmd := []string{"sh", "-c", "pg_restore --list " + r.RestoreOptions.DumpFile + " | head -n 10"}
247+
restoreMetaCmd := []string{"sh", "-c", "pg_restore --list " + r.RestoreOptions.DumpLocation + " | head -n 10"}
257248

258249
log.Dbg("Running a restore metadata command: ", restoreMetaCmd)
259250

@@ -295,7 +286,7 @@ func (r *RestoreJob) buildLogicalRestoreCommand() []string {
295286
restoreCmd = append(restoreCmd, "--table", table)
296287
}
297288

298-
restoreCmd = append(restoreCmd, r.RestoreOptions.DumpFile)
289+
restoreCmd = append(restoreCmd, r.RestoreOptions.DumpLocation)
299290

300291
return restoreCmd
301292
}

pkg/retrieval/engine/postgres/logical/restore_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestRestoreCommandBuilding(t *testing.T) {
2222
DBName: "testDB",
2323
ParallelJobs: 1,
2424
ForceInit: false,
25-
DumpFile: "/tmp/db.dump",
25+
DumpLocation: "/tmp/db.dump",
2626
},
2727
Command: []string{"pg_restore", "--username", "postgres", "--dbname", "postgres", "--create", "--no-privileges", "--no-owner", "--jobs", "1", "/tmp/db.dump"},
2828
},
@@ -38,7 +38,7 @@ func TestRestoreCommandBuilding(t *testing.T) {
3838
DBName: "testDB",
3939
ParallelJobs: 1,
4040
Partial: Partial{Tables: []string{"test", "users"}},
41-
DumpFile: "/tmp/db.dump",
41+
DumpLocation: "/tmp/db.dump",
4242
},
4343
Command: []string{"pg_restore", "--username", "postgres", "--dbname", "postgres", "--create", "--no-privileges", "--no-owner", "--jobs", "1", "--table", "test", "--table", "users", "/tmp/db.dump"},
4444
},

pkg/retrieval/engine/postgres/physical/custom.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package physical
77
import (
88
"bytes"
99
"fmt"
10-
11-
"github.com/docker/docker/api/types/mount"
1210
)
1311

1412
const (
@@ -35,11 +33,6 @@ func (c *custom) GetEnvVariables() []string {
3533
return []string{}
3634
}
3735

38-
// GetMounts returns volume configurations for a custom restore command.
39-
func (c *custom) GetMounts() []mount.Mount {
40-
return []mount.Mount{}
41-
}
42-
4336
// GetRestoreCommand returns a custom command to restore data.
4437
func (c *custom) GetRestoreCommand() string {
4538
return c.options.Command

pkg/retrieval/engine/postgres/physical/physical.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818

1919
"github.com/docker/docker/api/types"
2020
"github.com/docker/docker/api/types/container"
21-
"github.com/docker/docker/api/types/mount"
2221
"github.com/docker/docker/api/types/network"
2322
"github.com/docker/docker/client"
2423
"github.com/pkg/errors"
@@ -70,9 +69,6 @@ type restorer interface {
7069
// GetEnvVariables returns restorer environment variables.
7170
GetEnvVariables() []string
7271

73-
// GetMounts returns restorer volume configurations for mounting.
74-
GetMounts() []mount.Mount
75-
7672
// GetRestoreCommand returns a command to restore data.
7773
GetRestoreCommand() string
7874

@@ -400,9 +396,7 @@ func (r *RestoreJob) buildContainerConfig(password string) *container.Config {
400396
}
401397

402398
func (r *RestoreJob) buildHostConfig(ctx context.Context) (*container.HostConfig, error) {
403-
hostConfig := &container.HostConfig{
404-
Mounts: r.restorer.GetMounts(),
405-
}
399+
hostConfig := &container.HostConfig{}
406400

407401
if err := tools.AddVolumesToHostConfig(ctx, r.dockerClient, hostConfig, r.globalCfg.DataDir); err != nil {
408402
return nil, err

pkg/retrieval/engine/postgres/physical/wal_g.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ package physical
77
import (
88
"bytes"
99
"fmt"
10-
11-
"github.com/docker/docker/api/types/mount"
1210
)
1311

1412
const (
15-
walgTool = "walg"
16-
credentialsInternalPath = "/etc/sa/credentials.json"
17-
gcsStorage = "gcs"
13+
walgTool = "walg"
14+
gcsStorage = "gcs"
1815
)
1916

2017
// walg defines a WAL-G as an archival restoration tool.
@@ -41,29 +38,12 @@ func (w *walg) GetEnvVariables() []string {
4138
envVars := []string{}
4239

4340
if w.options.Storage == gcsStorage {
44-
envVars = append(envVars, "GOOGLE_APPLICATION_CREDENTIALS="+credentialsInternalPath)
41+
envVars = append(envVars, "GOOGLE_APPLICATION_CREDENTIALS="+w.options.CredentialsFile)
4542
}
4643

4744
return envVars
4845
}
4946

50-
// GetMounts returns restorer volume configurations for mounting.
51-
func (w *walg) GetMounts() []mount.Mount {
52-
mounts := []mount.Mount{}
53-
54-
if w.options.CredentialsFile != "" {
55-
mounts = append(mounts,
56-
mount.Mount{
57-
Type: mount.TypeBind,
58-
Source: w.options.CredentialsFile,
59-
Target: credentialsInternalPath,
60-
},
61-
)
62-
}
63-
64-
return mounts
65-
}
66-
6747
// GetRestoreCommand returns a command to restore data.
6848
func (w *walg) GetRestoreCommand() string {
6949
return fmt.Sprintf("wal-g backup-fetch %s %s", w.pgDataDir, w.options.BackupName)

pkg/retrieval/engine/postgres/tools/tools.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,23 @@ func AddVolumesToHostConfig(ctx context.Context, dockerClient *client.Client, ho
115115
log.Dbg("Virtualization system: ", hostInfo.VirtualizationSystem)
116116

117117
if hostInfo.VirtualizationRole == "guest" {
118-
insp, err := dockerClient.ContainerInspect(ctx, hostInfo.Hostname)
118+
inspection, err := dockerClient.ContainerInspect(ctx, hostInfo.Hostname)
119119
if err != nil {
120120
return err
121121
}
122122

123-
for _, mountPoint := range insp.Mounts {
124-
if !strings.HasPrefix(dataDir, mountPoint.Destination) {
125-
continue
123+
for _, mountPoint := range inspection.Mounts {
124+
// Rewrite mounting to data directory.
125+
if strings.HasPrefix(dataDir, mountPoint.Destination) {
126+
suffix := strings.TrimPrefix(dataDir, mountPoint.Destination)
127+
mountPoint.Source = path.Join(mountPoint.Source, suffix)
128+
mountPoint.Destination = dataDir
126129
}
127130

128-
suffix := strings.TrimPrefix(dataDir, mountPoint.Destination)
129-
130131
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
131132
Type: mountPoint.Type,
132-
Source: path.Join(mountPoint.Source, suffix),
133-
Target: dataDir,
133+
Source: mountPoint.Source,
134+
Target: mountPoint.Destination,
134135
ReadOnly: !mountPoint.RW,
135136
BindOptions: &mount.BindOptions{
136137
Propagation: mountPoint.Propagation,

0 commit comments

Comments
 (0)