Skip to content

Commit

Permalink
rewrite me
Browse files Browse the repository at this point in the history
  • Loading branch information
renescheepers committed Aug 18, 2022
1 parent f0e0da7 commit 5868680
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 73 deletions.
44 changes: 23 additions & 21 deletions lib/krane/cluster_resource_discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,31 @@ def fetch_resources(namespaced: false)
end

def fetch_group_kinds
output, err, st = kubectl.run("api-resources", "--no-headers=true", attempts: 2, use_namespace: false)
if st.success?
output.split("\n").map do |l|
matches = l.scan(/\S+/)

if matches.length == 4
# name, api, namespaced, kind
::Krane::APIResource.new(
::Krane::KubernetesResource.group_from_api_version(matches[1]),
matches[3],
matches[2] == "true"
)
else
# name, shortname, api, namespaced, kind
::Krane::APIResource.new(
::Krane::KubernetesResource.group_from_api_version(matches[2]),
matches[4],
matches[3] == "true"
)
@group_kinds ||= begin
output, err, st = kubectl.run("api-resources", "--no-headers=true", attempts: 2, use_namespace: false)
if st.success?
output.split("\n").map do |l|
matches = l.scan(/\S+/)

if matches.length == 4
# name, api, namespaced, kind
::Krane::APIResource.new(
::Krane::KubernetesResource.group_from_api_version(matches[1]),
matches[3],
matches[2] == "true"
)
else
# name, shortname, api, namespaced, kind
::Krane::APIResource.new(
::Krane::KubernetesResource.group_from_api_version(matches[2]),
matches[4],
matches[3] == "true"
)
end
end
else
raise FatalKubeAPIError, "Error retrieving group kinds: #{err}"
end
else
raise FatalKubeAPIError, "Error retrieving group kinds: #{err}"
end
end

Expand Down
70 changes: 32 additions & 38 deletions lib/krane/kubernetes_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,43 @@ def group

def kind
# Converts Krane::ApiextensionsK8sIo::CustomResourceDefinition to CustomResourceDefinition
if name.scan(/::/).length == 1
_, c_kind = name.split("::", 2)
else
_, _, c_kind = name.split("::", 3)
end

c_kind
name.demodulize
end

def group_kind
::Krane::KubernetesResource.combine_group_kind(group, kind)
end

def group_from_api_version(input)
input.include?("/") ? input.split("/").first : ""
end

def combine_group_kind(group, kind)
"#{kind}.#{group}"
end

def group_kind_to_const(group_kind)
kind, group = group_kind.split(".", 2)

group = group.split(".").map(&:capitalize).join("")

if group == ""
group_const = ::Krane
elsif ::Krane.const_defined?(group)
group_const = ::Krane.const_get(group)
else
return nil
end

unless group_const.const_defined?(kind)
return nil
end

group_const.const_get(kind)
rescue NameError => _
nil
end

private

def validate_definition_essentials(definition)
Expand Down Expand Up @@ -361,7 +385,7 @@ def debug_message(cause = nil, info_hash = {})
def fetch_events(kubectl)
return {} unless exists?

out, err, st = kubectl.run("get", "events", "--output=go-template=#{Event.go_template_for(group, kind, name)}",
out, _err, st = kubectl.run("get", "events", "--output=go-template=#{Event.go_template_for(group, kind, name)}",
log_failure: false, use_namespace: !global?)
return {} unless st.success?

Expand Down Expand Up @@ -547,36 +571,6 @@ def selected?(selector)
selector.nil? || selector.to_h <= labels
end

def self.group_from_api_version(input)
input.include?("/") ? input.split("/").first : ""
end

def self.combine_group_kind(group, kind)
"#{kind}.#{group}"
end

def self.group_kind_to_const(group_kind)
kind, group = group_kind.split(".", 2)

group = group.split(".").map(&:capitalize).join("")

if group == ""
group_const = ::Krane
elsif ::Krane.const_defined?(group)
group_const = ::Krane.const_get(group)
else
return nil
end

unless group_const.const_defined?(kind)
return nil
end

group_const.const_get(kind)
rescue NameError => _
nil
end

private

def validate_timeout_annotation
Expand Down
2 changes: 1 addition & 1 deletion lib/krane/task_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(context, namespace, logger = nil, kubeconfig = nil)
end

def group_kinds
@group_kinds ||= cluster_resource_discoverer.fetch_group_kinds
cluster_resource_discoverer.fetch_group_kinds
end

def kubeclient_builder
Expand Down
9 changes: 6 additions & 3 deletions test/helpers/fixture_deploy_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ def deploy_fixtures(set, subset: nil, **args) # extra args are passed through to
success
end

def deploy_global_fixtures(set, subset: nil, selector: nil, verify_result: true, prune: true, global_timeout: 300)
def deploy_global_fixtures(set, subset: nil, selector: nil, verify_result: true, prune: true, global_timeout: 300,
apply_scope_to_resources: true)
fixtures = load_fixtures(set, subset)
raise "Cannot deploy empty template set" if fixtures.empty?

selector = (selector == false ? "" : "#{selector},app=krane,test=#{@namespace}".sub(/^,/, ''))
apply_scope_to_resources(fixtures, labels: selector)
if apply_scope_to_resources
selector = (selector == false ? "" : "#{selector},app=krane,test=#{@namespace}".sub(/^,/, ''))
apply_scope_to_resources(fixtures, labels: selector)
end

yield fixtures if block_given?

Expand Down
20 changes: 10 additions & 10 deletions test/integration/krane_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ def test_deploy_fails_with_empty_yaml
assert_deploy_failure(deploy_raw_fixtures("empty-resources", subset: %w[empty1.yml empty2.yml]))

assert_logs_match_all([
"All required parameters and files are present",
"Result: FAILURE",
"No deployable resources were found!",
], in_order: true)
"All required parameters and files are present",
"Result: FAILURE",
"No deployable resources were found!",
], in_order: true)
end

def test_deploy_fails_with_empty_erb
assert_deploy_failure(deploy_raw_fixtures("empty-resources", subset: %w[empty3.yml.erb empty4.yml.erb], render_erb: true))

assert_logs_match_all([
"All required parameters and files are present",
"Result: FAILURE",
"No deployable resources were found!",
], in_order: true)
"All required parameters and files are present",
"Result: FAILURE",
"No deployable resources were found!",
], in_order: true)
end

def test_service_account_predeployed_before_unmanaged_pod
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_pruning_works
prune_matcher("role", "rbac.authorization.k8s.io", "role"),
prune_matcher("rolebinding", "rbac.authorization.k8s.io", "role-binding"),
prune_matcher("persistentvolumeclaim", "", "hello-pv-claim"),
prune_matcher("ingress", %w(networking.k8s.io extensions), "web")
prune_matcher("ingress", %w(networking.k8s.io extensions), "web"),
] # not necessarily listed in this order
expected_msgs = [/Pruned 2[013] resources and successfully deployed 6 resources/]
expected_pruned.map do |resource|
Expand Down Expand Up @@ -1827,7 +1827,7 @@ def test_succeeds_with_deploy_method_override
end

def test_duplicate_kind_resource_definition
result = deploy_global_fixtures("crd", subset: ["deployment.yml"])
result = deploy_global_fixtures("crd", subset: ["deployment.yml"], apply_scope_to_resources: false, selector: "app=krane")
assert_deploy_success(result)

result = deploy_fixtures("crd", subset: ["web.yml"], global_timeout: 30)
Expand Down

0 comments on commit 5868680

Please sign in to comment.