{%- macro get_required(props_dict) %}
{%- set reqs = [] %}
{%- for prop_name in props_dict %}
{%- if props_dict[prop_name].required %}
{%- set _ = reqs.append(prop_name) %}
{%- endif %}
{%- endfor %}
{%- if reqs | length > 0 %}
required:
{%- for req in reqs %}
- {{ req }}
{%- endfor %}
{%- else %}
required: []
{%- endif %}
{%- endmacro %}


{%- macro render_prop(name, prop) %}
{{ name }}:
  type: {{ prop.typ }}
  {%- if prop.typ == "array" %}
  items:
    type: {{ prop.subtype }}
    {%- if prop.subtype == "object" %}
    {{- get_required(prop.subtype_props) | indent(4) }}
    {%- if prop.subtype_desc is string and '\n' in prop.subtype_desc %}
    description: |-
      {{ prop.subtype_desc | indent(6) }}
    {%- else %}
    description: {{ prop.subtype_desc | yaml }}
    {%- endif %}
    properties:
    {%- for subprop_name in prop.subtype_props %}
      {{- render_prop(subprop_name, prop.subtype_props[subprop_name]) | indent(6) }}
    {%- endfor %}
    {%- endif %}
  {%- endif %}
  {%- for key in prop.args %}
  {%- if prop.args[key] is string and '\n' in prop.args[key] %}
  {{ key | maybekebab }}: |-
    {{ prop.args[key] | indent(4) }}
  {%- else %}
  {{ key | maybekebab }}: {{ prop.args[key] | yaml }}
  {%- endif %}
  {%- endfor %}
  {%- if prop.subprops is not none and prop.subprops|length > 0 %}
  {{- get_required(prop.subprops) | indent(2) }}
  properties:
    {%- for subprop_name in prop.subprops %}
  {{- render_prop(subprop_name, prop.subprops[subprop_name]) | indent(4) }}
    {%- endfor %}
  {%- endif %}
{%- endmacro %}

kind: CustomResourceDefinition
apiVersion: apiextensions.k8s.io/v1
metadata:
  name: {{ gvi.crd_name }}
spec:
  group: {{ gvi.group }}
  names:
    kind: {{ gvi.kind }}
    plural: {{ gvi.plural }}
    {%- if gvi.short_names is none or gvi.short_names|length == 0 %}
    shortNames: []
    {%- else %}
    shortNames:
    {%- for short_name in gvi.short_names %}
    - {{ short_name }}
    {% endfor %}
    {% endif %}
    singular: {{ gvi.singular }}
  scope: {{ gvi.scope }}
  versions:
  {%- for version in version_props %}
  {%- set props = version_props[version].props %}
  {%- set status = version_props[version].status %}
  - name: {{ version }}
    {%- if version in version_print_columns %}
    additionalPrinterColumns:
      {%- for col in version_print_columns[version] %}
      - jsonPath: {{ col.json_path }}
        name: {{ col.name }}
        type: {{ col.type }}
      {%- endfor %}
    {%- endif %}
    served: true
    {%- if version == gvi.api_version %}
    storage: true
    {%- else %}
    storage: false
    {%- endif %}
    subresources:
      status: {}
    schema:
      openAPIV3Schema:
        {%- if version in version_desc %}
          {%- if version_desc[version] is string and '\n' in version_desc[version] %}
        description: |-
          {{ version_desc[version] | indent(10) }}
          {%- else %}
        description: {{ version_desc[version] | yaml }}
          {%- endif %}
        {%- endif %}
        type: object
        {%- if props|length > 0 %}
        properties:
          {%- for prop_name in props %}
          {{- render_prop(prop_name, props[prop_name]) | indent(10) }}
          {%- endfor %}
        {%- endif %}
  {%- endfor %}
