debian-mirror-gitlab/lib/gitlab/kubernetes/helm/api.rb

114 lines
3.3 KiB
Ruby
Raw Normal View History

2018-03-17 18:26:18 +05:30
module Gitlab
module Kubernetes
module Helm
class Api
def initialize(kubeclient)
@kubeclient = kubeclient
@namespace = Gitlab::Kubernetes::Namespace.new(Gitlab::Kubernetes::Helm::NAMESPACE, kubeclient)
end
def install(command)
2018-11-18 11:00:15 +05:30
namespace.ensure_exists!
2018-11-20 20:47:30 +05:30
create_service_account(command)
create_cluster_role_binding(command)
2018-11-18 11:00:15 +05:30
create_config_map(command)
2018-11-20 20:47:30 +05:30
2018-11-18 11:00:15 +05:30
kubeclient.create_pod(command.pod_resource)
2018-03-17 18:26:18 +05:30
end
2018-12-05 23:21:45 +05:30
def update(command)
namespace.ensure_exists!
update_config_map(command)
kubeclient.create_pod(command.pod_resource)
end
2018-03-17 18:26:18 +05:30
##
# Returns Pod phase
#
# https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase
#
# values: "Pending", "Running", "Succeeded", "Failed", "Unknown"
#
2018-11-18 11:00:15 +05:30
def status(pod_name)
kubeclient.get_pod(pod_name, namespace.name).status.phase
2018-03-17 18:26:18 +05:30
end
2018-11-18 11:00:15 +05:30
def log(pod_name)
kubeclient.get_pod_log(pod_name, namespace.name).body
2018-03-17 18:26:18 +05:30
end
2018-11-18 11:00:15 +05:30
def delete_pod!(pod_name)
kubeclient.delete_pod(pod_name, namespace.name)
2018-03-17 18:26:18 +05:30
end
2018-12-05 23:21:45 +05:30
def get_config_map(config_map_name)
namespace.ensure_exists!
kubeclient.get_config_map(config_map_name, namespace.name)
end
2018-03-17 18:26:18 +05:30
private
2018-11-18 11:00:15 +05:30
attr_reader :kubeclient, :namespace
2018-03-27 19:54:05 +05:30
def create_config_map(command)
command.config_map_resource.tap do |config_map_resource|
2018-11-18 11:00:15 +05:30
kubeclient.create_config_map(config_map_resource)
2018-03-27 19:54:05 +05:30
end
2018-03-17 18:26:18 +05:30
end
2018-11-20 20:47:30 +05:30
2018-12-05 23:21:45 +05:30
def update_config_map(command)
command.config_map_resource.tap do |config_map_resource|
kubeclient.update_config_map(config_map_resource)
end
end
2018-11-20 20:47:30 +05:30
def create_service_account(command)
command.service_account_resource.tap do |service_account_resource|
break unless service_account_resource
if service_account_exists?(service_account_resource)
kubeclient.update_service_account(service_account_resource)
else
kubeclient.create_service_account(service_account_resource)
end
end
end
def create_cluster_role_binding(command)
command.cluster_role_binding_resource.tap do |cluster_role_binding_resource|
break unless cluster_role_binding_resource
if cluster_role_binding_exists?(cluster_role_binding_resource)
kubeclient.update_cluster_role_binding(cluster_role_binding_resource)
else
kubeclient.create_cluster_role_binding(cluster_role_binding_resource)
end
end
end
def service_account_exists?(resource)
resource_exists? do
kubeclient.get_service_account(resource.metadata.name, resource.metadata.namespace)
end
end
def cluster_role_binding_exists?(resource)
resource_exists? do
kubeclient.get_cluster_role_binding(resource.metadata.name)
end
end
def resource_exists?
yield
rescue ::Kubeclient::HttpError => e
raise e unless e.error_code == 404
false
end
2018-03-17 18:26:18 +05:30
end
end
end
end