-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from lfu/refactor_container_manager
Move kube/openshift specific template code into its provider directory.
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
app/models/manageiq/providers/kubernetes/container_manager/container_template.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class ManageIQ::Providers::Kubernetes::ContainerManager::ContainerTemplate < ManageIQ::Providers::ContainerManager::ContainerTemplate | ||
include ManageIQ::Providers::Kubernetes::ContainerManager::ContainerTemplateMixin | ||
end |
69 changes: 69 additions & 0 deletions
69
app/models/manageiq/providers/kubernetes/container_manager/container_template_mixin.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module ManageIQ::Providers::Kubernetes::ContainerManager::ContainerTemplateMixin | ||
extend ActiveSupport::Concern | ||
|
||
MIQ_ENTITY_MAPPING = { | ||
"Route" => ContainerRoute, | ||
"Build" => ContainerBuildPod, | ||
"BuildConfig" => ContainerBuild, | ||
"Template" => ContainerTemplate, | ||
"ResourceQuota" => ContainerQuota, | ||
"LimitRange" => ContainerLimit, | ||
"ReplicationController" => ContainerReplicator, | ||
"PersistentVolumeClaim" => PersistentVolumeClaim, | ||
"Pod" => ContainerGroup, | ||
"Service" => ContainerService, | ||
}.freeze | ||
|
||
def instantiate(params, project = nil) | ||
project ||= container_project.name | ||
processed_template = process_template(ext_management_system.connect, | ||
:metadata => { | ||
:name => name, | ||
:namespace => project | ||
}, | ||
:objects => objects, | ||
:parameters => params) | ||
create_objects(processed_template['objects'], project) | ||
@created_objects.each { |obj| obj[:miq_class] = MIQ_ENTITY_MAPPING[obj[:kind]] } | ||
end | ||
|
||
def process_template(client, template) | ||
client.process_template(template) | ||
rescue KubeException => e | ||
raise MiqException::MiqProvisionError, "Unexpected Exception while processing template: #{e}" | ||
end | ||
|
||
def create_objects(objects, project) | ||
@created_objects = [] | ||
objects.each { |obj| @created_objects << create_object(obj, project).to_h } | ||
end | ||
|
||
def create_object(obj, project) | ||
obj = obj.symbolize_keys | ||
obj[:metadata][:namespace] = project | ||
method_name = "create_#{obj[:kind].underscore}" | ||
begin | ||
ext_management_system.connect_client(obj[:apiVersion], method_name).send(method_name, obj) | ||
rescue KubeException => e | ||
rollback_objects(@created_objects) | ||
raise MiqException::MiqProvisionError, "Unexpected Exception while creating object: #{e}" | ||
end | ||
end | ||
|
||
# rollback_objects cannot catch children objects created during the template instantiation and therefore those objects | ||
# will remain in the cluster. | ||
def rollback_objects(objects) | ||
objects.each { |obj| rollback_object(obj) } | ||
end | ||
|
||
def rollback_object(obj) | ||
method_name = "delete_#{obj[:kind].underscore}" | ||
begin | ||
ext_management_system.connect_client(obj[:apiVersion], method_name).send(method_name, | ||
obj[:metadata][:name], | ||
obj[:metadata][:namespace]) | ||
rescue KubeException => e | ||
_log.error("Unexpected Exception while deleting object: #{e}") | ||
end | ||
end | ||
end |