Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for port mapping in docker hints #31243

Merged
merged 8 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...main[Check the HEAD dif
- Add action_input_type for the .fleet-actions-results {pull}30562[30562]
- Add cronjob metadata by default {pull}30637[30637]
- New option `setup.template.json.data_stream` is added to indicate if the JSON index template is a data stream. {pull}31048[31048]
- Add support for port mapping in docker hints. {pull}31243[31243]

*Auditbeat*

Expand Down
14 changes: 13 additions & 1 deletion libbeat/autodiscover/providers/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package docker

import (
"fmt"
"strconv"
"time"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -274,6 +275,7 @@ func (d *Provider) stopContainer(container *docker.Container, meta *dockerMetada

func (d *Provider) emitContainer(container *docker.Container, meta *dockerMetadata, flag string) {
var host string
var ports common.MapStr
if len(container.IPAddresses) > 0 {
host = container.IPAddresses[0]
}
Expand All @@ -292,16 +294,22 @@ func (d *Provider) emitContainer(container *docker.Container, meta *dockerMetada
}

events = append(events, event)
} else {
ports = common.MapStr{}
for _, port := range container.Ports {
ports[strconv.FormatUint(uint64(port.PrivatePort), 10)] = port.PublicPort
}
}

// Emit container container and port information

for _, port := range container.Ports {
event := bus.Event{
"provider": d.uuid,
"id": container.ID,
flag: true,
"host": host,
"port": port.PrivatePort,
"ports": ports,
"docker": meta.Docker,
"container": meta.Container,
"meta": meta.Metadata,
Expand Down Expand Up @@ -334,6 +342,7 @@ func (d *Provider) publish(events []bus.Event) {
event := bus.Event(common.MapStr(events[0]).Clone())
// Remove the port to avoid ambiguity during debugging
delete(event, "port")
delete(event, "ports")
event["config"] = configs

// Call all appenders to append any extra configuration
Expand All @@ -358,6 +367,9 @@ func (d *Provider) generateHints(event bus.Event) bus.Event {
if port, ok := event["port"]; ok {
e["port"] = port
}
if ports, ok := event["ports"]; ok {
e["ports"] = ports
}
if labels, err := dockerMeta.GetValue("labels"); err == nil {
hints := builder.GenerateHints(labels.(common.MapStr), "", d.config.Prefix)
e["hints"] = hints
Expand Down
16 changes: 15 additions & 1 deletion libbeat/autodiscover/providers/docker/docker_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"

"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"

"github.com/elastic/beats/v7/libbeat/autodiscover/template"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/bus"
Expand Down Expand Up @@ -69,7 +72,17 @@ func TestDockerStart(t *testing.T) {
// Start
cmd := []string{"echo", "Hi!"}
labels := map[string]string{"label": "foo", "label.child": "bar"}
ID, err := d.ContainerStart("busybox:latest", cmd, labels)
hostConfig := &container.HostConfig{
PortBindings: nat.PortMap{
"4140/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "4140",
},
},
},
}
ID, err := d.ContainerStart("busybox:latest", cmd, labels, hostConfig)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -120,6 +133,7 @@ func checkEvent(t *testing.T, listener bus.Listener, id string, start bool) {
assert.NotNil(t, getValue(e, "container.id"))
assert.NotNil(t, getValue(e, "container.name"))
assert.NotNil(t, getValue(e, "host"))
assert.NotNil(t, getValue(e, "ports"))
assert.Equal(t, getValue(e, "docker.container.id"), getValue(e, "meta.container.id"))
assert.Equal(t, getValue(e, "docker.container.name"), getValue(e, "meta.container.name"))
assert.Equal(t, getValue(e, "docker.container.image"), getValue(e, "meta.container.image.name"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestAddDockerMetadata(t *testing.T) {
image := "busybox"
cmd := []string{"sleep", "60"}
labels := map[string]string{"label": "foo"}
id, err := testClient.ContainerStart(image, cmd, labels)
id, err := testClient.ContainerStart(image, cmd, labels, nil)
require.NoError(t, err)
defer testClient.ContainerRemove(id)

Expand Down
4 changes: 2 additions & 2 deletions libbeat/tests/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewClient() (Client, error) {
}

// ContainerStart pulls and starts the given container
func (c Client) ContainerStart(image string, cmd []string, labels map[string]string) (string, error) {
func (c Client) ContainerStart(image string, cmd []string, labels map[string]string, hostConfig *container.HostConfig) (string, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. Consider defining new method to keep backwards compatibility. Though probably nothing is using this method outside of beats.

Suggested change
func (c Client) ContainerStart(image string, cmd []string, labels map[string]string, hostConfig *container.HostConfig) (string, error) {
// ...
type StartOptions struct {
HostConfig *container.HostConfig
}
// ...
func ContainerStart(image string, cmd []string, labels map[string]string) (string, error) {
return ContainerStartWithOptions(image, cmd, labels, StartOptions{})
}
// ...
func (c Client) ContainerStartWithOptions(image string, cmd []string, labels map[string]string, options StartOptions) (string, error) {
...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This test is not correct nevertheless so I will revert this. Unfortunately we don't have proper tests at the moment that I could extend to cover this feature addition.

err := c.imagePull(image)
if err != nil {
return "", err
Expand All @@ -54,7 +54,7 @@ func (c Client) ContainerStart(image string, cmd []string, labels map[string]str
Image: image,
Cmd: cmd,
Labels: labels,
}, nil, nil, nil, "")
}, hostConfig, nil, nil, "")
if err != nil {
return "", errors.Wrap(err, "creating container")
}
Expand Down
3 changes: 3 additions & 0 deletions metricbeat/docs/autodiscover-hints.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ For Kuberentes autodiscover events users can leverage port names as well,
like `http://${data.host}:${data.ports.prometheus}/metrics`.
In the last one we refer to the container port with name `prometheus`.

In order to target one specific exposed port `localhost:{data.ports.8222}` can be used
in template config, where `8222` is the internal container port of the exposed targeted port.

[float]
===== `co.elastic.metrics/metricsets`

Expand Down