From 68e3811654692affcb38ae055da8c4eda0e83c87 Mon Sep 17 00:00:00 2001 From: Nick Adcock Date: Thu, 10 Oct 2019 14:50:26 +0100 Subject: [PATCH] added app labels to container metadata 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 --- cmd/cnab-run/install.go | 13 +++++++++++++ e2e/commands_test.go | 11 +++++++++++ internal/names.go | 5 +++++ 3 files changed, 29 insertions(+) diff --git a/cmd/cnab-run/install.go b/cmd/cnab-run/install.go index dc7f0382e..4556a6b9f 100644 --- a/cmd/cnab-run/install.go +++ b/cmd/cnab-run/install.go @@ -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" ) @@ -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 } @@ -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 + } +} diff --git a/e2e/commands_test.go b/e2e/commands_test.go index 2b76c3d57..60822ae33 100644 --- a/e2e/commands_test.go +++ b/e2e/commands_test.go @@ -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") @@ -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) @@ -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) diff --git a/internal/names.go b/internal/names.go index 0176f6d3d..044e3fae7 100644 --- a/internal/names.go +++ b/internal/names.go @@ -73,6 +73,11 @@ const ( // CustomDockerAppName is the custom variable set by Docker App to // save custom informations CustomDockerAppName = "com.docker.app" + + // LabelAppNamespace is the label used to track app resources + LabelAppNamespace = Namespace + "namespace" + // LabelAppVersion is the label used to identify what version of docker app was used to create the app + LabelAppVersion = Namespace + "version" ) var (