Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
added app labels to container metadata
Browse files Browse the repository at this point in the history
Added 2 labels to the containers when they are deployed by docker app:
- com.docker.app.namespace: same as the com.docker.stack.namespace
- com.docker.app.version: version of docker app used for the deploy

These have been added so that Docker Desktop can differentiate between
docker app deployments vs docker stack services.

Signed-off-by: Nick Adcock <nick.adcock@docker.com>
  • Loading branch information
zappy-shu committed Oct 15, 2019
1 parent 3e97bfd commit bdb9d1a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/cnab-run/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/docker/cli/cli/command/stack"
"github.com/docker/cli/cli/command/stack/options"
"github.com/docker/cli/cli/command/stack/swarm"
composetypes "github.com/docker/cli/cli/compose/types"
"github.com/pkg/errors"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -50,6 +51,7 @@ func installAction(instanceName string) error {
if err != nil {
return err
}
addAppLabels(rendered, instanceName)
if err := os.Chdir(app.Path); err != nil {
return err
}
Expand Down Expand Up @@ -84,3 +86,14 @@ func getBundleImageMap() (map[string]bundle.Image, error) {
}
return result, nil
}

func addAppLabels(rendered *composetypes.Config, instanceName string) {
for i, service := range rendered.Services {
if service.Labels == nil {
service.Labels = map[string]string{}
}
service.Labels[internal.LabelAppNamespace] = instanceName
service.Labels[internal.LabelAppVersion] = internal.Version
rendered.Services[i] = service
}
}
11 changes: 11 additions & 0 deletions e2e/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ func testDockerAppLifecycle(t *testing.T, useBindMount bool) {
fmt.Sprintf("Creating service %s_api", appName),
fmt.Sprintf("Creating service %s_web", appName),
})
assertAppLabels(t, &cmd, appName, "db")

// List the installed application
cmd.Command = dockerCli.Command("app", "ls")
Expand All @@ -254,6 +255,7 @@ func testDockerAppLifecycle(t *testing.T, useBindMount bool) {
fmt.Sprintf("Updating service %s_api", appName),
fmt.Sprintf("Updating service %s_web", appName),
})
assertAppLabels(t, &cmd, appName, "db")

// Uninstall the application
cmd.Command = dockerCli.Command("app", "rm", appName)
Expand Down Expand Up @@ -405,6 +407,15 @@ func initializeDockerAppEnvironment(t *testing.T, cmd *icmd.Cmd, tmpDir *fs.Dir,
icmd.RunCmd(*cmd).Assert(t, icmd.Success)
}

func assertAppLabels(t *testing.T, cmd *icmd.Cmd, appName, containerName string) {
cmd.Command = dockerCli.Command("inspect", fmt.Sprintf("%s_%s", appName, containerName))
checkContains(t, icmd.RunCmd(*cmd).Assert(t, icmd.Success).Combined(),
[]string{
fmt.Sprintf(`"%s": "%s"`, internal.LabelAppNamespace, appName),
fmt.Sprintf(`"%s": ".+"`, internal.LabelAppVersion),
})
}

func checkContains(t *testing.T, combined string, expectedLines []string) {
for _, expected := range expectedLines {
exp := regexp.MustCompile(expected)
Expand Down
6 changes: 6 additions & 0 deletions internal/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ const (
// DockerInspectFormatEnvVar is the environment variable set by the CNAB runtime to select
// the inspect output format.
DockerInspectFormatEnvVar = "DOCKER_INSPECT_FORMAT"

labelAppPrefix = "com.docker.app."
// LabelAppNamespace is the label used to track app resources
LabelAppNamespace = labelAppPrefix + "namespace"
// LabelAppVersion is the label used to identify what version of docker app was used to create the app
LabelAppVersion = labelAppPrefix + "version"
)

var (
Expand Down

0 comments on commit bdb9d1a

Please sign in to comment.