-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathkubeutil.go
More file actions
87 lines (72 loc) · 2.37 KB
/
kubeutil.go
File metadata and controls
87 lines (72 loc) · 2.37 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build python && kubelet
package python
import (
"encoding/json"
"errors"
"time"
"github.com/DataDog/datadog-agent/pkg/util/cache"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes/kubelet"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/retry"
)
/*
#include <datadog_agent_rtloader.h>
#cgo !aix,!windows LDFLAGS: -ldatadog-agent-rtloader -ldl
#cgo aix LDFLAGS: -ldl
#cgo windows LDFLAGS: -ldatadog-agent-rtloader -lstdc++ -static
*/
import "C"
var (
kubeletCacheKey = cache.BuildAgentKey("py", "kubeutil", "connection_info")
// for testing
getConnectionsFunc = getConnections
)
func getConnections() map[string]string {
kubeutil, err := kubelet.GetKubeUtil()
if err != nil {
// Connection to the kubelet fail, return the error
log.Errorf("connection to kubelet failed: %v", err)
var e *retry.Error
if errors.As(err, &e) {
return map[string]string{"err": e.Unwrap().Error()}
}
return map[string]string{"err": err.Error()}
}
// At this point, we have valid credentials to get
return kubeutil.GetRawConnectionInfo()
}
// GetKubeletConnectionInfo returns a dict containing url and credentials to connect to the kubelet.
// The dict is empty if the kubelet was not detected. The call to kubeutil is cached for 5 minutes.
// See the documentation of kubelet.GetRawConnectionInfo for dict contents.
//
//export GetKubeletConnectionInfo
func GetKubeletConnectionInfo(payload **C.char) {
var creds string
var ok bool
if cached, hit := cache.Cache.Get(kubeletCacheKey); hit {
log.Debug("cache hit for kubelet connection info")
if creds, ok = cached.(string); !ok {
log.Error("invalid cache format, forcing a cache miss")
creds = ""
}
}
if creds == "" { // Cache miss
log.Debug("cache miss for kubelet connection info")
connections := getConnectionsFunc()
if connections == nil {
return
}
data, err := json.Marshal(connections)
if err != nil {
log.Errorf("could not serialized kubelet connections (%s): %s", connections, err)
return
}
creds = string(data)
cache.Cache.Set(kubeletCacheKey, creds, 1*time.Minute)
}
*payload = TrackedCString(creds)
}