Skip to content

Commit

Permalink
Merge pull request #469 from forta-protocol/fix-docker-client-filter-bug
Browse files Browse the repository at this point in the history
Fix Docker client filter bug
  • Loading branch information
canercidam authored Mar 31, 2022
2 parents 143d675 + 021101b commit e15ddbd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
42 changes: 28 additions & 14 deletions clients/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ const (
DockerLabelFortaSettingsAgentLogsEnable = "network.forta.settings.agent-logs.enable"
)

var defaultLabels = map[string]string{DockerLabelForta: "true"}
type dockerLabel struct {
Name string
Value string
}

var defaultLabels = []dockerLabel{
{Name: DockerLabelForta, Value: "true"},
}

// Client errors
var (
Expand Down Expand Up @@ -96,7 +103,7 @@ type dockerClient struct {
workers *workers.Group
username string
password string
labels map[string]string
labels []dockerLabel
}

func (cfg DockerContainerConfig) envVars() []string {
Expand Down Expand Up @@ -186,7 +193,7 @@ func (d *dockerClient) createNetwork(ctx context.Context, name string, internal
}

resp, err := d.cli.NetworkCreate(ctx, name, types.NetworkCreate{
Labels: d.labels,
Labels: labelsToMap(d.labels),
Internal: internal,
})
if err != nil {
Expand Down Expand Up @@ -420,7 +427,7 @@ func (d *dockerClient) StartContainer(ctx context.Context, config DockerContaine
cntCfg := &container.Config{
Image: config.Image,
Env: config.envVars(),
Labels: d.labels,
Labels: labelsToMap(d.labels),
}
// add custom labels
for k, v := range config.Labels {
Expand Down Expand Up @@ -681,22 +688,29 @@ func (d *dockerClient) GetContainerLogs(ctx context.Context, containerID, tail s

func (d *dockerClient) labelFilter() filters.Args {
filter := filters.NewArgs()
for k, v := range d.labels {
filter.Add("label", fmt.Sprintf("%s=%s", k, v))
for _, label := range d.labels {
filter.Add("label", fmt.Sprintf("%s=%s", label.Name, label.Value))
}
return filter
}

func initLabels(name string) map[string]string {
result := make(map[string]string)
for k, v := range defaultLabels {
result[k] = v
}
func initLabels(name string) []dockerLabel {
if len(name) == 0 {
return result
return defaultLabels
}

return append(defaultLabels, dockerLabel{
Name: DockerLabelFortaSupervisor,
Value: name,
})
}

func labelsToMap(labels []dockerLabel) map[string]string {
m := make(map[string]string)
for _, label := range labels {
m[label.Name] = label.Value
}
result[DockerLabelFortaSupervisor] = name
return result
return m
}

// NewDockerClient creates a new docker client
Expand Down
3 changes: 3 additions & 0 deletions cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func checkScannerState() error {
ENSAddress: cfg.ENSConfig.ContractAddress,
Name: "registry-client",
})
if err != nil {
return fmt.Errorf("failed to create registry client: %v", err)
}
scanner, err := registry.GetScanner(scannerAddressStr)
if err != nil {
return fmt.Errorf("failed to check scanner state: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type RegistryConfig struct {
JsonRpc JsonRpcConfig `yaml:"jsonRpc" json:"jsonRpc" default:"{\"url\": \"https://polygon-rpc.com\"}"`
IPFS IPFSConfig `yaml:"ipfs" json:"ipfs"`
ContractAddress string `yaml:"contractAddress" json:"contractAddress" validate:"eth_addr"`
ContainerRegistry string `yaml:"containerRegistry" json:"containerRegistry" validate:"hostname_port" default:"disco.forta.network" `
ContainerRegistry string `yaml:"containerRegistry" json:"containerRegistry" validate:"hostname|hostname_port" default:"disco.forta.network" `
Username string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
Disable bool `yaml:"disable" json:"disable"` // for testing situations
Expand Down

0 comments on commit e15ddbd

Please sign in to comment.