Skip to content

Commit 53d2e5e

Browse files
committed
Merge pull request moby#21270 from ehazlett/resource-labels
Add Label support for Images (build), Networks and Volumes on Creation
2 parents 62d4556 + fc214b4 commit 53d2e5e

File tree

33 files changed

+522
-74
lines changed

33 files changed

+522
-74
lines changed

api/client/build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
6262
cmd.Var(&flBuildArg, []string{"-build-arg"}, "Set build-time variables")
6363
isolation := cmd.String([]string{"-isolation"}, "", "Container isolation technology")
6464

65+
flLabels := opts.NewListOpts(nil)
66+
cmd.Var(&flLabels, []string{"-label"}, "Set metadata for an image")
67+
6568
ulimits := make(map[string]*units.Ulimit)
6669
flUlimits := runconfigopts.NewUlimitOpt(&ulimits)
6770
cmd.Var(flUlimits, []string{"-ulimit"}, "Ulimit options")
@@ -230,6 +233,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
230233
Ulimits: flUlimits.GetList(),
231234
BuildArgs: runconfigopts.ConvertKVStringsToMap(flBuildArg.GetAll()),
232235
AuthConfigs: cli.retrieveAuthConfigs(),
236+
Labels: runconfigopts.ConvertKVStringsToMap(flLabels.GetAll()),
233237
}
234238

235239
response, err := cli.client.ImageBuild(context.Background(), options)

api/client/network.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
4444
flIpamGateway := opts.NewListOpts(nil)
4545
flIpamAux := opts.NewMapOpts(nil, nil)
4646
flIpamOpt := opts.NewMapOpts(nil, nil)
47+
flLabels := opts.NewListOpts(nil)
4748

4849
cmd.Var(&flIpamSubnet, []string{"-subnet"}, "subnet in CIDR format that represents a network segment")
4950
cmd.Var(&flIpamIPRange, []string{"-ip-range"}, "allocate container ip from a sub-range")
5051
cmd.Var(&flIpamGateway, []string{"-gateway"}, "ipv4 or ipv6 Gateway for the master subnet")
5152
cmd.Var(flIpamAux, []string{"-aux-address"}, "auxiliary ipv4 or ipv6 addresses used by Network driver")
5253
cmd.Var(flOpts, []string{"o", "-opt"}, "set driver specific options")
5354
cmd.Var(flIpamOpt, []string{"-ipam-opt"}, "set IPAM driver specific options")
55+
cmd.Var(&flLabels, []string{"-label"}, "set metadata on a network")
5456

5557
flInternal := cmd.Bool([]string{"-internal"}, false, "restricts external access to the network")
5658
flIPv6 := cmd.Bool([]string{"-ipv6"}, false, "enable IPv6 networking")
@@ -82,6 +84,7 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
8284
CheckDuplicate: true,
8385
Internal: *flInternal,
8486
EnableIPv6: *flIPv6,
87+
Labels: runconfigopts.ConvertKVStringsToMap(flLabels.GetAll()),
8588
}
8689

8790
resp, err := cli.client.NetworkCreate(context.Background(), nc)

api/client/volume.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
Cli "github.com/docker/docker/cli"
1111
"github.com/docker/docker/opts"
1212
flag "github.com/docker/docker/pkg/mflag"
13+
runconfigopts "github.com/docker/docker/runconfig/opts"
1314
"github.com/docker/engine-api/types"
1415
"github.com/docker/engine-api/types/filters"
1516
)
@@ -128,13 +129,17 @@ func (cli *DockerCli) CmdVolumeCreate(args ...string) error {
128129
flDriverOpts := opts.NewMapOpts(nil, nil)
129130
cmd.Var(flDriverOpts, []string{"o", "-opt"}, "Set driver specific options")
130131

132+
flLabels := opts.NewListOpts(nil)
133+
cmd.Var(&flLabels, []string{"-label"}, "Set metadata for a volume")
134+
131135
cmd.Require(flag.Exact, 0)
132136
cmd.ParseFlags(args, true)
133137

134138
volReq := types.VolumeCreateRequest{
135139
Driver: *flDriver,
136140
DriverOpts: flDriverOpts.GetAll(),
137141
Name: *flName,
142+
Labels: runconfigopts.ConvertKVStringsToMap(flLabels.GetAll()),
138143
}
139144

140145
vol, err := cli.client.VolumeCreate(context.Background(), volReq)

api/server/router/build/build_routes.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui
8282
}
8383
options.BuildArgs = buildArgs
8484
}
85+
var labels = map[string]string{}
86+
labelsJSON := r.FormValue("labels")
87+
if labelsJSON != "" {
88+
if err := json.NewDecoder(strings.NewReader(labelsJSON)).Decode(&labels); err != nil {
89+
return nil, err
90+
}
91+
options.Labels = labels
92+
}
93+
8594
return options, nil
8695
}
8796

api/server/router/network/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Backend interface {
1212
GetNetworkByName(idName string) (libnetwork.Network, error)
1313
GetNetworksByID(partialID string) []libnetwork.Network
1414
GetAllNetworks() []libnetwork.Network
15-
CreateNetwork(name, driver string, ipam network.IPAM, options map[string]string, internal bool, enableIPv6 bool) (libnetwork.Network, error)
15+
CreateNetwork(name, driver string, ipam network.IPAM, options map[string]string, labels map[string]string, internal bool, enableIPv6 bool) (libnetwork.Network, error)
1616
ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error
1717
DisconnectContainerFromNetwork(containerName string, network libnetwork.Network, force bool) error
1818
DeleteNetwork(name string) error

api/server/router/network/network_routes.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
9191
warning = fmt.Sprintf("Network with name %s (id : %s) already exists", nw.Name(), nw.ID())
9292
}
9393

94-
nw, err = n.backend.CreateNetwork(create.Name, create.Driver, create.IPAM, create.Options, create.Internal, create.EnableIPv6)
94+
nw, err = n.backend.CreateNetwork(create.Name, create.Driver, create.IPAM, create.Options, create.Labels, create.Internal, create.EnableIPv6)
9595
if err != nil {
9696
return err
9797
}
@@ -163,16 +163,18 @@ func buildNetworkResource(nw libnetwork.Network) *types.NetworkResource {
163163
return r
164164
}
165165

166+
info := nw.Info()
166167
r.Name = nw.Name()
167168
r.ID = nw.ID()
168-
r.Scope = nw.Info().Scope()
169+
r.Scope = info.Scope()
169170
r.Driver = nw.Type()
170-
r.EnableIPv6 = nw.Info().IPv6Enabled()
171-
r.Internal = nw.Info().Internal()
172-
r.Options = nw.Info().DriverOptions()
171+
r.EnableIPv6 = info.IPv6Enabled()
172+
r.Internal = info.Internal()
173+
r.Options = info.DriverOptions()
173174
r.Containers = make(map[string]types.EndpointResource)
174-
buildIpamResources(r, nw)
175-
r.Internal = nw.Info().Internal()
175+
buildIpamResources(r, info)
176+
r.Internal = info.Internal()
177+
r.Labels = info.Labels()
176178

177179
epl := nw.Endpoints()
178180
for _, e := range epl {
@@ -191,10 +193,10 @@ func buildNetworkResource(nw libnetwork.Network) *types.NetworkResource {
191193
return r
192194
}
193195

194-
func buildIpamResources(r *types.NetworkResource, nw libnetwork.Network) {
195-
id, opts, ipv4conf, ipv6conf := nw.Info().IpamConfig()
196+
func buildIpamResources(r *types.NetworkResource, nwInfo libnetwork.NetworkInfo) {
197+
id, opts, ipv4conf, ipv6conf := nwInfo.IpamConfig()
196198

197-
ipv4Info, ipv6Info := nw.Info().IpamInfo()
199+
ipv4Info, ipv6Info := nwInfo.IpamInfo()
198200

199201
r.IPAM.Driver = id
200202

api/server/router/volume/backend.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
type Backend interface {
1111
Volumes(filter string) ([]*types.Volume, []string, error)
1212
VolumeInspect(name string) (*types.Volume, error)
13-
VolumeCreate(name, driverName string,
14-
opts map[string]string) (*types.Volume, error)
13+
VolumeCreate(name, driverName string, opts, labels map[string]string) (*types.Volume, error)
1514
VolumeRm(name string) error
1615
}

api/server/router/volume/volume_routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (v *volumeRouter) postVolumesCreate(ctx context.Context, w http.ResponseWri
4747
return err
4848
}
4949

50-
volume, err := v.backend.VolumeCreate(req.Name, req.Driver, req.DriverOpts)
50+
volume, err := v.backend.VolumeCreate(req.Name, req.Driver, req.DriverOpts, req.Labels)
5151
if err != nil {
5252
return err
5353
}

builder/dockerfile/builder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ func (b *Builder) build(config *types.ImageBuildOptions, context builder.Context
218218

219219
var shortImgID string
220220
for i, n := range b.dockerfile.Children {
221+
// we only want to add labels to the last layer
222+
if i == len(b.dockerfile.Children)-1 {
223+
b.addLabels()
224+
}
221225
select {
222226
case <-b.cancelled:
223227
logrus.Debug("Builder: build cancelled!")

builder/dockerfile/internals.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ import (
3737
"github.com/docker/engine-api/types/strslice"
3838
)
3939

40+
func (b *Builder) addLabels() {
41+
// merge labels
42+
if len(b.options.Labels) > 0 {
43+
logrus.Debugf("[BUILDER] setting labels %v", b.options.Labels)
44+
if b.runConfig.Labels == nil {
45+
b.runConfig.Labels = make(map[string]string)
46+
}
47+
for kL, vL := range b.options.Labels {
48+
b.runConfig.Labels[kL] = vL
49+
}
50+
}
51+
}
52+
4053
func (b *Builder) commit(id string, autoCmd strslice.StrSlice, comment string) error {
4154
if b.disableCommit {
4255
return nil
@@ -45,6 +58,7 @@ func (b *Builder) commit(id string, autoCmd strslice.StrSlice, comment string) e
4558
return fmt.Errorf("Please provide a source image with `from` prior to commit")
4659
}
4760
b.runConfig.Image = b.image
61+
4862
if id == "" {
4963
cmd := b.runConfig.Cmd
5064
if runtime.GOOS != "windows" {
@@ -81,6 +95,7 @@ func (b *Builder) commit(id string, autoCmd strslice.StrSlice, comment string) e
8195
if err != nil {
8296
return err
8397
}
98+
8499
b.image = imageID
85100
return nil
86101
}

0 commit comments

Comments
 (0)