-
Notifications
You must be signed in to change notification settings - Fork 898
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
Instantiate Container Template #10737
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,68 @@ class ContainerTemplate < ApplicationRecord | |
serialize :objects, Array | ||
|
||
acts_as_miq_taggable | ||
|
||
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[:kind] = MIQ_ENTITY_MAPPING[obj[:kind]] } | ||
end | ||
|
||
def process_template(client, template) | ||
client.process_template(template) | ||
rescue KubeException => e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We normally rescue provider exceptions and re-raise a corresponding miq-exception. |
||
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 | ||
|
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,10 @@ def self.event_monitor_class | |
def supported_auth_attributes | ||
%w(userid password auth_key) | ||
end | ||
|
||
def create_project(project) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bzwei |
||
connect.create_project_request(project) | ||
rescue KubeException => e | ||
raise MiqException::MiqProvisionError, "Unexpected Exception while creating project: #{e}" | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@simon3z I added this mapping (only for objects we collect & can instantiate) so the automation side will be able to know in which table to look for the objects.
We may consider adding a more general mapping to reuse it later on in different places.
cc @moolitayer