Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MiqRegion#remote_*_url returning invalid URL on podified #22668

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions app/models/miq_region.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,49 +189,35 @@ def remote_ui_miq_server
end

def remote_ui_ipaddress
server = remote_ui_miq_server
server.try(:ipaddress)
remote_ui_miq_server&.ui_ipaddress
end

def remote_ui_hostname
server = remote_ui_miq_server
server && (server.hostname || server.ipaddress)
remote_ui_miq_server&.ui_hostname
end
Comment on lines 191 to 197
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE these methods don't appear to be used outside of the remote_*_address/url methods but since they aren't private I don't want to remove them especially for something we might backport.

remote_ui_ipaddress, remote_ui_hostname, remote_ws_ipaddress, remote_ws_hostname


def remote_ui_url(contact_with = :hostname)
svr = remote_ui_miq_server
remote_ui_url_override = svr.settings_for_resource.ui.url if svr
return remote_ui_url_override if remote_ui_url_override

hostname = send("remote_ui_#{contact_with}")
hostname && "https://#{hostname}"
remote_ui_miq_server&.ui_url(contact_with)
end

def remote_ws_miq_server
MiqServer.in_region(region).recently_active.find_by(:has_active_webservices => true)
end

def remote_ws_address
::Settings.webservices.contactwith == 'hostname' ? remote_ws_hostname : remote_ws_ipaddress
remote_ws_miq_server&.ws_address
end

def remote_ws_ipaddress
miq_server = remote_ws_miq_server
miq_server.try(:ipaddress)
remote_ws_miq_server&.ws_ipaddress
end

def remote_ws_hostname
miq_server = remote_ws_miq_server
miq_server && (miq_server.hostname || miq_server.ipaddress)
remote_ws_miq_server&.ws_hostname
end

def remote_ws_url
svr = remote_ws_miq_server
remote_url_override = svr.settings_for_resource.webservices.url if svr
return remote_url_override if remote_url_override

hostname = remote_ws_address
hostname && URI::HTTPS.build(:host => hostname).to_s
remote_ws_miq_server&.ws_url
end

def api_system_auth_token(userid)
Expand Down
68 changes: 68 additions & 0 deletions app/models/miq_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,74 @@ def logon_status_details
result.merge(:message => message)
end

def ui_hostname
if MiqEnvironment::Command.is_podified?
ENV.fetch("APPLICATION_DOMAIN")
else
hostname || ipaddress
end
end

def ui_ipaddress
if MiqEnvironment::Command.is_podified?
nil
else
ipaddress
end
end

def ui_address(contact_with = :hostname)
if MiqEnvironment::Command.is_podified?
ENV.fetch("APPLICATION_DOMAIN")
else
contact_with == :hostname ? ui_hostname : ui_ipaddress
end
Comment on lines +424 to +428
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE it is a bit of duplication to check for is_podified? in both these methods and in ui_address, but with the contact_with == :hostname and ::Settings.webservices.contactwith == 'hostname' checks it seemed cleaner than trying to deal with contact_with = :ipaddress but that returning nil on podified.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is it probably belongs in the kubernetes module for the MiqServer::WorkerManagement but we include kubernetes, process, or systemd regardless of the type of server we're running. I guess this is the only thing you can do.

There are these methods in worker_management though, although the interface is ugly from here: server.worker_manager.class.podified?

It feels like the worker_manager should know this answer for what type it is and cache the result. Alas, this code is fine for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(it would have been nice to just override this method in kubernetes.rb but we include these modules regardless of what type of manager we are)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I didn't think that WorkerManagement was the right place for this since it is more appliance-level but I agree it would be much cleaner to have a k8s subclass for this. Maybe we do a similar model for EnvironmentManagement (aka convert from simple module to a class then have systemd/kubernetes subclasses)?

end

def ui_url(contact_with = :hostname)
url_override = settings_for_resource.ui.url
return url_override if url_override

host = ui_address(contact_with)
return if host.nil?

URI::HTTPS.build(:host => host).to_s
end

def ws_hostname
if MiqEnvironment::Command.is_podified?
ENV.fetch("APPLICATION_DOMAIN")
else
hostname || ipaddress
end
end

def ws_ipaddress
if MiqEnvironment::Command.is_podified?
nil
else
ipaddress
end
end

def ws_address
if MiqEnvironment::Command.is_podified?
ENV.fetch("APPLICATION_DOMAIN")
else
::Settings.webservices.contactwith == 'hostname' ? ws_hostname : ws_ipaddress
end
end

def ws_url
url_override = settings_for_resource.webservices.url
return url_override if url_override

host = ws_address
return if host.nil?

URI::HTTPS.build(:host => host).to_s
end

#
# Zone and Role methods
#
Expand Down
30 changes: 26 additions & 4 deletions spec/models/miq_region_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
let(:url) { "https://www.manageiq.org" }
let!(:web_server) do
FactoryBot.create(:miq_server, :has_active_webservices => true,
:hostname => hostname,
:ipaddress => ip)
:hostname => hostname,
:ipaddress => ip)
end

it "fetches the url from server" do
Expand All @@ -134,6 +134,17 @@
Vmdb::Settings.save!(web_server, :webservices => {:url => url})
expect(region.remote_ws_url).to eq(url)
end

context "podified" do
before do
expect(MiqEnvironment::Command).to receive(:is_podified?).and_return(true)
expect(ENV).to receive(:fetch).with("APPLICATION_DOMAIN").and_return("manageiq.apps.mycluster.com")
end

it "returns the applicationDomain from the CR" do
expect(region.remote_ws_url).to eq("https://manageiq.apps.mycluster.com")
end
end
end

it "with no recently active servers" do
Expand All @@ -151,8 +162,8 @@
let(:url) { "http://localhost:3000" }
let!(:ui_server) do
FactoryBot.create(:miq_server, :has_active_userinterface => true,
:hostname => hostname,
:ipaddress => ip)
:hostname => hostname,
:ipaddress => ip)
end

it "fetches the url from server" do
Expand All @@ -163,6 +174,17 @@
Vmdb::Settings.save!(ui_server, :ui => {:url => url})
expect(region.remote_ui_url).to eq(url)
end

context "podified" do
before do
expect(MiqEnvironment::Command).to receive(:is_podified?).and_return(true)
expect(ENV).to receive(:fetch).with("APPLICATION_DOMAIN").and_return("manageiq.apps.mycluster.com")
end

it "returns the applicationDomain from the CR" do
expect(region.remote_ui_url).to eq("https://manageiq.apps.mycluster.com")
end
end
end

it "with no recently active servers" do
Expand Down