File: k8s.cpp

package info (click to toggle)
falcosecurity-libs 0.1.1dev%2Bgit20220316.e5c53d64-5.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,732 kB
  • sloc: cpp: 55,770; ansic: 37,330; makefile: 74; sh: 13
file content (323 lines) | stat: -rw-r--r-- 8,636 bytes parent folder | download
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Copyright (C) 2021 The Falco Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/
//
// k8s.cpp
//
#include "k8s.h"
#include "k8s_component.h"
#include "k8s_dispatcher.h"
#include "sinsp.h"
#include "sinsp_int.h"
#include <sstream>
#include <utility>
#include <memory>
#include <algorithm>
#include <iostream>

k8s_component::type_map k8s::m_components;

k8s::k8s(const std::string& uri, bool is_captured,
#ifdef HAS_CAPTURE
		ssl_ptr_t ssl,
		bt_ptr_t bt,
		bool block,
#endif // HAS_CAPTURE
		filter_ptr_t event_filter,
		ext_list_ptr_t extensions,
		bool events_only
#ifdef HAS_CAPTURE
		,const std::string& node_selector
#endif // HAS_CAPTURE
		) :
		m_state(is_captured),
		m_event_filter(event_filter)
#ifdef HAS_CAPTURE
		,m_net(uri.empty() ?
			   nullptr : new k8s_net(*this, m_state, uri, ssl, bt, event_filter, block, node_selector))
#endif
{
	g_logger.log(std::string("Creating K8s object for [" +
							 (uri.empty() ? std::string("capture replay") : uri) + ']'),
							 sinsp_logger::SEV_DEBUG);
	if(m_components.empty())
	{
		if(events_only)
		{
			m_components.insert({ k8s_component::K8S_EVENTS, "events"});
			return;
		}
		m_components.insert({ k8s_component::K8S_NODES,                  "nodes"                  });
		m_components.insert({ k8s_component::K8S_NAMESPACES,             "namespaces"             });
		m_components.insert({ k8s_component::K8S_PODS,                   "pods"                   });
		m_components.insert({ k8s_component::K8S_REPLICATIONCONTROLLERS, "replicationcontrollers" });
		m_components.insert({ k8s_component::K8S_SERVICES,               "services"               });
		if(event_filter)
		{
			m_components.insert({ k8s_component::K8S_EVENTS, "events"});
		}
		if(extensions)
		{
			for(const auto& ext : *extensions)
			{
				if(ext == "daemonsets")
				{
					m_components.insert({ k8s_component::K8S_DAEMONSETS,  "daemonsets"  });
				}
				else if(ext == "deployments")
				{
					m_components.insert({ k8s_component::K8S_DEPLOYMENTS, "deployments" });
				}
				else if(ext == "replicasets")
				{
					m_components.insert({ k8s_component::K8S_REPLICASETS, "replicasets" });
				}
			}
		}
	}
}

k8s::~k8s()
{
	stop_watch();
	cleanup();
}

void k8s::stop_watch()
{
#ifdef HAS_CAPTURE
	if(m_net)
	{
		m_net->stop_watching();
	}
#endif
}

void k8s::cleanup()
{
#ifdef HAS_CAPTURE
	delete m_net;
	m_net = nullptr;
#endif
}

void k8s::check_components()
{
#ifdef HAS_CAPTURE
	if(m_net)
	{
		for (auto it = m_components.cbegin(); it != m_components.cend();)
		{
			if(m_net->has_handler(*it))
			{
				k8s_net::handler_ptr_t handler = k8s_net::get_handler(m_net->handlers(), *it);
				if(handler)
				{
					k8s_handler::api_error_ptr handler_error = handler->error();
					// HTTP error > 400 means non-existing, forbidden, etc.
					if(handler_error && handler_error->code() >= 400)
					{
						std::string handler_name = handler->name();
						if(!k8s_component::is_critical(handler_name))
						{
							g_logger.log("K8s: removing " + handler_name + " due to HTTP error " +
										 std::to_string(handler_error->code()) +
										 ", reason: " + handler_error->reason() +
										 ", message: " + handler_error->message(),
										 sinsp_logger::SEV_WARNING);
							m_components.erase(it++);
							continue;
						}
						else
						{
							throw sinsp_exception(handler_error->to_string());
						}
					}
				}
			}
			else
			{
				if(it->first != k8s_component::K8S_EVENTS)
				{
					m_net->add_handler(*it);
				}
				else if(m_event_filter) // events only if filter is enabled
				{
					m_net->add_handler(*it);
				}
			}
			++it;
		}
	}
	else
	{
		throw sinsp_exception("K8s net object is null.");
	}
#endif
}

const k8s_state_t& k8s::get_state()
{
	return m_state;
}

void k8s::watch()
{
#ifdef HAS_CAPTURE
	if(m_net)
	{
		check_components();
		m_net->watch();
	}
#endif
}

void k8s::simulate_watch_event(const std::string& json, int version)
{
	Json::Value root;
	Json::Reader reader;
	k8s_component::type component_type = k8s_component::K8S_COMPONENT_COUNT;
	if(reader.parse(json, root, false))
	{
		const Json::Value& kind = root["kind"];
		if(!kind.isNull() && kind.isString())
		{
			std::string type = kind.asString();
			if(type == "Namespace")                  { component_type = k8s_component::K8S_NAMESPACES;             }
			else if(type == "Node")                  { component_type = k8s_component::K8S_NODES;                  }
			else if(type == "Pod")                   { component_type = k8s_component::K8S_PODS;                   }
			else if(type == "ReplicationController") { component_type = k8s_component::K8S_REPLICATIONCONTROLLERS; }
			else if(type == "ReplicaSet")            { component_type = k8s_component::K8S_REPLICASETS;            }
			else if(type == "Service")               { component_type = k8s_component::K8S_SERVICES;               }
			else if(type == "DaemonSet")             { component_type = k8s_component::K8S_DAEMONSETS;             }
			else if(type == "Deployment")            { component_type = k8s_component::K8S_DEPLOYMENTS;            }
			else if(type == "EventList")             { component_type = k8s_component::K8S_EVENTS;                 }
			else
			{
				g_logger.log("Unrecognized component type: " + type, sinsp_logger::SEV_ERROR);
				return;
			}
		}
		else
		{
			g_logger.log("Component type not found in JSON", sinsp_logger::SEV_ERROR);
			return;
		}
	}
	else
	{
		g_logger.log("Error parsing JSON", sinsp_logger::SEV_ERROR);
		return;
	}

	if(m_state.get_capture_version() == k8s_state_t::CAPTURE_VERSION_NONE)
	{
		m_state.set_capture_version(version);
	}
	static bool version_logged = false;
	if(!version_logged)
	{
		g_logger.log("K8s capture version: " + std::to_string(version), sinsp_logger::SEV_DEBUG);
		version_logged = true;
	}
	switch(version)
	{
	case k8s_state_t::CAPTURE_VERSION_1: // old capture format
		if(component_type < k8s_component::K8S_COMPONENT_COUNT)
		{
			if(m_dispatch_map.find(component_type) == m_dispatch_map.end())
			{
				m_dispatch_map[component_type] =
					std::unique_ptr<k8s_dispatcher>(new k8s_dispatcher(component_type, m_state));
			}
			m_dispatch_map[component_type]->extract_data(root, false);
		}
		else
		{
			throw sinsp_exception(std::string("K8s capture: unknown component type (") +
							  std::to_string(component_type) + ")");
		}
		break;
	case k8s_state_t::CAPTURE_VERSION_2:
		if(component_type < k8s_component::K8S_COMPONENT_COUNT)
		{
			if(m_handler_map.find(component_type) == m_handler_map.end())
			{
				m_handler_map[component_type] = k8s_net::make_handler(m_state, component_type, false);
			}
			if(m_handler_map[component_type])
			{
				m_handler_map[component_type]->handle_json(std::move(root));
			}
			else
			{
				throw sinsp_exception(std::string("K8s capture replay: error creating ") +
									  k8s_component::get_name(component_type) +
									  " handler");
			}
		}
		else
		{
			throw sinsp_exception(std::string("K8s capture: unknown component type (") +
							  std::to_string(component_type) + ")");
		}
		break;
	default:
		throw sinsp_exception(std::string("K8s capture: invalid capture version (") +
							  std::to_string(version) + ")");
	}
}

std::size_t k8s::count(k8s_component::type component) const
{
	switch (component)
	{
	case k8s_component::K8S_NODES:
		return m_state.get_nodes().size();

	case k8s_component::K8S_NAMESPACES:
		return m_state.get_namespaces().size();

	case k8s_component::K8S_PODS:
		return m_state.get_pods().size();

	case k8s_component::K8S_REPLICATIONCONTROLLERS:
		return m_state.get_rcs().size();

	case k8s_component::K8S_REPLICASETS:
		return m_state.get_rss().size();

	case k8s_component::K8S_SERVICES:
		return m_state.get_services().size();

	case k8s_component::K8S_DAEMONSETS:
		return m_state.get_daemonsets().size();

	case k8s_component::K8S_DEPLOYMENTS:
		return m_state.get_deployments().size();

	case k8s_component::K8S_EVENTS:
		return m_state.get_events().size();

	case k8s_component::K8S_COMPONENT_COUNT:
	default:
		break;
	}

	std::ostringstream os;
	os << "Unknown component " << static_cast<int>(component);
	throw sinsp_exception(os.str());
}