-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
k8s_data_helpers.go
44 lines (38 loc) · 1.19 KB
/
k8s_data_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package k8stest // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8stest"
import (
"context"
"runtime"
"testing"
"time"
"github.com/docker/docker/api/types"
docker "github.com/docker/docker/client"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/labels"
)
func HostEndpoint(t *testing.T) string {
if runtime.GOOS == "darwin" {
return "host.docker.internal"
}
client, err := docker.NewClientWithOpts(docker.FromEnv)
require.NoError(t, err)
client.NegotiateAPIVersion(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
network, err := client.NetworkInspect(ctx, "kind", types.NetworkInspectOptions{})
require.NoError(t, err)
for _, ipam := range network.IPAM.Config {
return ipam.Gateway
}
require.Fail(t, "failed to find host endpoint")
return ""
}
func SelectorFromMap(labelMap map[string]any) labels.Selector {
labelStringMap := make(map[string]string)
for key, value := range labelMap {
labelStringMap[key] = value.(string)
}
labelSet := labels.Set(labelStringMap)
return labelSet.AsSelector()
}