2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
delete_pod!(command.pod_name)
|
2018-11-18 11:00:15 +05:30
|
|
|
kubeclient.create_pod(command.pod_resource)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
alias_method :update, :install
|
2018-12-05 23:21:45 +05:30
|
|
|
|
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)
|
2019-02-15 15:39:39 +05:30
|
|
|
rescue ::Kubeclient::ResourceNotFoundError
|
|
|
|
# no-op
|
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|
|
2019-03-02 22:35:43 +05:30
|
|
|
break unless config_map_resource
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
if config_map_exists?(config_map_resource)
|
|
|
|
kubeclient.update_config_map(config_map_resource)
|
|
|
|
else
|
|
|
|
kubeclient.create_config_map(config_map_resource)
|
|
|
|
end
|
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
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def config_map_exists?(resource)
|
|
|
|
kubeclient.get_config_map(resource.metadata.name, resource.metadata.namespace)
|
|
|
|
rescue ::Kubeclient::ResourceNotFoundError
|
|
|
|
false
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def service_account_exists?(resource)
|
|
|
|
kubeclient.get_service_account(resource.metadata.name, resource.metadata.namespace)
|
|
|
|
rescue ::Kubeclient::ResourceNotFoundError
|
|
|
|
false
|
2019-01-03 12:48:30 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def cluster_role_binding_exists?(resource)
|
|
|
|
kubeclient.get_cluster_role_binding(resource.metadata.name)
|
|
|
|
rescue ::Kubeclient::ResourceNotFoundError
|
2018-11-20 20:47:30 +05:30
|
|
|
false
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|