def main():

    parser = ArgumentParser(description=_PROG_HELP)
    parser.add_argument("--app_name",
                        required=True,
                        help="the name of the application instance")
    parser.add_argument("--app_uid",
                        required=True,
                        help="the uid of the application instance")
    parser.add_argument("--app_api_version",
                        required=True,
                        help="apiVersion of the Application CRD")
    parser.add_argument("--manifests",
                        required=True,
                        help="the configuration for tests")
    parser.add_argument("--out_manifests",
                        required=True,
                        help="the file to write non-test resources to")
    parser.add_argument("--out_test_manifests",
                        required=True,
                        help="the file to write test resources to")
    args = parser.parse_args()

    if os.path.isfile(args.manifests):
        resources = load_resources_yaml(args.manifests)
    else:
        resources = []
        for filename in os.listdir(args.manifests):
            resources += load_resources_yaml(
                os.path.join(args.manifests, filename))

    test_resources = []
    nontest_resources = []
    for resource in resources:
        full_name = "{}/{}".format(resource['kind'],
                                   deep_get(resource, 'metadata', 'name'))
        if deep_get(resource, 'metadata', 'annotations',
                    GOOGLE_CLOUD_TEST) == 'test':
            print("INFO Tester resource: {}".format(full_name))
            set_app_resource_ownership(app_uid=args.app_uid,
                                       app_name=args.app_name,
                                       app_api_version=args.app_api_version,
                                       resource=resource)
            test_resources.append(resource)
        else:
            print("INFO Prod resource: {}".format(full_name))
            nontest_resources.append(resource)

    if nontest_resources:
        with open(args.out_manifests, "w", encoding='utf-8') as outfile:
            yaml.safe_dump_all(nontest_resources,
                               outfile,
                               default_flow_style=False)

    if test_resources:
        with open(args.out_test_manifests, "a",
                  encoding='utf-8') as test_outfile:
            yaml.safe_dump_all(test_resources,
                               test_outfile,
                               default_flow_style=False)
Пример #2
0
def main():
    parser = ArgumentParser(description=_PROG_HELP)
    schema_values_common.add_to_argument_parser(parser)
    parser.add_argument('--manifests',
                        required=True,
                        help='The yaml file containing all resources')
    args = parser.parse_args()

    schema = schema_values_common.load_schema(args)
    resources = load_resources_yaml(args.manifests)

    app = find_application_resource(resources)
    mp_deploy_info = app.get('metadata', {}).get(
        'annotations', {}).get('marketplace.cloud.google.com/deploy-info')
    if not mp_deploy_info:
        raise Exception(
            'Application resource is missing '
            '"marketplace.cloud.google.com/deploy-info" annotation')
    validate_deploy_info_annotation(mp_deploy_info, schema)

    version = app.get('spec', {}).get('descriptor', {}).get('version')
    if not version or not isinstance(version, str):
        raise Exception(
            'Application resource must have a valid spec.descriptor.version value'
        )
    if schema.is_v2():
        published_version = schema.x_google_marketplace.published_version
        if version != published_version:
            raise Exception(
                'Application resource\'s spec.descriptor.version "{}" does not match '
                'schema.yaml\'s publishedVersion "{}"'.format(
                    version, published_version))
Пример #3
0
def main():
  parser = ArgumentParser()
  parser.add_argument("--manifest", help="the manifest file location to be cleared of tests")
  parser.add_argument("--deploy_tests", action="store_true", help="indicates whether tests should be deployed")
  args = parser.parse_args()

  manifest = args.manifest
  resources = load_resources_yaml(manifest)
  filtered_resources = []
  for resource in resources:
    helm_hook = deep_get(resource, "metadata", "annotations", _HELM_HOOK_KEY)
    if helm_hook is None:
      filtered_resources.append(resource)
    elif helm_hook == _HOOK_SUCCESS:
      if args.deploy_tests:
        annotations = deep_get(resource, "metadata", "annotations")
        del annotations[_HELM_HOOK_KEY]
        annotations[GOOGLE_CLOUD_TEST] = "test"
        filtered_resources.append(resource)
    elif helm_hook == _HOOK_FAILURE:
      if args.deploy_tests:
        raise Exception("Helm hook {} is not supported".format(helm_hook))
    else:
      raise Exception("Helm hook {} is not supported".format(helm_hook))

  with open(manifest, "w") as out:
    yaml.dump_all(filtered_resources, out,
                  default_flow_style=False, explicit_start=True)
Пример #4
0
def main():
    parser = ArgumentParser(description=_PROG_HELP)
    parser.add_argument("--manifests",
                        help="The folder containing the manifest templates, "
                        "or - to read from stdin",
                        required=True)
    parser.add_argument("--dest",
                        help="The output file for the resulting manifest, "
                        "or - to write to stdout",
                        required=True)
    parser.add_argument("--name",
                        help="The name of the application instance",
                        required=True)
    parser.add_argument(
        "--namespace",
        help="The namespace where the application is installed",
        required=True)
    args = parser.parse_args()

    resources = []
    if args.manifests == "-":
        resources = parse_resources_yaml(sys.stdin.read())
    elif os.path.isfile(args.manifests):
        resources = load_resources_yaml(args.manifests)
    else:
        resources = []
        for filename in os.listdir(args.manifests):
            resources += load_resources_yaml(
                os.path.join(args.manifests, filename))

    # Modify resources inlined.
    for resource in resources:
        labels = resource['metadata'].get('labels', {})
        resource['metadata']['labels'] = labels
        labels['app.kubernetes.io/name'] = args.name
        # For a resource that doesn't have a namespace (i.e. cluster resource),
        # also all label it with the namespace of the application.
        if 'namespace' not in resource['metadata']:
            labels['app.kubernetes.io/namespace'] = args.namespace

    if args.dest == "-":
        write_resources(resources, sys.stdout)
        sys.stdout.flush()
    else:
        with open(args.dest, "w", encoding='utf-8') as outfile:
            write_resources(resources, outfile)
Пример #5
0
def main():
    parser = ArgumentParser(description=_PROG_HELP)
    parser.add_argument('--namespace')
    parser.add_argument('--manifest')
    parser.add_argument('--timeout', type=int, default=300)
    args = parser.parse_args()

    Command('''
      kubectl apply
      --namespace="{}"
      --filename="{}"
      '''.format(args.namespace, args.manifest),
            print_call=True)

    resources = load_resources_yaml(args.manifest)

    for resource_def in resources:
        full_name = "{}/{}".format(resource_def['kind'],
                                   deep_get(resource_def, 'metadata', 'name'))

        if resource_def['kind'] != 'Pod':
            log("INFO Skip '{}'".format(full_name))
            continue

        start_time = time.time()
        poll_interval = 4
        tester_timeout = args.timeout

        while True:
            try:
                resource = Command('''
          kubectl get "{}"
          --namespace="{}"
          -o=json
          '''.format(full_name, args.namespace),
                                   print_call=True).json()
            except CommandException as ex:
                log(str(ex))
                log("INFO retrying")
                time.sleep(poll_interval)
                continue

            result = deep_get(resource, 'status', 'phase')

            if result == "Failed":
                print_logs(full_name, args.namespace)
                raise Exception("ERROR Tester '{}' failed".format(full_name))

            if result == "Succeeded":
                print_logs(full_name, args.namespace)
                log("INFO Tester '{}' succeeded".format(full_name))
                break

            if time.time() - start_time > tester_timeout:
                print_logs(full_name, args.namespace)
                raise Exception("ERROR Tester '{}' timeout".format(full_name))

            time.sleep(poll_interval)
def main():
    parser = ArgumentParser()
    parser.add_argument("-m",
                        "--manifest",
                        dest="manifest",
                        help="the manifest file to be parsed and updated")
    parser.add_argument("-a",
                        "--appname",
                        dest="application_name",
                        help="the application instance name")
    args = parser.parse_args()
    manifest = args.manifest
    app_name = args.application_name
    resources = load_resources_yaml(manifest)
    resources = map(lambda r: ensure_resource_has_app_label(r, app_name),
                    resources)
    with open(manifest, "w") as out:
        yaml.dump_all(resources,
                      out,
                      default_flow_style=False,
                      explicit_start=True)
def main():
    parser = ArgumentParser(description=_PROG_HELP)
    parser.add_argument("--app_name",
                        help="The name of the applictation instance",
                        required=True)
    parser.add_argument("--app_uid",
                        help="The uid of the applictation instance",
                        required=True)
    parser.add_argument("--app_api_version",
                        help="The apiVersion of the Application CRD",
                        required=True)
    parser.add_argument("--manifests",
                        help="The folder containing the manifest templates, "
                        "or - to read from stdin",
                        required=True)
    parser.add_argument("--dest",
                        help="The output file for the resulting manifest, "
                        "or - to write to stdout",
                        required=True)
    parser.add_argument(
        "--noapp",
        action="store_true",
        help="Do not look for Application resource to determine "
        "what kinds to include. I.e. set owner references for "
        "all of the resources in the manifests")
    args = parser.parse_args()

    resources = []
    if args.manifests == "-":
        resources = parse_resources_yaml(sys.stdin.read())
    elif os.path.isfile(args.manifests):
        resources = load_resources_yaml(args.manifests)
    else:
        resources = []
        for filename in os.listdir(args.manifests):
            resources += load_resources_yaml(
                os.path.join(args.manifests, filename))

    if not args.noapp:
        apps = [r for r in resources if r["kind"] == "Application"]

        if len(apps) == 0:
            raise Exception("Set of resources in {:s} does not include one of "
                            "Application kind".format(args.manifests))
        if len(apps) > 1:
            raise Exception(
                "Set of resources in {:s} includes more than one of "
                "Application kind".format(args.manifests))

        kinds = map(lambda x: x["kind"],
                    apps[0]["spec"].get("componentKinds", []))

        excluded_kinds = ["PersistentVolumeClaim", "Application"]
        included_kinds = [kind for kind in kinds if kind not in excluded_kinds]
    else:
        included_kinds = None

    if args.dest == "-":
        dump(sys.stdout,
             resources,
             included_kinds,
             app_name=args.app_name,
             app_uid=args.app_uid,
             app_api_version=args.app_api_version)
        sys.stdout.flush()
    else:
        with open(args.dest, "w") as outfile:
            dump(outfile,
                 resources,
                 included_kinds,
                 app_name=args.app_name,
                 app_uid=args.app_uid,
                 app_api_version=args.app_api_version)
                    "--manifest",
                    dest="manifest",
                    help="the manifest file")
parser.add_argument("-s",
                    "--status",
                    dest="status",
                    choices=['Failure', 'Pending', 'Success'],
                    help="the assembly status to set")

args = parser.parse_args()

assert args.manifest
assert os.path.exists(args.manifest)

resources = []
for r in load_resources_yaml(args.manifest):
    resources.append(r)
apps = [r for r in resources if r['kind'] == "Application"]

if len(apps) == 0:
    raise Exception("Set of resources in {:s} does not include one of "
                    "Application kind".format(args.manifest))
if len(apps) > 1:
    raise Exception("Set of resources in {:s} includes more than one of "
                    "Application kind".format(args.manifest))

apps[0]['spec']['assemblyPhase'] = args.status

with open(args.manifest, "w") as outfile:
    yaml.safe_dump_all(resources, outfile, default_flow_style=False, indent=2)
Пример #9
0
def main():
    parser = ArgumentParser(description=_PROG_HELP)
    parser.add_argument("--app_name",
                        help="The name of the application instance",
                        required=True)
    parser.add_argument("--app_uid",
                        help="The uid of the application instance",
                        required=True)
    parser.add_argument("--app_api_version",
                        help="The apiVersion of the Application CRD",
                        required=True)
    parser.add_argument(
        "--deployer_name",
        help="The name of the deployer service account instance. "
        "If deployer_uid is also set, the deployer service account is set "
        "as the owner of namespaced deployer components.")
    parser.add_argument(
        "--deployer_uid",
        help="The uid of the deployer service account instance. "
        "If deployer_name is also set, the deployer service account is set "
        "as the owner of namespaced deployer components.")
    parser.add_argument("--manifests",
                        help="The folder containing the manifest templates, "
                        "or - to read from stdin",
                        required=True)
    parser.add_argument("--dest",
                        help="The output file for the resulting manifest, "
                        "or - to write to stdout",
                        required=True)
    parser.add_argument(
        "--noapp",
        action="store_true",
        help="Do not look for Application resource to determine "
        "what kinds to include. I.e. set owner references for "
        "all of the (namespaced) resources in the manifests")
    args = parser.parse_args()

    resources = []
    if args.manifests == "-":
        resources = parse_resources_yaml(sys.stdin.read())
    elif os.path.isfile(args.manifests):
        resources = load_resources_yaml(args.manifests)
    else:
        resources = []
        for filename in os.listdir(args.manifests):
            resources += load_resources_yaml(
                os.path.join(args.manifests, filename))

    if not args.noapp:
        app = find_application_resource(resources)
        kinds = set([x["kind"] for x in app["spec"].get("componentKinds", [])])

        excluded_kinds = ["PersistentVolumeClaim", "Application"]
        included_kinds = [kind for kind in kinds if kind not in excluded_kinds]
    else:
        included_kinds = None

    if args.dest == "-":
        dump(sys.stdout,
             resources,
             included_kinds,
             app_name=args.app_name,
             app_uid=args.app_uid,
             app_api_version=args.app_api_version,
             deployer_name=args.deployer_name,
             deployer_uid=args.deployer_uid)
        sys.stdout.flush()
    else:
        with open(args.dest, "w", encoding='utf-8') as outfile:
            dump(outfile,
                 resources,
                 included_kinds,
                 app_name=args.app_name,
                 app_uid=args.app_uid,
                 app_api_version=args.app_api_version,
                 deployer_name=args.deployer_name,
                 deployer_uid=args.deployer_uid)
def main():
    parser = ArgumentParser(description=_PROG_HELP)
    parser.add_argument('--namespace')
    parser.add_argument('--manifest')
    parser.add_argument('--timeout', type=int, default=300)
    args = parser.parse_args()

    try:
        Command('''
        kubectl apply
        --namespace="{}"
        --filename="{}"
        '''.format(args.namespace, args.manifest),
                print_call=True)
    except CommandException as ex:
        log.error("{} Failed to apply tester job. Reason: {}", LOG_SMOKE_TEST,
                  ex.message)
        return

    resources = load_resources_yaml(args.manifest)

    for resource_def in resources:
        full_name = "{}/{}".format(resource_def['kind'],
                                   deep_get(resource_def, 'metadata', 'name'))

        if resource_def['kind'] != 'Pod':
            log.info("Skip '{}'", full_name)
            continue

        start_time = time.time()
        poll_interval = 4
        tester_timeout = args.timeout

        while True:
            try:
                resource = Command('''
          kubectl get "{}"
          --namespace="{}"
          -o=json
          '''.format(full_name, args.namespace),
                                   print_call=True).json()
            except CommandException as ex:
                log.info(str(ex))
                log.info("retrying")
                time.sleep(poll_interval)
                continue

            result = deep_get(resource, 'status', 'phase')

            if result == "Failed":
                print_tester_logs(full_name, args.namespace)
                log.error("{} Tester '{}' failed.", LOG_SMOKE_TEST, full_name)
                break

            if result == "Succeeded":
                print_tester_logs(full_name, args.namespace)
                log.info("{} Tester '{}' succeeded.", LOG_SMOKE_TEST,
                         full_name)
                break

            if time.time() - start_time > tester_timeout:
                print_tester_logs(full_name, args.namespace)
                log.error("{} Tester '{}' timeout.", LOG_SMOKE_TEST, full_name)

            time.sleep(poll_interval)