def dump()

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


def dump(outfile, resources, included_kinds, namespace, namespace_uid, app_name,
         app_uid, app_api_version, deployer_name, deployer_uid):

  def maybe_assign_ownership(resource):
    if resource["kind"] in _CLUSTER_SCOPED_KINDS:
      # Cluster-scoped resources cannot be owned by a namespaced resource:
      # https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#owners-and-dependents
      # Set the namespace as owner if provided, otherwise leave unowned.
      if namespace and namespace_uid:
        log.info("Namespace '{:s}' owns '{:s}/{:s}'", namespace,
                 resource["kind"], resource["metadata"]["name"])
        set_namespace_resource_ownership(
            namespace_uid=namespace_uid,
            namespace_name=namespace,
            resource=resource)
      else:
        log.info("Application '{:s}' does not own cluster-scoped '{:s}/{:s}'",
                 app_name, resource["kind"], resource["metadata"]["name"])

    # Deployer-owned resources should not be owned by the Application, as
    # they should be deleted with the deployer service account (not the app).
    elif deployer_name and deployer_uid and should_be_deployer_owned(resource):
      log.info("ServiceAccount '{:s}' owns '{:s}/{:s}'", deployer_name,
               resource["kind"], resource["metadata"]["name"])
      resource = copy.deepcopy(resource)
      set_service_account_resource_ownership(
          account_uid=deployer_uid,
          account_name=deployer_name,
          resource=resource)
    elif included_kinds is None or resource["kind"] in included_kinds:
      log.info("Application '{:s}' owns '{:s}/{:s}'", app_name,
               resource["kind"], resource["metadata"]["name"])
      resource = copy.deepcopy(resource)
      set_app_resource_ownership(
          app_uid=app_uid,
          app_name=app_name,
          app_api_version=app_api_version,
          resource=resource)

    return resource

  to_be_dumped = [maybe_assign_ownership(resource) for resource in resources]
  yaml.safe_dump_all(to_be_dumped, outfile, default_flow_style=False, indent=2)