Class: KubernetesClient

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_tokenObject (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

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'app/services/kubernetes_client.rb', line 10

def namespace
  @namespace
end

#uriObject (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_infoObject



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: [
          container_options(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_podsObject



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: 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.message}"
end