From 611b8c22f314a409d49a80d343f5d3239cd3fee2 Mon Sep 17 00:00:00 2001 From: Colton Gerke Date: Tue, 12 Nov 2024 14:30:41 -0600 Subject: [PATCH] Add support for deployments and services to be "predeployed" --- lib/krane/cluster_resource_discovery.rb | 34 +++++++++++++++++++++ lib/krane/deploy_task.rb | 6 +++- lib/krane/kubernetes_resource/deployment.rb | 9 ++++++ lib/krane/kubernetes_resource/service.rb | 9 ++++++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/lib/krane/cluster_resource_discovery.rb b/lib/krane/cluster_resource_discovery.rb index 44f968906..3ae3505c6 100644 --- a/lib/krane/cluster_resource_discovery.rb +++ b/lib/krane/cluster_resource_discovery.rb @@ -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| @@ -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 diff --git a/lib/krane/deploy_task.rb b/lib/krane/deploy_task.rb index 4973b48ac..6ad7255c7 100644 --- a/lib/krane/deploy_task.rb +++ b/lib/krane/deploy_task.rb @@ -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 diff --git a/lib/krane/kubernetes_resource/deployment.rb b/lib/krane/kubernetes_resource/deployment.rb index 7c4f81d7a..0a432a7b2 100644 --- a/lib/krane/kubernetes_resource/deployment.rb +++ b/lib/krane/kubernetes_resource/deployment.rb @@ -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}" diff --git a/lib/krane/kubernetes_resource/service.rb b/lib/krane/kubernetes_resource/service.rb index 78a7f35c3..7803939aa 100644 --- a/lib/krane/kubernetes_resource/service.rb +++ b/lib/krane/kubernetes_resource/service.rb @@ -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)