-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcontainer_manager.rb
152 lines (124 loc) · 4.46 KB
/
container_manager.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
ManageIQ::Providers::Kubernetes::ContainerManager.include(ActsAsStiLeafClass)
class ManageIQ::Providers::Openshift::ContainerManager < ManageIQ::Providers::Kubernetes::ContainerManager
DEFAULT_EXTERNAL_LOGGING_ROUTE_NAME = "logging-kibana-ops".freeze
has_one :infra_manager,
:foreign_key => :parent_ems_id,
:class_name => "ManageIQ::Providers::Openshift::InfraManager",
:autosave => true,
:inverse_of => :parent_manager,
:dependent => :destroy
include ManageIQ::Providers::Openshift::ContainerManager::Options
supports :catalog
supports :create
supports :external_logging
def self.ems_type
@ems_type ||= "openshift".freeze
end
def self.description
@description ||= "OpenShift".freeze
end
def self.event_monitor_class
ManageIQ::Providers::Openshift::ContainerManager::EventCatcher
end
def self.virtualization_options
[
{
:label => _('Disabled'),
:value => 'none',
},
{
:label => _('OpenShift Virtualization'),
:value => 'kubevirt',
:pivot => 'endpoints.kubevirt.hostname',
},
]
end
def self.raw_connect(hostname, port, options)
options[:service] ||= "openshift"
send("#{options[:service]}_connect", hostname, port, options)
end
def self.openshift_connect(hostname, port, options)
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)
kubernetes_connect(hostname, port, options)
end
def self.verify_default_credentials(hostname, port, options)
return false unless super
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)
# TODO: is there a more general way of detecting this?
case kind
when "BuildConfig", "Build"
"build.openshift.io/v1"
when "DeploymentConfig"
"apps.openshift.io/v1"
when "Image"
"image.openshift.io/v1"
when "Project"
"project.openshift.io/v1"
when "Route"
"route.openshift.io/v1"
when "Template"
"template.openshift.io/v1"
end
end
def v3?
api_version.to_s.split(".").first == "3"
end
def v4?
api_version.to_s.split(".").first == "4"
end
def connect_client(kind, api_version, method_name)
@clients ||= {}
api, version = api_version.split('/', 2)
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)
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
@clients[openshift_client_key].respond_to?(method_name) ? @clients[openshift_client_key] : @clients[kubernetes_client_key]
end
end
def hostname_for_service(service_type)
openshift_route_and_project = {
"prometheus" => %w[prometheus-k8s openshift-monitoring],
}
route_name, project_name = openshift_route_and_project[service_type]
return if route_name.nil?
routes = connect(:service => "openshift", :api_group => "route.openshift.io/v1")
routes.get_route(route_name, project_name)&.spec&.host
end
def external_logging_route_name
DEFAULT_EXTERNAL_LOGGING_ROUTE_NAME
end
def external_logging_query
nil # should be empty to return all
end
def external_logging_path
'/'
end
def create_project(project)
connect(:api_group => "project.openshift.io/v1").create_project_request(project)
rescue KubeException => e
raise MiqException::MiqProvisionError, "Unexpected Exception while creating project: #{e}"
end
def self.catalog_types
{"generic_container_template" => N_("OpenShift Template")}
end
def self.display_name(number = 1)
n_('Container Provider (OpenShift)', 'Container Providers (OpenShift)', number)
end
end