File: k8s_component.h

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 (1067 lines) | stat: -rw-r--r-- 23,469 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
/*
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_component.h
//
// kubernetes components (nodes, namespaces, pods, replication controllers, services)
// abstraction
//
#ifndef MINIMAL_BUILD
#pragma once

#include "json/json.h"
#include "sinsp.h"
#include "sinsp_int.h"
#include "logger.h"
#include "user_event.h"
#include "user_event_logger.h"
#include <vector>
#include <unordered_set>

typedef std::pair<std::string, std::string> k8s_pair_t;
typedef std::vector<k8s_pair_t>             k8s_pair_list;

class k8s_pod_t;
class k8s_service_t;

class k8s_container
{
public:
	typedef std::vector<k8s_container>  list;

	class port
	{
	public:
		void set_name(const std::string& name);

		const std::string& get_name() const;

		void set_port(uint32_t port);

		uint32_t get_port() const;

		void set_protocol(const std::string& protocol);

		const std::string& get_protocol() const;

		bool operator==(const port& other) const;

		bool operator!=(const port& other) const;

	private:
		std::string m_name;
		uint32_t    m_port = 0;
		std::string m_protocol;
	};

	typedef std::vector<port> port_list;

	k8s_container();

	k8s_container(const std::string& name, const port_list& ports);

	k8s_container(const k8s_container& other);

	k8s_container(k8s_container&& other);

	k8s_container& operator=(const k8s_container& other);

	bool has_port(const std::string& port_name) const;

	const port* get_port(const std::string& port_name) const;

	void set_name(const std::string& name);

	const std::string& get_name() const;

	bool operator==(const k8s_container& other) const;

	bool operator!=(const k8s_container& other) const;

private:
	std::string m_name;
	port_list   m_ports;
};

// 
// component
//

class k8s_component
{
public:
	enum type
	{
		K8S_NODES,
		K8S_NAMESPACES,
		K8S_PODS,
		K8S_REPLICATIONCONTROLLERS,
		K8S_SERVICES,
		K8S_EVENTS,
		K8S_REPLICASETS,
		K8S_DAEMONSETS,
		K8S_DEPLOYMENTS,
		K8S_COMPONENT_COUNT
	};

	typedef std::set<std::string>        ext_list_t;
	typedef std::shared_ptr<ext_list_t>  ext_list_ptr_t;
	typedef std::pair<type, std::string> component_pair;
	typedef std::map<type, std::string>  type_map;
	static const type_map list;
	enum msg_reason
	{
		COMPONENT_ADDED,
		COMPONENT_MODIFIED,
		COMPONENT_DELETED,
		COMPONENT_ERROR,
		COMPONENT_NONEXISTENT,
		COMPONENT_UNKNOWN // only to mark bad event messages
	};

	struct msg_data
	{
		msg_reason  m_reason = COMPONENT_UNKNOWN;
		std::string m_name;
		std::string m_uid;
		std::string m_namespace;
		std::string m_kind;

		bool is_valid() const
		{
			return m_reason != COMPONENT_UNKNOWN;
		}

		std::string get_reason_desc() const
		{
			switch(m_reason)
			{
				case COMPONENT_ADDED:       return "ADDED";
				case COMPONENT_MODIFIED:    return "MODIFIED";
				case COMPONENT_DELETED:     return "DELETED";
				case COMPONENT_ERROR:       return "ERROR";
				case COMPONENT_NONEXISTENT: return "NONEXISTENT";
				case COMPONENT_UNKNOWN:
				default:                    return "UNKNOWN";
			}
			return "UNKNOWN";
		}
	};

	k8s_component() = delete;

	k8s_component(type comp_type, const std::string& name, const std::string& uid, const std::string& ns = "");

	virtual ~k8s_component();

	const std::string& get_name() const;

	void set_name(const std::string& name);

	const std::string& get_uid() const;

	void set_uid(const std::string& uid);

	const std::string& get_namespace() const;

	void set_namespace(const std::string& ns);

	k8s_pair_t* get_label(const k8s_pair_t& label);

	const k8s_pair_list& get_labels() const;

	void set_labels(k8s_pair_list&& labels);

	void add_labels(k8s_pair_list&& labels);

	void swap_labels(k8s_pair_list& new_labels);

	void push_label(const k8s_pair_t& label);

	void emplace_label(const k8s_pair_t& label);

	k8s_pair_t* get_selector(const k8s_pair_t& selector);

	const k8s_pair_list& get_selectors() const;

	void set_selectors(k8s_pair_list&& selectors);

	void add_selectors(k8s_pair_list&& selectors);

	void swap_selectors(k8s_pair_list& new_selectors);

	void push_selector(const k8s_pair_t& selector);

	void emplace_selector(k8s_pair_t&& selector);

	virtual std::string get_node_name() const;

	template <typename C>
	static void extract_string_array(const Json::Value& arr, C& list)
	{
		if(!arr.isNull() && arr.isArray())
		{
			for (auto& item : arr)
			{
				if(item.isConvertibleTo(Json::stringValue))
				{
					list.emplace(item.asString());
				}
			}
		}
	}

	static k8s_pair_list extract_object(const Json::Value& object, const std::string& name);

	static const std::string& get_name(const component_pair& p);

	static std::string get_name(type t);
	static std::string get_name_u(type t);

	static type get_type(const component_pair& p);

	static type get_type(const std::string& name);

	static std::string get_api(type t, ext_list_ptr_t extensions = nullptr);
	static std::string get_api(const component_pair& p, ext_list_ptr_t extensions = nullptr);
	static std::string get_api(const std::string& name, ext_list_ptr_t extensions = nullptr);

	static std::string get_selector(type t);
	static std::string get_selector(const component_pair& p);
	static std::string get_selector(const std::string& name);

	static bool is_critical(type t);
	static bool is_critical(const component_pair& p);
	static bool is_critical(const std::string& name);

	bool selector_in_labels(const k8s_pair_t& selector, const k8s_pair_list& labels) const;

	bool selectors_in_labels(const k8s_pair_list& labels) const;

private:

	type          m_type;
	std::string   m_name;
	std::string   m_uid;
	std::string   m_ns;
	k8s_pair_list m_labels;
	k8s_pair_list m_selectors;

	friend class k8s_state_t;
	friend class k8s_dispatcher;
};


//
// namespace
//

class k8s_ns_t : public k8s_component
{
public:
	static const k8s_component::type COMPONENT_TYPE = K8S_NAMESPACES;
	k8s_ns_t(const std::string& name, const std::string& uid, const std::string& ns = "");
};


//
// node
//

class k8s_node_t : public k8s_component
{
public:
	typedef std::unordered_set<std::string> host_ip_list;

	static const k8s_component::type COMPONENT_TYPE = K8S_NODES;

	k8s_node_t(const std::string& name, const std::string& uid, const std::string& ns = "");

	const host_ip_list& get_host_ips() const;

	void set_host_ips(host_ip_list&& host_ips);

	void add_host_ips(host_ip_list&& host_ips);

	void emplace_host_ip(std::string&& host_ip);

	virtual std::string get_node_name() const;

	static host_ip_list extract_addresses(const Json::Value& status);

private:
	host_ip_list m_host_ips;
};


//
// pod
//

class k8s_pod_t : public k8s_component
{
public:
	typedef std::vector<std::string> container_id_list;
	typedef k8s_container::list container_list;

	static const k8s_component::type COMPONENT_TYPE = K8S_PODS;

	k8s_pod_t(const std::string& name, const std::string& uid, const std::string& ns = "");

	// container IDs
	const container_id_list& get_container_ids() const;
	void set_container_ids(container_id_list&& container_ids);
	void add_container_ids(container_id_list&& container_ids);
	void push_container_id(const std::string& container_id);
	void emplace_container_id(std::string&& container_id);

	// restart count
	size_t get_restart_count() const;
	void set_restart_count(int rc);

	// containers
	const container_list& get_containers() const;
	void set_containers(container_list&& containers);
	void add_containers(container_list&& containers);
	void push_container(const k8s_container& container);
	void emplace_container(k8s_container&& container);

	// node name, host IP and internal IP
	virtual std::string get_node_name() const;
	void set_node_name(const std::string& name);
	const std::string& get_host_ip() const;
	void set_host_ip(const std::string& host_ip);
	const std::string& get_internal_ip() const;
	void set_internal_ip(const std::string& internal_ip);

	// comparison
	bool operator==(const k8s_pod_t& other) const;
	bool operator!=(const k8s_pod_t& other) const;

private:
	container_id_list m_container_ids;
	container_list    m_containers;
	std::string       m_node_name;
	std::string       m_host_ip;
	std::string       m_internal_ip;
	int               m_restart_count_tot = -1;
	mutable int       m_restart_count_diff = 0;
};


//
// replicas
//

class k8s_replicas_t
{
public:
	static const int UNKNOWN_REPLICAS = -1;

	k8s_replicas_t(int spec_replicas = UNKNOWN_REPLICAS, int stat_replicas = UNKNOWN_REPLICAS);

	void set_spec_replicas(int replicas);
	int get_spec_replicas() const;
	void set_stat_replicas(int replicas);
	int get_stat_replicas() const;

	static int get_count(const Json::Value& item, const std::string& replica_name = "replicas");
	static void set_replicas(k8s_replicas_t& replicas, const Json::Value& item);

protected:
	int m_spec_replicas = UNKNOWN_REPLICAS;
	int m_stat_replicas = UNKNOWN_REPLICAS;
};


//
// replication controller
//

class k8s_rc_t : public k8s_component
{
public:
	static const k8s_component::type COMPONENT_TYPE = K8S_REPLICATIONCONTROLLERS;

	k8s_rc_t(const std::string& name,
			 const std::string& uid,
			 const std::string& ns = "",
			 k8s_component::type type = K8S_REPLICATIONCONTROLLERS);

	std::vector<const k8s_pod_t*> get_selected_pods(const std::vector<k8s_pod_t>& pods) const;

	void set_spec_replicas(int replicas);
	int get_spec_replicas() const;
	void set_stat_replicas(int replicas);
	int get_stat_replicas() const;
	void set_replicas(const Json::Value& item, const std::string& replica_name = "replicas");
	void set_replicas(int spec, int stat);

protected:
	k8s_replicas_t m_replicas;
};


//
// replica set
//

class k8s_rs_t : public k8s_rc_t
{
public:
	static const k8s_component::type COMPONENT_TYPE = K8S_REPLICASETS;

	k8s_rs_t(const std::string& name, const std::string& uid, const std::string& ns = "");

private:
};


//
// service
//

class k8s_service_t : public k8s_component
{
public:
	struct net_port
	{
		uint32_t    m_port = 0;
		std::string m_protocol;
		uint32_t    m_target_port = 0;
		uint32_t    m_node_port = 0;
	};

	typedef std::vector<net_port> port_list;

	static const k8s_component::type COMPONENT_TYPE = K8S_SERVICES;

	k8s_service_t(const std::string& name, const std::string& uid, const std::string& ns = "");

	const std::string& get_cluster_ip() const;

	void set_cluster_ip(const std::string& cluster_ip);

	const port_list& get_port_list() const;

	void set_port_list(port_list&& ports);

	std::vector<const k8s_pod_t*> get_selected_pods(const std::vector<k8s_pod_t>& pods) const;

private:
	std::string m_cluster_ip;
	port_list   m_ports;
};


//
// daemon set
//

class k8s_daemonset_t : public k8s_component
{
public:
	static const k8s_component::type COMPONENT_TYPE = K8S_DAEMONSETS;

	k8s_daemonset_t(const std::string& name, const std::string& uid, const std::string& ns = "");

	void set_desired_scheduled(int replicas);
	int get_desired_scheduled() const;
	void set_current_scheduled(int replicas);
	int get_current_scheduled() const;
	void set_scheduled(const Json::Value& item);
	void set_scheduled(int desired, int current);

private:
	k8s_replicas_t m_replicas;
};


//
// deployment
//

class k8s_deployment_t : public k8s_component
{
public:
	static const k8s_component::type COMPONENT_TYPE = K8S_DEPLOYMENTS;

	k8s_deployment_t(const std::string& name, const std::string& uid, const std::string& ns = "");

	void set_spec_replicas(int replicas);
	int get_spec_replicas() const;
	void set_stat_replicas(int replicas);
	int get_stat_replicas() const;
	void set_replicas(const Json::Value& item);
	void set_replicas(int desired, int current);

	std::vector<const k8s_pod_t*> get_selected_pods(const std::vector<k8s_pod_t>& pods) const;
	
private:
	k8s_replicas_t m_replicas;
};


//
// event
//

class k8s_state_t;
class event_scope;

class k8s_event_t : public k8s_component
{
public:
	static const k8s_component::type COMPONENT_TYPE = K8S_EVENTS;

	k8s_event_t(const std::string& name, const std::string& uid, const std::string& ns);

	bool update(const Json::Value& item, k8s_state_t& state);
	void post_process(k8s_state_t& state);
	bool has_pending_events() const;

private:
	typedef sinsp_user_event::tag_map_t tag_map_t;
	typedef user_event_logger::severity severity_t;
	typedef std::unordered_map<std::string, std::string> name_translation_map_t;

	void make_scope(const Json::Value& obj, event_scope& scope);
	void make_scope_impl(const Json::Value& obj, std::string comp, event_scope& scope, bool ns = true);

	name_translation_map_t  m_name_translation;
	std::map<std::string, Json::Value> m_postponed_events;
	bool m_force_delete = false;
};

typedef std::vector<k8s_ns_t>         k8s_namespaces;
typedef std::vector<k8s_node_t>       k8s_nodes;
typedef std::vector<k8s_pod_t>        k8s_pods;
typedef std::vector<k8s_rc_t>         k8s_controllers;
typedef std::vector<k8s_rs_t>         k8s_replicasets;
typedef std::vector<k8s_service_t>    k8s_services;
typedef std::vector<k8s_daemonset_t>  k8s_daemonsets;
typedef std::vector<k8s_deployment_t> k8s_deployments;
typedef std::vector<k8s_event_t>      k8s_events;

//
// container
//

inline const std::string& k8s_container::get_name() const
{
	return m_name;
}

inline void k8s_container::set_name(const std::string& name)
{
	m_name = name;
}

inline bool k8s_container::operator==(const k8s_container& other) const
{
	if(&other == this) { return true; }
	return (other.m_name == m_name) && (other.m_ports == m_ports);
}

inline bool k8s_container::operator!=(const k8s_container& other) const
{
	if(&other == this) { return false; }
	return !(other == *this);
}

//
// container::port
//

inline void k8s_container::port::set_name(const std::string& name)
{
	m_name = name;
}

inline const std::string& k8s_container::port::get_name() const
{
	return m_name;
}

inline void k8s_container::port::set_port(uint32_t port)
{
	m_port = port;
}

inline uint32_t k8s_container::port::get_port() const
{
	return m_port;
}

inline void k8s_container::port::set_protocol(const std::string& protocol)
{
	m_protocol = protocol;
}

inline const std::string& k8s_container::port::get_protocol() const
{
	return m_protocol;
}

inline bool k8s_container::port::operator==(const port& other) const
{
	if(&other == this) { return true; }
	return other.m_name == m_name &&
			other.m_port == m_port &&
			other.m_protocol == m_protocol;
}

inline bool k8s_container::port::operator!=(const port& other) const
{
	if(&other == this) { return false; }
	return !(other == *this);
}


//
// component
//

inline const std::string& k8s_component::get_name() const
{
	return m_name;
}

inline void k8s_component::set_name(const std::string& name)
{
	m_name = name;
}

inline const std::string& k8s_component::get_uid() const{
	
	return m_uid;
}

inline void k8s_component::set_uid(const std::string& uid)
{
	m_uid = uid;
}

inline const std::string& k8s_component::get_namespace() const
{
	return m_ns;
}

inline void k8s_component::set_namespace(const std::string& ns)
{
	m_ns = ns;
}

inline const k8s_pair_list& k8s_component::get_labels() const
{
	return m_labels;
}

inline void k8s_component::set_labels(k8s_pair_list&& labels)
{
	m_labels = std::move(labels);
}

inline void k8s_component::swap_labels(k8s_pair_list& new_labels)
{
	m_labels.swap(new_labels);
}

inline void k8s_component::push_label(const k8s_pair_t& label)
{
	m_labels.push_back(label);
}

inline void k8s_component::emplace_label(const k8s_pair_t& label)
{
	m_labels.emplace_back(label);
}

inline const k8s_pair_list& k8s_component::get_selectors() const
{
	return m_selectors;
}

inline void k8s_component::set_selectors(k8s_pair_list&& selectors)
{
	m_selectors = std::move(selectors);
}

inline void k8s_component::swap_selectors(k8s_pair_list& new_selectors)
{
	m_selectors.swap(new_selectors);
}

inline void k8s_component::push_selector(const k8s_pair_t& selector)
{
	m_selectors.push_back(selector);
}

inline void k8s_component::emplace_selector(k8s_pair_t&& selector)
{
	m_selectors.emplace_back(std::move(selector));
}

inline const std::string& k8s_component::get_name(const component_pair& p)
{
	return p.second;
}

inline k8s_component::type k8s_component::get_type(const component_pair& p)
{
	return p.first;
}

inline std::string k8s_component::get_node_name() const
{
	return "";
}

//
// node
//

inline const k8s_node_t::host_ip_list& k8s_node_t::get_host_ips() const
{
	return m_host_ips;
}

inline void k8s_node_t::set_host_ips(host_ip_list&& host_ips)
{
	m_host_ips = std::move(host_ips);
}

inline void k8s_node_t::add_host_ips(host_ip_list&& host_ips)
{
	m_host_ips.insert(host_ips.begin(), host_ips.end());
}

inline void k8s_node_t::emplace_host_ip(std::string&& host_ip)
{
	m_host_ips.emplace(std::move(host_ip));
}

inline std::string k8s_node_t::get_node_name() const
{
	return get_name();
}

//
// pod 
//

// container IDs

inline const k8s_pod_t::container_id_list& k8s_pod_t::get_container_ids() const
{
	return m_container_ids;
}

inline void k8s_pod_t::set_container_ids(container_id_list&& container_ids)
{
	m_container_ids = std::move(container_ids);
}

inline void k8s_pod_t::add_container_ids(container_id_list&& container_ids)
{
	m_container_ids.insert(m_container_ids.end(), container_ids.begin(), container_ids.end());
}

inline void k8s_pod_t::push_container_id(const std::string& container_id)
{
	m_container_ids.push_back(container_id);
}

inline void k8s_pod_t::emplace_container_id(std::string&& container_id)
{
	m_container_ids.emplace_back(std::move(container_id));
}

// restart count

inline size_t k8s_pod_t::get_restart_count() const
{
	int restart_count_diff = m_restart_count_diff;
	m_restart_count_diff = 0;
	return restart_count_diff;
}

inline void k8s_pod_t::set_restart_count(int rc)
{
	if(rc < 0)
	{
		g_logger.log("Unexpected K8S pod restart count received: " + std::to_string(rc),
					sinsp_logger::SEV_WARNING);
		return;
	}

	// only record current total on first call
	if(m_restart_count_tot == -1)
	{
		m_restart_count_tot = rc;
		return;
	}

	if(rc >= m_restart_count_tot)
	{
		m_restart_count_diff = rc - m_restart_count_tot;
	}
	else
	{
		g_logger.log("Unexpected K8S pod restart count received (" + std::to_string(rc) + 
					", last recorded value " + std::to_string(m_restart_count_tot) + "), resetting diff to zero.",
					sinsp_logger::SEV_WARNING);
		m_restart_count_diff = 0;
	}
	m_restart_count_tot = rc;
}

// comparison

inline bool k8s_pod_t::operator==(const k8s_pod_t& other) const
{
	if(&other == this) { return true; }
	return other.m_container_ids == m_container_ids &&
			other.m_containers == m_containers &&
			other.m_host_ip == m_host_ip &&
			other.m_internal_ip == m_internal_ip;
}

inline bool k8s_pod_t::operator!=(const k8s_pod_t& other) const
{
	if(&other == this) { return false; }
	return !(other == *this);
}

// containers

inline const k8s_pod_t::container_list& k8s_pod_t::get_containers() const
{
	return m_containers;
}

inline void k8s_pod_t::set_containers(container_list&& containers)
{
	m_containers = std::move(containers);
}

inline void k8s_pod_t::add_containers(container_list&& containers)
{
	m_containers.insert(m_containers.end(), containers.begin(), containers.end());
}

inline void k8s_pod_t::push_container(const k8s_container& container)
{
	m_containers.push_back(container);
}

inline void k8s_pod_t::emplace_container(k8s_container&& container)
{
	m_containers.emplace_back(std::move(container));
}

// getters/setters
inline std::string k8s_pod_t::get_node_name() const
{
	return m_node_name;
}

inline void k8s_pod_t::set_node_name(const std::string& name)
{
	m_node_name = name;
}

inline const std::string& k8s_pod_t::get_host_ip() const
{
	return m_host_ip;
}

inline void k8s_pod_t::set_host_ip(const std::string& host_ip)
{
	m_host_ip = host_ip;
}

inline const std::string& k8s_pod_t::get_internal_ip() const
{
	return m_internal_ip;
}

inline void k8s_pod_t::set_internal_ip(const std::string& internal_ip)
{
	m_internal_ip = internal_ip;
}


//
// replicas
//

inline void k8s_replicas_t::set_spec_replicas(int replicas)
{
	m_spec_replicas = replicas;
}

inline int k8s_replicas_t::get_spec_replicas() const
{
	return m_spec_replicas;
}

inline void k8s_replicas_t::set_stat_replicas(int replicas)
{
	m_stat_replicas = replicas;
}

inline int k8s_replicas_t::get_stat_replicas() const
{
	return m_stat_replicas;
}


//
// replication controller
//

inline void k8s_rc_t::set_spec_replicas(int replicas)
{
	m_replicas.set_spec_replicas(replicas);
}

inline int k8s_rc_t::get_spec_replicas() const
{
	return m_replicas.get_spec_replicas();
}

inline void k8s_rc_t::set_stat_replicas(int replicas)
{
	m_replicas.set_stat_replicas(replicas);
}

inline int k8s_rc_t::get_stat_replicas() const
{
	return m_replicas.get_stat_replicas();
}

inline void k8s_rc_t::set_replicas(const Json::Value& item, const std::string& replica_name)
{
	k8s_replicas_t::set_replicas(m_replicas, item);
}

//
// service
//

inline const std::string& k8s_service_t::get_cluster_ip() const
{
	return m_cluster_ip;
}

inline void k8s_service_t::set_cluster_ip(const std::string& cluster_ip)
{
	m_cluster_ip = cluster_ip;
}

inline const k8s_service_t::port_list& k8s_service_t::get_port_list() const
{
	return m_ports;
}

inline void k8s_service_t::set_port_list(port_list&& ports)
{
	m_ports = std::move(ports);
}

//
// deployment
//

inline void k8s_deployment_t::set_spec_replicas(int replicas)
{
	m_replicas.set_spec_replicas(replicas);
}

inline int k8s_deployment_t::get_spec_replicas() const
{
	return m_replicas.get_spec_replicas();
}

inline void k8s_deployment_t::set_stat_replicas(int replicas)
{
	m_replicas.set_stat_replicas(replicas);
}

inline int k8s_deployment_t::get_stat_replicas() const
{
	return m_replicas.get_stat_replicas();
}

inline void k8s_deployment_t::set_replicas(const Json::Value& item)
{
	k8s_replicas_t::set_replicas(m_replicas, item);
}

inline void k8s_deployment_t::set_replicas(int desired, int current)
{
	m_replicas.set_spec_replicas(desired);
	m_replicas.set_stat_replicas(current);
}


//
// daemon set
//

inline void k8s_daemonset_t::set_desired_scheduled(int scheduled)
{
	m_replicas.set_spec_replicas(scheduled);
}

inline int k8s_daemonset_t::get_desired_scheduled() const
{
	return m_replicas.get_spec_replicas();
}

inline void k8s_daemonset_t::set_current_scheduled(int scheduled)
{
	m_replicas.set_stat_replicas(scheduled);
}

inline int k8s_daemonset_t::get_current_scheduled() const
{
	return m_replicas.get_stat_replicas();
}

inline void k8s_daemonset_t::set_scheduled(const Json::Value& item)
{
	m_replicas.set_spec_replicas(k8s_replicas_t::get_count(item["status"], "desiredNumberScheduled"));
	m_replicas.set_stat_replicas(k8s_replicas_t::get_count(item["status"], "currentNumberScheduled"));
}

inline void k8s_daemonset_t::set_scheduled(int desired, int current)
{
	m_replicas.set_spec_replicas(desired);
	m_replicas.set_stat_replicas(current);
}

//
// event
//

inline bool k8s_event_t::has_pending_events() const
{
	return m_postponed_events.size() != 0;
}
#endif // MINIMAL_BUILD