Skip to content

Commit

Permalink
allow multiple yml docs in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Jul 1, 2021
1 parent 19180c0 commit aa78857
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ gem 'clamp'
gem 'graphql-client'
gem 'kubeclient'
gem 'liquid'
gem 'yaml-safe_load_stream'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ GEM
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
yaml-safe_load_stream (0.1.1)
zeitwerk (2.4.2)

PLATFORMS
Expand All @@ -129,6 +130,7 @@ DEPENDENCIES
simplecov
vcr
webmock
yaml-safe_load_stream

BUNDLED WITH
2.1.4
13 changes: 9 additions & 4 deletions lib/kubetruth/etl.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'benchmark'
require 'yaml'
require 'yaml/safe_load_stream'
using YAMLSafeLoadStream

require_relative 'config'
require_relative 'ctapi'
require_relative 'kubeapi'
Expand Down Expand Up @@ -153,12 +156,14 @@ def apply
secret_origins: secret_origins,
context: project.spec.context
)
parsed_yml = YAML.safe_load(resource_yml)
if parsed_yml

template_id = "mapping: #{project.spec.name}, mapping_namespace: #{namespace}, project: #{project.name}, template: #{template_name}"
parsed_ymls = YAML.safe_load_stream(resource_yml, template_id)
logger.debug {"Skipping empty template"} if parsed_ymls.empty?
parsed_ymls.each do |parsed_yml|
kube_apply(parsed_yml)
else
logger.debug {"Skipping empty template"}
end

end
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/kubetruth/etl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ class ForceExit < Exception; end
end

describe "#apply" do

let(:collection) { ProjectCollection.new }
let(:root_spec_crd) {
default_root_spec = YAML.load_file(File.expand_path("../../helm/kubetruth/values.yaml", __dir__)).deep_symbolize_keys
Expand Down Expand Up @@ -334,6 +335,18 @@ class ForceExit < Exception; end
etl.apply()
end

it "renders a stream of templates" do
config.root_spec.resource_templates = {"name1" => Template.new("stream_item: one\n---\nstream_item: two\n")}
allow(etl).to receive(:load_config).and_yield(@ns, config)

expect(collection).to receive(:names).and_return(["proj1"])

expect(etl).to receive(:kube_apply).with(hash_including("stream_item" => "one"))
expect(etl).to receive(:kube_apply).with(hash_including("stream_item" => "two"))

etl.apply()
end

it "skips empty templates" do
config.root_spec.resource_templates = {"name1" => Template.new("\n\n \n")}
allow(etl).to receive(:load_config).and_yield(@ns, config)
Expand Down

0 comments on commit aa78857

Please sign in to comment.