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

Commit e1cbea5

Browse files
committed
Add --label flag when running an application
This will add the given labels to all the containers inside the application (except for the invocation image). The labels are passed over by file, this is to be able to easely add new parameters in the future and limit the explosion of parameters we have in our bundle Signed-off-by: Djordje Lukic <[email protected]>
1 parent cbf819c commit e1cbea5

File tree

7 files changed

+79
-11
lines changed

7 files changed

+79
-11
lines changed

cmd/cnab-run/install.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func installAction(instanceName string) error {
5151
if err != nil {
5252
return err
5353
}
54+
if err = addLabels(rendered); err != nil {
55+
return err
56+
}
5457
addAppLabels(rendered, instanceName)
58+
5559
if err := os.Chdir(app.Path); err != nil {
5660
return err
5761
}
@@ -87,6 +91,28 @@ func getBundleImageMap() (map[string]bundle.Image, error) {
8791
return result, nil
8892
}
8993

94+
func addLabels(rendered *composetypes.Config) error {
95+
args, err := ioutil.ReadFile(internal.DockerArgsPath)
96+
if err != nil {
97+
return err
98+
}
99+
a := packager.DockerAppArgs{}
100+
err = json.Unmarshal(args, &a)
101+
if err != nil {
102+
return err
103+
}
104+
for k, v := range a.Labels {
105+
for i, service := range rendered.Services {
106+
if service.Labels == nil {
107+
service.Labels = map[string]string{}
108+
}
109+
service.Labels[k] = v
110+
rendered.Services[i] = service
111+
}
112+
}
113+
return nil
114+
}
115+
90116
func addAppLabels(rendered *composetypes.Config, instanceName string) {
91117
for i, service := range rendered.Services {
92118
if service.Labels == nil {

internal/commands/image/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func printImageIDs(dockerCli command.Cli, refs []pkg) error {
107107
}
108108
fmt.Fprintln(&buf, stringid.TruncateID(id.String()))
109109
}
110-
fmt.Fprintf(dockerCli.Out(), buf.String())
110+
fmt.Fprint(dockerCli.Out(), buf.String())
111111
return nil
112112
}
113113

internal/commands/image/list_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ import (
1414
"github.com/docker/distribution/reference"
1515
)
1616

17-
type mockRef string
18-
19-
func (ref mockRef) String() string {
20-
return string(ref)
21-
}
22-
2317
type bundleStoreStubForListCmd struct {
2418
refMap map[reference.Reference]*bundle.Bundle
2519
// in order to keep the reference in the same order between tests

internal/commands/parameters.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package commands
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
67
"os"
78
"strings"
89

910
"github.com/deislabs/cnab-go/bundle"
1011
"github.com/docker/app/internal"
12+
"github.com/docker/app/internal/packager"
1113
"github.com/docker/app/internal/store"
1214
"github.com/docker/app/types/parameters"
1315
cliopts "github.com/docker/cli/opts"
@@ -45,6 +47,22 @@ func withCommandLineParameters(overrides []string) mergeBundleOpt {
4547
}
4648
}
4749

50+
func withLabels(labels []string) mergeBundleOpt {
51+
return func(c *mergeBundleConfig) error {
52+
l := packager.DockerAppArgs{
53+
Labels: cliopts.ConvertKVStringsToMap(labels),
54+
}
55+
out, err := json.Marshal(l)
56+
if err != nil {
57+
return err
58+
}
59+
if _, ok := c.bundle.Parameters[internal.ParameterArgs]; ok {
60+
c.params[internal.ParameterArgs] = string(out)
61+
}
62+
return nil
63+
}
64+
}
65+
4866
func withSendRegistryAuth(sendRegistryAuth bool) mergeBundleOpt {
4967
return func(c *mergeBundleConfig) error {
5068
if _, ok := c.bundle.Definitions[internal.ParameterShareRegistryCredsName]; ok {

internal/commands/run.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type runOptions struct {
2525
kubeNamespace string
2626
stackName string
2727
cnabBundle string
28+
labels []string
2829
}
2930

3031
const longDescription = `Run an App from an App image.`
@@ -59,10 +60,11 @@ func runCmd(dockerCli command.Cli) *cobra.Command {
5960
}
6061
opts.parametersOptions.addFlags(cmd.Flags())
6162
opts.credentialOptions.addFlags(cmd.Flags())
62-
cmd.Flags().StringVar(&opts.orchestrator, "orchestrator", "", "Orchestrator to run on (swarm, kubernetes)")
63-
cmd.Flags().StringVar(&opts.kubeNamespace, "namespace", "default", "Kubernetes namespace in which to run the App")
64-
cmd.Flags().StringVar(&opts.stackName, "name", "", "Name of the running App")
65-
cmd.Flags().StringVar(&opts.cnabBundle, "cnab-bundle-json", "", "Run a CNAB bundle instead of a Docker App image")
63+
cmd.Flags().StringVar(&opts.orchestrator, "orchestrator", "", "Orchestrator to install on (swarm, kubernetes)")
64+
cmd.Flags().StringVar(&opts.kubeNamespace, "namespace", "default", "Kubernetes namespace to install into")
65+
cmd.Flags().StringVar(&opts.stackName, "name", "", "Assign a name to the installation")
66+
cmd.Flags().StringVar(&opts.cnabBundle, "cnab-bundle-json", "", "Run a CNAB bundle instead of a Docker App")
67+
cmd.Flags().StringArrayVar(&opts.labels, "label", nil, "Label to add to services")
6668

6769
return cmd
6870
}
@@ -130,6 +132,7 @@ func runBundle(dockerCli command.Cli, bndl *bundle.Bundle, opts runOptions, ref
130132
if err := mergeBundleParameters(installation,
131133
withFileParameters(opts.parametersFiles),
132134
withCommandLineParameters(opts.overrides),
135+
withLabels(opts.labels),
133136
withOrchestratorParameters(opts.orchestrator, opts.kubeNamespace),
134137
withSendRegistryAuth(opts.sendRegistryAuth),
135138
); err != nil {

internal/names.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const (
5252
ParameterRenderFormatName = Namespace + "render-format"
5353
// ParameterInspectFormatName is the name of the parameter containing the inspect format
5454
ParameterInspectFormatName = Namespace + "inspect-format"
55+
// ParameterArgs is the name of the parameter containing labels to be applied to service containers
56+
ParameterArgs = Namespace + "args"
5557
// ParameterShareRegistryCredsName is the name of the parameter which indicates if credentials should be shared
5658
ParameterShareRegistryCredsName = Namespace + "share-registry-creds"
5759

@@ -68,6 +70,8 @@ const (
6870
// the inspect output format.
6971
DockerInspectFormatEnvVar = "DOCKER_INSPECT_FORMAT"
7072

73+
DockerArgsPath = "/cnab/app/args.json"
74+
7175
// CustomDockerAppName is the custom variable set by Docker App to
7276
// save custom informations
7377
CustomDockerAppName = "com.docker.app"

internal/packager/cnab.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,24 @@ type DockerAppCustom struct {
2323
Payload json.RawMessage `json:"payload,omitempty"`
2424
}
2525

26+
// DockerAppArgs represent the object passed to the invocation image
27+
// by Docker App.
28+
type DockerAppArgs struct {
29+
// Labels are the labels to add to containers on run
30+
Labels map[string]string `json:"labels,omitempty"`
31+
}
32+
2633
// ToCNAB creates a CNAB bundle from an app package
2734
func ToCNAB(app *types.App, invocationImageName string) (*bundle.Bundle, error) {
2835
mapping := ExtractCNABParameterMapping(app.Parameters())
2936
flatParameters := app.Parameters().Flatten()
3037
definitions := definition.Definitions{
38+
internal.ParameterArgs: {
39+
Type: "string",
40+
Default: "",
41+
Title: "Labels",
42+
Description: "Labels to apply to service containers",
43+
},
3144
internal.ParameterOrchestratorName: {
3245
Type: "string",
3346
Enum: []interface{}{
@@ -73,6 +86,16 @@ func ToCNAB(app *types.App, invocationImageName string) (*bundle.Bundle, error)
7386
},
7487
}
7588
parameters := map[string]bundle.Parameter{
89+
internal.ParameterArgs: {
90+
Destination: &bundle.Location{
91+
Path: internal.DockerArgsPath,
92+
},
93+
ApplyTo: []string{
94+
"install",
95+
"upgrade",
96+
},
97+
Definition: internal.ParameterArgs,
98+
},
7699
internal.ParameterOrchestratorName: {
77100
Destination: &bundle.Location{
78101
EnvironmentVariable: internal.DockerStackOrchestratorEnvVar,

0 commit comments

Comments
 (0)