forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_orchestrator.rb
94 lines (76 loc) · 2.47 KB
/
container_orchestrator.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
require 'kubeclient'
class ContainerOrchestrator
include_concern 'ObjectDefinition'
TOKEN_FILE = "/run/secrets/kubernetes.io/serviceaccount/token".freeze
CA_CERT_FILE = "/run/secrets/kubernetes.io/serviceaccount/ca.crt".freeze
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)
end
def create_deployment_config(name)
definition = deployment_config_definition(name)
yield(definition) if block_given?
connection.create_deployment_config(definition)
rescue KubeException => e
raise unless e.message =~ /already exists/
end
def create_service(name, port)
definition = service_definition(name, port)
yield(definition) if block_given?
kube_connection.create_service(definition)
rescue KubeException => e
raise unless e.message =~ /already exists/
end
def create_secret(name, data)
definition = secret_definition(name, data)
yield(definition) if block_given?
kube_connection.create_secret(definition)
rescue KubeException => e
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
scale(name, 0)
connection.delete_deployment_config(name, my_namespace)
delete_replication_controller(rc.metadata.name) if rc
end
def delete_replication_controller(name)
kube_connection.delete_replication_controller(name, my_namespace)
end
def delete_service(name)
kube_connection.delete_service(name, my_namespace)
end
def delete_secret(name)
kube_connection.delete_secret(name, my_namespace)
end
private
def connection
@connection ||= raw_connect(manager_uri("/oapi"))
end
def kube_connection
@kube_connection ||= raw_connect(manager_uri("/api"))
end
def raw_connect(uri)
ssl_options = {
:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
:ca_file => CA_CERT_FILE
}
Kubeclient::Client.new(
uri,
:auth_options => { :bearer_token_file => TOKEN_FILE },
:ssl_options => ssl_options
)
end
def manager_uri(path)
URI::HTTPS.build(
:host => ENV["KUBERNETES_SERVICE_HOST"],
:port => ENV["KUBERNETES_SERVICE_PORT"],
:path => path
)
end
end