Class: Krane::CustomResource

Inherits:
KubernetesResource show all
Defined in:
lib/krane/kubernetes_resource/custom_resource.rb

Constant Summary collapse

TIMEOUT_MESSAGE_DIFFERENT_GENERATIONS =
<<~MSG
  This resource's status could not be used to determine rollout success because it is not up-to-date
  (.metadata.generation != .status.observedGeneration).
MSG

Constants inherited from KubernetesResource

KubernetesResource::ALLOWED_DEPLOY_METHOD_OVERRIDES, KubernetesResource::DEBUG_RESOURCE_NOT_FOUND_MESSAGE, KubernetesResource::DEPLOY_METHOD_OVERRIDE_ANNOTATION, KubernetesResource::DISABLED_EVENT_INFO_MESSAGE, KubernetesResource::DISABLED_LOG_INFO_MESSAGE, KubernetesResource::DISABLE_FETCHING_EVENT_INFO, KubernetesResource::DISABLE_FETCHING_LOG_INFO, KubernetesResource::GLOBAL, KubernetesResource::LAST_APPLIED_ANNOTATION, KubernetesResource::LOG_LINE_COUNT, KubernetesResource::SENSITIVE_TEMPLATE_CONTENT, KubernetesResource::SERVER_DRY_RUNNABLE, KubernetesResource::SERVER_DRY_RUN_DISABLED_ERROR, KubernetesResource::STANDARD_TIMEOUT_MESSAGE, KubernetesResource::SYNC_DEPENDENCIES, KubernetesResource::TIMEOUT, KubernetesResource::TIMEOUT_OVERRIDE_ANNOTATION, KubernetesResource::UNUSUAL_FAILURE_MESSAGE

Instance Attribute Summary

Attributes inherited from KubernetesResource

#context, #deploy_started_at, #global, #name, #namespace

Instance Method Summary collapse

Methods inherited from KubernetesResource

#<=>, #after_sync, build, class_for_kind, #current_generation, #debug_message, #deploy_method, #deploy_method_override, #deploy_started?, #deploy_timed_out?, #disappeared?, #exists?, #fetch_events, #file_path, #global?, #group, #id, kind, #kubectl_resource_type, #observed_generation, #pretty_status, #pretty_timeout_type, #report_status_to_statsd, #selected?, #sensitive_template_content?, #server_dry_run_validated?, #server_dry_runnable_resource?, #sync, #sync_debug_info, #terminating?, timeout, #timeout_override, #to_kubeclient_resource, #use_generated_name, #uses_generate_name?, #validation_error_msg, #validation_failed?, #version

Constructor Details

#initialize(namespace:, context:, definition:, logger:, statsd_tags: [], crd:) ⇒ CustomResource

Returns a new instance of CustomResource.



11
12
13
14
15
# File 'lib/krane/kubernetes_resource/custom_resource.rb', line 11

def initialize(namespace:, context:, definition:, logger:, statsd_tags: [], crd:)
  super(namespace: namespace, context: context, definition: definition,
        logger: logger, statsd_tags: statsd_tags)
  @crd = crd
end

Instance Method Details

#deploy_failed?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'lib/krane/kubernetes_resource/custom_resource.rb', line 28

def deploy_failed?
  return super unless rollout_conditions
  return false unless observed_generation == current_generation

  rollout_conditions.rollout_failed?(@instance_data)
end

#deploy_succeeded?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/krane/kubernetes_resource/custom_resource.rb', line 21

def deploy_succeeded?
  return super unless rollout_conditions
  return false unless observed_generation == current_generation

  rollout_conditions.rollout_successful?(@instance_data)
end

#failure_messageObject



35
36
37
38
39
# File 'lib/krane/kubernetes_resource/custom_resource.rb', line 35

def failure_message
  return super unless rollout_conditions
  messages = rollout_conditions.failure_messages(@instance_data)
  messages.join("\n") if messages.present?
end

#statusObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/krane/kubernetes_resource/custom_resource.rb', line 49

def status
  if !exists? || rollout_conditions.nil?
    super
  elsif deploy_succeeded?
    "Healthy"
  elsif deploy_failed?
    "Unhealthy"
  else
    "Unknown"
  end
end

#timeoutObject



17
18
19
# File 'lib/krane/kubernetes_resource/custom_resource.rb', line 17

def timeout
  timeout_override || @crd.timeout_for_instance || TIMEOUT
end

#timeout_messageObject



41
42
43
44
45
46
47
# File 'lib/krane/kubernetes_resource/custom_resource.rb', line 41

def timeout_message
  if rollout_conditions && current_generation != observed_generation
    TIMEOUT_MESSAGE_DIFFERENT_GENERATIONS
  else
    super
  end
end

#typeObject



61
62
63
# File 'lib/krane/kubernetes_resource/custom_resource.rb', line 61

def type
  kind
end

#validate_definitionObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/krane/kubernetes_resource/custom_resource.rb', line 65

def validate_definition(*, **)
  super

  @crd.validate_rollout_conditions
rescue RolloutConditionsError => e
  @validation_errors << "The CRD that specifies this resource is using invalid rollout conditions. " \
  "Krane will not be able to continue until those rollout conditions are fixed.\n" \
  "Rollout conditions can be found on the CRD that defines this resource (#{@crd.name}), " \
  "under the annotation #{Annotation.for(CustomResourceDefinition::ROLLOUT_CONDITIONS_ANNOTATION)}.\n" \
  "Validation failed with: #{e}"
end