Module: Kubes::Kubectl::Ordering

Included in:
Compiler, Batch
Defined in:
lib/kubes/kubectl/ordering.rb

Instance Method Summary collapse

Instance Method Details

#config_skip?(file) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/kubes/kubectl/ordering.rb', line 27

def config_skip?(file)
  skip = Kubes.config.skip
  skip += ENV['KUBES_SKIP'].split(' ') if ENV['KUBES_SKIP']
  !!skip.detect { |pattern| file.include?(pattern) }
end

#filesObject



56
57
58
59
60
61
62
63
64
# File 'lib/kubes/kubectl/ordering.rb', line 56

def files
  files = []
  Dir.glob(search_expr).each do |path|
    next unless process?(path)
    file = path.sub("#{Kubes.root}/", '')
    files << file
  end
  files
end

#filter_skip(sorted) ⇒ Object



21
22
23
24
25
# File 'lib/kubes/kubectl/ordering.rb', line 21

def filter_skip(sorted)
  sorted.reject do |file|
    config_skip?(file)
  end
end

#index_for(type, value) ⇒ Object

type: kinds or roles value: Examples: kind: deployment, role: web



35
36
37
38
39
40
# File 'lib/kubes/kubectl/ordering.rb', line 35

def index_for(type, value)
  order = Kubes.config.kubectl.order.send(type) # kinds or roles
  index = order.index(value.to_s) || 999
  i = index.to_s.rjust(3, "0") # pad with 0
  "#{i}-#{value}" # append name so that terms with same index get order alphabetically
end

#process?(path) ⇒ Boolean

Only considering files 2 layers deep. So:

Yes = web/deployment.yaml
No = web/deployment/dev.yaml

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/kubes/kubectl/ordering.rb', line 71

def process?(path)
  if Kubes.kustomize?
    File.file?(path)
  else
    consider?(path) && two_levels_deep?(path)
  end
end

#search_exprObject

kubes apply # nil, resource: nil kubes apply clock # “clock”, resource: nil kubes apply clock deployment # “clock”, resource: “deployment”



45
46
47
48
49
50
51
52
53
54
# File 'lib/kubes/kubectl/ordering.rb', line 45

def search_expr
  role, resource = @options[:role], @options[:resource]
  if role && resource
    "#{Kubes.root}/.kubes/output/#{role}/#{resource}.yaml"
  elsif role
    "#{Kubes.root}/.kubes/output/#{role}/*.yaml"
  else
    "#{Kubes.root}/.kubes/output/**/*.yaml"
  end
end

#sorted_filesObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kubes/kubectl/ordering.rb', line 3

def sorted_files
  sorted = files.sort_by do |file|
    # .kubes/output/web/service.yaml
    words = file.split('/')
    role, kind = words[2], words[3] # web, deployment
    kind = kind.sub('.yaml','').underscore.camelize if kind

    kind_i = index_for(:kinds, kind)
    role_i = index_for(:roles, role)

    "#{role_i}/#{kind_i}"
  end

  sorted = filter_skip(sorted)

  @name == "delete" ? sorted.reverse : sorted
end

#two_levels_deep?(path) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/kubes/kubectl/ordering.rb', line 79

def two_levels_deep?(path)
  rel_path = path.sub(%r{.*\.kubes/(resources|output)/},'')
  rel_path.split('/').size == 2
end