Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log docker info from compose #591

Merged
merged 3 commits into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions compose.go
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"

"github.com/compose-spec/compose-go/types"
"github.com/docker/cli/cli/command"
Expand All @@ -22,6 +23,7 @@ const (
envComposeFile = "COMPOSE_FILE"
)

var composeLogOnce sync.Once
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, such a cool feature in Go 😮

var ErrNoStackConfigured = errors.New("no stack files configured")

type composeStackOptions struct {
Expand Down Expand Up @@ -129,6 +131,11 @@ func NewDockerComposeWith(opts ...ComposeStackOption) (*dockerCompose, error) {
containers: make(map[string]*DockerContainer),
}

// log docker server info only once
composeLogOnce.Do(func() {
logDockerServerInfo(context.Background(), dockerCli.Client(), Logger)
})

return composeAPI, nil
}

Expand Down
15 changes: 9 additions & 6 deletions docker.go
Expand Up @@ -804,26 +804,29 @@ func NewDockerProvider(provOpts ...DockerProviderOption) (*DockerProvider, error
}

// log docker server info only once
logOnce.Do(p.logDockerServerInfo)
logOnce.Do(func() {
logDockerServerInfo(context.Background(), p.client, p.Logger)
})

return p, nil
}

func (p *DockerProvider) logDockerServerInfo() {
func logDockerServerInfo(ctx context.Context, client client.APIClient, logger Logging) {
infoMessage := `%v - Connected to docker:
Server Version: %v
API Version: %v
Operating System: %v
Total Memory: %v MB
`

info, err := p.client.Info(context.Background())
info, err := client.Info(ctx)
if err != nil {
p.Logger.Printf("failed getting information about docker server: %s", err)
logger.Printf("failed getting information about docker server: %s", err)
return
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this if was incomplete, as it should exit the function.

I wonder if we should raise an error here, as if the docker info failed, then we cannot continue. In the meantime, we can live with the return, as this function is called after the docker client creation, so it's expected using the client is healthy

}

p.Logger.Printf(infoMessage, packagePath,
info.ServerVersion, p.client.ClientVersion(),
logger.Printf(infoMessage, packagePath,
info.ServerVersion, client.ClientVersion(),
info.OperatingSystem, info.MemTotal/1024/1024)
}

Expand Down