diff --git a/app/models/manageiq/providers/openshift/container_manager.rb b/app/models/manageiq/providers/openshift/container_manager.rb index 1b6b072..a8e4ae0 100644 --- a/app/models/manageiq/providers/openshift/container_manager.rb +++ b/app/models/manageiq/providers/openshift/container_manager.rb @@ -50,7 +50,7 @@ def self.raw_connect(hostname, port, options) end def self.openshift_connect(hostname, port, options) - api_group = options[:api_group] || "apps.openshift.io/v1" + api_group = options[:api_group] || "config.openshift.io/v1" api_path, api_version = api_group.split("/") options = {:path => "/apis/#{api_path}", :version => api_version}.merge(options) @@ -62,6 +62,10 @@ def self.verify_default_credentials(hostname, port, options) ocp = openshift_connect(hostname, port, options) !!ocp&.api_valid? + rescue Kubeclient::ResourceNotFoundError + # If the /apis/config.openshift.io/v1 endpoint isn't available then we have + # connected to an unsupported version of openshift + raise MiqException::Error, _("Unsupported OpenShift version") end def self.api_group_for_kind(kind) @@ -96,15 +100,16 @@ def connect_client(kind, api_version, method_name) if version @clients[api_version] ||= connect(:service => 'kubernetes', :version => version, :path => '/apis/' + api) else + kubernetes_client_key = File.join("/api", api_version) + @clients[kubernetes_client_key] ||= connect(:service => 'kubernetes', :version => api_version) + # If we're given an OpenShift object lookup its v4 API Group api_group = self.class.api_group_for_kind(kind) - path = api_group ? "/apps/#{api_group}" : "/oapi" + if api_group + openshift_client_key = File.join(path, "/apps/#{api_group}") + @clients[openshift_client_key] ||= connect(:api_group => api_group, :version => api_version) + end - openshift_client_key = File.join(path, api_version) - kubernetes_client_key = File.join("/api", api_version) - - @clients[openshift_client_key] ||= connect(:api_group => api_group, :version => api_version) - @clients[kubernetes_client_key] ||= connect(:service => 'kubernetes', :version => api_version) @clients[openshift_client_key].respond_to?(method_name) ? @clients[openshift_client_key] : @clients[kubernetes_client_key] end end diff --git a/lib/manageiq-providers-openshift.rb b/lib/manageiq-providers-openshift.rb deleted file mode 100644 index 2edabb0..0000000 --- a/lib/manageiq-providers-openshift.rb +++ /dev/null @@ -1,2 +0,0 @@ -require "manageiq/providers/openshift/engine" -require "manageiq/providers/openshift/version" diff --git a/lib/manageiq/providers/openshift.rb b/lib/manageiq/providers/openshift.rb index f484bb8..773b09a 100644 --- a/lib/manageiq/providers/openshift.rb +++ b/lib/manageiq/providers/openshift.rb @@ -1 +1,3 @@ require "manageiq/providers/openshift/engine" +require "manageiq/providers/openshift/version" + diff --git a/spec/models/manageiq/providers/openshift/container_manager_spec.rb b/spec/models/manageiq/providers/openshift/container_manager_spec.rb index f027d84..29f7072 100644 --- a/spec/models/manageiq/providers/openshift/container_manager_spec.rb +++ b/spec/models/manageiq/providers/openshift/container_manager_spec.rb @@ -1,4 +1,19 @@ describe ManageIQ::Providers::Openshift::ContainerManager do + describe ".verify_default_credentials" do + context "with a v3 cluster" do + it "raises an unsupported exception" do + require "kubeclient" + + allow(Kubeclient::Client) + .to receive(:new) + .with(URI.parse("https://openshiftv3:8443/oapi"), "v1", anything) + .and_raise(Kubeclient::ResourceNotFoundError.new(404, "404 Not Found", nil)) + + expect { described_class.verify_default_credentials("openshiftv3", 8443, {:path => "/oapi"}) }.to raise_error(MiqException::Error, "Unsupported OpenShift version") + end + end + end + it "#catalog_types" do ems = FactoryBot.create(:ems_openshift) expect(ems.catalog_types).to include("generic_container_template") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1e2ff9f..bc6814a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,7 +7,7 @@ Dir[ManageIQ::Providers::Kubernetes::Engine.root.join("spec/support/**/*.rb")].each { |f| require f } Dir[File.join(__dir__, "support/**/*.rb")].each { |f| require f } -require "manageiq-providers-openshift" +require "manageiq/providers/openshift" VCR.configure do |config| config.ignore_hosts 'codeclimate.com' if ENV['CI'] diff --git a/spec/vcr_cassettes/container_template.yml b/spec/vcr_cassettes/container_template.yml index 39fba27..99a1508 100644 --- a/spec/vcr_cassettes/container_template.yml +++ b/spec/vcr_cassettes/container_template.yml @@ -181,49 +181,6 @@ http_interactions: http_version: recorded_at: Tue, 10 Jan 2023 15:02:41 GMT - request: - method: get - uri: https://host.example.com:8443/apis/apps.openshift.io/v1 - body: - encoding: US-ASCII - string: '' - headers: - Accept: - - "*/*" - User-Agent: - - rest-client/2.1.0 (linux-gnu x86_64) ruby/3.0.4p208 - Authorization: - - Bearer theToken - Accept-Encoding: - - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 - response: - status: - code: 200 - message: OK - headers: - Audit-Id: - - 6f26dc0f-8946-4664-b7f4-a2e89eb250d8 - - 6f26dc0f-8946-4664-b7f4-a2e89eb250d8 - Cache-Control: - - no-cache, private - - no-store - Content-Length: - - '999' - Content-Type: - - application/json - Date: - - Tue, 10 Jan 2023 15:02:41 GMT - X-Kubernetes-Pf-Flowschema-Uid: - - 1b99b53f-667e-43b6-af56-957dcd30e4a7 - X-Kubernetes-Pf-Prioritylevel-Uid: - - 956bc4ac-462e-42d5-a2da-66c720f4480b - body: - encoding: UTF-8 - string: '{"kind":"APIResourceList","apiVersion":"v1","groupVersion":"apps.openshift.io/v1","resources":[{"name":"deploymentconfigs","singularName":"","namespaced":true,"kind":"DeploymentConfig","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["dc"],"categories":["all"],"storageVersionHash":"6xyoXGsxfiA="},{"name":"deploymentconfigs/instantiate","singularName":"","namespaced":true,"kind":"DeploymentRequest","verbs":["create"]},{"name":"deploymentconfigs/log","singularName":"","namespaced":true,"kind":"DeploymentLog","verbs":["get"]},{"name":"deploymentconfigs/rollback","singularName":"","namespaced":true,"kind":"DeploymentConfigRollback","verbs":["create"]},{"name":"deploymentconfigs/scale","singularName":"","namespaced":true,"group":"extensions","version":"v1beta1","kind":"Scale","verbs":["get","patch","update"]},{"name":"deploymentconfigs/status","singularName":"","namespaced":true,"kind":"DeploymentConfig","verbs":["get","patch","update"]}]} - - ' - http_version: - recorded_at: Tue, 10 Jan 2023 15:02:41 GMT -- request: method: get uri: https://host.example.com:8443/api/v1 body: