Sha256: c86b129dd9c450c9383a8e2abe77fbd493fa94c94042e17529360e7be3aa0a8f

Contents?: true

Size: 1.78 KB

Versions: 10

Compression:

Stored size: 1.78 KB

Contents

module Vidar
  module K8s
    class PodSet
      def initialize(namespace:, filter: nil)
        @namespace = namespace
        @filter = filter
      end

      def deployed?
        if items.empty?
          Log.error "Could not fetch pod list"
          return false
        end

        Log.line

        containers.each(&:print)

        Log.line

        containers.all?(&:deployed?)
      end

      def success?
        return false if containers.empty?

        containers.all?(&:success?)
      end

      def containers
        return all_containers unless filter

        all_containers.select { |cs| cs.name.to_s.include?(filter) }
      end

      private

      attr_reader :namespace, :filter

      def items
        @items ||= begin
          json = JSON.parse(kubectl_get.strip)
          json["items"] || []
        end
      end

      def kubectl_get
        if namespace == "all"
          `kubectl get pods --all-namespaces -o json`
        else
          `kubectl get pods -n #{namespace} -o json`
        end
      end

      def ready_and_running_containers
        containers.select(&:ready_and_running?)
      end

      def all_containers
        @all_containers ||= containers_data.map { |status| Container.new(status) }
      end

      def containers_data
        items.map do |i|
          owner_references = i.dig("metadata", "ownerReferences") || []
          kind = (owner_references[0] || {})["kind"]
          namespace = i.dig("metadata", "namespace")
          statuses = i.dig("status", "containerStatuses") || i.dig("status", "conditions") || []
          statuses.each do |s|
            s["namespace"] = namespace
            s["kind"] = kind
            s["pod_name"] = i.dig("metadata", "name")
          end
          statuses
        end.flatten
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
vidar-1.4.2 lib/vidar/k8s/pod_set.rb
vidar-1.4.1 lib/vidar/k8s/pod_set.rb
vidar-1.4.0 lib/vidar/k8s/pod_set.rb
vidar-1.3.2 lib/vidar/k8s/pod_set.rb
vidar-1.3.1 lib/vidar/k8s/pod_set.rb
vidar-1.3.0 lib/vidar/k8s/pod_set.rb
vidar-1.2.0 lib/vidar/k8s/pod_set.rb
vidar-1.1.5 lib/vidar/k8s/pod_set.rb
vidar-1.1.4 lib/vidar/k8s/pod_set.rb
vidar-1.1.3 lib/vidar/k8s/pod_set.rb