Skip to content

Commit

Permalink
Make the dry run option exercise more of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Apr 30, 2021
1 parent 853ef3a commit 8d9c9b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
49 changes: 19 additions & 30 deletions lib/kubetruth/etl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,10 @@ def apply
config_param_hash = params_to_hash(config_params)
secret_param_hash = params_to_hash(secret_params)

if @dry_run
logger.info("Config maps that would be created are:")
logger.info(config_params.pretty_print_inspect)

if ! project_spec.skip_secrets
logger.info("Secrets that would be created are:")
secret_params.each {|p| p.value = "<masked>" if p.secret}
logger.info(secret_params.pretty_print_inspect)
end
apply_config_map(namespace: data[:namespace], name: data[:configmap_name], param_hash: config_param_hash)

next
else
apply_config_map(namespace: data[:namespace], name: data[:configmap_name], param_hash: config_param_hash)

if ! project_spec.skip_secrets
apply_secret(namespace: data[:namespace], name: data[:secret_name], param_hash: secret_param_hash)
end
if ! project_spec.skip_secrets
apply_secret(namespace: data[:namespace], name: data[:secret_name], param_hash: secret_param_hash)
end
end
end
Expand Down Expand Up @@ -238,52 +225,54 @@ def params_to_hash(param_list)

def apply_config_map(namespace:, name:, param_hash:)
logger.info("Applying config map #{namespace}:#{name}")
logger.debug { " with params: #{param_hash.keys.inspect}}" }

kapi = kubeapi(namespace)
kapi.ensure_namespace
kapi.ensure_namespace unless @dry_run
logger.debug { "Existing config maps (ns=#{kapi.namespace}): #{kapi.get_config_map_names}" }

begin
resource = kapi.get_config_map(name)
data = resource.data.to_h
logger.debug("Config map for '#{name}': #{data.inspect}")
logger.debug { "Existing config map for '#{name}': #{data.inspect}" }
if ! kapi.under_management?(resource)
logger.warn "Skipping config map '#{name}' as it doesn't have a label indicating it is under kubetruth management"
elsif param_hash != data.transform_keys! {|k| k.to_s }
logger.info "Updating config map '#{name}' with params: #{param_hash.inspect}"
kapi.update_config_map(name, param_hash)
logger.info "Updating config map '#{name}'"
kapi.update_config_map(name, param_hash) unless @dry_run
else
logger.info "No changes needed for config map '#{name}' with params: #{param_hash.inspect}}"
logger.info "No changes needed for config map '#{name}'"
end
rescue Kubeclient::ResourceNotFoundError
logger.info "Creating config map '#{name}' with params: #{param_hash.inspect}}"
kapi.create_config_map(name, param_hash)
logger.info "Creating config map '#{name}'"
kapi.create_config_map(name, param_hash) unless @dry_run
end
end

def apply_secret(namespace:, name:, param_hash:)
logger.info("Applying secrets #{namespace}:#{name}")
logger.debug { " with params: #{param_hash.keys.inspect}" }

kapi = kubeapi(namespace)
kapi.ensure_namespace
kapi.ensure_namespace unless @dry_run
logger.debug { "Existing secrets (ns=#{kapi.namespace}): #{kapi.get_secret_names}" }

begin
logger.debug { "Namespace '#{kapi.namespace}'" }
resource = kapi.get_secret(name)
data = kapi.secret_hash(resource)
logger.debug { "Secret keys for '#{name}': #{data.transform_keys! {|k| k.to_s }}" }
logger.debug { "Existing Secret for '#{name}': #{data.transform_keys! {|k| k.to_s }}" }
if ! kapi.under_management?(resource)
logger.warn "Skipping secret '#{name}' as it doesn't have a label indicating it is under kubetruth management"
elsif param_hash != data.transform_keys! {|k| k.to_s }
logger.info "Updating secret '#{name}' with params: #{param_hash.keys.inspect}"
kapi.update_secret(name, param_hash)
logger.info "Updating secret '#{name}'"
kapi.update_secret(name, param_hash) unless @dry_run
else
logger.info "No changes needed for secret '#{name}' with params: #{param_hash.keys.inspect}}"
logger.info "No changes needed for secret '#{name}'"
end
rescue Kubeclient::ResourceNotFoundError
logger.info "Creating secret '#{name}' with params: #{param_hash.keys.inspect}}"
kapi.create_secret(name, param_hash)
logger.info "Creating secret '#{name}'"
kapi.create_secret(name, param_hash) unless @dry_run
end
end

Expand Down
12 changes: 9 additions & 3 deletions spec/kubetruth/etl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def kubeapi(ns)
kapi = double(Kubetruth::KubeApi)
ns = ns.present? ? ns : nil
allow(Kubetruth::KubeApi).to receive(:new).with(hash_including(namespace: ns)).and_return(kapi)
allow(kapi).to receive(:get_config_map).and_return(Kubeclient::Resource.new)
allow(kapi).to receive(:get_secret).and_return(Kubeclient::Resource.new)
allow(kapi).to receive(:under_management?).and_return(true)
allow(kapi).to receive(:secret_hash).and_return({})
allow(kapi).to receive(:get_config_map_names).and_return([])
allow(kapi).to receive(:get_secret_names).and_return([])
allow(kapi).to receive(:ensure_namespace)
Expand Down Expand Up @@ -524,11 +528,13 @@ class ForceExit < Exception; end
Parameter.new(key: "param2", value: "value2", secret: true)
]
etl = described_class.new(init_args.merge(dry_run: true))
etl.load_config.root_spec.skip_secrets = true
expect(etl.ctapi).to receive(:project_names).and_return(["default"])
expect(etl).to receive(:get_params).and_return(params)
expect(etl).to_not receive(:apply_config_map)
expect(etl).to_not receive(:apply_secret)
expect(@kubeapi).to_not receive(:ensure_namespace)
expect(@kubeapi).to_not receive(:create_config_map)
expect(@kubeapi).to_not receive(:update_config_map)
expect(@kubeapi).to_not receive(:create_secret)
expect(@kubeapi).to_not receive(:update_secret)
etl.apply
expect(Logging.contents).to match("Performing dry-run")
end
Expand Down

0 comments on commit 8d9c9b4

Please sign in to comment.