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
|
/*
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.
*/
//
// marathon_component.h
//
// marathon components (groups, apps, tasks)
// abstraction
//
#ifndef MINIMAL_BUILD
#pragma once
#include "json/json.h"
#include "sinsp.h"
#include "sinsp_int.h"
#include "mesos_component.h"
#include <vector>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <memory>
typedef std::pair<std::string, std::string> marathon_pair_t;
typedef std::vector<marathon_pair_t> marathon_pair_list;
class marathon_app;
//
// component
//
class marathon_component
{
public:
enum type
{
MARATHON_GROUP,
MARATHON_APP
};
typedef std::pair<type, std::string> component_pair;
typedef std::map<type, std::string> component_map;
static const component_map list;
marathon_component() = delete;
marathon_component(type t, const std::string& id);
marathon_component(const marathon_component& other);
marathon_component(marathon_component&& other);
marathon_component& operator=(const marathon_component& other);
marathon_component& operator=(const marathon_component&& other);
const std::string& get_id() const;
void set_id(const std::string& id);
const std::string& get_name() const;
void set_name(const std::string& name);
static std::string get_type_name(type t);
static type get_type(const std::string& name);
protected:
typedef std::shared_ptr<marathon_app> app_ptr_t;
typedef std::map<std::string, app_ptr_t> task_app_map_t;
static void cache_task_app(const std::string& task_id, app_ptr_t app);
static void uncache_task_app(const std::string& task_id);
static app_ptr_t get_cached_app(const std::string& task_id);
private:
type m_type;
std::string m_id;
static task_app_map_t m_task_app_cache;
};
//
// group
//
class marathon_group : public marathon_component, public std::enable_shared_from_this<marathon_group>
{
public:
typedef std::shared_ptr<marathon_group> ptr_t;
typedef std::shared_ptr<marathon_app> app_ptr_t;
typedef std::unordered_map<std::string, std::shared_ptr<marathon_app>> app_map_t;
typedef std::vector<std::shared_ptr<marathon_app>> app_list_t;
typedef std::map<std::string, std::shared_ptr<marathon_group>> group_map_t;
marathon_group(const std::string& id, const std::string& framework_id);
app_ptr_t get_app(const std::string& id);
app_ptr_t get_app(mesos_task::ptr_t task) const;
void add_or_replace_app(std::shared_ptr<marathon_app>);
bool remove_app(const std::string& id);
bool remove_task(const std::string& id);
void add_or_replace_group(std::shared_ptr<marathon_group>);
const app_map_t& get_apps() const;
const group_map_t& get_groups() const;
ptr_t get_group(const std::string& group_id);
ptr_t get_group(mesos_task::ptr_t task);
bool remove(const std::string& id);
void print(int indent = 0) const;
const std::string& get_framework_id() const;
void set_framework_id(const std::string& id);
private:
template <typename M, typename P>
static void add_or_replace_component(M& component_map, P comp)
{
typename M::value_type val = {comp->get_id(), comp};
std::pair<typename M::iterator, bool> ret = component_map.insert(val);
if (!ret.second) ret.first->second = comp;
}
bool remove_group(const std::string& id);
ptr_t get_parent(const std::string& id);
app_map_t m_apps;
group_map_t m_groups;
std::string m_framework_id;
};
//
// app
//
class marathon_app : public marathon_component, public std::enable_shared_from_this<marathon_app>
{
public:
typedef std::shared_ptr<marathon_app> ptr_t;
typedef std::vector<std::string> task_list_t;
marathon_app(const std::string& uid);
~marathon_app();
void add_task(mesos_framework::task_ptr_t ptask);
bool remove_task(const std::string& task_id);
const task_list_t& get_tasks() const;
bool has_task(const std::string& task_id);
std::string get_group_id() const;
static std::string get_group_id(const std::string& app_id);
void set_labels(const Json::Value& labels);
const mesos_pair_list& get_labels() const;
mesos_pair_t get_label(const std::string& key) const;
private:
task_list_t m_tasks;
mesos_pair_list m_labels;
friend class mesos;
};
typedef marathon_group::app_map_t marathon_apps;
typedef marathon_group::group_map_t marathon_groups;
//
// component
//
inline const std::string& marathon_component::get_id() const
{
return m_id;
}
inline void marathon_component::set_id(const std::string& id)
{
m_id = id;
}
inline const std::string& marathon_component::get_name() const
{
return m_id;
}
inline void marathon_component::set_name(const std::string& name)
{
m_id = name;
}
//
// group
//
inline const marathon_group::app_map_t& marathon_group::get_apps() const
{
return m_apps;
}
inline const marathon_group::group_map_t& marathon_group::get_groups() const
{
return m_groups;
}
inline void marathon_group::add_or_replace_group(std::shared_ptr<marathon_group> group)
{
add_or_replace_component(m_groups, group);
}
inline void marathon_group::add_or_replace_app(std::shared_ptr<marathon_app> app)
{
add_or_replace_component(m_apps, app);
}
inline const std::string& marathon_group::get_framework_id() const
{
return m_framework_id;
}
inline void marathon_group::set_framework_id(const std::string& id)
{
m_framework_id = id;
}
//
// app
//
inline const marathon_app::task_list_t& marathon_app::get_tasks() const
{
return m_tasks;
}
inline const mesos_pair_list& marathon_app::get_labels() const
{
return m_labels;
}
#endif // MINIMAL_BUILD
|