mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
2.0 KiB
57 lines
2.0 KiB
4 years ago
|
// 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.
|
||
|
|
||
4 years ago
|
package moby
|
||
4 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
|
||
|
"github.com/docker/docker/api/types"
|
||
4 years ago
|
"github.com/docker/docker/client"
|
||
4 years ago
|
"github.com/prometheus/prometheus/util/strutil"
|
||
|
)
|
||
|
|
||
|
const (
|
||
4 years ago
|
labelNetworkPrefix = "network_"
|
||
|
labelNetworkID = labelNetworkPrefix + "id"
|
||
|
labelNetworkName = labelNetworkPrefix + "name"
|
||
|
labelNetworkScope = labelNetworkPrefix + "scope"
|
||
|
labelNetworkInternal = labelNetworkPrefix + "internal"
|
||
|
labelNetworkIngress = labelNetworkPrefix + "ingress"
|
||
|
labelNetworkLabelPrefix = labelNetworkPrefix + "label_"
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
func getNetworksLabels(ctx context.Context, client *client.Client, labelPrefix string) (map[string]map[string]string, error) {
|
||
|
networks, err := client.NetworkList(ctx, types.NetworkListOptions{})
|
||
4 years ago
|
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{
|
||
4 years ago
|
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),
|
||
4 years ago
|
}
|
||
|
for k, v := range network.Labels {
|
||
|
ln := strutil.SanitizeLabelName(k)
|
||
4 years ago
|
labels[network.ID][labelPrefix+labelNetworkLabelPrefix+ln] = v
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return labels, nil
|
||
|
}
|