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 21, 2018
1 parent 306a5f3 commit ad1d6ec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
32 changes: 21 additions & 11 deletions lib/kubernetes-deploy/kubernetes_resource/custom_resource.rb
@@ -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,26 @@ def timeout
end

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

rollout_params[:success_queries]&.all? do |query|
jsonpath_query(path: query[:path]) == query[:value]
end
end

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

rollout_params[:failure_queries].any? do |query|
jsonpath_query(path: query[:path]) == query[:value]
end
end

def failure_message
condition("Failed")["message"] || "unknown error deploying #{id}"
messages = rollout_params[:failure_queries].map do |query|
jsonpath_query(path: query[:error_msg_path]) if query[:error_msg_path]
end.compact
messages.present? ? messages.join("\n") : "error deploying #{id}"
end

def id
Expand All @@ -52,8 +56,14 @@ def kind
@definition["kind"]
end

def monitor_rollout?
@crd.monitor_rollouts?
def rollout_params
@rollout_params ||= @crd.rollout_params
end

def jsonpath_query(path:)
JsonPath.new(path).first(@instance_data)
rescue StandardError
raise FatalDeploymentError, "fatal error for #{id}. Failed to parse JsonPath for #{path}"
end
end
end
Expand Up @@ -3,7 +3,7 @@ module KubernetesDeploy
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 @@ -47,12 +47,24 @@ def timeout_for_children
@definition.dig("metadata", "annotations", CHILD_CR_TIMEOUT_ANNOTATION)&.to_i
end

def monitor_rollouts?
@definition.dig("metadata", "annotations", MONITOR_ROLLOUT_ANNOTATION) == "true"
def rollout_params
return nil unless rollout_params_string

params = JSON.parse(rollout_params_string)
{
success_queries: params["success_queries"] || default_success_query,
failure_queries: params["failure_queries"] || default_failure_query,
}.deep_symbolize_keys
rescue JSON::ParserError
raise FatalDeploymentError, "custom rollout params are not valid JSON: '#{rollout_params_string}'"
end

private

def rollout_params_string
@definition.dig("metadata", "annotations", ROLLOUT_PARAMS_ANNOTATION)
end

def names_accepted_condition
conditions = @instance_data.dig("status", "conditions") || []
conditions.detect { |c| c["type"] == "NamesAccepted" } || {}
Expand All @@ -61,5 +73,17 @@ def names_accepted_condition
def names_accepted_status
names_accepted_condition["status"]
end

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

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

0 comments on commit ad1d6ec

Please sign in to comment.