module EE
  module KubernetesService
    def rollout_status(environment)
      result = with_reactive_cache do |data|
        specs = filter_by_label(data[:deployments], app: environment.slug)

        ::Gitlab::Kubernetes::RolloutStatus.from_specs(*specs)
      end
      result || ::Gitlab::Kubernetes::RolloutStatus.loading
    end

    def calculate_reactive_cache
      result = super
      result[:deployments] = read_deployments if result

      result
    end

    def reactive_cache_updated
      super

      ::Gitlab::EtagCaching::Store.new.tap do |store|
        store.touch(
          ::Gitlab::Routing.url_helpers.project_environments_path(project, format: :json))
      end
    end

    def read_deployments
      kubeclient = build_kubeclient!(api_path: 'apis/extensions', api_version: 'v1beta1')

      kubeclient.get_deployments(namespace: actual_namespace).as_json
    rescue KubeException => err
      raise err unless err.error_code == 404

      []
    end
  end
end
