Skip to content

Commit

Permalink
Update docker client dependency (#1877)
Browse files Browse the repository at this point in the history
* chore: update docker client lib

Signed-off-by: Matej Vasek <[email protected]>

* fixup: "host" OCI pusher

Signed-off-by: Matej Vasek <[email protected]>

* fixup: downgrade github.com/containers/image/v5

Signed-off-by: Matej Vasek <[email protected]>

* fixup: platform tests

Signed-off-by: Matej Vasek <[email protected]>

---------

Signed-off-by: Matej Vasek <[email protected]>
  • Loading branch information
matejvasek authored Jul 20, 2023
1 parent 36489f3 commit 5948cf4
Show file tree
Hide file tree
Showing 1,304 changed files with 49,368 additions and 48,417 deletions.
160 changes: 82 additions & 78 deletions go.mod

Large diffs are not rendered by default.

1,412 changes: 183 additions & 1,229 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/docker/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewClient(defaultHost string) (dockerClient client.CommonAPIClient, dockerH
}

if !isSSH {
opts := []client.Opt{client.FromEnv}
opts := []client.Opt{client.FromEnv, client.WithAPIVersionNegotiation()}
if isTCP {
if httpClient := newHttpClient(); httpClient != nil {
opts = append(opts, client.WithHTTPClient(httpClient))
Expand Down
22 changes: 9 additions & 13 deletions pkg/docker/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
gcrTypes "github.com/google/go-containerregistry/pkg/v1/types"

"knative.dev/func/pkg/docker"
)

Expand Down Expand Up @@ -52,16 +50,9 @@ func TestPlatform(t *testing.T) {
t.Fatal(err)
}

zeroHash := v1.Hash{
Algorithm: "sha256",
Hex: "0000000000000000000000000000000000000000000000000000000000000000",
}

var imgIdx = mutate.AppendManifests(empty.Index, mutate.IndexAddendum{
Add: empty.Index,
Add: img,
Descriptor: v1.Descriptor{
MediaType: gcrTypes.DockerManifestList,
Digest: zeroHash,
Platform: &v1.Platform{
Architecture: "ppc64le",
OS: "linux",
Expand Down Expand Up @@ -99,8 +90,13 @@ func TestPlatform(t *testing.T) {
if err != nil {
t.Errorf("unexpeced error: %v", err)
}
if ref != testRegistry+"/default/builder@sha256:0000000000000000000000000000000000000000000000000000000000000000" {
t.Error("incorrect reference")

imgDigest, err := img.Digest()
if err != nil {
t.Fatal(err)
}
if ref != testRegistry+"/default/builder@"+imgDigest.String() {
t.Errorf("incorrect reference: %q", ref)
}
}

Expand All @@ -118,7 +114,7 @@ func startRegistry(t *testing.T) (addr string) {

go func() {
err = s.Serve(l)
if err != nil && !errors.Is(err, net.ErrClosed) {
if err != nil && !errors.Is(err, http.ErrServerClosed) {
fmt.Fprintln(os.Stderr, "ERROR: ", err)
}
}()
Expand Down
3 changes: 2 additions & 1 deletion pkg/docker/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
fn "knative.dev/func/pkg/functions"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/google/go-containerregistry/pkg/authn"
Expand Down Expand Up @@ -157,7 +158,7 @@ func (n *Pusher) daemonPush(ctx context.Context, f fn.Function, credentials Cred
}
defer cli.Close()

authConfig := types.AuthConfig{
authConfig := registry.AuthConfig{
Username: credentials.Username,
Password: credentials.Password,
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/docker/pusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"crypto/x509/pkix"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand All @@ -27,6 +28,7 @@ import (
"time"

"github.com/docker/docker/api/types"
api "github.com/docker/docker/api/types/image"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/registry"
Expand Down Expand Up @@ -316,6 +318,10 @@ func (m *mockPusherDockerClient) ImagePush(ctx context.Context, ref string, opti
return m.imagePush(ctx, ref, options)
}

func (m *mockPusherDockerClient) ImageHistory(context.Context, string) ([]api.HistoryResponseItem, error) {
return nil, errors.New("the ImageHistory() function is not implemented")
}

func (m *mockPusherDockerClient) Close() error {
return m.close()
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/docker/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (n *Runner) Run(ctx context.Context, f fn.Function, startTimeout time.Durat

// Channels for gathering runtime errors from the container instance
copyErrCh = make(chan error, 10)
contBodyCh <-chan container.ContainerWaitOKBody
contBodyCh <-chan container.WaitResponse
contErrCh <-chan error

// Combined runtime error channel for sending all errors to caller
Expand Down Expand Up @@ -116,7 +116,11 @@ func (n *Runner) Run(ctx context.Context, f fn.Function, startTimeout time.Durat
timeout = DefaultStopTimeout
ctx = context.Background()
)
if err = c.ContainerStop(ctx, id, &timeout); err != nil {
timeoutSecs := int(timeout.Seconds())
ctrStopOpts := container.StopOptions{
Timeout: &timeoutSecs,
}
if err = c.ContainerStop(ctx, id, ctrStopOpts); err != nil {
return fmt.Errorf("error stopping container %v: %v\n", id, err)
}
if err = c.ContainerRemove(ctx, id, types.ContainerRemoveOptions{}); err != nil {
Expand Down
34 changes: 21 additions & 13 deletions pkg/docker/zz_close_guarding_client_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"net"
"net/http"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -109,7 +108,7 @@ func (c *closeGuardingClient) ContainerCommit(arg0 context.Context, arg1 string,
return c.pimpl.ContainerCommit(arg0, arg1, arg2)
}

func (c *closeGuardingClient) ContainerCreate(arg0 context.Context, arg1 *container.Config, arg2 *container.HostConfig, arg3 *network.NetworkingConfig, arg4 *v1.Platform, arg5 string) (container.ContainerCreateCreatedBody, error) {
func (c *closeGuardingClient) ContainerCreate(arg0 context.Context, arg1 *container.Config, arg2 *container.HostConfig, arg3 *network.NetworkingConfig, arg4 *v1.Platform, arg5 string) (container.CreateResponse, error) {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
Expand All @@ -118,7 +117,7 @@ func (c *closeGuardingClient) ContainerCreate(arg0 context.Context, arg1 *contai
return c.pimpl.ContainerCreate(arg0, arg1, arg2, arg3, arg4, arg5)
}

func (c *closeGuardingClient) ContainerDiff(arg0 context.Context, arg1 string) ([]container.ContainerChangeResponseItem, error) {
func (c *closeGuardingClient) ContainerDiff(arg0 context.Context, arg1 string) ([]container.FilesystemChange, error) {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
Expand Down Expand Up @@ -262,7 +261,7 @@ func (c *closeGuardingClient) ContainerResize(arg0 context.Context, arg1 string,
return c.pimpl.ContainerResize(arg0, arg1, arg2)
}

func (c *closeGuardingClient) ContainerRestart(arg0 context.Context, arg1 string, arg2 *time.Duration) error {
func (c *closeGuardingClient) ContainerRestart(arg0 context.Context, arg1 string, arg2 container.StopOptions) error {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
Expand Down Expand Up @@ -307,7 +306,7 @@ func (c *closeGuardingClient) ContainerStatsOneShot(arg0 context.Context, arg1 s
return c.pimpl.ContainerStatsOneShot(arg0, arg1)
}

func (c *closeGuardingClient) ContainerStop(arg0 context.Context, arg1 string, arg2 *time.Duration) error {
func (c *closeGuardingClient) ContainerStop(arg0 context.Context, arg1 string, arg2 container.StopOptions) error {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
Expand Down Expand Up @@ -343,7 +342,7 @@ func (c *closeGuardingClient) ContainerUpdate(arg0 context.Context, arg1 string,
return c.pimpl.ContainerUpdate(arg0, arg1, arg2)
}

func (c *closeGuardingClient) ContainerWait(arg0 context.Context, arg1 string, arg2 container.WaitCondition) (<-chan container.ContainerWaitOKBody, <-chan error) {
func (c *closeGuardingClient) ContainerWait(arg0 context.Context, arg1 string, arg2 container.WaitCondition) (<-chan container.WaitResponse, <-chan error) {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
Expand Down Expand Up @@ -406,13 +405,13 @@ func (c *closeGuardingClient) Dialer() func(context.Context) (net.Conn, error) {
return c.pimpl.Dialer()
}

func (c *closeGuardingClient) DiskUsage(arg0 context.Context) (types.DiskUsage, error) {
func (c *closeGuardingClient) DiskUsage(arg0 context.Context, arg1 types.DiskUsageOptions) (types.DiskUsage, error) {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
panic("use of closed client")
}
return c.pimpl.DiskUsage(arg0)
return c.pimpl.DiskUsage(arg0, arg1)
}

func (c *closeGuardingClient) DistributionInspect(arg0 context.Context, arg1 string, arg2 string) (registry.DistributionInspect, error) {
Expand Down Expand Up @@ -802,7 +801,7 @@ func (c *closeGuardingClient) PluginUpgrade(arg0 context.Context, arg1 string, a
return c.pimpl.PluginUpgrade(arg0, arg1, arg2)
}

func (c *closeGuardingClient) RegistryLogin(arg0 context.Context, arg1 types.AuthConfig) (registry.AuthenticateOKBody, error) {
func (c *closeGuardingClient) RegistryLogin(arg0 context.Context, arg1 registry.AuthConfig) (registry.AuthenticateOKBody, error) {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
Expand Down Expand Up @@ -1009,7 +1008,7 @@ func (c *closeGuardingClient) TaskLogs(arg0 context.Context, arg1 string, arg2 t
return c.pimpl.TaskLogs(arg0, arg1, arg2)
}

func (c *closeGuardingClient) VolumeCreate(arg0 context.Context, arg1 volume.VolumeCreateBody) (types.Volume, error) {
func (c *closeGuardingClient) VolumeCreate(arg0 context.Context, arg1 volume.CreateOptions) (volume.Volume, error) {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
Expand All @@ -1018,7 +1017,7 @@ func (c *closeGuardingClient) VolumeCreate(arg0 context.Context, arg1 volume.Vol
return c.pimpl.VolumeCreate(arg0, arg1)
}

func (c *closeGuardingClient) VolumeInspect(arg0 context.Context, arg1 string) (types.Volume, error) {
func (c *closeGuardingClient) VolumeInspect(arg0 context.Context, arg1 string) (volume.Volume, error) {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
Expand All @@ -1027,7 +1026,7 @@ func (c *closeGuardingClient) VolumeInspect(arg0 context.Context, arg1 string) (
return c.pimpl.VolumeInspect(arg0, arg1)
}

func (c *closeGuardingClient) VolumeInspectWithRaw(arg0 context.Context, arg1 string) (types.Volume, []uint8, error) {
func (c *closeGuardingClient) VolumeInspectWithRaw(arg0 context.Context, arg1 string) (volume.Volume, []uint8, error) {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
Expand All @@ -1036,7 +1035,7 @@ func (c *closeGuardingClient) VolumeInspectWithRaw(arg0 context.Context, arg1 st
return c.pimpl.VolumeInspectWithRaw(arg0, arg1)
}

func (c *closeGuardingClient) VolumeList(arg0 context.Context, arg1 filters.Args) (volume.VolumeListOKBody, error) {
func (c *closeGuardingClient) VolumeList(arg0 context.Context, arg1 volume.ListOptions) (volume.ListResponse, error) {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
Expand All @@ -1062,3 +1061,12 @@ func (c *closeGuardingClient) VolumesPrune(arg0 context.Context, arg1 filters.Ar
}
return c.pimpl.VolumesPrune(arg0, arg1)
}

func (c *closeGuardingClient) VolumeUpdate(arg0 context.Context, arg1 string, arg2 swarm.Version, arg3 volume.UpdateOptions) error {
c.m.RLock()
defer c.m.RUnlock()
if c.closed {
panic("use of closed client")
}
return c.pimpl.VolumeUpdate(arg0, arg1, arg2, arg3)
}
44 changes: 12 additions & 32 deletions pkg/oci/pusher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package oci

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"testing"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/registry"

fn "knative.dev/func/pkg/functions"
. "knative.dev/func/pkg/testing"
)
Expand All @@ -33,38 +34,17 @@ func TestPusher(t *testing.T) {
// Start a handler on an OS-chosen port which confirms that an incoming
// requests looks for the most part like what we'd expect to see from a
// container push.
regLog := io.Discard
if verbose {
regLog = os.Stderr
}
regHandler := registry.New(registry.Logger(log.New(regLog, "img reg handler: ", log.LstdFlags)))
handler := http.NewServeMux()
handler.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
if verbose {
fmt.Println("Mock registry server received request:")
fmt.Println("--------------------------------------")
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
t.Fatal(err)
}
fmt.Println(string(requestDump))
}

// Ignore all requests except the PUTs
if req.Method != http.MethodPut {
return
}

// Ignore all PUTs except the the image index
if req.Header.Get("Content-Type") != "application/vnd.oci.image.index.v1+json" {
return
}

// Decode the request as an index JSON
index := v1.IndexManifest{}
d := json.NewDecoder(req.Body)
defer req.Body.Close()
if err := d.Decode(&index); err != nil {
t.Fatal(err)
regHandler.ServeHTTP(res, req)
if req.Method == http.MethodPut && req.URL.Path == "/v2/funcs/f/manifests/latest" {
success = true
}

success = true

})
l, err := net.Listen("tcp4", "127.0.0.1:")
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## 0.7.4 (Jun 6, 2023)

BUG FIXES

- client: fixing an issue where the Content-Type header wouldn't be sent with an empty payload when using HTTP/2 [GH-194]

## 0.7.3 (May 15, 2023)

Initial release
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @hashicorp/release-engineering
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Copyright (c) 2015 HashiCorp, Inc.

Mozilla Public License, version 2.0

1. Definitions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

// Package retryablehttp provides a familiar HTTP client interface with
// automatic retries and exponential backoff. It is a thin wrapper over the
// standard net/http client library and exposes nearly the same public API.
Expand Down Expand Up @@ -257,10 +260,17 @@ func getBodyReaderAndContentLength(rawBody interface{}) (ReaderFunc, int64, erro
if err != nil {
return nil, 0, err
}
bodyReader = func() (io.Reader, error) {
return bytes.NewReader(buf), nil
if len(buf) == 0 {
bodyReader = func() (io.Reader, error) {
return http.NoBody, nil
}
contentLength = 0
} else {
bodyReader = func() (io.Reader, error) {
return bytes.NewReader(buf), nil
}
contentLength = int64(len(buf))
}
contentLength = int64(len(buf))

// No body provided, nothing to do
case nil:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package retryablehttp

import (
Expand Down
Loading

0 comments on commit 5948cf4

Please sign in to comment.