3535#include "target.h"
3636#include "target_kubernetes.h"
3737
38+ /*
39+ * CONTAINER_ID_REGEX is the regex used to extract the Docker container id from a cgroup path.
40+ * CONTAINER_ID_REGEX_EXPECTED_MATCHES is the number of matches expected from the regex. (num groups + 1)
41+ */
42+ #define CONTAINER_ID_REGEX \
43+ "perf_event/kubepods/" \
44+ "(besteffort/|burstable/|)" \
45+ "(pod[a-zA-Z0-9][a-zA-Z0-9.-]+)/" /* Pod ID */ \
46+ "([a-f0-9]{64})" /* Container ID */ \
47+ "(/[a-zA-Z0-9][a-zA-Z0-9.-]+|)" /* Resource group */
48+ #define CONTAINER_ID_REGEX_EXPECTED_MATCHES 5
49+
50+ /*
51+ * CONTAINER_NAME_REGEX is the regex used to extract the name of the Docker container from its json configuration file.
52+ * CONTAINER_NAME_REGEX_EXPECTED_MATCHES is the number of matches expected from the regex. (num groups + 1)
53+ */
54+ #define CONTAINER_NAME_REGEX "\"Name\":\"/([a-zA-Z0-9][a-zA-Z0-9_.-]+)\""
55+ #define CONTAINER_NAME_REGEX_EXPECTED_MATCHES 2
56+
57+
3858int
3959target_kubernetes_validate (const char * cgroup_path )
4060{
@@ -44,7 +64,7 @@ target_kubernetes_validate(const char *cgroup_path)
4464 if (!cgroup_path )
4565 return false;
4666
47- if (!regcomp (& re , TARGET_KUBERNETES_EXTRACT_CONTAINER_ID_REGEX , REG_EXTENDED | REG_NEWLINE | REG_NOSUB )) {
67+ if (!regcomp (& re , CONTAINER_ID_REGEX , REG_EXTENDED | REG_NEWLINE | REG_NOSUB )) {
4868 if (!regexec (& re , cgroup_path , 0 , NULL , 0 ))
4969 is_kubernetes_target = true;
5070
@@ -58,20 +78,18 @@ static char *
5878build_container_config_path (const char * cgroup_path )
5979{
6080 regex_t re ;
61- const size_t num_matches = 5 ;
62- regmatch_t matches [ num_matches ] ;
63- int res ;
64- char path_buffer [ TARGET_KUBERNETES_CONFIG_PATH_BUFFER_SIZE ] ;
81+ regmatch_t matches [ CONTAINER_ID_REGEX_EXPECTED_MATCHES ] ;
82+ regoff_t length ;
83+ const char * id = NULL ;
84+ char buffer [ PATH_MAX ] = { 0 } ;
6585 char * config_path = NULL ;
6686
67- if (!regcomp (& re , TARGET_KUBERNETES_EXTRACT_CONTAINER_ID_REGEX , REG_EXTENDED | REG_NEWLINE )) {
68- if (!regexec (& re , cgroup_path , num_matches , matches , 0 )) {
69- res = snprintf (path_buffer , TARGET_KUBERNETES_CONFIG_PATH_BUFFER_SIZE ,
70- "/var/lib/docker/containers/%.*s/config.v2.json" ,
71- matches [3 ].rm_eo - matches [3 ].rm_so , cgroup_path + matches [3 ].rm_so );
72-
73- if (res > 0 && res < TARGET_KUBERNETES_CONFIG_PATH_BUFFER_SIZE )
74- config_path = strdup (path_buffer );
87+ if (!regcomp (& re , CONTAINER_ID_REGEX , REG_EXTENDED | REG_NEWLINE )) {
88+ if (!regexec (& re , cgroup_path , CONTAINER_ID_REGEX_EXPECTED_MATCHES , matches , 0 )) {
89+ id = cgroup_path + matches [3 ].rm_so ;
90+ length = matches [3 ].rm_eo - matches [3 ].rm_so ;
91+ snprintf (buffer , PATH_MAX , "/var/lib/docker/containers/%.*s/config.v2.json" , length , id );
92+ config_path = strdup (buffer );
7593 }
7694 regfree (& re );
7795 }
@@ -87,8 +105,7 @@ target_kubernetes_resolve_name(struct target *target)
87105 char * json = NULL ;
88106 size_t json_len ;
89107 regex_t re ;
90- const size_t num_matches = 2 ;
91- regmatch_t matches [num_matches ];
108+ regmatch_t matches [CONTAINER_NAME_REGEX_EXPECTED_MATCHES ];
92109 char * target_name = NULL ;
93110
94111 config_path = build_container_config_path (target -> cgroup_path );
@@ -98,8 +115,8 @@ target_kubernetes_resolve_name(struct target *target)
98115 json_file = fopen (config_path , "r" );
99116 if (json_file ) {
100117 if (getline (& json , & json_len , json_file ) != -1 ) {
101- if (!regcomp (& re , TARGET_KUBERNETES_EXTRACT_CONTAINER_NAME_REGEX , REG_EXTENDED | REG_NEWLINE )) {
102- if (!regexec (& re , json , num_matches , matches , 0 ))
118+ if (!regcomp (& re , CONTAINER_NAME_REGEX , REG_EXTENDED | REG_NEWLINE )) {
119+ if (!regexec (& re , json , CONTAINER_NAME_REGEX_EXPECTED_MATCHES , matches , 0 ))
103120 target_name = strndup (json + matches [1 ].rm_so , matches [1 ].rm_eo - matches [1 ].rm_so );
104121
105122 regfree (& re );
0 commit comments