Skip to content

Commit

Permalink
Merge changes from 'predeploy-more-resources' into powerhrg
Browse files Browse the repository at this point in the history
  • Loading branch information
c-gerke committed Dec 5, 2024
1 parent ddddb0e commit e1fcfd0
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 133 deletions.
68 changes: 0 additions & 68 deletions lib/krane/cluster_resource_discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,6 @@ def crds
end
end

def services
@services ||= fetch_services.map do |svc|
Service.new(namespace: namespace, context: context, logger: logger,
definition: svc, statsd_tags: @namespace_tags)
end
end

def deployments
@deployments ||= fetch_deployments.map do |deployment|
Deployment.new(namespace: namespace, context: context, logger: logger,
definition: deployment, statsd_tags: @namespace_tags)
end
end

def jobs
@jobs ||= fetch_jobs.map do |job|
Job.new(namespace: namespace, context: context, logger: logger,
definition: job, statsd_tags: @namespace_tags)
end
end

def ingresses
@ingresses ||= fetch_ingresses.map do |ingress|
Ingress.new(namespace: namespace, context: context, logger: logger,
definition: ingress, statsd_tags: @namespace_tags)
end
end

def prunable_resources(namespaced:)
black_list = %w(Namespace Node ControllerRevision Event)
fetch_resources(namespaced: namespaced).map do |resource|
Expand Down Expand Up @@ -126,46 +98,6 @@ def fetch_crds
end
end

def fetch_services
raw_json, err, st = kubectl.run("get", "Service", output: "json", attempts: 5,
use_namespace: false)
if st.success?
MultiJson.load(raw_json)["items"]
else
raise FatalKubeAPIError, "Error retrieving Service: #{err}"
end
end

def fetch_deployments
raw_json, err, st = kubectl.run("get", "Deployment", output: "json", attempts: 5,
use_namespace: false)
if st.success?
MultiJson.load(raw_json)["items"]
else
raise FatalKubeAPIError, "Error retrieving Deployment: #{err}"
end
end

def fetch_jobs
raw_json, err, st = kubectl.run("get", "Job", output: "json", attempts: 5,
use_namespace: false)
if st.success?
MultiJson.load(raw_json)["items"]
else
raise FatalKubeAPIError, "Error retrieving Job: #{err}"
end
end

def fetch_ingresses
raw_json, err, st = kubectl.run("get", "Ingress", output: "json", attempts: 5,
use_namespace: false)
if st.success?
MultiJson.load(raw_json)["items"]
else
raise FatalKubeAPIError, "Error retrieving Ingress: #{err}"
end
end

def kubectl
@kubectl ||= Kubectl.new(task_config: @task_config, log_failure_by_default: true)
end
Expand Down
8 changes: 8 additions & 0 deletions lib/krane/deploy_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def predeploy_sequence
Ingress
Pod
Job
CronJob
DaemonSet
HorizontalPodAutoscaler
PodDisruptionBudget
PodSetBase
PodTemplate
ReplicaSet
StatefulSet
).map { |r| [r, default_group] }

crs = cluster_resource_discoverer.crds.select(&:predeployed?).map { |cr| [cr.kind, { group: cr.group }] }
Expand Down
14 changes: 13 additions & 1 deletion lib/krane/kubernetes_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,21 @@ def use_generated_name(instance_data)
@file = create_definition_tempfile
end

PREDEPLOYED_RESOURCE_TYPES = [
"ResourceQuota",
"NetworkPolicy",
"ConfigMap",
"PersistentVolumeClaim",
"ServiceAccount",
"Role",
"RoleBinding",
"Secret",
"Pod"
]

def predeployed?
predeployed = krane_annotation_value("predeployed")
predeployed.nil? || predeployed == "false"
PREDEPLOYED_RESOURCE_TYPES.include?(type) || predeployed == "true"
end

class Event
Expand Down
5 changes: 5 additions & 0 deletions lib/krane/kubernetes_resource/custom_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def type
kind
end

def predeployed?
predeployed = krane_annotation_value("predeployed")
predeployed.nil? || predeployed == "true"
end

def validate_definition(*, **)
super

Expand Down
8 changes: 0 additions & 8 deletions lib/krane/kubernetes_resource/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ def deploy_failed?
observed_generation == current_generation
end

def predeployed?
krane_annotation_value("predeployed") == "true"
end

def kind
@definition["kind"]
end

def failure_message
return unless @latest_rs.present?
"Latest ReplicaSet: #{@latest_rs.name}\n\n#{@latest_rs.failure_message}"
Expand Down
7 changes: 0 additions & 7 deletions lib/krane/kubernetes_resource/ingress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,5 @@ def deploy_failed?
false
end

def predeployed?
krane_annotation_value("predeployed") == "true"
end

def kind
@definition["kind"]
end
end
end
8 changes: 0 additions & 8 deletions lib/krane/kubernetes_resource/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ def deploy_failed?
(@instance_data.dig("status", "failed") || 0) >= @instance_data.dig("spec", "backoffLimit")
end

def predeployed?
krane_annotation_value("predeployed") == "true"
end

def kind
@definition["kind"]
end

def status
if !exists?
super
Expand Down
8 changes: 0 additions & 8 deletions lib/krane/kubernetes_resource/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ def timeout_message
"Please confirm the spec.selector is correct and the targeted workload is healthy."
end

def predeployed?
krane_annotation_value("predeployed") == "true"
end

def kind
@definition["kind"]
end

private

def fetch_related_workloads(cache)
Expand Down
62 changes: 32 additions & 30 deletions lib/krane/resource_deployer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,40 +94,42 @@ def deploy_resources(resources, prune: false, verify:, record_summary: true)
logger.info("Deploying #{resource.id} (#{resource.pretty_timeout_type})")
end

# Apply can be done in one large batch, the rest have to be done individually
applyables, individuals = resources.partition { |r| r.deploy_method == :apply }
# Prunable resources should also applied so that they can be pruned
pruneable_types = @prune_allowlist.map { |t| t.split("/").last }
applyables += individuals.select { |r| pruneable_types.include?(r.type) && !r.deploy_method_override }

individuals.each do |individual_resource|
individual_resource.deploy_started_at = Time.now.utc
case individual_resource.deploy_method
when :create
err, status = create_resource(individual_resource)
when :replace
err, status = replace_or_create_resource(individual_resource)
when :replace_force
err, status = replace_or_create_resource(individual_resource, force: true)
else
# Fail Fast! This is a programmer mistake.
raise ArgumentError, "Unexpected deploy method! (#{individual_resource.deploy_method.inspect})"
end
StatsD.client.measure('sync.duration', tags: statsd_tags) do
# Apply can be done in one large batch, the rest have to be done individually
applyables, individuals = resources.partition { |r| r.deploy_method == :apply }
# Prunable resources should also applied so that they can be pruned
pruneable_types = @prune_allowlist.map { |t| t.split("/").last }
applyables += individuals.select { |r| pruneable_types.include?(r.type) && !r.deploy_method_override }

individuals.each do |individual_resource|
individual_resource.deploy_started_at = Time.now.utc
case individual_resource.deploy_method
when :create
err, status = create_resource(individual_resource)
when :replace
err, status = replace_or_create_resource(individual_resource)
when :replace_force
err, status = replace_or_create_resource(individual_resource, force: true)
else
# Fail Fast! This is a programmer mistake.
raise ArgumentError, "Unexpected deploy method! (#{individual_resource.deploy_method.inspect})"
end

next if status.success?
next if status.success?

raise FatalDeploymentError, <<~MSG
Failed to replace or create resource: #{individual_resource.id}
#{individual_resource.sensitive_template_content? ? '<suppressed sensitive output>' : err}
MSG
end
raise FatalDeploymentError, <<~MSG
Failed to replace or create resource: #{individual_resource.id}
#{individual_resource.sensitive_template_content? ? '<suppressed sensitive output>' : err}
MSG
end

apply_all(applyables, prune)
apply_all(applyables, prune)

if verify
watcher = Krane::ResourceWatcher.new(resources: resources, deploy_started_at: deploy_started_at,
timeout: @global_timeout, task_config: @task_config, sha: @current_sha)
watcher.run(record_summary: record_summary)
if verify
watcher = Krane::ResourceWatcher.new(resources: resources, deploy_started_at: deploy_started_at,
timeout: @global_timeout, task_config: @task_config, sha: @current_sha)
watcher.run(record_summary: record_summary)
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/krane/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
module Krane
VERSION = "3.4.2-powerhrg-6"
VERSION = "3.4.2-powerhrg-7"
end
Loading

0 comments on commit e1fcfd0

Please sign in to comment.