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

127 lines
3.7 KiB
Ruby
Raw Normal View History

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
2020-04-08 14:13:33 +05:30
class API
2018-03-17 18:26:18 +05:30
def initialize(kubeclient)
@kubeclient = kubeclient
2020-03-09 13:42:32 +05:30
@namespace = Gitlab::Kubernetes::Namespace.new(
Gitlab::Kubernetes::Helm::NAMESPACE,
kubeclient,
labels: Gitlab::Kubernetes::Helm::NAMESPACE_LABELS
)
2018-03-17 18:26:18 +05:30
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
2019-07-31 22:56:46 +05:30
def uninstall(command)
namespace.ensure_exists!
create_config_map(command)
delete_pod!(command.pod_name)
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)
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
2020-05-24 23:13:21 +05:30
kubeclient.update_cluster_role_binding(cluster_role_binding_resource)
2018-11-20 20:47:30 +05:30
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