-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add kubeutil python module to support kubelet check (#1146)
- Loading branch information
Showing
19 changed files
with
374 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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 2018 Datadog, Inc. | ||
|
||
// +build cpython,kubelet | ||
|
||
#include "kubeutil.h" | ||
|
||
// Functions | ||
PyObject* GetKubeletConnectionInfo(); | ||
|
||
static PyMethodDef kubeutilMethods[] = { | ||
{"get_connection_info", GetKubeletConnectionInfo, METH_NOARGS, "Get kubelet connection information."}, | ||
{NULL, NULL} | ||
}; | ||
|
||
void initkubeutil() | ||
{ | ||
PyGILState_STATE gstate; | ||
gstate = PyGILState_Ensure(); | ||
|
||
PyObject *ku = Py_InitModule("kubeutil", kubeutilMethods); | ||
|
||
PyGILState_Release(gstate); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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 2018 Datadog, Inc. | ||
|
||
// +build cpython,kubelet | ||
|
||
package py | ||
|
||
import ( | ||
"time" | ||
"unsafe" | ||
|
||
log "github.com/cihub/seelog" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/util/cache" | ||
"github.com/DataDog/datadog-agent/pkg/util/kubernetes/kubelet" | ||
) | ||
|
||
// #cgo pkg-config: python-2.7 | ||
// #cgo linux CFLAGS: -std=gnu99 | ||
// #include "api.h" | ||
// #include "kubeutil.h" | ||
import "C" | ||
|
||
var kubeletCacheKey = cache.BuildAgentKey("py", "kubeutil", "connection_info") | ||
|
||
// 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() *C.PyObject { | ||
var creds map[string]string | ||
var ok bool | ||
dict := C.PyDict_New() | ||
|
||
if cached, hit := cache.Cache.Get(kubeletCacheKey); hit { | ||
creds, ok = cached.(map[string]string) | ||
if !ok { | ||
log.Errorf("invalid cache format, forcing a cache miss") | ||
creds = nil | ||
} | ||
} | ||
|
||
if creds == nil { // Cache miss | ||
kubeutil, err := kubelet.GetKubeUtil() | ||
if err != nil { | ||
// Connection to the kubelet fail, return empty dict | ||
return dict | ||
} | ||
// At this point, we have valid credentials to get | ||
creds = kubeutil.GetRawConnectionInfo() | ||
cache.Cache.Set(kubeletCacheKey, creds, 5*time.Minute) | ||
} | ||
|
||
for k, v := range creds { | ||
cKey := C.CString(k) | ||
pyKey := C.PyString_FromString(cKey) | ||
defer C.Py_DecRef(pyKey) | ||
C.free(unsafe.Pointer(cKey)) | ||
|
||
cVal := C.CString(v) | ||
pyVal := C.PyString_FromString(cVal) | ||
defer C.Py_DecRef(pyVal) | ||
C.free(unsafe.Pointer(cVal)) | ||
|
||
C.PyDict_SetItem(dict, pyKey, pyVal) | ||
} | ||
return dict | ||
} | ||
|
||
func initkubeutil() { | ||
C.initkubeutil() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 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 2018 Datadog, Inc. | ||
|
||
// +build cpython,kubelet | ||
|
||
#ifndef KUBEUTIL_HEADER | ||
#define KUBEUTIL_HEADER | ||
|
||
#include <Python.h> | ||
|
||
void initkubeutil(); | ||
|
||
#endif /* KUBEUTIL_HEADER */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// 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 2018 Datadog, Inc. | ||
|
||
// +build cpython,!kubelet | ||
|
||
package py | ||
|
||
// Stub | ||
func initkubeutil() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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 2018 Datadog, Inc. | ||
|
||
// +build cpython,kubelet | ||
|
||
// NOTICE: See TestMain function in `utils_test.go` for Python initialization | ||
package py | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/config" | ||
"github.com/DataDog/datadog-agent/pkg/util/cache" | ||
"github.com/DataDog/datadog-agent/pkg/util/kubernetes/kubelet" | ||
) | ||
|
||
func TestGetKubeletConnectionInfoNotFound(t *testing.T) { | ||
config.Datadog.Set("kubernetes_http_kubelet_port", 0) | ||
config.Datadog.Set("kubernetes_https_kubelet_port", 0) | ||
kubelet.ResetGlobalKubeUtil() | ||
cache.Cache.Delete(kubeletCacheKey) | ||
|
||
check, _ := getCheckInstance("testkubeutil", "TestCheck") | ||
err := check.Run() | ||
assert.Nil(t, err) | ||
|
||
warnings := check.GetWarnings() | ||
require.Len(t, warnings, 1) | ||
assert.Equal(t, "Kubelet not found", warnings[0].Error()) | ||
} | ||
|
||
func TestGetKubeletConnectionInfoFromCache(t *testing.T) { | ||
dummyCreds := map[string]string{ | ||
"url": "https://10.0.0.1:10250", | ||
} | ||
cache.Cache.Set(kubeletCacheKey, dummyCreds, 5*time.Minute) | ||
|
||
check, _ := getCheckInstance("testkubeutil", "TestCheck") | ||
err := check.Run() | ||
assert.Nil(t, err) | ||
|
||
warnings := check.GetWarnings() | ||
require.Len(t, warnings, 1) | ||
assert.Equal(t, "Found kubelet at https://10.0.0.1:10250", warnings[0].Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 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 2018 Datadog, Inc. | ||
|
||
from checks import AgentCheck | ||
from kubeutil import get_connection_info | ||
|
||
|
||
class TestCheck(AgentCheck): | ||
def check(self, instance): | ||
creds = get_connection_info() | ||
if creds.get("url"): | ||
self.warning("Found kubelet at " + creds.get("url")) | ||
else: | ||
self.warning("Kubelet not found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.