2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-22 12:23:43 -05:00
|
|
|
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-06-25 05:57:29 -04:00
|
|
|
namespace.ensure_exists!
|
2018-09-06 06:03:38 -04:00
|
|
|
|
|
|
|
create_service_account(command)
|
|
|
|
create_cluster_role_binding(command)
|
2018-08-07 08:39:38 -04:00
|
|
|
create_config_map(command)
|
2018-09-06 06:03:38 -04:00
|
|
|
|
2018-11-26 08:54:44 -05:00
|
|
|
delete_pod!(command.pod_name)
|
2018-06-25 05:57:29 -04:00
|
|
|
kubeclient.create_pod(command.pod_resource)
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
|
2019-02-07 16:40:55 -05:00
|
|
|
alias_method :update, :install
|
2018-09-30 17:32:09 -04:00
|
|
|
|
2019-04-09 18:57:23 -04:00
|
|
|
def uninstall(command)
|
|
|
|
namespace.ensure_exists!
|
2019-06-04 07:25:33 -04:00
|
|
|
create_config_map(command)
|
2019-04-09 18:57:23 -04:00
|
|
|
|
|
|
|
delete_pod!(command.pod_name)
|
|
|
|
kubeclient.create_pod(command.pod_resource)
|
|
|
|
end
|
|
|
|
|
2017-12-22 12:23:43 -05:00
|
|
|
##
|
|
|
|
# Returns Pod phase
|
|
|
|
#
|
|
|
|
# https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase
|
|
|
|
#
|
|
|
|
# values: "Pending", "Running", "Succeeded", "Failed", "Unknown"
|
|
|
|
#
|
2018-06-25 05:57:29 -04:00
|
|
|
def status(pod_name)
|
|
|
|
kubeclient.get_pod(pod_name, namespace.name).status.phase
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
|
2018-06-25 05:57:29 -04:00
|
|
|
def log(pod_name)
|
|
|
|
kubeclient.get_pod_log(pod_name, namespace.name).body
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
|
2018-06-25 05:57:29 -04:00
|
|
|
def delete_pod!(pod_name)
|
|
|
|
kubeclient.delete_pod(pod_name, namespace.name)
|
2018-11-26 08:54:44 -05:00
|
|
|
rescue ::Kubeclient::ResourceNotFoundError
|
|
|
|
# no-op
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
|
2018-09-30 17:32:09 -04:00
|
|
|
def get_config_map(config_map_name)
|
|
|
|
namespace.ensure_exists!
|
|
|
|
|
|
|
|
kubeclient.get_config_map(config_map_name, namespace.name)
|
|
|
|
end
|
|
|
|
|
2017-12-22 12:23:43 -05:00
|
|
|
private
|
|
|
|
|
2018-06-26 13:18:33 -04:00
|
|
|
attr_reader :kubeclient, :namespace
|
|
|
|
|
2018-03-01 18:46:02 -05:00
|
|
|
def create_config_map(command)
|
|
|
|
command.config_map_resource.tap do |config_map_resource|
|
2019-02-07 16:40:55 -05:00
|
|
|
break unless config_map_resource
|
|
|
|
|
2018-11-11 18:23:32 -05:00
|
|
|
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-01 18:46:02 -05:00
|
|
|
end
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
2018-09-06 06:03:38 -04:00
|
|
|
|
2018-09-30 17:32:09 -04:00
|
|
|
def update_config_map(command)
|
|
|
|
command.config_map_resource.tap do |config_map_resource|
|
|
|
|
kubeclient.update_config_map(config_map_resource)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-06 06:03:38 -04:00
|
|
|
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
|
|
|
|
|
2018-11-11 18:23:32 -05:00
|
|
|
def config_map_exists?(resource)
|
|
|
|
kubeclient.get_config_map(resource.metadata.name, resource.metadata.namespace)
|
|
|
|
rescue ::Kubeclient::ResourceNotFoundError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-09-06 06:03:38 -04:00
|
|
|
def service_account_exists?(resource)
|
2018-11-13 07:46:01 -05:00
|
|
|
kubeclient.get_service_account(resource.metadata.name, resource.metadata.namespace)
|
|
|
|
rescue ::Kubeclient::ResourceNotFoundError
|
|
|
|
false
|
2018-09-06 06:03:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cluster_role_binding_exists?(resource)
|
2018-11-13 07:46:01 -05:00
|
|
|
kubeclient.get_cluster_role_binding(resource.metadata.name)
|
|
|
|
rescue ::Kubeclient::ResourceNotFoundError
|
2018-09-06 06:03:38 -04:00
|
|
|
false
|
|
|
|
end
|
2017-12-22 12:23:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|