Sha256: 78258b0a03501973fbd03b87b5944fd1ed83068102bbd6b903e5bd9c064e9d5b

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

class Kubes::Kubectl
  class Batch
    include Kubes::Hooks::Concern
    include Kubes::Logging
    include Kubes::Util::Consider
    include Kubes::Util::Sh
    include Ordering

    def initialize(name, options={})
      @name, @options = name.to_s, options
    end

    def run
      # @options[:preview] is really only used for kubectl delete
      logger.info "Will run:" if @options[:preview]
      switch_context do
        run_hooks("kubes.rb", name: @name) do
          sorted_files.each do |file|
            if @options[:preview]
              logger.info "    kubectl #{@name} -f #{file}"
            else
              Kubes::Kubectl.run(@name, @options.merge(file: file))
            end
          end
        end
      end
    end

    def switch_context(&block)
      kubectl = Kubes.config.kubectl
      context = kubectl.context

      unless context
        block.call
        return
      end

      previous_context = sh_capture("kubectl config current-context")
      if previous_context == context
        block.call
        return
      end

      logger.debug "Switching kubectl context to: #{context}"
      sh("kubectl config use-context #{context}", mute: true)
      result = block.call
      if !previous_context.blank? && !kubectl.context_keep
        sh("kubectl config use-context #{previous_context}", mute: true)
      end
      result
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
kubes-0.6.1 lib/kubes/kubectl/batch.rb
kubes-0.6.0 lib/kubes/kubectl/batch.rb
kubes-0.5.1 lib/kubes/kubectl/batch.rb
kubes-0.5.0 lib/kubes/kubectl/batch.rb