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

add services/volumes/hash flag for config command #632

Merged
merged 2 commits into from
Dec 20, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1171,9 +1171,15 @@ Validate and view the Compose file

Usage: `nerdctl compose config`

Unimplemented `docker-compose config` (V1) flags: `--resolve-image-digests`, `--no-interpolate`, `--services`, `--volumes`, `--hash`
Flags:
- :whale: `-q, --quiet`: Pull without printing progress information
- :whale: `--services`: Print the service names, one per line.
- :whale: `--volumes`: Print the volume names, one per line.
- :whale: `--hash="*"`: Print the service config hash, one per line.

Unimplemented `docker-compose config` (V1) flags: `--resolve-image-digests`, `--no-interpolate`

Unimplemented `docker compose config` (V2) flags: `--resolve-image-digests`, `--no-interpolate`, `--services`, `--volumes`, `--hash`, `--format`, `--output`, `--profiles`
Unimplemented `docker compose config` (V2) flags: `--resolve-image-digests`, `--no-interpolate`, `--format`, `--output`, `--profiles`

### :whale: nerdctl compose kill
Force stop service containers
Expand Down
30 changes: 27 additions & 3 deletions cmd/nerdctl/compose_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"fmt"

"github.com/containerd/nerdctl/pkg/composer"
"github.com/spf13/cobra"
)

Expand All @@ -31,6 +32,12 @@ func newComposeConfigCommand() *cobra.Command {
SilenceErrors: true,
}
composeConfigCommand.Flags().BoolP("quiet", "q", false, "Only validate the configuration, don't print anything.")
composeConfigCommand.Flags().Bool("services", false, "Print the service names, one per line.")
composeConfigCommand.Flags().Bool("volumes", false, "Print the volume names, one per line.")
composeConfigCommand.Flags().String("hash", "", "Print the service config hash, one per line.")
junnplus marked this conversation as resolved.
Show resolved Hide resolved
composeConfigCommand.RegisterFlagCompletionFunc("hash", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"\"*\""}, cobra.ShellCompDirectiveNoFileComp
})
return composeConfigCommand
}

Expand All @@ -43,6 +50,18 @@ func composeConfigAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
services, err := cmd.Flags().GetBool("services")
if err != nil {
return err
}
volumes, err := cmd.Flags().GetBool("volumes")
if err != nil {
return err
}
hash, err := cmd.Flags().GetString("hash")
if err != nil {
return err
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
Expand All @@ -54,8 +73,13 @@ func composeConfigAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if !quiet {
return c.Config(ctx)
if quiet {
return nil
}
co := composer.ConfigOptions{
Services: services,
Volumes: volumes,
Hash: hash,
}
return nil
return c.Config(ctx, cmd.OutOrStdout(), co)
}
39 changes: 39 additions & 0 deletions cmd/nerdctl/compose_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package main

import (
"fmt"
"testing"

"github.com/containerd/nerdctl/pkg/testutil"
"gotest.tools/v3/assert"
)

func TestComposeConfig(t *testing.T) {
Expand All @@ -36,3 +38,40 @@ services:

base.ComposeCmd("-f", comp.YAMLFullPath(), "config").AssertOutContains("hello:")
}

func TestComposeConfigWithPrintService(t *testing.T) {
base := testutil.NewBase(t)

var dockerComposeYAML = `
services:
hello1:
image: alpine:3.13
`

comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()

base.ComposeCmd("-f", comp.YAMLFullPath(), "config", "--services").AssertOutExactly("hello1\n")
}

func TestComposeConfigWithPrintServiceHash(t *testing.T) {
base := testutil.NewBase(t)

var dockerComposeYAML = `
services:
hello1:
image: alpine:%s
`

comp := testutil.NewComposeDir(t, fmt.Sprintf(dockerComposeYAML, "3.13"))
defer comp.CleanUp()

base.ComposeCmd("-f", comp.YAMLFullPath(), "config", "--hash=*").AssertOutContains("hello1")
hash := base.ComposeCmd("-f", comp.YAMLFullPath(), "config", "--hash=hello1").Out()

newComp := testutil.NewComposeDir(t, fmt.Sprintf(dockerComposeYAML, "3.14"))
defer newComp.CleanUp()

newHash := base.ComposeCmd("-f", newComp.YAMLFullPath(), "config", "--hash=hello1").Out()
assert.Assert(t, hash != newHash)
}
junnplus marked this conversation as resolved.
Show resolved Hide resolved
57 changes: 55 additions & 2 deletions pkg/composer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,69 @@ package composer

import (
"context"
"encoding/json"
"fmt"
"io"
"strings"

"github.com/compose-spec/compose-go/types"
"github.com/opencontainers/go-digest"
"gopkg.in/yaml.v2"
)

func (c *Composer) Config(ctx context.Context) error {
type ConfigOptions struct {
Services bool
Volumes bool
Hash string
}

func (c *Composer) Config(ctx context.Context, w io.Writer, co ConfigOptions) error {
if co.Services {
for _, service := range c.project.Services {
fmt.Fprintln(w, service.Name)
}
return nil
}
if co.Volumes {
for volume := range c.project.Volumes {
fmt.Fprintln(w, volume)
}
return nil
}
if co.Hash != "" {
var services []string
if co.Hash != "*" {
services = strings.Split(co.Hash, ",")
}
if err := c.project.WithServices(services, func(svc types.ServiceConfig) error {
hash, err := ServiceHash(svc)
if err != nil {
return err
}
fmt.Fprintf(w, "%s %s\n", svc.Name, hash)
return nil
}); err != nil {
return err
}
return nil
}
projectYAML, err := yaml.Marshal(c.project)
if err != nil {
return err
}
fmt.Printf("%s", projectYAML)
fmt.Fprintf(w, "%s", projectYAML)
return nil
}

// ServiceHash is from https://github.com/docker/compose/blob/v2.2.2/pkg/compose/hash.go#L28-L38
func ServiceHash(o types.ServiceConfig) (string, error) {
// remove the Build config when generating the service hash
o.Build = nil
o.PullPolicy = ""
o.Scale = 1
bytes, err := json.Marshal(o)
if err != nil {
return "", err
}
return digest.SHA256.FromBytes(bytes).Encoded(), nil
}