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
|
/*
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_namespace_handler.cpp
//
#ifndef CYGWING_AGENT
#include "k8s_namespace_handler.h"
#include "sinsp.h"
#include "sinsp_int.h"
// filters normalize state and event JSONs, so they can be processed generically:
// event is turned into a single-entry array, state is turned into an array of ADDED events
std::string k8s_namespace_handler::EVENT_FILTER =
"{"
" type: .type,"
" apiVersion: .object.apiVersion,"
" kind: .object.kind,"
" items:"
" [ .object |"
" {"
" name: .metadata.name,"
" uid: .metadata.uid,"
" timestamp: .metadata.creationTimestamp,"
" labels: .metadata.labels"
" }"
" ]"
"}";
std::string k8s_namespace_handler::STATE_FILTER =
"{"
" type: \"ADDED\","
" apiVersion: .apiVersion,"
" kind: \"Namespace\","
" items:"
" ["
" .items[] |"
" {"
" name: .metadata.name,"
" uid: .metadata.uid,"
" timestamp: .metadata.creationTimestamp,"
" labels: .metadata.labels"
" }"
" ]"
"}";
k8s_namespace_handler::k8s_namespace_handler(k8s_state_t& state
#if defined(HAS_CAPTURE) && !defined(_WIN32)
,ptr_t dependency_handler
,collector_ptr_t collector
,std::string url
,const std::string& http_version
,ssl_ptr_t ssl
,bt_ptr_t bt
,bool connect
,bool blocking_socket
#endif // HAS_CAPTURE
):
k8s_handler("k8s_namespace_handler", true,
#if defined(HAS_CAPTURE) && !defined(_WIN32)
url, "/api/v1/namespaces",
STATE_FILTER, EVENT_FILTER, "", collector,
http_version, 1000L, ssl, bt, true,
connect, dependency_handler, blocking_socket,
#endif // HAS_CAPTURE
~0, &state)
{
}
k8s_namespace_handler::~k8s_namespace_handler()
{
}
bool k8s_namespace_handler::handle_component(const Json::Value& json, const msg_data* data)
{
if(data)
{
if(m_state)
{
if((data->m_reason == k8s_component::COMPONENT_ADDED) ||
(data->m_reason == k8s_component::COMPONENT_MODIFIED))
{
k8s_ns_t& ns =
m_state->get_component<k8s_namespaces, k8s_ns_t>(m_state->get_namespaces(),
data->m_name, data->m_uid);
k8s_pair_list entries = k8s_component::extract_object(json, "labels");
if(entries.size() > 0)
{
ns.set_labels(std::move(entries));
}
}
else if(data->m_reason == k8s_component::COMPONENT_DELETED)
{
if(!m_state->delete_component(m_state->get_namespaces(), data->m_uid))
{
log_not_found(*data);
return false;
}
}
else if(data->m_reason != k8s_component::COMPONENT_ERROR)
{
g_logger.log(std::string("Unsupported K8S " + name() + " event reason: ") +
std::to_string(data->m_reason), sinsp_logger::SEV_ERROR);
return false;
}
}
else
{
throw sinsp_exception("K8s node handler: state is null.");
}
}
else
{
throw sinsp_exception("K8s namespace handler: data is null.");
}
return true;
}
#endif // CYGWING_AGENT
|