Skip to content

Commit 30e03f7

Browse files
committed
policy: Add NetworkPolicyResourcesDiscoveryService
Add new cilium/versioned.h generic container for transactional selector updates. Add a new NetworkPolicyResourceDiscoveryService that implements delta (and SotW) updates for policies and selectors, and where policies refer to selectors by their resource name. NPRDS adds a top-level oneof wrapper that wraps either a Selector or a NetworkPolicy. NetworkPolicy definition is shared with NPDS, but PortNetworkPolicyRule adds a new selectors field that is only used with NPRDS. Add 'policy_type' enum to BpfMetadata config to control whether NPDS (default) or NPRDS is used. Store the latest desired ConfigSource in the policy map and use it for: - initial policy map subscription - re-subscription when connection under current subscription is terminated - a healthy network policy stream is not disrupted, unless the desired config is for delta xDS and the current one is not This means that we switch to NPRDS (Delta) mode eagerly when we have evidence that the agent is capable, but we switch to NPDS (SotW) mode only when xDS stream transport had failed to connect or closes. This should work for Cilium Agent upgrades and downgrades, as the agent expresses the desired mode, and listens for both. Clear the resource map on a first update on a new stream. This fixes NACK cases where further updates on the stream would have IP collisions with resources that were kept from the previous stream. Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
1 parent 329393a commit 30e03f7

18 files changed

Lines changed: 5532 additions & 329 deletions

cilium/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ envoy_cc_library(
2828
],
2929
)
3030

31+
envoy_cc_library(
32+
name = "versioned_lib",
33+
hdrs = ["versioned.h"],
34+
repository = "@envoy",
35+
deps = [
36+
"@com_google_absl//absl/container:flat_hash_map",
37+
"@com_google_absl//absl/container:flat_hash_set",
38+
"@envoy//source/common/common:assert_lib",
39+
],
40+
)
41+
3142
envoy_cc_library(
3243
name = "network_policy_lib",
3344
srcs = [
@@ -45,6 +56,7 @@ envoy_cc_library(
4556
"//cilium:conntrack_lib",
4657
"//cilium:grpc_subscription_lib",
4758
"//cilium:ipcache_lib",
59+
"//cilium:versioned_lib",
4860
"//cilium/api:npds_cc_proto",
4961
"@envoy//envoy/singleton:manager_interface",
5062
"@envoy//source/common/common:logger_lib",

cilium/api/bpf_metadata.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,11 @@ message BpfMetadata {
8686

8787
// Configuration for the source of Cilium xDS updates.
8888
envoy.config.core.v3.ConfigSource config_source = 16;
89+
90+
// Policy type to use
91+
enum PolicyType {
92+
NPDS = 0; // Legacy NPDS (default)
93+
NPRDS = 1; // New NetworkPolicyResource (NPRDS)
94+
}
95+
bool policy_type = 17;
8996
}

cilium/api/npds.proto

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import "validate/validate.proto";
1717
// [#protodoc-title: Network policy management and NPDS]
1818

1919
// Each resource name is a network policy identifier.
20+
// Deprecated: This service will be removed when Cilium 1.20 is the oldest supported release.
2021
service NetworkPolicyDiscoveryService {
2122
option (envoy.annotations.resource).type = "cilium.NetworkPolicy";
2223

@@ -37,6 +38,36 @@ service NetworkPolicyDiscoveryService {
3738
}
3839
}
3940

41+
// Policy and selector resource names are exact-match identifiers in NPRDS.
42+
service NetworkPolicyResourceDiscoveryService {
43+
option (envoy.annotations.resource).type = "cilium.NetworkPolicyResource";
44+
45+
rpc StreamNetworkPolicyResources(stream envoy.service.discovery.v3.DiscoveryRequest)
46+
returns (stream envoy.service.discovery.v3.DiscoveryResponse) {
47+
}
48+
49+
rpc DeltaNetworkPolicyResources(stream envoy.service.discovery.v3.DeltaDiscoveryRequest)
50+
returns (stream envoy.service.discovery.v3.DeltaDiscoveryResponse) {
51+
}
52+
}
53+
54+
// An NPRDS resource that carries either an endpoint policy or a shared selector.
55+
message NetworkPolicyResource {
56+
oneof resource {
57+
NetworkPolicy policy = 1;
58+
Selector selector = 2;
59+
}
60+
}
61+
62+
// A shared set of remote identities referenced by selector resource name.
63+
// Unlike the old state-of-the-world remote identity lists, an empty selector
64+
// matches nothing.
65+
message Selector {
66+
// The set of numeric remote security IDs selected by this selector.
67+
// If empty, this selector selects no remote identities.
68+
repeated uint32 remote_identities = 1;
69+
}
70+
4071
// A network policy that is enforced by a filter on the network flows to/from
4172
// associated hosts.
4273
message NetworkPolicy {
@@ -157,6 +188,12 @@ message PortNetworkPolicyRule {
157188
// Optional. If not specified, any remote host is matched by this predicate.
158189
repeated uint32 remote_policies = 7;
159190

191+
// Optional selector resource names that can be resolved to shared remote
192+
// policy sets in delta NPDS.
193+
// Selector references are matched by exact selector resource name.
194+
// Optional. If not specified, any remote host is matched by this predicate.
195+
repeated string selectors = 11;
196+
160197
// Optional downstream TLS context. If present, the incoming connection must
161198
// be a TLS connection.
162199
TLSContext downstream_tls_context = 3;

cilium/bpf_metadata.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,16 @@ Config::Config(const ::cilium::BpfMetadata& config,
281281
// instances!
282282
// Only created if either ipcache_ or hosts_ map exists
283283
if (ipcache_ || hosts_) {
284+
bool use_nprds = config.policy_type() == cilium::BpfMetadata::NPRDS;
284285
npmap_ =
285286
context.serverFactoryContext().singletonManager().getTyped<const Cilium::NetworkPolicyMap>(
286287
SINGLETON_MANAGER_REGISTERED_NAME(cilium_network_policy),
287-
[&context, config_source = config_source_] {
288-
return std::make_shared<Cilium::NetworkPolicyMap>(context, config_source, true);
288+
[&context, use_nprds, config_source = config_source_] {
289+
return std::make_shared<Cilium::NetworkPolicyMap>(context, use_nprds, config_source,
290+
true);
289291
});
290-
// update desired config source on the map
291-
npmap_->setConfigSource(config_source_);
292+
// update desired config on the map
293+
npmap_->setConfig(use_nprds, config_source_);
292294
}
293295
}
294296

cilium/grpc_subscription.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ TypeUrlToServiceMap* buildTypeUrlToServiceMap() {
8686
// https://www.mail-archive.com/protobuf@googlegroups.com/msg04540.html.
8787
for (absl::string_view name : {
8888
"cilium.NetworkPolicyDiscoveryService",
89+
"cilium.NetworkPolicyResourceDiscoveryService",
8990
"cilium.NetworkPolicyHostsDiscoveryService",
9091
}) {
9192
const auto* service_desc =

0 commit comments

Comments
 (0)