Skip to content

Commit

Permalink
PoC for parameterized status monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
timothysmith0609 committed Nov 20, 2018
1 parent 306a5f3 commit 3454521
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/kubernetes-deploy/kubernetes_resource/custom_resource.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
require 'pry'
require 'jsonpath'
module KubernetesDeploy
class CustomResource < KubernetesResource
def initialize(namespace:, context:, definition:, logger:, statsd_tags: [], crd:)
Expand All @@ -13,23 +14,24 @@ def timeout
end

def deploy_succeeded?
if monitor_rollout?
condition("Ready")["status"] == "True"
else
super
return super unless monitor_rollout?

rollout_params[:success_queries]&.all? do |condition|
JsonPath.new(condition[:query]).first(@instance_data) == condition[:value]
end
end

def deploy_failed?
if monitor_rollout?
condition("Failed")["status"] == "True"
else
super
return super unless monitor_rollout?
return true unless rollout_params.present?

rollout_params[:failure_queries].any? do |condition|
JsonPath.new(condition[:query]).first(@instance_data) == condition[:value]
end
end

def failure_message
condition("Failed")["message"] || "unknown error deploying #{id}"
"error deploying #{id}"
end

def id
Expand All @@ -55,5 +57,9 @@ def kind
def monitor_rollout?
@crd.monitor_rollouts?
end

def rollout_params
@rollout_params ||= @crd.rollout_params
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class CustomResourceDefinition < KubernetesResource
TIMEOUT = 2.minutes
CHILD_CR_TIMEOUT_ANNOTATION = "kubernetes-deploy.shopify.io/cr-timeout-override"
MONITOR_ROLLOUT_ANNOTATION = "kubernetes-deploy.shopify.io/monitor-rollout"
ROLLOUT_PARAMS_ANNOTATION = "kubernetes-deploy.shopify.io/cr-rollout-params"
GLOBAL = true

def deploy_succeeded?
Expand Down Expand Up @@ -51,6 +52,18 @@ def monitor_rollouts?
@definition.dig("metadata", "annotations", MONITOR_ROLLOUT_ANNOTATION) == "true"
end

def rollout_params
params = JSON.parse(@definition.dig("metadata", "annotations", ROLLOUT_PARAMS_ANNOTATION))
{
success_queries: params["success_queries"] || default_success_query,
failure_queries: params["failure_queries"] || default_failure_query,
timeout: params["timeout"] || TIMEOUT
}
rescue JSON::ParserError
@logger.error("JSON::ParserError, custom rollout params is not valid JSON")
{}
end

private

def names_accepted_condition
Expand All @@ -61,5 +74,13 @@ def names_accepted_condition
def names_accepted_status
names_accepted_condition["status"]
end

def default_success_query
[{ query: '$.status.Conditions[?(@.type == "Ready")].status', value: "True" }]
end

def default_failure_query
[{ query: '$.status.Conditions[?(@.type == "Failed")].status', value: "True" }]
end
end
end

0 comments on commit 3454521

Please sign in to comment.