Class: KubernetesClient
- Inherits:
-
Object
- Object
- KubernetesClient
- Defined in:
- app/services/kubernetes_client.rb
Defined Under Namespace
Classes: LogsNotFoundError, NetworkError, PodNotFoundError
Constant Summary collapse
- LOG_UNAVAILABLE_HTTP_ERROR =
400
Instance Attribute Summary collapse
-
#bearer_token ⇒ Object
readonly
Returns the value of attribute bearer_token.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #api_info ⇒ Object
-
#create_pod(pod_name:, image:, cmd:, node_selector:, internal_mounts: [], external_mounts: []) ⇒ Object
rubocop:disable Metrics/ParameterLists.
- #fetch_pod(pod_name:) ⇒ Object
-
#fetch_pod_logs(pod_name:) ⇒ Object
rubocop:enable Metrics/ParameterLists.
- #fetch_pods ⇒ Object
- #force_delete_pod(pod_name:) ⇒ Object
- #handle_exception(pod_name = nil) ⇒ Object
-
#initialize(uri:, bearer_token:, namespace:) ⇒ KubernetesClient
constructor
A new instance of KubernetesClient.
Constructor Details
#initialize(uri:, bearer_token:, namespace:) ⇒ KubernetesClient
Returns a new instance of KubernetesClient.
14 15 16 17 18 |
# File 'app/services/kubernetes_client.rb', line 14 def initialize(uri:, bearer_token:, namespace:) @uri = uri @bearer_token = bearer_token @namespace = namespace end |
Instance Attribute Details
#bearer_token ⇒ Object (readonly)
Returns the value of attribute bearer_token.
12 13 14 |
# File 'app/services/kubernetes_client.rb', line 12 def bearer_token @bearer_token end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
12 13 14 |
# File 'app/services/kubernetes_client.rb', line 12 def namespace @namespace end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
12 13 14 |
# File 'app/services/kubernetes_client.rb', line 12 def uri @uri end |
Instance Method Details
#api_info ⇒ Object
20 21 22 |
# File 'app/services/kubernetes_client.rb', line 20 def api_info handle_exception { pod_client.api } end |
#create_pod(pod_name:, image:, cmd:, node_selector:, internal_mounts: [], external_mounts: []) ⇒ Object
rubocop:disable Metrics/ParameterLists
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 |
# File 'app/services/kubernetes_client.rb', line 25 def create_pod(pod_name:, image:, cmd:, node_selector:, internal_mounts: [], external_mounts: []) handle_exception do pod = Kubeclient::Resource.new( metadata: { name: pod_name, namespace: namespace }, spec: { containers: [ (name: pod_name, image: image, cmd: cmd, internal_mounts: internal_mounts) ], restartPolicy: "Never", nodeSelector: { node_selector => "" }, tolerations: [ { key: node_selector, effect: "NoSchedule" } ], volumes: external_mounts } ) pod_client.create_pod(pod) pod_name end end |
#fetch_pod(pod_name:) ⇒ Object
65 66 67 |
# File 'app/services/kubernetes_client.rb', line 65 def fetch_pod(pod_name:) handle_exception(pod_name) { pod_client.get_pod(pod_name, namespace) } end |
#fetch_pod_logs(pod_name:) ⇒ Object
rubocop:enable Metrics/ParameterLists
55 56 57 58 59 60 61 62 63 |
# File 'app/services/kubernetes_client.rb', line 55 def fetch_pod_logs(pod_name:) handle_exception(pod_name) do pod_client.get_pod_log(pod_name, namespace).body rescue Kubeclient::HttpError => e raise LogsNotFoundError if e.error_code == LOG_UNAVAILABLE_HTTP_ERROR raise end end |
#fetch_pods ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'app/services/kubernetes_client.rb', line 73 def fetch_pods handle_exception do pod_client .get_pods(namespace: namespace) .each_with_object({}) do |pod, result| result[pod..name] = pod end end end |
#force_delete_pod(pod_name:) ⇒ Object
69 70 71 |
# File 'app/services/kubernetes_client.rb', line 69 def force_delete_pod(pod_name:) handle_exception(pod_name) { pod_client.delete_pod(pod_name, namespace, delete_options: ) } end |
#handle_exception(pod_name = nil) ⇒ Object
83 84 85 86 87 88 89 |
# File 'app/services/kubernetes_client.rb', line 83 def handle_exception(pod_name = nil) yield rescue Kubeclient::ResourceNotFoundError raise PodNotFoundError, "Pod not found #{pod_name}" rescue Kubeclient::HttpError, SocketError, Errno::ECONNREFUSED, OpenSSL::SSL::SSLError => e raise NetworkError, "#{e.class}: #{e.}" end |