kerbi 0.0.1 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cli/base_handler.rb +194 -0
  3. data/lib/cli/base_serializer.rb +120 -0
  4. data/lib/cli/config_handler.rb +51 -0
  5. data/lib/cli/entry_serializers.rb +99 -0
  6. data/lib/cli/project_handler.rb +2 -2
  7. data/lib/cli/release_handler.rb +41 -0
  8. data/lib/cli/release_serializer.rb +46 -0
  9. data/lib/cli/root_handler.rb +34 -13
  10. data/lib/cli/state_handler.rb +88 -0
  11. data/lib/cli/values_handler.rb +4 -3
  12. data/{boilerplate → lib/code-gen/new-project}/Gemfile.erb +0 -0
  13. data/lib/code-gen/new-project/kerbifile.rb.erb +9 -0
  14. data/lib/code-gen/new-project/values.yaml.erb +1 -0
  15. data/lib/config/cli_schema.rb +343 -28
  16. data/lib/config/config_file.rb +60 -0
  17. data/lib/config/globals.rb +4 -0
  18. data/lib/config/run_opts.rb +162 -0
  19. data/lib/config/state_consts.rb +11 -0
  20. data/lib/kerbi.rb +35 -10
  21. data/lib/main/code_gen.rb +1 -1
  22. data/lib/main/errors.rb +115 -0
  23. data/lib/main/mixer.rb +20 -10
  24. data/lib/mixins/cli_state_helpers.rb +108 -0
  25. data/lib/mixins/cm_backend_testing.rb +109 -0
  26. data/lib/mixins/entry_tag_logic.rb +183 -0
  27. data/lib/state/base_backend.rb +93 -0
  28. data/lib/state/config_map_backend.rb +173 -0
  29. data/lib/state/entry.rb +173 -0
  30. data/lib/state/entry_set.rb +137 -0
  31. data/lib/state/metadata.yaml.erb +11 -0
  32. data/lib/state/mixers.rb +23 -0
  33. data/lib/state/resources.yaml.erb +17 -0
  34. data/lib/utils/cli.rb +108 -9
  35. data/lib/utils/helm.rb +10 -12
  36. data/lib/utils/k8s_auth.rb +87 -0
  37. data/lib/utils/misc.rb +36 -1
  38. data/lib/utils/mixing.rb +1 -1
  39. data/lib/utils/values.rb +13 -22
  40. data/spec/cli/config_handler_spec.rb +38 -0
  41. data/spec/cli/release_handler_spec.rb +127 -0
  42. data/spec/cli/root_handler_spec.rb +100 -0
  43. data/spec/cli/state_handler_spec.rb +108 -0
  44. data/spec/cli/values_handler_spec.rb +17 -0
  45. data/spec/fixtures/expectations/common/bad-tag.txt +1 -0
  46. data/spec/fixtures/expectations/config/bad-set.txt +1 -0
  47. data/spec/fixtures/expectations/config/set.txt +1 -0
  48. data/spec/fixtures/expectations/config/show-default.yaml +6 -0
  49. data/spec/fixtures/expectations/release/delete.txt +1 -0
  50. data/spec/fixtures/expectations/release/init-already-existed.txt +2 -0
  51. data/spec/fixtures/expectations/release/init-both-created.txt +2 -0
  52. data/spec/fixtures/expectations/release/list.txt +5 -0
  53. data/spec/fixtures/expectations/release/status-all-working.txt +5 -0
  54. data/spec/fixtures/expectations/release/status-data-unreadable.txt +5 -0
  55. data/spec/fixtures/expectations/release/status-not-provisioned.txt +5 -0
  56. data/spec/fixtures/expectations/root/template-inlines.yaml +31 -0
  57. data/spec/fixtures/expectations/root/template-production.yaml +31 -0
  58. data/spec/fixtures/expectations/root/template-read-inlines.yaml +31 -0
  59. data/spec/fixtures/expectations/root/template-read.yaml +31 -0
  60. data/spec/fixtures/expectations/root/template-write.yaml +31 -0
  61. data/spec/fixtures/expectations/root/template.yaml +31 -0
  62. data/spec/fixtures/expectations/root/values.json +28 -0
  63. data/spec/fixtures/expectations/state/delete.txt +1 -0
  64. data/spec/fixtures/expectations/state/demote.txt +1 -0
  65. data/spec/fixtures/expectations/state/list.json +51 -0
  66. data/spec/fixtures/expectations/state/list.txt +6 -0
  67. data/spec/fixtures/expectations/state/list.yaml +35 -0
  68. data/spec/fixtures/expectations/state/promote.txt +1 -0
  69. data/spec/fixtures/expectations/state/prune-candidates.txt +1 -0
  70. data/spec/fixtures/expectations/state/retag.txt +1 -0
  71. data/spec/fixtures/expectations/state/set.txt +1 -0
  72. data/spec/fixtures/expectations/state/show.json +13 -0
  73. data/spec/fixtures/expectations/state/show.txt +13 -0
  74. data/spec/fixtures/expectations/state/show.yaml +8 -0
  75. data/spec/fixtures/expectations/values/order-of-precedence.yaml +4 -0
  76. data/spec/main/configmap_backend_spec.rb +110 -0
  77. data/spec/main/project_code_gen_spec.rb +8 -2
  78. data/spec/main/state_entry_set_spec.rb +112 -0
  79. data/spec/main/state_entry_spec.rb +109 -0
  80. data/spec/mini-projects/hello-kerbi/common/metadata.yaml.erb +5 -0
  81. data/spec/mini-projects/hello-kerbi/consts.rb +5 -0
  82. data/spec/mini-projects/hello-kerbi/helpers.rb +8 -0
  83. data/spec/mini-projects/hello-kerbi/kerbifile.rb +18 -0
  84. data/spec/mini-projects/hello-kerbi/pod-and-service.yaml.erb +23 -0
  85. data/spec/mini-projects/hello-kerbi/values/production.yaml +2 -0
  86. data/spec/mini-projects/hello-kerbi/values/v2.yaml +2 -0
  87. data/spec/mini-projects/hello-kerbi/values/values.yaml +4 -0
  88. data/spec/spec_helper.rb +143 -1
  89. data/spec/utils/helm_spec.rb +89 -109
  90. data/spec/utils/k8s_auth_spec.rb +32 -0
  91. data/spec/utils/misc_utils_spec.rb +9 -0
  92. data/spec/utils/values_utils_spec.rb +12 -19
  93. metadata +143 -16
  94. data/boilerplate/kerbifile.rb.erb +0 -9
  95. data/boilerplate/values.yaml.erb +0 -1
  96. data/lib/cli/base.rb +0 -83
  97. data/lib/config/cli_opts.rb +0 -50
  98. data/lib/config/manager.rb +0 -36
  99. data/lib/main/state_manager.rb +0 -47
  100. data/lib/utils/kubectl.rb +0 -58
  101. data/spec/main/examples_spec.rb +0 -12
  102. data/spec/main/state_manager_spec.rb +0 -84
  103. data/spec/utils/state_utils.rb +0 -15
@@ -1,50 +0,0 @@
1
- module Kerbi
2
-
3
- ##
4
- # Convenience accessor struct for getting values from
5
- # the CLI args.
6
- class CliOpts
7
-
8
- attr_reader :options
9
-
10
- def initialize(options={})
11
- @options = options.deep_dup
12
- end
13
-
14
- def output_format
15
- value = options[consts::OUTPUT_FMT]
16
- value || "yaml"
17
- end
18
-
19
- def outputs_yaml?
20
- self.output_format == 'yaml'
21
- end
22
-
23
- def outputs_json?
24
- self.output_format == 'json'
25
- end
26
-
27
- def ruby_version
28
- options[consts::RUBY_VER]
29
- end
30
-
31
- def fname_exprs
32
- options[consts::VALUE_FNAMES] || []
33
- end
34
-
35
- def inline_val_exprs
36
- options[consts::INLINE_ASSIGNMENT] || []
37
- end
38
-
39
- def use_state?
40
- options[consts::USE_STATE_VALUES].present?
41
- end
42
-
43
- private
44
-
45
- def consts
46
- Kerbi::Consts::OptionKeys
47
- end
48
-
49
- end
50
- end
@@ -1,36 +0,0 @@
1
- module Kerbi
2
- module Config
3
- class Manager
4
- attr_reader :bundle
5
-
6
- def initialize
7
- # TODO load me from filesystem
8
- @bundle = {
9
- tmp_helm_values_path: '/tmp/kerbi-helm-vals.yaml',
10
- helm_exec: "helm"
11
- }
12
- end
13
-
14
- def self.inst
15
- # TODO create ~/.config/kerbi etc...
16
- @_instance ||= self.new
17
- end
18
-
19
- def self.tmp_helm_values_path
20
- inst.bundle[:tmp_helm_values_path]
21
- end
22
-
23
- def self.helm_exec
24
- inst.bundle[:helm_exec]
25
- end
26
-
27
- def self.tmp_helm_values_path=(val)
28
- inst.bundle[:tmp_helm_values_path] = val
29
- end
30
-
31
- def self.helm_exec=(val)
32
- inst.bundle[:helm_exec] = val
33
- end
34
- end
35
- end
36
- end
@@ -1,47 +0,0 @@
1
- module Kerbi
2
- class StateManager
3
- def self.patch
4
- self.create_configmap_if_missing
5
- patch_values = self.compile_patch
6
- config_map = utils::State.kubectl_get_cm("state", raise_on_err: false)
7
- crt_values = utils::State.read_cm_data(config_map)
8
- merged_vars = crt_values.deep_merge(patch_values)
9
- new_body = { **config_map, data: { variables: JSON.dump(merged_vars) } }
10
- yaml_body = YAML.dump(new_body.deep_stringify_keys)
11
- Utils::Kubectl.apply_tmpfile(yaml_body, args_manager.get_kmd_arg_str)
12
- end
13
-
14
- def compile_patch
15
- values = {}
16
-
17
- args_manager.get_fnames.each do |fname|
18
- new_values = YAML.load_file(fname).deep_symbolize_keys
19
- values.deep_merge!(new_values)
20
- end
21
-
22
- args_manager.get_inlines.each do |assignment_str|
23
- assignment = Utils::Utils.str_assign_to_h(assignment_str)
24
- values.deep_merge!(assignment)
25
- end
26
- values
27
- end
28
-
29
- def get_crt_vars
30
- create_configmap_if_missing
31
- get_configmap_values(get_configmap)
32
- end
33
-
34
- def create_configmap_if_missing
35
- unless get_configmap(raise_on_er: false)
36
- kmd = "create cm state #{args_manager.get_kmd_arg_str}"
37
- Utils::Kubectl.kmd(kmd)
38
- end
39
- end
40
-
41
- private
42
-
43
- def utils
44
- Kerbi::Utils
45
- end
46
- end
47
- end
data/lib/utils/kubectl.rb DELETED
@@ -1,58 +0,0 @@
1
- module Kerbi
2
- module Utils
3
- module Kubectl
4
- def self.kmd(cmd, options = {})
5
- cmd = "kubectl #{cmd}"
6
- self.eval_shell_cmd(cmd, options)
7
- end
8
-
9
- def self.jkmd(cmd, options={})
10
- cmd = "kubectl #{cmd} -o json"
11
- begin
12
- output = self.eval_shell_cmd(cmd, options)
13
- as_hash = JSON.parse(output).deep_symbolize_keys
14
- if as_hash.has_key?(:items)
15
- as_hash[:items]
16
- else
17
- as_hash
18
- end
19
- rescue
20
- nil
21
- end
22
- end
23
-
24
- def self.eval_shell_cmd(cmd, options={})
25
- print_err = options[:print_err]
26
- raise_on_err = options[:raise_on_err]
27
- begin
28
- output, status = Open3.capture2e(cmd)
29
- if status.success?
30
- output = output[0..output.length - 2] if output.end_with?("\n")
31
- output
32
- else
33
- if print_err
34
- puts "Command \"#{cmd}\" error status #{status} with message:"
35
- puts output
36
- puts "---"
37
- end
38
- nil
39
- end
40
- rescue Exception => e
41
- if print_err
42
- puts "Command \"#{cmd}\" failed with message:"
43
- puts e.message
44
- puts "---"
45
- end
46
- nil
47
- end
48
- end
49
-
50
- def self.apply_tmpfile(yaml_str, append)
51
- tmp_fname = "/tmp/man-#{SecureRandom.hex(32)}.yaml"
52
- File.write(tmp_fname, yaml_str)
53
- kmd("apply -f #{tmp_fname} #{append}", print_err: true)
54
- File.delete(tmp_fname)
55
- end
56
- end
57
- end
58
- end
@@ -1,12 +0,0 @@
1
- require_relative './../spec_helper'
2
- require_relative './../../examples/hello-kerbi/kerbifile'
3
-
4
- RSpec.describe "Examples Directory" do
5
- describe "The mixer" do
6
- describe "#run" do
7
- it "runs" do
8
- # puts Kerbi::Globals.mixers.first.new({ }).run
9
- end
10
- end
11
- end
12
- end
@@ -1,84 +0,0 @@
1
- # require_relative './../spec_helper'
2
-
3
- # RSpec.describe Kerbi::StateManager do
4
- #
5
- # subject { Kerbi::StateManager.new }
6
- #
7
- # context = ENV['KERBI_RSPEC_K8S_CONTEXT'] || 'kind-kind'
8
- #
9
- # before :each do
10
- # puts "KONTEXT #{context}"
11
- # end
12
- #
13
- # describe "#patch" do
14
- # context "when there is no entry for variables" do
15
- #
16
- # before :each do
17
- # system(
18
- # "kubectl delete cm state -n default --context #{context}",
19
- # out: File::NULL,
20
- # err: File::NULL
21
- # )
22
- # end
23
- #
24
- # it "patches the configmap" do
25
- # argue("--context #{context} --set foo.bar=baz")
26
- # old_values = subject.get_crt_vars
27
- # expect(old_values).to eq({})
28
- # subject.patch
29
- # new_values = subject.get_crt_vars
30
- # expect(new_values).to eq({foo: {bar: "baz"}})
31
- # end
32
- # end
33
- #
34
- # context "when there is an entry for variables" do
35
- #
36
- # before :each do
37
- # system "kubectl delete cm state -n default --context #{context}"
38
- # end
39
- #
40
- # it "patches the configmap" do
41
- # argue("--context #{context} --set foo.bar=baz")
42
- # subject.patch
43
- #
44
- # argue("--context #{context} --set foo.bar=car")
45
- # subject.patch
46
- #
47
- # new_values = subject.get_crt_vars
48
- # expect(new_values).to eq({foo: {bar: "car"}})
49
- # end
50
- #
51
- # end
52
- # end
53
- #
54
- # describe "#get_configmap" do
55
- # context "when it does not exist" do
56
- #
57
- # before :each do
58
- # system "kubectl delete cm state -n default --context #{context}"
59
- # end
60
- #
61
- # it "returns the configmap as a hash" do
62
- # argue("--context #{context}")
63
- # result = subject.get_configmap(raise_on_er: false)
64
- # expect(result).to be_nil
65
- # end
66
- # end
67
- #
68
- # context "when it does exist" do
69
- # before :each do
70
- # system "kubectl create cm state -n default --context #{context}"
71
- # end
72
- #
73
- # it "returns the configmap as a hash" do
74
- # argue("--context #{context}")
75
- # result = subject.get_configmap
76
- # expect(result).to_not be_nil
77
- # must_have = { name: "state", namespace: "default" }
78
- # expect(result[:metadata].slice(:name, :namespace)).to eq(must_have)
79
- # end
80
- # end
81
- # end
82
-
83
-
84
- # end
@@ -1,15 +0,0 @@
1
- module Kerbi
2
- module Utils
3
- module State
4
- def self.kubectl_get_cm(res_name, opts={})
5
- kmd = "get configmap #{res_name} #{opts[:kubectl_args]}"
6
- Utils::Kubectl.jkmd(kmd, **opts)
7
- end
8
-
9
- def self.read_cm_data(configmap)
10
- json_enc_vars = configmap.dig(:data, :variables) || '{}'
11
- JSON.parse(json_enc_vars).deep_symbolize_keys
12
- end
13
- end
14
- end
15
- end