Skip to content

Commit

Permalink
Support IPv6 clusters with Container Insights (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
movence authored Dec 20, 2023
1 parent c00ca1f commit 7a53f89
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (

"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/net"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/kubelet"
)

var kubeletNewClientProvider = kubelet.NewClientProvider

type KubeletClient struct {
KubeIP string
Port string
Expand All @@ -26,7 +29,11 @@ func NewKubeletClient(kubeIP string, port string, logger *zap.Logger) (*KubeletC
KubeIP: kubeIP,
}

endpoint := kubeIP + ":" + port
endpoint := kubeIP
if net.IsIPv6String(kubeIP) {
endpoint = "[" + endpoint + "]"
}
endpoint = endpoint + ":" + port

// use service account for authentication
clientConfig := &kubelet.ClientConfig{
Expand All @@ -35,7 +42,7 @@ func NewKubeletClient(kubeIP string, port string, logger *zap.Logger) (*KubeletC
},
}

clientProvider, err := kubelet.NewClientProvider(endpoint, clientConfig, logger)
clientProvider, err := kubeletNewClientProvider(endpoint, clientConfig, logger)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package kubeletutil

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

kube "github.com/open-telemetry/opentelemetry-collector-contrib/internal/kubelet"
)

type mockClientProvider struct {
endpoint string
cfg *kube.ClientConfig
}

func (cp *mockClientProvider) BuildClient() (kube.Client, error) {
return &fakeClient{
endpoint: cp.endpoint,
}, nil
}

type fakeClient struct {
endpoint string
}

func (f *fakeClient) Get(path string) ([]byte, error) {
return []byte(path), nil
}

func TestNewKubeletClient(t *testing.T) {
kubeletNewClientProvider = func(endpoint string, cfg *kube.ClientConfig, logger *zap.Logger) (kube.ClientProvider, error) {
return &mockClientProvider{
endpoint: endpoint,
cfg: cfg,
}, nil
}

tests := []struct {
kubeIP string
port string
wantEndpoint string
}{
{
kubeIP: "0.0.0.0",
port: "8000",
wantEndpoint: "0.0.0.0:8000",
},
{
kubeIP: "2001:db8:3333:4444:5555:6666:7777:8888",
port: "8000",
wantEndpoint: "[2001:db8:3333:4444:5555:6666:7777:8888]:8000",
},
{
kubeIP: "2001:db8:3333:4444:5555:6666:1.2.3.4",
port: "8000",
wantEndpoint: "[2001:db8:3333:4444:5555:6666:1.2.3.4]:8000",
},
}
for _, tt := range tests {
client, err := NewKubeletClient(tt.kubeIP, tt.port, zap.NewNop())
require.NoError(t, err)
assert.Equal(t, client.KubeIP, tt.kubeIP)
fc := (client.restClient).(*fakeClient)
assert.NotNil(t, fc)
assert.Equal(t, fc.endpoint, tt.wantEndpoint)
}
}

0 comments on commit 7a53f89

Please sign in to comment.