Sha256: c1e6cd4d2c88617ada5e46d7827971cfea8ae5af1e1d3c202ecf849f31e616ba

Contents?: true

Size: 1.95 KB

Versions: 38

Compression:

Stored size: 1.95 KB

Contents

module Kubes
  class Kubectl
    extend Memoist
    include Kubes::Util::Sh
    include Kubes::Hooks::Concern

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

    def run
      validate!

      options = @options.dup
      options[:exit_on_fail] = exit_on_fail unless exit_on_fail.nil?

      params = args.flatten.join(' ')
      args = "#{@name} #{params}" # @name: apply or delete

      run_hooks("kubectl.rb", name: @name, file: @options[:file]) do
        if options[:capture]
          self.class.capture(args, options) # already includes kubectl
        else
          self.class.execute(args, options)
        end
      end
    end

    # Useful for kustomize mode
    def validate!
      return true unless Kubes.kustomize?

      unless @options[:role]
        logger.error "Missing argument: A folder must be provided when using kustomization.yaml files".color(:red)
        logger.info "Please provide a folder"
        exit 1
      end
    end

    def exit_on_fail
      return false if ENV['KUBES_EXIT_ON_FAIL'] == '0'
      Kubes.config.kubectl.exit_on_fail[@name]
    end

    def args
      # base at end in case of redirection. IE: command > /path
      custom.args + default.args
    end

    def custom
      custom = Kubes::Args::Custom.new(@name, "#{Kubes.root}/.kubes/config/kubectl/args.rb")
      custom.build
      custom
    end
    memoize :custom

    def default
      klass = Kubes.kustomize? ? Args::Kustomize : Args::Standard
      klass.new(@name, @options)
    end
    memoize :default

    class << self
      include Kubes::Util::Sh
      def run(name, options={})
        new(name, options).run
      end

      def execute(args, options={})
        sh("kubectl #{args}", options)
      end

      def capture(args, options={})
        resp = sh_capture("kubectl #{args}", options)
        if args.include?('-o json')
          JSON.load(resp) # data
        else
          resp
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
kubes-0.9.3 lib/kubes/kubectl.rb
kubes-0.9.2 lib/kubes/kubectl.rb
kubes-0.9.1 lib/kubes/kubectl.rb
kubes-0.9.0 lib/kubes/kubectl.rb
kubes-0.8.10 lib/kubes/kubectl.rb
kubes-0.8.9 lib/kubes/kubectl.rb
kubes-0.8.8 lib/kubes/kubectl.rb
kubes-0.8.7 lib/kubes/kubectl.rb
kubes-0.8.6 lib/kubes/kubectl.rb
kubes-0.8.5 lib/kubes/kubectl.rb
kubes-0.8.4 lib/kubes/kubectl.rb
kubes-0.8.3 lib/kubes/kubectl.rb
kubes-0.8.2 lib/kubes/kubectl.rb
kubes-0.8.1 lib/kubes/kubectl.rb
kubes-0.8.0 lib/kubes/kubectl.rb
kubes-0.7.10 lib/kubes/kubectl.rb
kubes-0.7.9 lib/kubes/kubectl.rb
kubes-0.7.8 lib/kubes/kubectl.rb
kubes-0.7.7 lib/kubes/kubectl.rb
kubes-0.7.6 lib/kubes/kubectl.rb