Skip to content

Commit

Permalink
test and safer copy
Browse files Browse the repository at this point in the history
  • Loading branch information
timothysmith0609 committed Jan 15, 2025
1 parent 18e0e5d commit e9c6d45
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 25 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

37 changes: 31 additions & 6 deletions lib/krane/kubernetes_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Krane
class KubernetesResource
using PsychK8sCompatibility

attr_reader :name, :namespace, :context
attr_reader :name, :namespace, :context, :definition
attr_writer :type, :deploy_started_at, :global

GLOBAL = false
Expand Down Expand Up @@ -385,14 +385,12 @@ def use_generated_name(instance_data)
# local definition (for example, pruning a Pod), it will fail because the server has already modified the object.
# To work around this, we update the local definition with the server's modified version.
def update_definition!(definition)
@definition = sanitized_definition(definition)
@definition = sanitize_definition(definition)
@file = create_definition_tempfile
end

def sanitized_definition(definition)
definition.delete("status")
definition["metadata"].delete("resourceVersion")
definition
def sanitize_definition(definition)
@definition = safe_deep_copy(@definition, definition)
end

class Event
Expand Down Expand Up @@ -626,5 +624,32 @@ def statsd_tags
tags = %W(context:#{context} namespace:#{namespace} type:#{type} status:#{status})
tags | @optional_statsd_tags
end

# Recursively construct a new definition from an updated document
def safe_deep_copy(current, incoming, acc={})
current.keys.each do |key|
case key
when Hash
acc[key] = _safe_deep_copy(current[key], incoming[key])
when Array
acc[key] = incoming[key].map { |v| _safe_deep_copy(v, incoming) }
else
acc[key] = incoming[key]
end
end
acc
end

def _safe_deep_copy(current, incoming, acc={})
case current
when Hash
acc[key] = _safe_deep_copy(current[key], incoming[key])
when Array
acc = incoming.map { |v| _safe_deep_copy(v, incoming)}
else
acc = incoming
end
acc
end
end
end
38 changes: 19 additions & 19 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'pry'
# require 'pry'

if ENV["COVERAGE"]
require 'simplecov'
Expand Down Expand Up @@ -34,24 +34,24 @@
c.stubbing_non_public_method = :prevent
end

if ENV["PARALLELIZE_ME"]
Minitest::Reporters.use!([
Minitest::Reporters::ParallelizableReporter.new(
fast_fail: ENV['VERBOSE'] == '1',
slow_count: 10,
detailed_skip: false,
verbose: ENV['VERBOSE'] == '1'
),
])
else
Minitest::Reporters.use!([
Minitest::Reporters::DefaultReporter.new(
slow_count: 10,
detailed_skip: false,
verbose: ENV['VERBOSE'] == '1'
),
])
end
# if ENV["PARALLELIZE_ME"]
# Minitest::Reporters.use!([
# Minitest::Reporters::ParallelizableReporter.new(
# fast_fail: ENV['VERBOSE'] == '1',
# slow_count: 10,
# detailed_skip: false,
# verbose: ENV['VERBOSE'] == '1'
# ),
# ])
# else
# Minitest::Reporters.use!([
# Minitest::Reporters::DefaultReporter.new(
# slow_count: 10,
# detailed_skip: false,
# verbose: ENV['VERBOSE'] == '1'
# ),
# ])
# end

module Krane
class TestCase < ::Minitest::Test
Expand Down
46 changes: 46 additions & 0 deletions test/unit/krane/kubernetes_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,52 @@ def test_build_handles_hardcoded_and_core_and_dynamic_objects
assert_equal(resource.class, Krane::KubernetesResource)
end

def test_sanitized_definition_copy
definition = {
"kind" => "Unknown",
"metadata" => {
"annotations" => {
"one" => "val1",
"two" => "val2",
},
"name" => "test",
},
"spec" => {
"containers" => [
"fake" => "key"
]
}
}

incoming = {
"kind" => "Unknown",
"metadata" => {
"annotations" => {
"one" => "val1",
"two" => "val2",
"three" => "val3"
},
"name" => "test",
},
"spec" => {
"containers" => [
"fake" => "key",
"new" => "value",
"nested" => {
"key" => "value"
}
],
"should-not" => "appear"
}
}

resource = Krane::KubernetesResource.build(namespace: "test", context: "test", logger: @logger,
statsd_tags: [], definition: definition)
resource.sanitize_definition(incoming)
incoming["spec"].delete("should-not")
assert_equal(resource.definition, incoming)
end

private

def kubectl
Expand Down

0 comments on commit e9c6d45

Please sign in to comment.