class Kubecontrol::Client

Constants

DEFAULT_NAMESPACE

Attributes

namespace[RW]

Public Class Methods

new(namespace = DEFAULT_NAMESPACE) click to toggle source
# File lib/kubecontrol/client.rb, line 10
def initialize(namespace = DEFAULT_NAMESPACE)
  @namespace = namespace
end

Public Instance Methods

apply(file_path: nil, kustomization_dir: nil) click to toggle source
# File lib/kubecontrol/client.rb, line 14
def apply(file_path: nil, kustomization_dir: nil)
  raise ArgumentError.new('Must pass a file_path or kustomization_dir keyword argument') if (file_path.nil? && kustomization_dir.nil?) || (file_path && kustomization_dir)

  if file_path
    kubectl_command("apply -f #{file_path}")
  else
    kubectl_command("apply -k #{kustomization_dir}")
  end
end
deployments() click to toggle source
# File lib/kubecontrol/client.rb, line 28
def deployments
  get_resource(Resources::Deployment, 5)
end
find_deployment_by_name(name_regex) click to toggle source
# File lib/kubecontrol/client.rb, line 56
def find_deployment_by_name(name_regex)
  deployments.find { |deployment| deployment.name.match?(name_regex) }
end
find_pod_by_name(name_regex) click to toggle source
# File lib/kubecontrol/client.rb, line 52
def find_pod_by_name(name_regex)
  pods.find { |pod| pod.name.match?(name_regex) }
end
find_secret_by_name(name_regex) click to toggle source
# File lib/kubecontrol/client.rb, line 44
def find_secret_by_name(name_regex)
  secrets.find { |secret| secret.name.match?(name_regex) }
end
find_service_by_name(name_regex) click to toggle source
# File lib/kubecontrol/client.rb, line 48
def find_service_by_name(name_regex)
  services.find { |service| service.name.match?(name_regex) }
end
find_stateful_set_by_name(name_regex) click to toggle source
# File lib/kubecontrol/client.rb, line 60
def find_stateful_set_by_name(name_regex)
  stateful_sets.find { |stateful_set| stateful_set.name.match?(name_regex) }
end
kubectl_command(command) click to toggle source
# File lib/kubecontrol/client.rb, line 64
def kubectl_command(command)
  stdout_data, stderr_data, status = Open3.capture3("kubectl -n #{namespace} #{command}")
  exit_code = status.exitstatus

  [stdout_data, stderr_data, exit_code]
end
pods() click to toggle source
# File lib/kubecontrol/client.rb, line 24
def pods
  get_resource(Resources::Pod, 5)
end
secrets() click to toggle source
# File lib/kubecontrol/client.rb, line 40
def secrets
  get_resource(Resources::Secret, 4)
end
services() click to toggle source
# File lib/kubecontrol/client.rb, line 36
def services
  get_resource(Resources::Service, 6)
end
stateful_sets() click to toggle source
# File lib/kubecontrol/client.rb, line 32
def stateful_sets
  get_resource(Resources::StatefulSet, 3)
end

Private Instance Methods

get_resource(klass, number_of_columns) click to toggle source
# File lib/kubecontrol/client.rb, line 73
def get_resource(klass, number_of_columns)
  get_result, _stderr, _exit_code = kubectl_command("get #{klass::RESOURCE_NAME}")
  return [] if get_result.empty?

  resources_array = get_result.split
  resources_array.shift number_of_columns # remove output table headers
  resources_array.each_slice(number_of_columns).map do |resource_data|
    klass.new(*resource_data, namespace, self)
  end
end