From 01e8cda89a8c5baf2dc787d8bd8e40fed16fe2c6 Mon Sep 17 00:00:00 2001 From: Philipp Thun Date: Tue, 10 Jun 2025 14:43:24 +0200 Subject: [PATCH] Workaround changed behavior in Protobuf's Message#to_h The behavior of 'to_h' for Protobuf objects changed [1]; unset and default values are now being omitted. Thus the update of Protobuf libraries [2] led to an incompatible change as the Cloud Controller returned "null" instead of "0" for the external and internal ports as part of the process stats endpoint. With this change the hash is created manually by accessing all fields in the Protobuf object(s) directly. [1] https://github.com/protocolbuffers/protobuf/pull/15234 [2] https://github.com/cloudfoundry/cloud_controller_ng/pull/4359 --- .../reporters/instances_stats_reporter.rb | 18 +++++++++++++++- .../instances_stats_reporter_spec.rb | 21 ++++++++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/cloud_controller/diego/reporters/instances_stats_reporter.rb b/lib/cloud_controller/diego/reporters/instances_stats_reporter.rb index b31d1713610..c4ea6e4eb73 100644 --- a/lib/cloud_controller/diego/reporters/instances_stats_reporter.rb +++ b/lib/cloud_controller/diego/reporters/instances_stats_reporter.rb @@ -78,7 +78,7 @@ def build_info(state, actual_lrp, process, stats, quota_stats, log_cache_errors) host: actual_lrp.actual_lrp_net_info.address, instance_guid: actual_lrp.actual_lrp_instance_key.instance_guid, port: get_default_port(actual_lrp.actual_lrp_net_info), - net_info: actual_lrp.actual_lrp_net_info.to_h, + net_info: actual_lrp_net_info_to_hash(actual_lrp.actual_lrp_net_info), uptime: nanoseconds_to_seconds((Time.now.to_f * 1e9) - actual_lrp.since), fds_quota: process.file_descriptors }.merge(metrics_data_for_instance(stats, quota_stats, log_cache_errors, Time.now.to_datetime.rfc3339, actual_lrp.actual_lrp_key.index)) @@ -198,6 +198,22 @@ def get_default_port(net_info) 0 end + + def actual_lrp_net_info_to_hash(net_info) + %i[address instance_address ports preferred_address].index_with do |field_name| + if field_name == :ports + net_info.ports.map(&method(:port_mapping_to_hash)) + else + net_info.send(field_name) + end + end + end + + def port_mapping_to_hash(port_mapping) + %i[container_port container_tls_proxy_port host_port host_tls_proxy_port].index_with do |field_name| + port_mapping.send(field_name) + end + end end end end diff --git a/spec/unit/lib/cloud_controller/diego/reporters/instances_stats_reporter_spec.rb b/spec/unit/lib/cloud_controller/diego/reporters/instances_stats_reporter_spec.rb index ef393228292..2c702118907 100644 --- a/spec/unit/lib/cloud_controller/diego/reporters/instances_stats_reporter_spec.rb +++ b/spec/unit/lib/cloud_controller/diego/reporters/instances_stats_reporter_spec.rb @@ -78,6 +78,17 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:) [container_metric_batch] end + let(:expected_lrp_1_net_info) do + { + address: 'lrp-host', + instance_address: '', + ports: [ + { container_port: DEFAULT_APP_PORT, container_tls_proxy_port: 0, host_port: 2222, host_tls_proxy_port: 0 }, + { container_port: 1111, container_tls_proxy_port: 0, host_port: 0, host_tls_proxy_port: 0 } + ], + preferred_address: :UNKNOWN + } + end let(:expected_stats_response) do { 0 => { @@ -90,7 +101,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:) host: 'lrp-host', instance_guid: 'instance-a', port: 2222, - net_info: lrp_1_net_info.to_h, + net_info: expected_lrp_1_net_info, uptime: two_days_in_seconds, mem_quota: 1234, disk_quota: 10_234, @@ -217,7 +228,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:) host: 'lrp-host', instance_guid: 'instance-a', port: 2222, - net_info: lrp_1_net_info.to_h, + net_info: expected_lrp_1_net_info, uptime: two_days_in_seconds, mem_quota: nil, disk_quota: nil, @@ -272,7 +283,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:) host: 'lrp-host', instance_guid: 'instance-a', port: 2222, - net_info: lrp_1_net_info.to_h, + net_info: expected_lrp_1_net_info, uptime: two_days_in_seconds, mem_quota: nil, disk_quota: nil, @@ -519,7 +530,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:) host: 'lrp-host', instance_guid: 'instance-a', port: 2222, - net_info: lrp_1_net_info.to_h, + net_info: expected_lrp_1_net_info, uptime: two_days_in_seconds, mem_quota: nil, disk_quota: nil, @@ -653,7 +664,7 @@ def make_actual_lrp(instance_guid:, index:, state:, error:, since:) host: 'lrp-host', instance_guid: 'instance-a', port: 2222, - net_info: lrp_1_net_info.to_h, + net_info: expected_lrp_1_net_info, uptime: two_days_in_seconds, mem_quota: nil, disk_quota: nil,