Skip to content

Commit

Permalink
add active_templates in crd to make it easier to selectively enable o…
Browse files Browse the repository at this point in the history
…f templates
  • Loading branch information
wr0ngway committed Dec 22, 2021
1 parent ffc8563 commit 3399bce
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .app.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
org: cloudtruth
name: kubetruth
version: 1.0.4
version: 1.1.0
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Parameterize the helm install with `--set *` or `--values yourConfig.yaml` to co
| projectMappings.root.log_level | Sets the kubetruth logging level while handling the selected projects | enum(debug, info, warn, error, fatal) | `as set by cli` | no |
| projectMappings.root.included_projects | Include the parameters from other projects into the selected ones. This can be recursive in a depth first fashion, so if A imports B and B imports C, then A will get B's and C's parameters. For key conflicts, if A includes B and B includes C, then the precendence is A overrides B overrides C. If A includes \[B, C], then the precendence is A overrides C overrides B. | list | [] | no |
| projectMappings.root.context | Additional variables made available to the resource templates. Can also be templates | map | [default](helm/kubetruth/values.yaml#L93-L129) | no |
| projectMappings.root.active_templates | Selects the templates that should be active, includes all templates when nil, none when empty | list | nil | no |
| projectMappings.root.resource_templates | The templates to use in generating kubernetes resources (ConfigMap/Secrets/other) | map | [default](helm/kubetruth/values.yaml#L93-L129) | no |
| projectMappings.<override_name>.* | Define override mappings to override settings from the root selector for specific projects. When doing this on the command-line (e.g. for `helm install`), it may be more convenient to use `--values <file>` instead of `--set` for large data sets | map | {} | no |

Expand Down Expand Up @@ -185,8 +186,11 @@ ones:
| `encode64` | The argument bas64 encoded |
| `decode64` | The argument bas64 decoded |
| `sha256` | The sha256 digest of the argument |
| `inflate` | Converts a map of key/values into a nested data structure based on a delimiter in the key name, e.g. `{foo.baz.bum: 2}` => `{foo: {bar: {baz: 2}}}` |
| `inflate` | Converts a map of key/values into a nested data structure based on a delimiter in the key name, e.g. `{foo.baz.bum: 2}` => `{foo: {bar: {baz: 2}}}` Inverse of deflate|
| `deflate` | Converts a nested data structure into a single level map using a delimiter to indicate level in the key name, e.g. `{foo: {bar: {baz: 2}}}` => `{foo.baz.bum: 2}` Inverse of inflate |
| `typify` | Converts string values into primitive types (int, float, bool) where applicable for a nested data structure |
| `mconcat` | Combines two hashes into one, a.l.a ruby merge |
| `re_replace` | Regexp search and replace, a.l.a ruby gsub, e.g. `"foo" | re_replace: "o+", "X"` |

The default `resource_templates` make use of the `context` attribute to allow
simpler modification of some common fields. These include:
Expand Down
5 changes: 5 additions & 0 deletions helm/helmv2/templates/projectmapping.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ spec:
additionalProperties:
type: string
description: Context variables that can be used by templates. The values can also be templates
active_templates:
type: array
items:
type: string
description: Select the templates that should be active, includes all templates when nil, none when empty
resource_templates:
type: object
additionalProperties:
Expand Down
5 changes: 5 additions & 0 deletions helm/kubetruth/crds/projectmapping.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ spec:
type: object
additionalProperties: true
description: Context variables that can be used by templates. The values can also be templates
active_templates:
type: array
items:
type: string
description: Selects the templates that should be active, includes all templates when nil, none when empty
resource_templates:
type: object
additionalProperties:
Expand Down
1 change: 1 addition & 0 deletions helm/kubetruth/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ projectMappings:
resource_name: "{{ project | dns_safe }}"
resource_namespace: "{{ mapping_namespace }}"
skip_secrets: false
active_templates:
resource_templates:
configmap: |
{%- if parameters.size > 0 %}
Expand Down
7 changes: 7 additions & 0 deletions lib/kubetruth/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DuplicateSelection < Kubetruth::Error; end
:log_level,
:included_projects,
:context,
:active_templates,
:resource_templates,
keyword_init: true
) do
Expand All @@ -42,6 +43,11 @@ def convert_types(hash)
end
end

def templates
return resource_templates if active_templates.nil?
resource_templates.select {|k, v| active_templates.include?(k) }
end

end

DEFAULT_SPEC = {
Expand All @@ -55,6 +61,7 @@ def convert_types(hash)
log_level: nil,
included_projects: [],
context: {},
active_templates: nil,
resource_templates: []
}.freeze

Expand Down
5 changes: 3 additions & 2 deletions lib/kubetruth/etl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,10 @@ def apply
v.nil?
end

project.spec.resource_templates.each_with_index do |pair, i|
resource_templates = project.spec.templates
resource_templates.each_with_index do |pair, i|
template_name, template = *pair
logger.debug { "Processing template '#{template_name}' (#{i+1}/#{project.spec.resource_templates.size})" }
logger.debug { "Processing template '#{template_name}' (#{i+1}/#{resource_templates.size})" }
resource_yml = template.render(
template: template_name,
kubetruth_namespace: kubeapi.namespace,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions spec/kubetruth/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ module Kubetruth
it "sets mappings" do
expect(config.instance_variable_get(:@project_mapping_crds)).to eq([])
end

end

describe "templates" do

it "returns all templates when active_templates nil" do
spec = described_class::ProjectSpec.new(
resource_templates: {"name1" => "template1"}
)

expect(spec.templates).to equal(spec[:resource_templates])
end

it "filters by active_templates" do
spec = described_class::ProjectSpec.new(
active_templates: %w[name2 name3],
resource_templates: {"name1" => "template1", "name2" => "template2", "name3" => "template3", "name4" => "template4"}
)

expect(spec.templates.keys).to eq(["name2" , "name3"])
end

end

Expand Down
20 changes: 15 additions & 5 deletions spec/kubetruth/etl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class ForceExit < Exception; end

it "renders multiple templates" do
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("kind" => "ConfigMap"))
Expand All @@ -428,6 +428,19 @@ class ForceExit < Exception; end
etl.apply()
end

it "renders only active templates" do
config.root_spec.active_templates = ["secret"]
allow(etl).to receive(:load_config).and_yield(@ns, config)

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

expect(etl).to_not receive(:kube_apply).with(hash_including("kind" => "ConfigMap"))
expect(etl).to receive(:kube_apply).with(hash_including("kind" => "Secret"))

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)
Expand Down Expand Up @@ -572,10 +585,7 @@ class ForceExit < Exception; end
describe "default templates" 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
default_root_spec[:projectMappings][:root]
}
let(:root_spec_crd) { default_root_spec }
let(:config) {
Kubetruth::Config.new([root_spec_crd])
}
Expand Down
2 changes: 1 addition & 1 deletion spec/kubetruth/kubeapi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def apiserver; "https://127.0.0.1"; end
expect(crds.keys.first).to eq(kubeapi.namespace)
expect(crds.values.first).to match(hash_including("#{helm_name}-root"))
expect(crds.values.first["#{helm_name}-root"][:name]).to eq("#{helm_name}-root")
expect(crds.values.first["#{helm_name}-root"].keys.sort).to eq((default_root_spec.keys << :name).sort)
expect(default_root_spec.keys).to include(*crds.values.first["#{helm_name}-root"].keys)
end

it "can watch project mappings" do
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def logger
end

config.after(:each) do |example|
if example.exception
if ENV['DEBUG'] || example.exception
puts
puts "Debug log for failing spec: #{example.full_description}"
puts Kubetruth::Logging.contents
Expand Down

0 comments on commit 3399bce

Please sign in to comment.