From aa788578ac9c836b50ee40ea714e807e9f332b79 Mon Sep 17 00:00:00 2001 From: Matt Conway Date: Thu, 1 Jul 2021 16:32:41 -0400 Subject: [PATCH] allow multiple yml docs in templates --- Gemfile | 1 + Gemfile.lock | 2 ++ lib/kubetruth/etl.rb | 13 +++++++++---- spec/kubetruth/etl_spec.rb | 13 +++++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index c9c4be1..269b521 100644 --- a/Gemfile +++ b/Gemfile @@ -24,3 +24,4 @@ gem 'clamp' gem 'graphql-client' gem 'kubeclient' gem 'liquid' +gem 'yaml-safe_load_stream' diff --git a/Gemfile.lock b/Gemfile.lock index 6e9ca9a..6c7718d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 @@ -129,6 +130,7 @@ DEPENDENCIES simplecov vcr webmock + yaml-safe_load_stream BUNDLED WITH 2.1.4 diff --git a/lib/kubetruth/etl.rb b/lib/kubetruth/etl.rb index 3855bb7..5404a88 100644 --- a/lib/kubetruth/etl.rb +++ b/lib/kubetruth/etl.rb @@ -1,5 +1,8 @@ require 'benchmark' require 'yaml' +require 'yaml/safe_load_stream' +using YAMLSafeLoadStream + require_relative 'config' require_relative 'ctapi' require_relative 'kubeapi' @@ -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 diff --git a/spec/kubetruth/etl_spec.rb b/spec/kubetruth/etl_spec.rb index 9b14b69..01eed2e 100644 --- a/spec/kubetruth/etl_spec.rb +++ b/spec/kubetruth/etl_spec.rb @@ -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 @@ -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)