prometheus/discovery/moby/network.go
Julien Pivotto 5bce801a09
Rename discovery/dockerswarm to discovery/moby (#8691)
This makes it clear that the dockerswarm package does more than docker
swarm, but does also docker.

I have picked moby as it is the upstream name: https://mobyproject.org/

There is no user-facing change, except in the case of a bad
configuration. Previously, a user who would have a bad docker sd config
would see an error like:

> field xx not found in type dockerswarm.plain

Now that error would be turned into:

> field xx not found in type moby.plain

While not perfect, it should at not be confusing between docker and
dockerswarm.

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
2021-04-13 09:33:54 +02:00

57 lines
2 KiB
Go

// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package moby
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/prometheus/prometheus/util/strutil"
)
const (
labelNetworkPrefix = "network_"
labelNetworkID = labelNetworkPrefix + "id"
labelNetworkName = labelNetworkPrefix + "name"
labelNetworkScope = labelNetworkPrefix + "scope"
labelNetworkInternal = labelNetworkPrefix + "internal"
labelNetworkIngress = labelNetworkPrefix + "ingress"
labelNetworkLabelPrefix = labelNetworkPrefix + "label_"
)
func getNetworksLabels(ctx context.Context, client *client.Client, labelPrefix string) (map[string]map[string]string, error) {
networks, err := client.NetworkList(ctx, types.NetworkListOptions{})
if err != nil {
return nil, err
}
labels := make(map[string]map[string]string, len(networks))
for _, network := range networks {
labels[network.ID] = map[string]string{
labelPrefix + labelNetworkID: network.ID,
labelPrefix + labelNetworkName: network.Name,
labelPrefix + labelNetworkScope: network.Scope,
labelPrefix + labelNetworkInternal: fmt.Sprintf("%t", network.Internal),
labelPrefix + labelNetworkIngress: fmt.Sprintf("%t", network.Ingress),
}
for k, v := range network.Labels {
ln := strutil.SanitizeLabelName(k)
labels[network.ID][labelPrefix+labelNetworkLabelPrefix+ln] = v
}
}
return labels, nil
}