Skip to content

Commit

Permalink
Merge pull request #2042 from danishprakash/compose-profile-support
Browse files Browse the repository at this point in the history
cmd/compose: add support for profiles
  • Loading branch information
AkihiroSuda authored Mar 1, 2023
2 parents 9511a78 + ca5f02f commit a56e239
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/nerdctl/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func newComposeCommand() *cobra.Command {
composeCommand.PersistentFlags().StringP("project-name", "p", "", "Specify an alternate project name")
composeCommand.PersistentFlags().String("env-file", "", "Specify an alternate environment file")
composeCommand.PersistentFlags().String("ipfs-address", "", "multiaddr of IPFS API (default uses $IPFS_PATH env variable if defined or local directory ~/.ipfs)")
composeCommand.PersistentFlags().StringArray("profile", []string{}, "Specify a profile to enable")

composeCommand.AddCommand(
newComposeUpCommand(),
Expand Down Expand Up @@ -87,11 +88,16 @@ func getComposeOptions(cmd *cobra.Command, debugFull, experimental bool) (compos
if err != nil {
return composer.Options{}, err
}
profiles, err := cmd.Flags().GetStringArray("profile")
if err != nil {
return composer.Options{}, err
}

return composer.Options{
Project: projectName,
ProjectDirectory: projectDirectory,
ConfigPaths: files,
Profiles: profiles,
EnvFile: envFile,
NerdctlCmd: nerdctlCmd,
NerdctlArgs: nerdctlArgs,
Expand Down
1 change: 1 addition & 0 deletions cmd/nerdctl/compose_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func composeUpAction(cmd *cobra.Command, services []string) error {
if err != nil {
return err
}
options.Services = services
c, err := compose.New(client, globalOptions, options, cmd.OutOrStdout(), cmd.ErrOrStderr())
if err != nil {
return err
Expand Down
42 changes: 42 additions & 0 deletions cmd/nerdctl/compose_up_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,45 @@ volumes:
db:
`, testutil.WordpressImage, testutil.MariaDBImage))
}

func TestComposeUpProfile(t *testing.T) {
base := testutil.NewBase(t)
serviceRegular := testutil.Identifier(t) + "-regular"
serviceProfiled := testutil.Identifier(t) + "-profiled"

dockerComposeYAML := fmt.Sprintf(`
services:
%s:
image: %[3]s
%[2]s:
image: %[3]s
profiles:
- test-profile
`, serviceRegular, serviceProfiled, testutil.NginxAlpineImage)

// * Test with profile
// Should run both the services:
// - matching active profile
// - one without profile
comp1 := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp1.CleanUp()
base.ComposeCmd("-f", comp1.YAMLFullPath(), "--profile", "test-profile", "up", "-d").AssertOK()

psCmd := base.Cmd("ps", "-a", "--format={{.Names}}")
psCmd.AssertOutContains(serviceRegular)
psCmd.AssertOutContains(serviceProfiled)
base.ComposeCmd("-f", comp1.YAMLFullPath(), "--profile", "test-profile", "down", "-v").AssertOK()

// * Test without profile
// Should run:
// - service without profile
comp2 := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp2.CleanUp()
base.ComposeCmd("-f", comp2.YAMLFullPath(), "up", "-d").AssertOK()
defer base.ComposeCmd("-f", comp2.YAMLFullPath(), "down", "-v").AssertOK()

psCmd = base.Cmd("ps", "-a", "--format={{.Names}}")
psCmd.AssertOutContains(serviceRegular)
psCmd.AssertOutNotContains(serviceProfiled)
}
1 change: 1 addition & 0 deletions docs/command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,7 @@ Flags:
- :whale: `-f, --file`: Specify an alternate compose file
- :whale: `-p, --project-name`: Specify an alternate project name
- :nerd_face: `--ipfs-address`: Multiaddr of IPFS API (default uses `$IPFS_PATH` env variable if defined or local directory `~/.ipfs`)
- :whale: `--profile: Specify a profile to enable

### :whale: nerdctl compose up

Expand Down
11 changes: 11 additions & 0 deletions pkg/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Options struct {
Project string // empty for default
ProjectDirectory string
ConfigPaths []string
Profiles []string
Services []string
EnvFile string
NerdctlCmd string
NerdctlArgs []string
Expand Down Expand Up @@ -84,6 +86,15 @@ func New(o Options, client *containerd.Client) (*Composer, error) {
return nil, err
}

if len(o.Services) > 0 {
s, err := project.GetServices(o.Services...)
if err != nil {
return nil, err
}
o.Profiles = append(o.Profiles, s.GetProfiles()...)
}
project.ApplyProfiles(o.Profiles)

if o.DebugPrintFull {
projectJSON, _ := json.MarshalIndent(project, "", " ")
logrus.Debug("printing project JSON")
Expand Down

0 comments on commit a56e239

Please sign in to comment.