Sha256: 9aeed5d89eb23e1795c9ec9add4f58113ea92615af43486a42ed5ed092cb4163

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module CronKubernetes
  module Context
    # Kubeclient Context from `kubectl` config file.
    class Kubectl
      def applicable?
        File.exist?(kubeconfig)
      end

      def context
        config = Kubeclient::Config.read(kubeconfig)

        CronKubernetes::KubeclientContext::Context.new(
          config.context.api_endpoint,
          config.context.api_version,
          config.context.namespace,
          auth_options: auth_options(config),
          ssl_options:  config.context.ssl_options
        )
      end

      private

      def kubeconfig
        File.join(ENV["HOME"], ".kube", "config")
      end

      def auth_options(config)
        options = config.context.auth_options
        return options unless options.empty?

        google_application_default_credentials
      end

      def google_application_default_credentials
        return unless defined?(Google) && defined?(Google::Auth)

        {bearer_token: Kubeclient::GoogleApplicationDefaultCredentials.token}
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cron-kubernetes-3.0.0 lib/cron_kubernetes/context/kubectl.rb
cron-kubernetes-2.0.0 lib/cron_kubernetes/context/kubectl.rb