Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 64e685e

Browse files
Merge pull request #683 from zappy-shu/add-app-metadata-labels
added app labels to container metadata
2 parents a4645a5 + 68e3811 commit 64e685e

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

cmd/cnab-run/install.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/docker/cli/cli/command/stack"
1515
"github.com/docker/cli/cli/command/stack/options"
1616
"github.com/docker/cli/cli/command/stack/swarm"
17+
composetypes "github.com/docker/cli/cli/compose/types"
1718
"github.com/pkg/errors"
1819
"github.com/spf13/pflag"
1920
)
@@ -50,6 +51,7 @@ func installAction(instanceName string) error {
5051
if err != nil {
5152
return err
5253
}
54+
addAppLabels(rendered, instanceName)
5355
if err := os.Chdir(app.Path); err != nil {
5456
return err
5557
}
@@ -84,3 +86,14 @@ func getBundleImageMap() (map[string]bundle.Image, error) {
8486
}
8587
return result, nil
8688
}
89+
90+
func addAppLabels(rendered *composetypes.Config, instanceName string) {
91+
for i, service := range rendered.Services {
92+
if service.Labels == nil {
93+
service.Labels = map[string]string{}
94+
}
95+
service.Labels[internal.LabelAppNamespace] = instanceName
96+
service.Labels[internal.LabelAppVersion] = internal.Version
97+
rendered.Services[i] = service
98+
}
99+
}

e2e/commands_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ func testDockerAppLifecycle(t *testing.T, useBindMount bool) {
230230
fmt.Sprintf("Creating service %s_api", appName),
231231
fmt.Sprintf("Creating service %s_web", appName),
232232
})
233+
assertAppLabels(t, &cmd, appName, "db")
233234

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

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

410+
func assertAppLabels(t *testing.T, cmd *icmd.Cmd, appName, containerName string) {
411+
cmd.Command = dockerCli.Command("inspect", fmt.Sprintf("%s_%s", appName, containerName))
412+
checkContains(t, icmd.RunCmd(*cmd).Assert(t, icmd.Success).Combined(),
413+
[]string{
414+
fmt.Sprintf(`"%s": "%s"`, internal.LabelAppNamespace, appName),
415+
fmt.Sprintf(`"%s": ".+"`, internal.LabelAppVersion),
416+
})
417+
}
418+
408419
func checkContains(t *testing.T, combined string, expectedLines []string) {
409420
for _, expected := range expectedLines {
410421
exp := regexp.MustCompile(expected)

internal/names.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ const (
7373
// CustomDockerAppName is the custom variable set by Docker App to
7474
// save custom informations
7575
CustomDockerAppName = "com.docker.app"
76+
77+
// LabelAppNamespace is the label used to track app resources
78+
LabelAppNamespace = Namespace + "namespace"
79+
// LabelAppVersion is the label used to identify what version of docker app was used to create the app
80+
LabelAppVersion = Namespace + "version"
7681
)
7782

7883
var (

0 commit comments

Comments
 (0)