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

Commit

Permalink
Merge pull request #683 from zappy-shu/add-app-metadata-labels
Browse files Browse the repository at this point in the history
added app labels to container metadata
  • Loading branch information
silvin-lubecki committed Oct 15, 2019
2 parents a4645a5 + 68e3811 commit 64e685e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/cnab-run/install.go
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
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
5 changes: 5 additions & 0 deletions internal/names.go
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

0 comments on commit 64e685e

Please sign in to comment.