def main()

in marketplace/deployer_util/wait_for_ready.py [0:0]


def main():
  parser = ArgumentParser(description=_PROG_HELP)
  parser.add_argument('--name')
  parser.add_argument('--namespace')
  parser.add_argument('--timeout', type=float)
  args = parser.parse_args()

  log.info("Wait {} seconds for the application '{}' to get into ready state",
           args.timeout, args.name)
  previous_healthy = False

  min_time_before_healthy = 30
  poll_interval = 4

  application = Command(
      '''
    kubectl get "applications.app.k8s.io/{}"
      --namespace="{}"
      --output=json
    '''.format(args.name, args.namespace),
      print_call=True).json()

  top_level_kinds = [
      kind['kind'] for kind in application['spec']['componentKinds']
  ]

  poll_start_time = time.time()

  while True:
    top_level_resources = []
    for kind in top_level_kinds:
      resources = Command('''
        kubectl get "{}"
        --namespace="{}"
        --selector app.kubernetes.io/name="{}"
        --output=json
        '''.format(kind, args.namespace, args.name)).json()
      top_level_resources.extend(resources['items'])

    if len(top_level_resources) == 0:
      raise Exception("ERROR no top level resources found")

    log.info("Top level resources: {}", len(top_level_resources))
    healthy = True
    for resource in top_level_resources:
      healthy = is_healthy(resource)
      if not healthy:
        break

    if previous_healthy != healthy:
      log.info(
          "Initialization: Found applications.app.k8s.io/{} ready status to be {}.",
          args.name, healthy)
      previous_healthy = healthy
      if healthy:
        log.info("Wait {} seconds to make sure app stays in healthy state.",
                 min_time_before_healthy)
        healthy_start_time = time.time()

    if healthy:
      elapsed_healthy_time = time.time() - healthy_start_time
      if elapsed_healthy_time > min_time_before_healthy:
        break

    if time.time() - poll_start_time > args.timeout:
      raise Exception(
          "ERROR Application did not get ready before timeout of {} seconds"
          .format(args.timeout))

    time.sleep(poll_interval)