class Krane::KubeclientBuilder

Attributes

kubeconfig_files[R]

Public Class Methods

new(kubeconfig: nil) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 15
def initialize(kubeconfig: nil)
  files = kubeconfig || ENV["KUBECONFIG"] || "#{Dir.home}/.kube/config"
  # Split the list by colon for Linux and Mac, and semicolon for Windows.
  @kubeconfig_files = files.split(/[:;]/).map!(&:strip).reject(&:empty?)
end

Public Instance Methods

build_apiextensions_v1beta1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 68
def build_apiextensions_v1beta1_kubeclient(context)
  build_kubeclient(
    api_version: "v1beta1",
    context: context,
    endpoint_path: "/apis/apiextensions.k8s.io"
  )
end
build_apps_v1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 60
def build_apps_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context,
    endpoint_path: "/apis/apps"
  )
end
build_autoscaling_v1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 76
def build_autoscaling_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v2beta1",
    context: context,
    endpoint_path: "/apis/autoscaling"
  )
end
build_batch_v1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 44
def build_batch_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context,
    endpoint_path: "/apis/batch/"
  )
end
build_batch_v1beta1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 36
def build_batch_v1beta1_kubeclient(context)
  build_kubeclient(
    api_version: "v1beta1",
    context: context,
    endpoint_path: "/apis/batch/"
  )
end
build_networking_v1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 92
def build_networking_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context,
    endpoint_path: "/apis/networking.k8s.io"
  )
end
build_policy_v1beta1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 52
def build_policy_v1beta1_kubeclient(context)
  build_kubeclient(
    api_version: "v1beta1",
    context: context,
    endpoint_path: "/apis/policy/"
  )
end
build_rbac_v1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 84
def build_rbac_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context,
    endpoint_path: "/apis/rbac.authorization.k8s.io"
  )
end
build_scheduling_v1beta1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 108
def build_scheduling_v1beta1_kubeclient(context)
  build_kubeclient(
    api_version: "v1beta1",
    context: context,
    endpoint_path: "/apis/scheduling.k8s.io"
  )
end
build_storage_v1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 100
def build_storage_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context,
    endpoint_path: "/apis/storage.k8s.io"
  )
end
build_v1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 21
def build_v1_kubeclient(context)
  build_kubeclient(
    api_version: "v1",
    context: context
  )
end
build_v1beta1_kubeclient(context) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 28
def build_v1beta1_kubeclient(context)
  build_kubeclient(
    api_version: "v1beta1",
    context: context,
    endpoint_path: "/apis/extensions/"
  )
end
validate_config_files() click to toggle source
# File lib/krane/kubeclient_builder.rb, line 116
def validate_config_files
  errors = []
  if @kubeconfig_files.empty?
    errors << "Kubeconfig file name(s) not set in $KUBECONFIG"
  else
    @kubeconfig_files.each do |f|
      # If any files in the list are not valid, we can't be sure the merged context list is what the user intended
      errors << "Kubeconfig not found at #{f}" unless File.file?(f)
    end
  end
  errors
end
validate_config_files!() click to toggle source
# File lib/krane/kubeclient_builder.rb, line 129
def validate_config_files!
  errors = validate_config_files
  raise TaskConfigurationError, errors.join(', ') if errors.present?
end

Private Instance Methods

build_kubeclient(api_version:, context:, endpoint_path: nil) click to toggle source
# File lib/krane/kubeclient_builder.rb, line 136
def build_kubeclient(api_version:, context:, endpoint_path: nil)
  validate_config_files!
  @kubeclient_configs ||= @kubeconfig_files.map { |f| Kubeclient::Config.read(f) }
  # Find a context defined in kube conf files that matches the input context by name
  config = @kubeclient_configs.find { |c| c.contexts.include?(context) }
  raise ContextMissingError.new(context, @kubeconfig_files) unless config

  kube_context = config.context(context)
  client = Kubeclient::Client.new(
    "#{kube_context.api_endpoint}#{endpoint_path}",
    api_version,
    ssl_options: kube_context.ssl_options,
    auth_options: kube_context.auth_options,
    timeouts: {
      open: Krane::Kubectl::DEFAULT_TIMEOUT,
      read: Krane::Kubectl::DEFAULT_TIMEOUT,
    }
  )
  client.discover
  client
end