Example #1
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))
 def test_find_application_resource_multiple_ones_exist(self):
     resources = [
         {
             'apiVersion': 'app.k8s.io/v1alpha1',
             'kind': 'Application',
         },
         {
             'apiVersion': 'app.k8s.io/v1beta1',
             'kind': 'Application',
         },
     ]
     self.assertRaisesRegexp(Exception, r'.*multiple Applications.*',
                             lambda: find_application_resource(resources))
 def test_find_application_resource_none_exists(self):
     resources = [
         {
             'apiVersion': 'v1',
             'kind': 'ServiceAccount',
         },
         {
             'apiVersion': 'application.mycompany.com/v1beta1',
             'kind': 'Application',
         },
         {
             'apiVersion': 'batch/v1',
             'kind': 'Job',
         },
     ]
     self.assertRaisesRegexp(Exception,
                             r'.*does not include an Application.*',
                             lambda: find_application_resource(resources))
 def test_find_application_resource(self):
     expected_app_resource = {
         # Intentionally some version we don't support,
         # but the api group should suffice.
         'apiVersion': 'app.k8s.io/v9999',
         'kind': 'Application',
     }
     resources = [
         {
             'apiVersion': 'v1',
             'kind': 'ServiceAccount',
         },
         {
             'apiVersion': 'application.mycompany.com/v1beta1',
             'kind': 'Application',
         },
         expected_app_resource,
         {
             'apiVersion': 'batch/v1',
             'kind': 'Job',
         },
     ]
     self.assertEqual(expected_app_resource,
                      find_application_resource(resources))
Example #5
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)