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

added app labels to container metadata #683

Merged
merged 1 commit into from
Oct 15, 2019
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
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),
zappy-shu marked this conversation as resolved.
Show resolved Hide resolved
})
}

func checkContains(t *testing.T, combined string, expectedLines []string) {
for _, expected := range expectedLines {
exp := regexp.MustCompile(expected)
Expand Down
5 changes: 5 additions & 0 deletions internal/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down