Sha256: 46351fa961bd62220e70d38fc475b2527e48e7ab84741b62093e99cdf3a53a02

Contents?: true

Size: 1.99 KB

Versions: 50

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true
require 'krane/kubernetes_resource/pod_set_base'

module Krane
  class ReplicaSet < PodSetBase
    TIMEOUT = 5.minutes
    SYNC_DEPENDENCIES = %w(Pod)
    attr_reader :pods

    def initialize(namespace:, context:, definition:, logger:, statsd_tags: nil,
      parent: nil, deploy_started_at: nil)
      @parent = parent
      @deploy_started_at = deploy_started_at
      @pods = []
      super(namespace: namespace, context: context, definition: definition,
            logger: logger, statsd_tags: statsd_tags)
    end

    def sync(cache)
      super
      @pods = exists? ? find_pods(cache) : []
    end

    def status
      return super unless rollout_data.present?
      rollout_data.map { |state_replicas, num| "#{num} #{state_replicas.chop.pluralize(num)}" }.join(", ")
    end

    def deploy_succeeded?
      return false if stale_status?
      desired_replicas == rollout_data["availableReplicas"].to_i &&
      desired_replicas == rollout_data["readyReplicas"].to_i
    end

    def deploy_failed?
      pods.present? &&
      pods.all?(&:deploy_failed?) &&
      !stale_status?
    end

    def desired_replicas
      return -1 unless exists?
      @instance_data["spec"]["replicas"].to_i
    end

    def ready_replicas
      return -1 unless exists?
      rollout_data['readyReplicas'].to_i
    end

    def available_replicas
      return -1 unless exists?
      rollout_data["availableReplicas"].to_i
    end

    private

    def stale_status?
      observed_generation != current_generation
    end

    def rollout_data
      return { "replicas" => 0 } unless exists?
      { "replicas" => 0 }.merge(
        @instance_data["status"].slice("replicas", "availableReplicas", "readyReplicas")
      )
    end

    def parent_of_pod?(pod_data)
      return false unless pod_data.dig("metadata", "ownerReferences")
      pod_data["metadata"]["ownerReferences"].any? { |ref| ref["uid"] == @instance_data["metadata"]["uid"] }
    end

    def unmanaged?
      @parent.blank?
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
krane-3.7.0 lib/krane/kubernetes_resource/replica_set.rb
krane-3.6.2 lib/krane/kubernetes_resource/replica_set.rb
krane-3.6.1 lib/krane/kubernetes_resource/replica_set.rb
krane-3.5.2 lib/krane/kubernetes_resource/replica_set.rb
krane-3.5.1 lib/krane/kubernetes_resource/replica_set.rb
krane-3.5.0 lib/krane/kubernetes_resource/replica_set.rb
krane-3.4.2 lib/krane/kubernetes_resource/replica_set.rb
krane-3.4.1 lib/krane/kubernetes_resource/replica_set.rb
krane-3.4.0 lib/krane/kubernetes_resource/replica_set.rb
krane-3.3.0 lib/krane/kubernetes_resource/replica_set.rb
krane-3.2.0 lib/krane/kubernetes_resource/replica_set.rb
krane-3.1.0 lib/krane/kubernetes_resource/replica_set.rb
krane-3.0.1 lib/krane/kubernetes_resource/replica_set.rb
krane-3.0.0 lib/krane/kubernetes_resource/replica_set.rb
krane-2.4.9 lib/krane/kubernetes_resource/replica_set.rb
krane-2.4.8 lib/krane/kubernetes_resource/replica_set.rb
krane-2.4.7 lib/krane/kubernetes_resource/replica_set.rb
krane-2.4.6 lib/krane/kubernetes_resource/replica_set.rb
krane-2.4.5 lib/krane/kubernetes_resource/replica_set.rb
krane-2.4.4 lib/krane/kubernetes_resource/replica_set.rb