def test_multiple_entries_in_content(self):
        content = """
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: $APP_INSTANCE_NAME-mysql-pvc
  labels:
    app.kubernetes.io/name: "$APP_INSTANCE_NAME"
    app.kubernetes.io/component: wordpress-mysql
spec:
  accessModes: [ReadWriteOnce]
  storageClassName: standard
  resources:
    requests:
      storage: 5Gi
---
# this entry will be ignored. And so will the one below with no comments
---
---
apiVersion: v1
kind: Service
metadata:
  name: $APP_INSTANCE_NAME-mysql-svc
  labels:
    app.kubernetes.io/name: "$APP_INSTANCE_NAME"
    app.kubernetes.io/component: wordpress-mysql
spec:
  ports:
  - port: 3306
  selector:
    app.kubernetes.io/name: $APP_INSTANCE_NAME
    app.kubernetes.io/component: wordpress-mysql
  clusterIP: None
---
"""
        docs = parse_resources_yaml(content)
        self.assertEqual(len(docs), 2)

        self.assertEqual(docs[0]['apiVersion'], "v1")
        self.assertEqual(docs[0]['kind'], "PersistentVolumeClaim")
        self.assertEqual(docs[0]['spec']['resources']['requests']['storage'],
                         "5Gi")

        self.assertEqual(docs[1]['apiVersion'], "v1")
        self.assertEqual(docs[1]['kind'], "Service")
        self.assertEqual(docs[1]['spec']['ports'][0]['port'], 3306)
Esempio n. 2
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)
    def test_single_entry(self):
        content = """---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: $APP_INSTANCE_NAME-mysql
  labels: &MysqlDeploymentLabels
    app.kubernetes.io/name: "$APP_INSTANCE_NAME"
    app.kubernetes.io/component: wordpress-mysql
spec:
  replicas: 1
  selector:
    matchLabels: *MysqlDeploymentLabels
  template:
    metadata:
      labels: *MysqlDeploymentLabels
    spec:
      containers:
      - image: $IMAGE_MYSQL
        name: mysql
        env:
        - name: "MYSQL_ROOT_PASSWORD"
          value: "example-password"
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: data
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: $APP_INSTANCE_NAME-mysql-pvc"""

        docs = parse_resources_yaml(content)
        self.assertEqual(len(docs), 1)

        doc = docs[0]
        self.assertEqual(doc['apiVersion'], "apps/v1beta2")
        self.assertEqual(doc['kind'], "Deployment")
        self.assertEqual(
            doc['spec']['template']['spec']['containers'][0]['name'], "mysql")
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)
Esempio n. 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)