Skip to content

Commit

Permalink
Fixes #36928 - (sorta) handle load balanced smart proxies when regist…
Browse files Browse the repository at this point in the history
…ering hosts
  • Loading branch information
jeremylenz committed Nov 17, 2023
1 parent c045d9b commit 4245df0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
19 changes: 16 additions & 3 deletions app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ def enabled_repos
#api :POST, "/environments/:environment_id/consumers", N_("Register a consumer in environment")
def consumer_create
host = Katello::RegistrationManager.process_registration(rhsm_params, find_content_view_environments)

host.reload

update_host_registered_through(host, request.headers)
Expand Down Expand Up @@ -240,8 +239,8 @@ def consumer_activate
activation_keys = find_activation_keys

host = Katello::RegistrationManager.process_registration(rhsm_params, nil, activation_keys)

update_host_registered_through(host, request.headers)

host.reload

render :json => Resources::Candlepin::Consumer.get(host.subscription_facet.uuid)
Expand Down Expand Up @@ -273,6 +272,7 @@ def get_parent_host(headers)
hostnames = headers["HTTP_X_FORWARDED_HOST"]
host = hostnames.split(/[,,:]/)[0].strip if hostnames
host || URI.parse(Setting[:foreman_url]).host
"loadbalancer.example.com"
end

def get_content_source_id(hostname)
Expand Down Expand Up @@ -474,7 +474,20 @@ def client_authorized?
def update_host_registered_through(host, headers)
parent_host = get_parent_host(headers)
host.subscription_facet.update_attribute(:registered_through, parent_host)
content_source_id = get_content_source_id(parent_host)
set_host_content_source(host, parent_host)
end

def registering_thru_load_balancer?(hostname)
::SmartProxy.behind_load_balancer(hostname).present?
end

def set_host_content_source(host, content_source_hostname)
content_source_id = get_content_source_id(content_source_hostname)
if registering_thru_load_balancer?(content_source_hostname)
Rails.logger.info "Host %s registered through load balancer %s" % [host.name, parent_host]
content_source_id = ::SmartProxy.behind_load_balancer(content_source_hostname)&.first&.id
end
Rails.logger.warn "Host %s registered through unknown proxy %s" % [host.name, parent_host] if content_source_id.nil?
host.content_facet.update_attribute(:content_source_id, content_source_id)
end

Expand Down
11 changes: 8 additions & 3 deletions app/models/katello/concerns/host_managed_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,17 @@ def update_os_from_facts

def remote_execution_proxies(provider, *_rest)
proxies = super
if (name = subscription_facet&.registered_through)
registered_through = SmartProxy.with_features(provider)
name = subscription_facet&.registered_through
result = []
if name.present?
result = SmartProxy.with_features(provider)
.authorized
.where(name: name)
if result.blank?
result = SmartProxy.behind_load_balancer(name)
end
end
proxies[:registered_through] = registered_through || []
proxies[:registered_through] = result
proxies
end
end
Expand Down
21 changes: 21 additions & 0 deletions app/models/katello/concerns/smart_proxy_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ def refresh
}
scope :with_content, -> { with_features(PULP_FEATURE, PULP_NODE_FEATURE, PULP3_FEATURE) }

def self.load_balanced
proxies = self.with_content # load balancing is only supported for pulp proxies
ids = proxies.select { |proxy| proxy.load_balanced? }.map(&:id)
proxies.where(id: ids)
end

def self.behind_load_balancer(load_balancer_hostname)
proxies = self.with_content
ids = proxies.select { |proxy| proxy.load_balanced? && proxy.registration_host == load_balancer_hostname }.map(&:id)
proxies.where(id: ids)
end

def self.with_repo(repo)
joins(:capsule_lifecycle_environments).
where("#{Katello::CapsuleLifecycleEnvironment.table_name}.lifecycle_environment_id" => repo.environment_id)
Expand Down Expand Up @@ -121,6 +133,15 @@ def alternate_content_sources
SmartProxy.joins(:smart_proxy_alternate_content_sources).where('katello_smart_proxy_alternate_content_sources.smart_proxy_id' => self.id)
end

def registration_host
url = self.setting('Registration', 'registration_url').presence || self.url
name == "rhel8b.fedora.example.com" ? "loadbalancer.example.com" : URI.parse(url).host
end

def load_balanced?
URI.parse(self.url).host != self.registration_host
end

def update_content_counts!
# {:content_view_versions=>{87=>{:repositories=>{1=>{:metadata=>{},:counts=>{:rpms=>98, :module_streams=>9898}}}}}
new_content_counts = { content_view_versions: {} }
Expand Down

0 comments on commit 4245df0

Please sign in to comment.