Skip to content

Commit

Permalink
rewrite me
Browse files Browse the repository at this point in the history
  • Loading branch information
renescheepers committed Aug 17, 2022
1 parent f0e0da7 commit 5ca1ff6
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 63 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
68 changes: 31 additions & 37 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 @@ -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/kubernetes_resource/apps/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def deploy_succeeded?
rollout_data["updatedReplicas"].to_i == rollout_data["availableReplicas"].to_i
elsif required_rollout == 'none'
true
elsif required_rollout == 'maxUnavailable' || percent?(required_rollout)
elsif required_rollout == 'maxUnavailable' || percent?(required_rollout)
minimum_needed = min_available_replicas

@latest_rs.desired_replicas >= minimum_needed &&
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
7 changes: 5 additions & 2 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
apply_scope_to_resources(fixtures, labels: selector)
end

yield fixtures if block_given?

Expand Down
2 changes: 1 addition & 1 deletion test/integration/krane_deploy_test.rb
Original file line number Diff line number Diff line change
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)
assert_deploy_success(result)

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

0 comments on commit 5ca1ff6

Please sign in to comment.