forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkubeutil.c
More file actions
70 lines (54 loc) · 2.2 KB
/
Copy pathkubeutil.c
File metadata and controls
70 lines (54 loc) · 2.2 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
// 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 2019-present Datadog, Inc.
#include "kubeutil.h"
#include "cgo_free.h"
#include "stringutils.h"
// these must be set by the Agent
static cb_get_connection_info_t cb_get_connection_info = NULL;
// forward declarations
static PyObject *get_connection_info(PyObject *, PyObject *);
static PyMethodDef methods[] = {
{ "get_connection_info", (PyCFunction)get_connection_info, METH_NOARGS, "Get kubelet connection information." },
{ NULL, NULL } // guards
};
static struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, KUBEUTIL_MODULE_NAME, NULL, -1, methods };
PyMODINIT_FUNC PyInit_kubeutil(void)
{
return PyModule_Create(&module_def);
}
void _set_get_connection_info_cb(cb_get_connection_info_t cb)
{
cb_get_connection_info = cb;
}
/*! \fn void get_connection_info(PyObject *self, PyObject *args)
\brief Implements the python method to collect the kubernetes connection information
by calling the corresponding callback.
\param self A PyObject* pointer to the kubeutil module.
\param args A PyObject* pointer to an empty tuple as this method has no input args.
\return a PyObject * pointer to a python dictionary containing the K8s connection info.
This function is callable as the `kubeutil.get_connection_info` python method, the
callback is expected to have been set previously, if not `None` will be returned.
*/
PyObject *get_connection_info(PyObject *self, PyObject *args)
{
char *data = NULL;
// callback must be set
if (cb_get_connection_info == NULL) {
Py_RETURN_NONE;
}
cb_get_connection_info(&data);
// create a new ref
PyObject *conn_info_dict = from_json(data);
// free the memory allocated by the Agent
cgo_free(data);
if (conn_info_dict == NULL || !PyDict_Check(conn_info_dict)) {
// clear error set by `from_json` (if any)
PyErr_Clear();
// create a new ref and drop the other one
Py_XDECREF(conn_info_dict);
return PyDict_New();
}
return conn_info_dict;
}