Skip to content

Commit

Permalink
Upgrad elastic-agent-system-metrics to v0.10.7. (#40397)
Browse files Browse the repository at this point in the history
* Upgrad elastic-agent-system-metrics to v0.10.7.

Upgrades Docker to v26.1.4

* Update autodiscover to v0.8.0

* Fix breaking API changes in new docker version.

* Fix more breaking imports.

* Remove docker ioutils deprecated function.

* Remove another instance of ioutils.TempDir.

* Run mage fmt

* Update elastic-agent-system-metrics to v0.10.8

* Update elastic-agent-autodiscover to v0.8.1.

* Switch type usages

* Add work around for duplicate definition of my_strlen.c

* Fix typo.

---------

Co-authored-by: Pierre HILBERT <[email protected]>
(cherry picked from commit 3c65545)

# Conflicts:
#	NOTICE.txt
#	go.mod
#	go.sum
#	libbeat/tests/compose/wrapper.go
#	libbeat/tests/docker/docker.go
#	metricbeat/module/docker/image/data.go
#	metricbeat/module/docker/info/data.go
#	x-pack/agentbeat/magefile.go
  • Loading branch information
cmacknz authored and mergify[bot] committed Jul 31, 2024
1 parent c60e553 commit bc15fb7
Show file tree
Hide file tree
Showing 15 changed files with 5,823 additions and 2,191 deletions.
7,181 changes: 5,018 additions & 2,163 deletions NOTICE.txt

Large diffs are not rendered by default.

209 changes: 207 additions & 2 deletions go.mod

Large diffs are not rendered by default.

296 changes: 296 additions & 0 deletions go.sum

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions libbeat/autodiscover/template/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"path/filepath"
"testing"

"github.com/docker/docker/pkg/ioutils"
"github.com/stretchr/testify/assert"

"github.com/elastic/beats/v7/libbeat/common"
Expand Down Expand Up @@ -331,7 +330,7 @@ func createAnExistingKeystore(path string, secret string) keystore.Keystore {

// create a temporary file on disk to save the keystore.
func getTemporaryKeystoreFile() string {
path, err := ioutils.TempDir("", "testing")
path, err := os.MkdirTemp("", "testing")
if err != nil {
panic(err)
}
Expand Down
17 changes: 14 additions & 3 deletions libbeat/tests/compose/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/pkg/errors"
Expand Down Expand Up @@ -319,7 +320,7 @@ func (d *wrapperDriver) containers(ctx context.Context, projectFilter Filter, fi

var containers []types.Container
for _, f := range serviceFilters {
list, err := d.client.ContainerList(ctx, types.ContainerListOptions{
list, err := d.client.ContainerList(ctx, container.ListOptions{
All: true,
Filters: f,
})
Expand All @@ -343,10 +344,20 @@ func (d *wrapperDriver) containers(ctx context.Context, projectFilter Filter, fi
// running containers.
// It kills all containers not related to services in `except`.
func (d *wrapperDriver) KillOld(ctx context.Context, except []string) error {
list, err := d.client.ContainerList(ctx, types.ContainerListOptions{All: true})
list, err := d.client.ContainerList(ctx, container.ListOptions{All: true})
if err != nil {
return errors.Wrap(err, "listing containers to be killed")
}
<<<<<<< HEAD
=======

rmOpts := container.RemoveOptions{
RemoveVolumes: true,
Force: true,
RemoveLinks: true,
}

>>>>>>> 3c65545078 (Upgrad elastic-agent-system-metrics to v0.10.7. (#40397))
for _, container := range list {
container := wrapperContainer{info: container}
serviceName, ok := container.info.Labels[labelComposeService]
Expand Down Expand Up @@ -374,7 +385,7 @@ func (d *wrapperDriver) serviceNames(ctx context.Context) ([]string, error) {

// Inspect a container.
func (d *wrapperDriver) Inspect(ctx context.Context, serviceName string) (string, error) {
list, err := d.client.ContainerList(ctx, types.ContainerListOptions{All: true})
list, err := d.client.ContainerList(ctx, container.ListOptions{All: true})
if err != nil {
return "", errors.Wrap(err, "listing containers to be inspected")
}
Expand Down
22 changes: 18 additions & 4 deletions libbeat/tests/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"

"github.com/elastic/beats/v7/libbeat/common/docker"
Expand Down Expand Up @@ -59,33 +60,46 @@ func (c Client) ContainerStart(image string, cmd []string, labels map[string]str
return "", errors.Wrap(err, "creating container")
}

<<<<<<< HEAD
if err := c.cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return "", errors.Wrap(err, "starting container")
=======
if err := c.cli.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
return "", fmt.Errorf("starting container: %w", err)
>>>>>>> 3c65545078 (Upgrad elastic-agent-system-metrics to v0.10.7. (#40397))
}

return resp.ID, nil
}

// imagePull pulls an image
func (c Client) imagePull(image string) (err error) {
func (c Client) imagePull(img string) (err error) {
ctx := context.Background()
_, _, err = c.cli.ImageInspectWithRaw(ctx, image)
_, _, err = c.cli.ImageInspectWithRaw(ctx, img)
if err == nil {
// Image already available, do nothing
return nil
}
for retry := 0; retry < 3; retry++ {
err = func() error {
respBody, err := c.cli.ImagePull(ctx, image, types.ImagePullOptions{})
respBody, err := c.cli.ImagePull(ctx, img, image.PullOptions{})
if err != nil {
<<<<<<< HEAD
return errors.Wrapf(err, "pullling image %s", image)
=======
return fmt.Errorf("pullling image %s: %w", img, err)
>>>>>>> 3c65545078 (Upgrad elastic-agent-system-metrics to v0.10.7. (#40397))
}
defer respBody.Close()

// Read all the response, to be sure that the pull has finished before returning.
_, err = io.Copy(ioutil.Discard, respBody)
if err != nil {
<<<<<<< HEAD
return errors.Wrapf(err, "reading response for image %s", image)
=======
return fmt.Errorf("reading response for image %s: %w", img, err)
>>>>>>> 3c65545078 (Upgrad elastic-agent-system-metrics to v0.10.7. (#40397))
}
return nil
}()
Expand Down Expand Up @@ -123,7 +137,7 @@ func (c Client) ContainerKill(ID string) error {
// ContainerRemove kills and removes the given container
func (c Client) ContainerRemove(ID string) error {
ctx := context.Background()
return c.cli.ContainerRemove(ctx, ID, types.ContainerRemoveOptions{
return c.cli.ContainerRemove(ctx, ID, container.RemoveOptions{
RemoveVolumes: true,
Force: true,
})
Expand Down
3 changes: 1 addition & 2 deletions metricbeat/autodiscover/builder/hints/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"sort"
"testing"

"github.com/docker/docker/pkg/ioutils"
"github.com/stretchr/testify/assert"

"github.com/elastic/beats/v7/libbeat/common"
Expand Down Expand Up @@ -760,7 +759,7 @@ func createAnExistingKeystore(path string, secret string) keystore.Keystore {

// create a temporary file on disk to save the keystore.
func getTemporaryKeystoreFile() string {
path, err := ioutils.TempDir("", "testing")
path, err := os.MkdirTemp("", "testing")
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions metricbeat/module/docker/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package container
import (
"context"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/pkg/errors"

Expand Down Expand Up @@ -68,7 +68,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// This is based on https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-containers.
func (m *MetricSet) Fetch(ctx context.Context, r mb.ReporterV2) error {
// Fetch a list of all containers.
containers, err := m.dockerClient.ContainerList(ctx, types.ContainerListOptions{})
containers, err := m.dockerClient.ContainerList(ctx, container.ListOptions{})
if err != nil {
return errors.Wrap(err, "failed to get docker containers list")
}
Expand Down
3 changes: 2 additions & 1 deletion metricbeat/module/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"

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

Expand Down Expand Up @@ -103,7 +104,7 @@ func NewDockerClient(endpoint string, config Config) (*client.Client, error) {
func FetchStats(client *client.Client, timeout time.Duration) ([]Stat, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
containers, err := client.ContainerList(ctx, types.ContainerListOptions{})
containers, err := client.ContainerList(ctx, container.ListOptions{})
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions metricbeat/module/docker/event/event_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"testing"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"

"github.com/elastic/beats/v7/auditbeat/core"
Expand Down Expand Up @@ -77,7 +77,7 @@ func createEvent(t *testing.T) {
}
defer c.Close()

reader, err := c.ImagePull(context.Background(), "busybox", types.ImagePullOptions{})
reader, err := c.ImagePull(context.Background(), "busybox", image.PullOptions{})
if err != nil {
t.Fatal(err)
}
Expand All @@ -92,7 +92,7 @@ func createEvent(t *testing.T) {
t.Fatal(err)
}

c.ContainerRemove(context.Background(), resp.ID, types.ContainerRemoveOptions{})
c.ContainerRemove(context.Background(), resp.ID, container.RemoveOptions{})
}

func getConfig() map[string]interface{} {
Expand Down
4 changes: 2 additions & 2 deletions metricbeat/module/docker/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package healthcheck
import (
"context"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/pkg/errors"

Expand Down Expand Up @@ -67,7 +67,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// This is based on https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-containers.
func (m *MetricSet) Fetch(r mb.ReporterV2) error {
// Fetch a list of all containers.
containers, err := m.dockerClient.ContainerList(context.TODO(), types.ContainerListOptions{})
containers, err := m.dockerClient.ContainerList(context.TODO(), container.ListOptions{})
if err != nil {
return errors.Wrap(err, "failed to get docker containers list")
}
Expand Down
26 changes: 22 additions & 4 deletions metricbeat/module/docker/image/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,26 @@ package image
import (
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/docker"
)

<<<<<<< HEAD
func eventsMapping(imagesList []types.ImageSummary, dedot bool) []common.MapStr {
events := []common.MapStr{}
=======
func eventsMapping(imagesList []image.Summary, dedot bool) []mapstr.M {
events := []mapstr.M{}
>>>>>>> 3c65545078 (Upgrad elastic-agent-system-metrics to v0.10.7. (#40397))
for _, image := range imagesList {
events = append(events, eventMapping(&image, dedot))
}
return events
}

<<<<<<< HEAD
func eventMapping(image *types.ImageSummary, dedot bool) common.MapStr {
event := common.MapStr{
"id": common.MapStr{
Expand All @@ -44,11 +50,23 @@ func eventMapping(image *types.ImageSummary, dedot bool) common.MapStr {
"size": common.MapStr{
"regular": image.Size,
"virtual": image.VirtualSize,
=======
func eventMapping(img *image.Summary, dedot bool) mapstr.M {
event := mapstr.M{
"id": mapstr.M{
"current": img.ID,
"parent": img.ParentID,
},
"tags": image.RepoTags,
"created": common.Time(time.Unix(img.Created, 0)),
"size": mapstr.M{
"regular": img.Size,
"virtual": img.VirtualSize,
>>>>>>> 3c65545078 (Upgrad elastic-agent-system-metrics to v0.10.7. (#40397))
},
"tags": img.RepoTags,
}
if len(image.Labels) > 0 {
labels := docker.DeDotLabels(image.Labels, dedot)
if len(img.Labels) > 0 {
labels := docker.DeDotLabels(img.Labels, dedot)
event["labels"] = labels
}
return event
Expand Down
7 changes: 6 additions & 1 deletion metricbeat/module/docker/info/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
package info

import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/system"

"github.com/elastic/beats/v7/libbeat/common"
)

<<<<<<< HEAD
func eventMapping(info *types.Info) common.MapStr {
event := common.MapStr{
=======
func eventMapping(info *system.Info) mapstr.M {
event := mapstr.M{
>>>>>>> 3c65545078 (Upgrad elastic-agent-system-metrics to v0.10.7. (#40397))
"id": info.ID,
"containers": common.MapStr{
"total": info.Containers,
Expand Down
Loading

0 comments on commit bc15fb7

Please sign in to comment.