Skip to content

Commit

Permalink
refactor: inject Logrus logger in the Connect Go HTTP server and sepa…
Browse files Browse the repository at this point in the history
…rated context while pulling Docker images (#1829)

## Description:
inject Logrus logger in the Connect Go HTTP server and separated context
while pulling Docker images

## Is this change user facing?
NO

## References (if applicable):
This is related to this ticket:
#1768
  • Loading branch information
leoporoli committed Nov 21, 2023
1 parent 1be17e3 commit c78cd99
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
10 changes: 8 additions & 2 deletions connect-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
"github.com/sirupsen/logrus"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)

const (
ConnectHTTPServerLogPrefix = "[Connect-HTTP-ERROR]"
)

type ConnectServer struct {
listenPort uint16
path string
Expand Down Expand Up @@ -59,8 +64,9 @@ func (server *ConnectServer) RunServerUntilStopped(
mux.Handle(server.path, server.handler)

httpServer := http.Server{
Addr: fmt.Sprintf(":%v", server.listenPort),
Handler: cors.Handler(h2c.NewHandler(mux, &http2.Server{})),
Addr: fmt.Sprintf(":%v", server.listenPort),
Handler: cors.Handler(h2c.NewHandler(mux, &http2.Server{})),
ErrorLog: log.New(logrus.StandardLogger().Out, ConnectHTTPServerLogPrefix, log.Ldate|log.Ltime|log.Lshortfile),
}

go func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ func (manager *DockerManager) pullImage(context context.Context, imageName strin
return stacktrace.Propagate(err, "An error occurred communicating with docker engine")
}
logrus.Infof("Pulling image '%s'", imageName)
err, retryWithLinuxAmd64 := pullImage(context, manager.dockerClientNoTimeout, imageName, defaultPlatform)
err, retryWithLinuxAmd64 := pullImage(manager.dockerClientNoTimeout, imageName, defaultPlatform)
if err == nil {
return nil
}
Expand All @@ -1387,7 +1387,7 @@ func (manager *DockerManager) pullImage(context context.Context, imageName strin
}
// we retry with linux/amd64
logrus.Debugf("Retrying pulling image '%s' for '%s'", imageName, linuxAmd64)
err, _ = pullImage(context, manager.dockerClientNoTimeout, imageName, linuxAmd64)
err, _ = pullImage(manager.dockerClientNoTimeout, imageName, linuxAmd64)
if err != nil {
return stacktrace.Propagate(err, "Had previously failed with a manifest error so tried pulling image '%v' for platform '%v' but failed", imageName, linuxAmd64)
}
Expand Down Expand Up @@ -1994,9 +1994,12 @@ func getEndpointSettingsForIpAddress(ipAddress string, alias string) *network.En
return config
}

func pullImage(ctx context.Context, dockerClient *client.Client, imageName string, platform string) (error, bool) {
func pullImage(dockerClient *client.Client, imageName string, platform string) (error, bool) {
// Own context for pulling images because we do not want to cancel this works in case the main context in the request is cancelled
// if the fist request fails the image will be ready for following request making the process faster
pullImageCtx := context.Background()
logrus.Tracef("Starting pulling '%s' for platform '%s'", imageName, platform)
out, err := dockerClient.ImagePull(ctx, imageName, types.ImagePullOptions{
out, err := dockerClient.ImagePull(pullImageCtx, imageName, types.ImagePullOptions{
All: false,
RegistryAuth: "",
PrivilegeFunc: nil,
Expand Down

0 comments on commit c78cd99

Please sign in to comment.