Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #411 from vania-pooh/master
Browse files Browse the repository at this point in the history
Using map for Docker labels everywhere
  • Loading branch information
aandryashin authored Apr 1, 2018
2 parents e707a8b + 5ea50e4 commit 705752f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
4 changes: 2 additions & 2 deletions docs/special-capabilities.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ Thus entries from capabilities override entries from configuration file if some

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: array, format: <key>=<value>
.Type: map, format: "<key>": "<value>"
----
labels: ["environment=testing", "build-number=14353"]
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.
Expand Down
19 changes: 7 additions & 12 deletions service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
)

const (
comma = ","
equality = "="
sysAdmin = "SYS_ADMIN"
overrideVideoOutputDir = "OVERRIDE_VIDEO_OUTPUT_DIR"
vncPort = "5900"
Expand Down Expand Up @@ -201,7 +199,11 @@ func getLogConfig(logConfig ctr.LogConfig, caps session.Caps) ctr.LogConfig {
}
_, ok = logConfig.Config[labels]
if len(caps.Labels) > 0 && !ok {
logConfig.Config[labels] = strings.Join(caps.Labels, comma)
joinedLabels := []string{}
for k, v := range caps.Labels {
joinedLabels = append(joinedLabels, fmt.Sprintf("%s=%s", k, v))
}
logConfig.Config[labels] = strings.Join(joinedLabels, ",")
}
}
return logConfig
Expand Down Expand Up @@ -262,15 +264,8 @@ func getLabels(service *config.Browser, caps session.Caps) map[string]string {
labels[k] = v
}
if len(caps.Labels) > 0 {
for _, lbl := range caps.Labels {
kv := strings.SplitN(lbl, equality, 2)
if len(kv) == 2 {
key := kv[0]
value := kv[1]
labels[key] = value
} else {
labels[lbl] = ""
}
for k, v := range caps.Labels {
labels[k] = v
}
}
return labels
Expand Down
22 changes: 11 additions & 11 deletions service_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package main

import (
"bytes"
"fmt"
. "github.com/aandryashin/matchers"
"github.com/aerokube/selenoid/config"
"github.com/aerokube/selenoid/service"
"github.com/aerokube/selenoid/session"
"github.com/aerokube/util"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"golang.org/x/net/websocket"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"sync"
"testing"
"time"
"golang.org/x/net/websocket"
"github.com/aerokube/util"
"bytes"
"net"
"io"
)

var (
Expand Down Expand Up @@ -264,7 +264,7 @@ func createDockerStarter(t *testing.T, env *service.Environment, cfg *config.Con
VideoFrameRate: 25,
Env: []string{"LANG=ru_RU.UTF-8", "LANGUAGE=ru:en"},
HostsEntries: []string{"example.com:192.168.0.1", "test.com:192.168.0.2"},
Labels: []string{"label1=some-value", "label2"},
Labels: map[string]string{"label1": "some-value", "label2": ""},
ApplicationContainers: []string{"one", "two"},
TimeZone: "Europe/Moscow",
ContainerHostname: "some-hostname",
Expand Down Expand Up @@ -327,8 +327,8 @@ func TestGetVNC(t *testing.T) {

srv := httptest.NewServer(handler())
defer srv.Close()
testTcpServer := testTCPServer("test-data")

testTcpServer := testTCPServer("test-data")
sessions.Put("test-session", &session.Session{
VNC: testTcpServer.Addr().String(),
})
Expand All @@ -354,7 +354,7 @@ func testTCPServer(data string) net.Listener {
return l
}

func readDataFromWebSocket(t * testing.T, wsURL string) string {
func readDataFromWebSocket(t *testing.T, wsURL string) string {
ws, err := websocket.Dial(wsURL, "", "http://localhost")
AssertThat(t, err, Is{nil})

Expand All @@ -369,10 +369,10 @@ func TestGetLogs(t *testing.T) {

srv := httptest.NewServer(handler())
defer srv.Close()

sessions.Put("test-session", &session.Session{
Container: &session.Container{
ID: "e90e34656806",
ID: "e90e34656806",
IPAddress: "127.0.0.1",
},
})
Expand Down
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Caps struct {
Env []string `json:"env"`
ApplicationContainers []string `json:"applicationContainers"`
HostsEntries []string `json:"hostsEntries"`
Labels []string `json:"labels"`
Labels map[string]string `json:"labels"`
ExtensionCapabilities map[string]interface{} `json:"selenoid:options"`
}

Expand Down

0 comments on commit 705752f

Please sign in to comment.