Skip to content

Commit

Permalink
Add support for deployments and services to be "predeployed"
Browse files Browse the repository at this point in the history
  • Loading branch information
c-gerke committed Nov 12, 2024
1 parent cc6a219 commit 611b8c2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
34 changes: 34 additions & 0 deletions lib/krane/cluster_resource_discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ 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 prunable_resources(namespaced:)
black_list = %w(Namespace Node ControllerRevision Event)
fetch_resources(namespaced: namespaced).map do |resource|
Expand Down Expand Up @@ -98,6 +112,26 @@ 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 kubectl
@kubectl ||= Kubectl.new(task_config: @task_config, log_failure_by_default: true)
end
Expand Down
6 changes: 5 additions & 1 deletion lib/krane/deploy_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ def predeploy_sequence
).map { |r| [r, default_group] }

after_crs = %w(
Deployment
Service
Pod
).map { |r| [r, default_group] }

crs = cluster_resource_discoverer.crds.select(&:predeployed?).map { |cr| [cr.kind, { group: cr.group }] }
Hash[before_crs + crs + after_crs]
svc = cluster_resource_discoverer.services.select(&:predeployed?).map { |svc| [svc.kind, { group: svc.group }] }
deployment = cluster_resource_discoverer.deployments.select(&:predeployed?).map { |deployment| [deployment.kind, { group: deployment.group }] }
Hash[before_crs + crs + svc + deployment + after_crs]
end

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

def predeployed?
predeployed = krane_annotation_value("predeployed")
predeployed.nil? || 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
9 changes: 9 additions & 0 deletions lib/krane/kubernetes_resource/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def timeout_message
"Please confirm the spec.selector is correct and the targeted workload is healthy."
end

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

def kind
@definition["kind"]
end

private

def fetch_related_workloads(cache)
Expand Down

0 comments on commit 611b8c2

Please sign in to comment.