Skip to content

Commit

Permalink
Kubernetes-ify the container orchestrator
Browse files Browse the repository at this point in the history
This will allow the orchestrator to run on either OpenShift
or Kubernetes by using only objects supported by both

Fixes ManageIQ#17845
  • Loading branch information
carbonin committed Nov 12, 2019
1 parent afbdc27 commit 0bba669
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/models/miq_worker/deployment_per_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module DeploymentPerWorker
extend ActiveSupport::Concern

def create_container_objects
ContainerOrchestrator.new.create_deployment_config(worker_deployment_name) do |definition|
ContainerOrchestrator.new.create_deployment(worker_deployment_name) do |definition|
configure_worker_deployment(definition, 1)
definition[:spec][:template][:spec][:containers].first[:env] << {:name => "EMS_IDS", :value => Array.wrap(self.class.ems_id_from_queue_name(queue_name)).join(",")}
end
end

def delete_container_objects
ContainerOrchestrator.new.delete_deployment_config(worker_deployment_name)
ContainerOrchestrator.new.delete_deployment(worker_deployment_name)
end

def stop_container
Expand Down
4 changes: 2 additions & 2 deletions app/models/miq_worker/replica_per_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module ReplicaPerWorker
extend ActiveSupport::Concern

def create_container_objects
ContainerOrchestrator.new.create_deployment_config(worker_deployment_name) do |definition|
ContainerOrchestrator.new.create_deployment(worker_deployment_name) do |definition|
configure_worker_deployment(definition)
end
scale_deployment
end

def delete_container_objects
ContainerOrchestrator.new.delete_deployment_config(worker_deployment_name)
ContainerOrchestrator.new.delete_deployment(worker_deployment_name)
end

def stop_container
Expand Down
4 changes: 2 additions & 2 deletions app/models/miq_worker/service_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def create_container_objects
orchestrator = ContainerOrchestrator.new

orchestrator.create_service(worker_deployment_name, SERVICE_PORT)
orchestrator.create_deployment_config(worker_deployment_name) do |definition|
orchestrator.create_deployment(worker_deployment_name) do |definition|
configure_worker_deployment(definition)

definition[:spec][:serviceName] = worker_deployment_name
Expand All @@ -24,7 +24,7 @@ def create_container_objects

def delete_container_objects
orch = ContainerOrchestrator.new
orch.delete_deployment_config(worker_deployment_name)
orch.delete_deployment(worker_deployment_name)
orch.delete_service(worker_deployment_name)
end

Expand Down
34 changes: 11 additions & 23 deletions lib/container_orchestrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ def self.available?
File.exist?(TOKEN_FILE) && File.exist?(CA_CERT_FILE)
end

def scale(deployment_config_name, replicas)
connection.patch_deployment_config(deployment_config_name, { :spec => { :replicas => replicas } }, my_namespace)
def scale(deployment_name, replicas)
kube_apps_connection.patch_deployment(deployment_name, { :spec => { :replicas => replicas } }, my_namespace)
end

def create_deployment_config(name)
definition = deployment_config_definition(name)
def create_deployment(name)
definition = deployment_definition(name)
yield(definition) if block_given?
connection.create_deployment_config(definition)
kube_apps_connection.create_deployment(definition)
rescue KubeException => e
raise unless e.message =~ /already exists/
end
Expand All @@ -38,21 +38,9 @@ def create_secret(name, data)
raise unless e.message =~ /already exists/
end

def delete_deployment_config(name)
rc = kube_connection.get_replication_controllers(
:label_selector => "openshift.io/deployment-config.name=#{name}",
:namespace => my_namespace
).first

def delete_deployment(name)
scale(name, 0)
connection.delete_deployment_config(name, my_namespace)
delete_replication_controller(rc.metadata.name) if rc
rescue KubeException => e
raise unless e.message =~ /not found/
end

def delete_replication_controller(name)
kube_connection.delete_replication_controller(name, my_namespace)
kube_apps_connection.delete_deployment(name, my_namespace)
rescue KubeException => e
raise unless e.message =~ /not found/
end
Expand All @@ -71,14 +59,14 @@ def delete_secret(name)

private

def connection
@connection ||= raw_connect(manager_uri("/oapi"))
end

def kube_connection
@kube_connection ||= raw_connect(manager_uri("/api"))
end

def kube_apps_connection
@kube_apps_connection ||= raw_connect(manager_uri("/apis/apps"))
end

def raw_connect(uri)
ssl_options = {
:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
Expand Down
5 changes: 2 additions & 3 deletions lib/container_orchestrator/object_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ class ContainerOrchestrator
module ObjectDefinition
private

def deployment_config_definition(name)
def deployment_definition(name)
{
:metadata => {
:name => name,
:labels => {:app => app_name},
:namespace => my_namespace,
},
:spec => {
:selector => {:name => name, :app => app_name},
:selector => {:matchLabels => {:name => name}},
:template => {
:metadata => {:name => name, :labels => {:name => name, :app => app_name}},
:spec => {
:serviceAccount => "miq-anyuid",
:serviceAccountName => "miq-anyuid",
:containers => [{
:name => name,
Expand Down

0 comments on commit 0bba669

Please sign in to comment.