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:, internal_mounts: [], external_mounts: [], node_selector:) ⇒ 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.
12 13 14 15 16 |
# File 'app/services/kubernetes_client.rb', line 12 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.
10 11 12 |
# File 'app/services/kubernetes_client.rb', line 10 def bearer_token @bearer_token end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
10 11 12 |
# File 'app/services/kubernetes_client.rb', line 10 def namespace @namespace end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
10 11 12 |
# File 'app/services/kubernetes_client.rb', line 10 def uri @uri end |
Instance Method Details
#api_info ⇒ Object
18 19 20 |
# File 'app/services/kubernetes_client.rb', line 18 def api_info handle_exception { pod_client.api } end |
#create_pod(pod_name:, image:, cmd:, internal_mounts: [], external_mounts: [], node_selector:) ⇒ Object
rubocop:disable Metrics/ParameterLists
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 |
# File 'app/services/kubernetes_client.rb', line 23 def create_pod(pod_name:, image:, cmd:, internal_mounts: [], external_mounts: [], node_selector:) 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
63 64 65 |
# File 'app/services/kubernetes_client.rb', line 63 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
53 54 55 56 57 58 59 60 61 |
# File 'app/services/kubernetes_client.rb', line 53 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
71 72 73 74 75 76 77 78 79 |
# File 'app/services/kubernetes_client.rb', line 71 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
67 68 69 |
# File 'app/services/kubernetes_client.rb', line 67 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
81 82 83 84 85 86 87 |
# File 'app/services/kubernetes_client.rb', line 81 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 |