Skip to content

Commit 2e17cf4

Browse files
committed
Move ExternalIPsConfig to its own file.
1 parent 13f2ef5 commit 2e17cf4

File tree

7 files changed

+103
-86
lines changed

7 files changed

+103
-86
lines changed

collector/lib/CollectorConfig.cpp

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -544,54 +544,4 @@ std::pair<option::ArgStatus, std::string> CollectorConfig::CheckConfiguration(co
544544
return std::make_pair(ARG_OK, "");
545545
}
546546

547-
CollectorConfig::ExternalIPsConfig::ExternalIPsConfig(std::optional<sensor::CollectorConfig> runtime_config, bool default_enabled) {
548-
if (!runtime_config.has_value()) {
549-
direction_enabled_ = default_enabled ? Direction::BOTH : Direction::NONE;
550-
return;
551-
}
552-
553-
// At this point we know runtime_config has a value, we can access it directly
554-
const auto& external_ips = runtime_config->networking().external_ips();
555-
if (external_ips.enabled() != sensor::ExternalIpsEnabled::ENABLED) {
556-
direction_enabled_ = Direction::NONE;
557-
return;
558-
}
559-
560-
switch (external_ips.direction()) {
561-
case sensor::ExternalIpsDirection::INGRESS:
562-
direction_enabled_ = Direction::INGRESS;
563-
break;
564-
case sensor::ExternalIpsDirection::EGRESS:
565-
direction_enabled_ = Direction::EGRESS;
566-
break;
567-
default:
568-
direction_enabled_ = Direction::BOTH;
569-
break;
570-
}
571-
}
572-
573-
std::ostream& operator<<(std::ostream& os, const collector::CollectorConfig::ExternalIPsConfig& config) {
574-
os << "direction(";
575-
576-
switch (config.GetDirection()) {
577-
case CollectorConfig::ExternalIPsConfig::Direction::NONE:
578-
os << "NONE";
579-
break;
580-
case CollectorConfig::ExternalIPsConfig::Direction::INGRESS:
581-
os << "INGRESS";
582-
break;
583-
case CollectorConfig::ExternalIPsConfig::Direction::EGRESS:
584-
os << "EGRESS";
585-
break;
586-
case CollectorConfig::ExternalIPsConfig::Direction::BOTH:
587-
os << "BOTH";
588-
break;
589-
default:
590-
os << "invalid";
591-
break;
592-
}
593-
594-
return os << ")";
595-
}
596-
597547
} // namespace collector

collector/lib/CollectorConfig.h

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <internalapi/sensor/collector.pb.h>
1515

1616
#include "CollectionMethod.h"
17+
#include "ExternalIPsConfig.h"
1718
#include "HostConfig.h"
1819
#include "Logging.h"
1920
#include "NetworkConnection.h"
@@ -94,32 +95,6 @@ class CollectorConfig {
9495
bool ImportUsers() const { return import_users_; }
9596
bool CollectConnectionStatus() const { return collect_connection_status_; }
9697

97-
// Encapsulates the configuration of the External-IPs feature
98-
class ExternalIPsConfig {
99-
public:
100-
enum Direction {
101-
NONE = 0,
102-
INGRESS = 1 << 0,
103-
EGRESS = 1 << 1,
104-
BOTH = INGRESS | EGRESS,
105-
};
106-
107-
// Are External-IPs enabled in the provided direction ?
108-
bool IsEnabled(Direction direction) { return (direction & direction_enabled_) == direction; }
109-
110-
// Direction in which External-IPs are enabled
111-
Direction GetDirection() const { return direction_enabled_; }
112-
113-
// Extract the External-IPs configuration from the provided runtime-conf.
114-
// If the runtime-configuration is unset then 'default_enabled' is used
115-
// as a fallback to enable in both directions.
116-
// 'runtime_config' should be locked prior to calling.
117-
ExternalIPsConfig(std::optional<sensor::CollectorConfig> runtime_config, bool default_enabled);
118-
119-
private:
120-
Direction direction_enabled_;
121-
};
122-
12398
// EnableExternalIPs will check for the existence
12499
// of a runtime configuration, and defer to that value
125100
// otherwise, we rely on the feature flag (env var)
@@ -287,6 +262,5 @@ class CollectorConfig {
287262
};
288263

289264
std::ostream& operator<<(std::ostream& os, const CollectorConfig& c);
290-
std::ostream& operator<<(std::ostream& os, const collector::CollectorConfig::ExternalIPsConfig& config);
291265

292266
} // end namespace collector

collector/lib/ExternalIPsConfig.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "ExternalIPsConfig.h"
2+
3+
namespace collector {
4+
5+
ExternalIPsConfig::ExternalIPsConfig(std::optional<sensor::CollectorConfig> runtime_config, bool default_enabled) {
6+
if (!runtime_config.has_value()) {
7+
direction_enabled_ = default_enabled ? Direction::BOTH : Direction::NONE;
8+
return;
9+
}
10+
11+
// At this point we know runtime_config has a value, we can access it directly
12+
const auto& external_ips = runtime_config->networking().external_ips();
13+
if (external_ips.enabled() != sensor::ExternalIpsEnabled::ENABLED) {
14+
direction_enabled_ = Direction::NONE;
15+
return;
16+
}
17+
18+
switch (external_ips.direction()) {
19+
case sensor::ExternalIpsDirection::INGRESS:
20+
direction_enabled_ = Direction::INGRESS;
21+
break;
22+
case sensor::ExternalIpsDirection::EGRESS:
23+
direction_enabled_ = Direction::EGRESS;
24+
break;
25+
default:
26+
direction_enabled_ = Direction::BOTH;
27+
break;
28+
}
29+
}
30+
31+
std::ostream& operator<<(std::ostream& os, const ExternalIPsConfig& config) {
32+
os << "direction(";
33+
34+
switch (config.GetDirection()) {
35+
case ExternalIPsConfig::Direction::NONE:
36+
os << "NONE";
37+
break;
38+
case ExternalIPsConfig::Direction::INGRESS:
39+
os << "INGRESS";
40+
break;
41+
case ExternalIPsConfig::Direction::EGRESS:
42+
os << "EGRESS";
43+
break;
44+
case ExternalIPsConfig::Direction::BOTH:
45+
os << "BOTH";
46+
break;
47+
default:
48+
os << "invalid";
49+
break;
50+
}
51+
52+
return os << ")";
53+
}
54+
55+
} // namespace collector

collector/lib/ExternalIPsConfig.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <optional>
4+
#include <ostream>
5+
6+
#include <internalapi/sensor/collector.pb.h>
7+
8+
namespace collector {
9+
10+
// Encapsulates the configuration of the External-IPs feature
11+
class ExternalIPsConfig {
12+
public:
13+
enum Direction {
14+
NONE = 0,
15+
INGRESS = 1 << 0,
16+
EGRESS = 1 << 1,
17+
BOTH = INGRESS | EGRESS,
18+
};
19+
20+
// Are External-IPs enabled in the provided direction ?
21+
bool IsEnabled(Direction direction) const { return (direction & direction_enabled_) == direction; }
22+
23+
// Direction in which External-IPs are enabled
24+
Direction GetDirection() const { return direction_enabled_; }
25+
26+
// Extract the External-IPs configuration from the provided runtime-conf.
27+
// If the runtime-configuration is unset then 'default_enabled' is used
28+
// as a fallback to enable in both directions.
29+
// 'runtime_config' should be locked prior to calling.
30+
ExternalIPsConfig(std::optional<sensor::CollectorConfig> runtime_config, bool default_enabled);
31+
32+
ExternalIPsConfig() : direction_enabled_(Direction::NONE) {}
33+
34+
private:
35+
Direction direction_enabled_;
36+
};
37+
38+
std::ostream& operator<<(std::ostream& os, const ExternalIPsConfig& config);
39+
40+
} // end namespace collector

collector/lib/NetworkStatusNotifier.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ namespace collector {
1616

1717
namespace {
1818

19-
using Direction = CollectorConfig::ExternalIPsConfig::Direction;
20-
2119
storage::L4Protocol TranslateL4Protocol(L4Proto proto) {
2220
switch (proto) {
2321
case L4Proto::TCP:
@@ -227,7 +225,7 @@ void NetworkStatusNotifier::RunSingle(IDuplexClientWriter<sensor::NetworkConnect
227225
auto next_scrape = std::chrono::system_clock::now();
228226
int64_t time_at_last_scrape = NowMicros();
229227

230-
CollectorConfig::ExternalIPsConfig prevEnableExternalIPs = config_.ExternalIPsConf();
228+
ExternalIPsConfig prevEnableExternalIPs = config_.ExternalIPsConf();
231229

232230
while (writer->Sleep(next_scrape)) {
233231
CLOG(DEBUG) << "Starting network status notification";
@@ -244,12 +242,12 @@ void NetworkStatusNotifier::RunSingle(IDuplexClientWriter<sensor::NetworkConnect
244242
const sensor::NetworkConnectionInfoMessage* msg;
245243
ConnMap new_conn_state, delta_conn;
246244
AdvertisedEndpointMap new_cep_state;
247-
CollectorConfig::ExternalIPsConfig externalIPsConfig = config_.ExternalIPsConf();
245+
ExternalIPsConfig externalIPsConfig = config_.ExternalIPsConf();
248246

249247
WITH_TIMER(CollectorStats::net_fetch_state) {
250248
conn_tracker_->EnableExternalIPs(
251-
externalIPsConfig.IsEnabled(Direction::INGRESS),
252-
externalIPsConfig.IsEnabled(Direction::EGRESS));
249+
externalIPsConfig.IsEnabled(ExternalIPsConfig::Direction::INGRESS),
250+
externalIPsConfig.IsEnabled(ExternalIPsConfig::Direction::EGRESS));
253251

254252
new_conn_state = conn_tracker_->FetchConnState(true, true);
255253
if (config_.EnableAfterglow()) {

collector/test/CollectorConfigTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "gtest/gtest.h"
99

1010
using namespace testing;
11-
using Direction = collector::CollectorConfig::ExternalIPsConfig::Direction;
11+
using Direction = collector::ExternalIPsConfig::Direction;
1212

1313
namespace collector {
1414

@@ -149,7 +149,7 @@ TEST(CollectorConfigTest, TestEnableExternalIpsRuntimeConfig) {
149149
external_ips_config->set_enabled(sensor::ExternalIpsEnabled::ENABLED);
150150
config.SetRuntimeConfig(runtime_config);
151151

152-
EXPECT_EQ(CollectorConfig::ExternalIPsConfig::Direction::BOTH, config.ExternalIPsConf().GetDirection());
152+
EXPECT_EQ(Direction::BOTH, config.ExternalIPsConf().GetDirection());
153153
EXPECT_TRUE(config.ExternalIPsConf().IsEnabled(Direction::INGRESS));
154154
EXPECT_TRUE(config.ExternalIPsConf().IsEnabled(Direction::EGRESS));
155155
EXPECT_TRUE(config.ExternalIPsConf().IsEnabled(Direction::BOTH));

collector/test/ConfigLoaderTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace collector {
1313
using namespace google::protobuf::util;
14-
using Direction = CollectorConfig::ExternalIPsConfig::Direction;
14+
using Direction = ExternalIPsConfig::Direction;
1515

1616
std::string ErrorsToString(const std::vector<ParserError>& errors) {
1717
std::stringstream ss;

0 commit comments

Comments
 (0)