Skip to content

Commit

Permalink
Merge pull request aerokube#351 from vania-pooh/master
Browse files Browse the repository at this point in the history
Added ability to pass custom labels to containers (fixes aerokube#350)
  • Loading branch information
aandryashin committed Feb 14, 2018
2 parents f262404 + a9a7955 commit 4cbf6cb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Browser struct {
Env []string `json:"env,omitempty"`
Hosts []string `json:"hosts,omitempty"`
ShmSize int64 `json:"shmSize,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}

// Versions configuration
Expand Down
7 changes: 5 additions & 2 deletions docs/browsers-configuration-file.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Selenoid requires a simple JSON configuration file of the following format:
"port": "4444", <6>
"tmpfs": {"/tmp": "size=512m"}, <7>
"path" : "/wd/hub", <8>
"volumes" : ["/to:/from:ro"], <9>
"volumes" : ["/from:/to:ro"], <9>
"env" : ["TZ=Europe/Moscow"], <10>
"hosts" : ["example.com:192.168.0.1"], <11>
"shmSize" : 268435456, <12>
Expand Down Expand Up @@ -116,9 +116,10 @@ Selenoid proxies connections to either Selenium server or standalone driver bina
"port": ""
"tmpfs": {"/tmp": "size=512m", "/var": "size=128m"},
"path" : "",
"volumes": ["/to:/from", "/another:/one:ro"],
"volumes": ["/from:/to", "/another:/one:ro"],
"env" : ["TZ=Europe/Moscow", "ONE_MORE_VARIABLE=itsValue"],
"hosts" : ["one.example.com:192.168.0.1", "two.example.com:192.168.0.2"],
"labels" : {"component": "frontend", "project": "my-project"},
"shmSize" : 268435456,
},
----
Expand All @@ -142,6 +143,8 @@ We recommend to use our https://github.com/aerokube/cm[configuration tool] to av

* *hosts* (_optional_) - This field allows to add custom `/etc/hosts` entries to running container. Specified as an array of `hostname:ip` pairs.

* *labels* (_optional_) - This field allows to add custom labels to running container. Specified as an object of `"key": "value"` pairs.

* *shmSize* (_optional_) - Use it to override shared memory size for browser container.

=== Syncing Browser Images from Existing File
Expand Down
11 changes: 11 additions & 0 deletions docs/special-capabilities.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ hostsEntries: "example.com:192.168.0.1,test.com:192.168.0.2"
Entries will be inserted to `/etc/hosts` before entries from browsers configuration file.
Thus entries from capabilities override entries from configuration file if some hosts are equal.

=== Container Labels: labels

In big clusters you may want to pass additional metadata to every browser session: environment, VCS revision, build number and so on. These labels can be then used to enrich session logs and send them to a centralized log storage. Later this metadata can be used for more efficient search through logs.

.Type: string, format: <key>:<value> (comma-separated)
----
labels: "environment:testing,build-number:14353"
----

Labels from this capability override labels from browsers configuration file. When `name` capability is specified - it is automatically added as a label to container.

=== Specifying Capabilities via Protocol Extensions

Some Selenium clients allow passing only a limited number of capabilities specified in https://w3c.github.io/webdriver/webdriver-spec.html[WebDriver specification]. For such cases Selenoid supports reading capabilities using https://w3c.github.io/webdriver/webdriver-spec.html#protocol-extensions[WebDriver protocol extensions] feature. The following two examples deliver the same result. Usually capabilities are passed like this:
Expand Down
35 changes: 34 additions & 1 deletion service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

const (
comma = ","
colon = ":"
sysAdmin = "SYS_ADMIN"
overrideVideoOutputDir = "OVERRIDE_VIDEO_OUTPUT_DIR"
)
Expand Down Expand Up @@ -85,6 +86,7 @@ func (d *Docker) StartWithCancel() (*StartedService, error) {
Image: image.(string),
Env: env,
ExposedPorts: portConfig.ExposedPorts,
Labels: getLabels(d.Service, d.Caps),
},
&hostConfig,
&network.NetworkingConfig{}, "")
Expand Down Expand Up @@ -174,13 +176,21 @@ func getPortConfig(service *config.Browser, caps session.Caps, env Environment)
ExposedPorts: exposedPorts}, nil
}

const (
tag = "tag"
labels = "labels"
)

func getLogConfig(logConfig ctr.LogConfig, caps session.Caps) ctr.LogConfig {
if logConfig.Config != nil {
const tag = "tag"
_, ok := logConfig.Config[tag]
if caps.TestName != "" && !ok {
logConfig.Config[tag] = caps.TestName
}
_, ok = logConfig.Config[labels]
if caps.Labels != "" && !ok {
logConfig.Config[labels] = caps.Labels
}
}
return logConfig
}
Expand Down Expand Up @@ -230,6 +240,29 @@ func getExtraHosts(service *config.Browser, caps session.Caps) []string {
return extraHosts
}

func getLabels(service *config.Browser, caps session.Caps) map[string]string {
labels := make(map[string]string)
if caps.TestName != "" {
labels["name"] = caps.TestName
}
for k, v := range service.Labels {
labels[k] = v
}
if caps.Labels != "" {
for _, lbl := range strings.Split(caps.Labels, comma) {
kv := strings.SplitN(lbl, colon, 2)
if len(kv) == 2 {
key := kv[0]
value := kv[1]
labels[key] = value
} else {
labels[lbl] = ""
}
}
}
return labels
}

func getHostPort(env Environment, service *config.Browser, caps session.Caps, stat types.ContainerJSON, selenium nat.Port, vnc nat.Port) (string, string) {
seleniumHostPort, vncHostPort := "", ""
if env.IP == "" {
Expand Down
2 changes: 2 additions & 0 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func testConfig(env *service.Environment) *config.Config {
Tmpfs: map[string]string{"/tmp": "size=128m"},
Port: p,
Volumes: []string{"/test:/test"},
Labels: map[string]string{"key": "value"},
},
},
}
Expand Down Expand Up @@ -237,6 +238,7 @@ func createDockerStarter(t *testing.T, env *service.Environment, cfg *config.Con
VideoScreenSize: "1024x768",
VideoFrameRate: 25,
HostsEntries: "example.com:192.168.0.1,test.com:192.168.0.2",
Labels: "label1:some-value,label2",
ApplicationContainers: "one,two",
TimeZone: "Europe/Moscow",
ContainerHostname: "some-hostname",
Expand Down
1 change: 1 addition & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Caps struct {
ContainerHostname string `json:"containerHostname"`
ApplicationContainers string `json:"applicationContainers"`
HostsEntries string `json:"hostsEntries"`
Labels string `json:"labels"`
ExtensionCapabilities map[string]interface{} `json:"selenoid:options"`
}

Expand Down

0 comments on commit 4cbf6cb

Please sign in to comment.