Skip to content

Commit

Permalink
Addition of OpenShift provider
Browse files Browse the repository at this point in the history
This change Contains:
provider, parser, refresher,
refresh worker, persistence of container_project and container_route
and autofill of default port values for both containers providers in the UI
  • Loading branch information
abonas committed May 11, 2015
1 parent f250585 commit cb5418a
Show file tree
Hide file tree
Showing 19 changed files with 304 additions and 75 deletions.
3 changes: 2 additions & 1 deletion lib/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ gem "memoist", "~>0.11.0", :require => false
gem "more_core_extensions", "~>1.2.0", :require => false
gem "nokogiri", "~>1.5.0", :require => false
gem "ovirt", "~>0.4.2", :require => false
gem "kubeclient", ">=0.1.11", :require => false
gem "kubeclient", ">=0.1.14", :require => false
gem "openshift_client", ">=0.0.5", :require => false
gem "rest-client", :require => false, :git => "git://github.com/rest-client/rest-client.git", :ref => "08480eb86aef1e"
gem "parallel", "~>0.5.21", :require => false
gem "Platform", "=0.4.0", :require => false
Expand Down
4 changes: 4 additions & 0 deletions vmdb/app/controllers/ems_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ def get_form_vars
@edit[:new][:emstype] = params[:server_emstype]
if ["openstack", "openstack_infra"].include?(params[:server_emstype])
@edit[:new][:port] = @ems.port ? @ems.port : 5000
elsif params[:server_emstype] == EmsKubernetes.ems_type
@edit[:new][:port] = @ems.port ? @ems.port : EmsKubernetes.new.port
elsif params[:server_emstype] == EmsOpenshift.ems_type
@edit[:new][:port] = @ems.port ? @ems.port : EmsOpenshift.new.port
else
@edit[:new][:port] = nil
end
Expand Down
15 changes: 15 additions & 0 deletions vmdb/app/helpers/ems_container_helper/textual_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def textual_group_properties

def textual_group_relationships
items = %w(container_nodes container_services container_groups)
if @ems.kind_of?(EmsOpenshift)
items.concat(%w(container_routes container_projects))
end
items.collect { |m| send("textual_#{m}") }.flatten.compact
end

Expand Down Expand Up @@ -72,6 +75,18 @@ def textual_refresh_status
}
end

def textual_container_routes
count_of_routes = @ems.number_of(:container_routes)
label = "Container Routes"
{:label => label, :image => "container_route", :value => count_of_routes}
end

def textual_container_projects
count_of_projects = @ems.number_of(:container_projects)
label = "Container Projects"
{:label => label, :image => "container_project", :value => count_of_projects}
end

def textual_container_nodes
count_of_nodes = @ems.number_of(:container_nodes)
label = "Container Nodes"
Expand Down
7 changes: 7 additions & 0 deletions vmdb/app/models/container_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ContainerProject < ActiveRecord::Base
include CustomAttributeMixin
include ReportableMixin
belongs_to :ext_management_system, :foreign_key => "ems_id"

has_many :labels, :class_name => CustomAttribute, :as => :resource, :conditions => {:section => "labels"}
end
7 changes: 7 additions & 0 deletions vmdb/app/models/container_route.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ContainerRoute < ActiveRecord::Base
include CustomAttributeMixin
include ReportableMixin

belongs_to :ext_management_system, :foreign_key => "ems_id"
has_many :labels, :class_name => CustomAttribute, :as => :resource, :conditions => {:section => "labels"}
end
79 changes: 6 additions & 73 deletions vmdb/app/models/ems_kubernetes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
class EmsKubernetes < EmsContainer
has_many :container_nodes, :foreign_key => :ems_id, :dependent => :destroy
has_many :container_groups, :foreign_key => :ems_id, :dependent => :destroy
has_many :container_services, :foreign_key => :ems_id, :dependent => :destroy
has_many :container_replication_controllers, :foreign_key => :ems_id, :dependent => :destroy

# TODO: support real authentication using certificates
before_validation :ensure_authentications_record
include ContainerProviderMixin

default_value_for :port, 6443

Expand All @@ -15,7 +9,7 @@ class EmsKubernetes < EmsContainer
# because it's not guaranteed that the next default version will work with
# our specific code in ManageIQ.
def self.api_version
'v1beta3'
kubernetes_version
end

def self.ems_type
Expand All @@ -26,72 +20,11 @@ def self.description
@description ||= "Kubernetes".freeze
end

def self.event_monitor_class
MiqEventCatcherKubernetes
end

def self.raw_connect(hostname, port)
require 'kubeclient'
api_endpoint = raw_api_endpoint(hostname, port)
kube = Kubeclient::Client.new(api_endpoint, api_version)
# TODO: support real authentication using certificates
kube.ssl_options(:verify_ssl => OpenSSL::SSL::VERIFY_NONE)
kube
end

def self.raw_api_endpoint(hostname, port)
URI::HTTPS.build(:host => hostname, :port => port.presence.try(:to_i))
end

# UI methods for determining availability of fields
def supports_port?
true
end

def api_endpoint
self.class.raw_api_endpoint(hostname, port)
end

def connect(options = {})
hostname = options[:hostname] || self.address
port = options[:port] || self.port

self.class.raw_connect(hostname, port)
end

def verify_credentials(auth_type = nil, options = {})
# TODO: support real authentication using certificates
options = options.merge(:auth_type => auth_type)

with_provider_connection(options, &:api_valid?)
rescue SocketError,
Errno::ECONNREFUSED,
RestClient::ResourceNotFound,
RestClient::InternalServerError => err
raise MiqException::MiqUnreachableError, err.message, err.backtrace
rescue RestClient::Unauthorized => err
raise MiqException::MiqInvalidCredentialsError, err.message, err.backtrace
def self.raw_connect(hostname, port, _service = nil)
kubernetes_connect(hostname, port)
end

# required by aggregate_hardware
def all_computer_system_ids
MiqPreloader.preload(container_nodes, :computer_system)
container_nodes.collect { |n| n.computer_system.id }
end

def aggregate_logical_cpus(targets = nil)
aggregate_hardware(:computer_systems, :logical_cpus, targets)
end

def aggregate_memory(targets = nil)
aggregate_hardware(:computer_systems, :memory_cpu, targets)
end

private

def ensure_authentications_record
# TODO: support real authentication using certificates
return if authentications.present?
update_authentication(:default => {:userid => "_", :save => false})
def self.event_monitor_class
MiqEventCatcherKubernetes
end
end
34 changes: 34 additions & 0 deletions vmdb/app/models/ems_openshift.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class EmsOpenshift < EmsContainer
include ContainerProviderMixin

has_many :container_routes, :foreign_key => :ems_id, :dependent => :destroy
has_many :container_projects, :foreign_key => :ems_id, :dependent => :destroy

default_value_for :port, 8443

def self.api_version
'v1beta1'
end

def self.ems_type
@ems_type ||= "openshift".freeze
end

def self.description
@description ||= "OpenShift".freeze
end

def self.raw_connect(hostname, port, service = nil)
service ||= "openshift"
send("#{service}_connect", hostname, port)
end

def self.openshift_connect(hostname, port)
require 'openshift_client'
api_endpoint = raw_api_endpoint(hostname, port)
osclient = OpenshiftClient::Client.new(api_endpoint, api_version)
# TODO: support real authentication using certificates
osclient.ssl_options(:verify_ssl => OpenSSL::SSL::VERIFY_NONE)
osclient
end
end
44 changes: 44 additions & 0 deletions vmdb/app/models/ems_refresh/parsers/openshift.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module EmsRefresh
module Parsers
class Openshift < Kubernetes
def ems_inv_to_hashes(inventory)
super(inventory)
get_projects(inventory)
get_routes(inventory)
EmsRefresh.log_inv_debug_trace(@data, "data:")
@data
end

def get_routes(inventory)
process_collection(inventory["route"], :container_routes) { |n| parse_route(n) }
end

def get_projects(inventory)
process_collection(inventory["project"], :container_projects) { |n| parse_project(n) }
end

def parse_project(project)
new_result = parse_base_item(project)
new_result.except!(:namespace)
new_result.merge!(
:labels => parse_labels(project),
:display_name => project.displayName
)
new_result
end

def parse_route(route)
new_result = parse_base_item(route)
new_result.merge!(
# TODO: persist tls
:host_name => route.host,
:labels => parse_labels(route),
# TODO: this part needs to be modified to service_id instead
:service_name => route.serviceName,
:path => route.path
)
new_result
end
end
end
end
14 changes: 14 additions & 0 deletions vmdb/app/models/ems_refresh/refreshers/openshift_refresher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module EmsRefresh
module Refreshers
class OpenshiftRefresher < BaseRefresher
include EmsRefresherMixin

def parse_inventory(ems, _targets = nil)
all_entities = ems.with_provider_connection(&:all_entities)
all_entities.merge!(ems.with_provider_connection(:service => EmsKubernetes.ems_type, &:all_entities))
EmsRefresh.log_inv_debug_trace(all_entities, "inv_hash:")
EmsRefresh::Parsers::Openshift.ems_inv_to_hashes(all_entities)
end
end
end
end
35 changes: 34 additions & 1 deletion vmdb/app/models/ems_refresh/save_inventory_container.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module EmsRefresh::SaveInventoryContainer
def save_ems_container_inventory(ems, hashes, target = nil)
target = ems if target.nil?
child_keys = [:container_nodes, :container_groups, :container_services, :container_replication_controllers]
child_keys = [:container_nodes, :container_groups, :container_services,
:container_replication_controllers, :container_routes, :container_projects]

# Save and link other subsections
child_keys.each do |k|
Expand All @@ -11,6 +12,38 @@ def save_ems_container_inventory(ems, hashes, target = nil)
ems.save!
end

def save_container_projects_inventory(ems, hashes, target = nil)
return if hashes.nil?
target = ems if target.nil?

ems.container_projects(true)
deletes = if target.kind_of?(ExtManagementSystem)
ems.container_projects.dup
else
[]
end

save_inventory_multi(:container_projects, ems, hashes, deletes, [:ems_ref],
:labels)
store_ids_for_new_records(ems.container_projects, hashes, :ems_ref)
end

def save_container_routes_inventory(ems, hashes, target = nil)
return if hashes.nil?
target = ems if target.nil?

ems.container_routes(true)
deletes = if target.kind_of?(ExtManagementSystem)
ems.container_routes.dup
else
[]
end

save_inventory_multi(:container_routes, ems, hashes, deletes, [:ems_ref],
:labels)
store_ids_for_new_records(ems.container_routes, hashes, :ems_ref)
end

def save_container_nodes_inventory(ems, hashes, target = nil)
return if hashes.nil?
target = ems if target.nil?
Expand Down
5 changes: 5 additions & 0 deletions vmdb/app/models/miq_ems_refresh_worker_openshift.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MiqEmsRefreshWorkerOpenshift < MiqEmsRefreshWorker
def self.ems_class
EmsOpenshift
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module MiqServer::WorkerManagement::Monitor::ClassNames
MiqEmsRefreshWorkerForemanConfiguration
MiqEmsRefreshWorkerForemanProvisioning
MiqEmsRefreshWorkerKubernetes
MiqEmsRefreshWorkerOpenshift
MiqEmsRefreshWorkerMicrosoft
MiqEmsRefreshWorkerRedhat
MiqEmsRefreshWorkerOpenstack
Expand Down Expand Up @@ -60,6 +61,7 @@ module MiqServer::WorkerManagement::Monitor::ClassNames
MiqEmsRefreshWorkerForemanConfiguration
MiqEmsRefreshWorkerForemanProvisioning
MiqEmsRefreshWorkerKubernetes
MiqEmsRefreshWorkerOpenshift
MiqEmsRefreshWorkerMicrosoft
MiqEmsRefreshWorkerRedhat
MiqEmsRefreshWorkerOpenstack
Expand Down
Loading

0 comments on commit cb5418a

Please sign in to comment.