Skip to content

Commit 115bf1c

Browse files
committed
Merge branch 'linter-staticcheck-deprecations' into 'master'
chore: fix deprecation linter warnings See merge request postgres-ai/database-lab!1067
2 parents 2ce4eb1 + 0e9c71d commit 115bf1c

File tree

9 files changed

+95
-37
lines changed

9 files changed

+95
-37
lines changed

engine/.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ linters-settings:
6464
disabled-tags:
6565
- experimental
6666
staticcheck:
67-
checks: [ "all", "-SA1019" ] # TODO: fix deprecation warnings for old Docker API types in a separate MR
67+
checks: [ "all" ]
6868

6969
linters:
7070
enable:

engine/internal/provision/docker/docker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"strconv"
1616
"strings"
1717

18-
"github.com/docker/docker/api/types"
18+
"github.com/docker/docker/api/types/container"
1919
"github.com/docker/docker/api/types/filters"
2020
imagetypes "github.com/docker/docker/api/types/image"
2121
"github.com/docker/docker/client"
@@ -131,7 +131,7 @@ func createDefaultVolumes(c *resources.AppConfig) (string, []string) {
131131
func getMountVolumes(r runners.Runner, c *resources.AppConfig, containerID string) ([]string, error) {
132132
inspectCmd := "docker inspect -f '{{ json .Mounts }}' " + containerID
133133

134-
var mountPoints []types.MountPoint
134+
var mountPoints []container.MountPoint
135135

136136
out, err := r.Run(inspectCmd, true)
137137
if err != nil {
@@ -145,7 +145,7 @@ func getMountVolumes(r runners.Runner, c *resources.AppConfig, containerID strin
145145
return buildVolumesFromMountPoints(c, mountPoints), nil
146146
}
147147

148-
func buildVolumesFromMountPoints(c *resources.AppConfig, mountPoints []types.MountPoint) []string {
148+
func buildVolumesFromMountPoints(c *resources.AppConfig, mountPoints []container.MountPoint) []string {
149149
unixSocketCloneDir := c.Pool.SocketCloneDir(c.CloneName)
150150
mounts := tools.GetMountsFromMountPoints(c.CloneDir(), mountPoints)
151151
volumes := make([]string, 0, len(mounts))

engine/internal/provision/docker/docker_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package docker
77
import (
88
"testing"
99

10-
"github.com/docker/docker/api/types"
10+
"github.com/docker/docker/api/types/container"
1111
"github.com/stretchr/testify/assert"
1212

1313
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/resources"
@@ -34,7 +34,7 @@ func TestSystemVolumes(t *testing.T) {
3434
func TestVolumesBuilding(t *testing.T) {
3535
testCases := []struct {
3636
appConfig *resources.AppConfig
37-
mountPoints []types.MountPoint
37+
mountPoints []container.MountPoint
3838
expectedVolumes []string
3939
}{
4040
{
@@ -50,7 +50,7 @@ func TestVolumesBuilding(t *testing.T) {
5050
SocketSubDir: "sockets",
5151
},
5252
},
53-
mountPoints: []types.MountPoint{
53+
mountPoints: []container.MountPoint{
5454
{Type: "bind", Source: "/lib/modules", Destination: "/lib/modules"},
5555
{Type: "bind", Source: "/proc", Destination: "/host_proc"},
5656
{Type: "bind", Source: "/tmp", Destination: "/tmp"},

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

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
package logical
77

88
import (
9+
"archive/tar"
910
"bufio"
1011
"bytes"
1112
"context"
1213
"fmt"
14+
"io"
1315
"io/fs"
1416
"os"
1517
"path"
@@ -22,7 +24,6 @@ import (
2224
"github.com/docker/docker/api/types/container"
2325
"github.com/docker/docker/api/types/mount"
2426
"github.com/docker/docker/client"
25-
"github.com/docker/docker/pkg/archive"
2627
"github.com/pkg/errors"
2728

2829
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/resources"
@@ -655,35 +656,58 @@ func (r *RestoreJob) prepareDB(ctx context.Context, contID, dbName string) error
655656
}
656657

657658
func (r *RestoreJob) prepareArchive(ctx context.Context, contID string, tempFile *os.File, dstPath string) error {
658-
srcInfo, err := archive.CopyInfoSourcePath(tempFile.Name(), false)
659+
archiveReader, err := createTarArchive(tempFile, dstPath)
659660
if err != nil {
660661
return err
661662
}
662663

663-
srcArchive, err := archive.TarResource(srcInfo)
664-
if err != nil {
665-
return err
664+
dstDir := filepath.Dir(dstPath)
665+
if err := r.dockerClient.CopyToContainer(ctx, contID, dstDir, archiveReader, container.CopyToContainerOptions{
666+
AllowOverwriteDirWithFile: true,
667+
CopyUIDGID: true,
668+
}); err != nil {
669+
log.Err(err)
670+
671+
return errors.Wrap(err, "failed to copy auxiliary file")
666672
}
667673

668-
defer func() { _ = srcArchive.Close() }()
674+
return nil
675+
}
676+
677+
// createTarArchive creates a tar archive from a file for the given destination path.
678+
func createTarArchive(tempFile *os.File, dstPath string) (io.Reader, error) {
679+
var buf bytes.Buffer
680+
tarWriter := tar.NewWriter(&buf)
669681

670-
dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, archive.CopyInfo{Path: dstPath})
682+
fileInfo, err := tempFile.Stat()
671683
if err != nil {
672-
return err
684+
return nil, errors.Wrap(err, "failed to stat file")
673685
}
674686

675-
defer func() { _ = preparedArchive.Close() }()
687+
header := &tar.Header{
688+
Name: filepath.Base(dstPath),
689+
Mode: int64(fileInfo.Mode()),
690+
Size: fileInfo.Size(),
691+
ModTime: fileInfo.ModTime(),
692+
}
676693

677-
if err := r.dockerClient.CopyToContainer(ctx, contID, dstDir, preparedArchive, container.CopyToContainerOptions{
678-
AllowOverwriteDirWithFile: true,
679-
CopyUIDGID: true,
680-
}); err != nil {
681-
log.Err(err)
694+
if err := tarWriter.WriteHeader(header); err != nil {
695+
return nil, errors.Wrap(err, "failed to write tar header")
696+
}
682697

683-
return errors.Wrap(err, "failed to copy auxiliary file")
698+
if _, err := tempFile.Seek(0, 0); err != nil {
699+
return nil, errors.Wrap(err, "failed to seek file")
684700
}
685701

686-
return nil
702+
if _, err := io.Copy(tarWriter, tempFile); err != nil {
703+
return nil, errors.Wrap(err, "failed to copy file to tar")
704+
}
705+
706+
if err := tarWriter.Close(); err != nil {
707+
return nil, errors.Wrap(err, "failed to close tar writer")
708+
}
709+
710+
return &buf, nil
687711
}
688712

689713
// formatDBName extracts a database name from a file name and adjusts it.

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package logical
66

77
import (
8+
"archive/tar"
89
"context"
10+
"io"
911
"os"
1012
"path"
1113
"testing"
@@ -446,5 +448,38 @@ func TestDBNameFormatter(t *testing.T) {
446448
formattedDB := formatDBName(tc.filename)
447449
assert.Equal(t, tc.dbname, formattedDB)
448450
}
451+
}
449452

453+
func TestCreateTarArchive(t *testing.T) {
454+
// create a temporary file with test content
455+
tempFile, err := os.CreateTemp("", "test-archive-*.sql")
456+
require.NoError(t, err)
457+
defer os.Remove(tempFile.Name())
458+
459+
testContent := []byte("SELECT 1; -- test database dump content\n")
460+
_, err = tempFile.Write(testContent)
461+
require.NoError(t, err)
462+
463+
// call the actual function being tested
464+
dstPath := "/tmp/testdb.sql"
465+
archiveReader, err := createTarArchive(tempFile, dstPath)
466+
require.NoError(t, err)
467+
require.NotNil(t, archiveReader)
468+
469+
// verify the tar archive is valid and contains expected file
470+
tarReader := tar.NewReader(archiveReader)
471+
tarHeader, err := tarReader.Next()
472+
require.NoError(t, err)
473+
474+
assert.Equal(t, "testdb.sql", tarHeader.Name)
475+
assert.Equal(t, int64(len(testContent)), tarHeader.Size)
476+
477+
// read and verify content
478+
content, err := io.ReadAll(tarReader)
479+
require.NoError(t, err)
480+
assert.Equal(t, testContent, content)
481+
482+
// verify no more files in the tar
483+
_, err = tarReader.Next()
484+
assert.Equal(t, io.EOF, err)
450485
}

engine/internal/retrieval/engine/postgres/physical/physical.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
"strings"
1616
"time"
1717

18+
"github.com/containerd/errdefs"
1819
"github.com/docker/docker/api/types/container"
1920
"github.com/docker/docker/api/types/filters"
2021
"github.com/docker/docker/api/types/mount"
2122
"github.com/docker/docker/client"
22-
2323
"github.com/pkg/errors"
2424

2525
"gitlab.com/postgres-ai/database-lab/v3/internal/diagnostic"
@@ -319,7 +319,7 @@ func (r *RestoreJob) syncInstanceName() string {
319319

320320
func (r *RestoreJob) runSyncInstance(ctx context.Context) (err error) {
321321
syncContainer, err := r.dockerClient.ContainerInspect(ctx, r.syncInstanceName())
322-
if err != nil && !client.IsErrNotFound(err) {
322+
if err != nil && !errdefs.IsNotFound(err) {
323323
return errors.Wrap(err, "failed to inspect sync container")
324324
}
325325

engine/internal/retrieval/engine/postgres/tools/cont/container.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strings"
1111
"time"
1212

13-
"github.com/docker/docker/api/types"
1413
"github.com/docker/docker/api/types/container"
1514
"github.com/docker/docker/api/types/filters"
1615
"github.com/docker/docker/client"
@@ -152,7 +151,7 @@ func cleanUpContainers(ctx context.Context, dockerCli *client.Client, instanceID
152151
return nil
153152
}
154153

155-
func getContainerList(ctx context.Context, d *client.Client, instanceID string, pairs []filters.KeyValuePair) ([]types.Container, error) {
154+
func getContainerList(ctx context.Context, d *client.Client, instanceID string, pairs []filters.KeyValuePair) ([]container.Summary, error) {
156155
filterPairs := append([]filters.KeyValuePair{
157156
{
158157
Key: labelFilter,
@@ -183,7 +182,7 @@ func shouldStopInternalProcess(controlLabel string) bool {
183182
return controlLabel == DBLabSyncLabel
184183
}
185184

186-
func getContainerName(controlCont types.Container) string {
185+
func getContainerName(controlCont container.Summary) string {
187186
return strings.Join(controlCont.Names, ", ")
188187
}
189188

engine/internal/retrieval/engine/postgres/tools/tools.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func AddVolumesToHostConfig(ctx context.Context, docker *client.Client, hostConf
182182
}
183183

184184
// GetMountsFromMountPoints creates a list of mounts.
185-
func GetMountsFromMountPoints(dataDir string, mountPoints []types.MountPoint) []mount.Mount {
185+
func GetMountsFromMountPoints(dataDir string, mountPoints []container.MountPoint) []mount.Mount {
186186
mounts := make([]mount.Mount, 0, len(mountPoints))
187187
seen := make(map[string]struct{})
188188

@@ -520,7 +520,7 @@ func RemoveContainer(ctx context.Context, dockerClient *client.Client, container
520520

521521
// PullImage pulls a Docker image.
522522
func PullImage(ctx context.Context, dockerClient *client.Client, image string) error {
523-
inspectionResult, _, err := dockerClient.ImageInspectWithRaw(ctx, image)
523+
inspectionResult, err := dockerClient.ImageInspect(ctx, image)
524524
if err != nil {
525525
if _, ok := err.(errdefs.ErrNotFound); !ok {
526526
return errors.Wrapf(err, "failed to inspect image %q", image)

engine/internal/retrieval/engine/postgres/tools/tools_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"os"
99
"testing"
1010

11-
"github.com/docker/docker/api/types"
11+
"github.com/docker/docker/api/types/container"
1212
"github.com/docker/docker/api/types/mount"
1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/require"
@@ -39,13 +39,13 @@ func TestGetMountsFromMountPoints(t *testing.T) {
3939
testCases := []struct {
4040
name string
4141
dataDir string
42-
mountPoints []types.MountPoint
42+
mountPoints []container.MountPoint
4343
expectedPoints []mount.Mount
4444
}{
4545
{
4646
name: "simple mount without transformation",
4747
dataDir: "/var/lib/dblab/clones/dblab_clone_6000/data",
48-
mountPoints: []types.MountPoint{{
48+
mountPoints: []container.MountPoint{{
4949
Type: mount.TypeBind,
5050
Source: "/var/lib/pgsql/data",
5151
Destination: "/var/lib/postgresql/data",
@@ -63,7 +63,7 @@ func TestGetMountsFromMountPoints(t *testing.T) {
6363
{
6464
name: "mount with path transformation",
6565
dataDir: "/var/lib/dblab/clones/dblab_clone_6000/data",
66-
mountPoints: []types.MountPoint{{
66+
mountPoints: []container.MountPoint{{
6767
Type: mount.TypeBind,
6868
Source: "/var/lib/postgresql",
6969
Destination: "/var/lib/dblab",
@@ -81,7 +81,7 @@ func TestGetMountsFromMountPoints(t *testing.T) {
8181
{
8282
name: "deduplicate identical mounts",
8383
dataDir: "/var/lib/dblab/data",
84-
mountPoints: []types.MountPoint{
84+
mountPoints: []container.MountPoint{
8585
{Type: mount.TypeBind, Source: "/host/dump", Destination: "/var/lib/dblab/dump"},
8686
{Type: mount.TypeBind, Source: "/host/dump", Destination: "/var/lib/dblab/dump"},
8787
},
@@ -98,7 +98,7 @@ func TestGetMountsFromMountPoints(t *testing.T) {
9898
{
9999
name: "deduplicate mounts with trailing slashes",
100100
dataDir: "/var/lib/dblab/data",
101-
mountPoints: []types.MountPoint{
101+
mountPoints: []container.MountPoint{
102102
{Type: mount.TypeBind, Source: "/host/dump/", Destination: "/var/lib/dblab/dump"},
103103
{Type: mount.TypeBind, Source: "/host/dump", Destination: "/var/lib/dblab/dump/"},
104104
},
@@ -115,7 +115,7 @@ func TestGetMountsFromMountPoints(t *testing.T) {
115115
{
116116
name: "volume mount uses name instead of path",
117117
dataDir: "/var/lib/dblab/data",
118-
mountPoints: []types.MountPoint{{
118+
mountPoints: []container.MountPoint{{
119119
Type: mount.TypeVolume,
120120
Name: "3749a7e336f27d8c1ce2a81c7b945954f7522ecc3a4be4a3855bf64473f63a89",
121121
Source: "/var/lib/docker/volumes/3749a7e336f27d8c1ce2a81c7b945954f7522ecc3a4be4a3855bf64473f63a89/_data",

0 commit comments

Comments
 (0)