Skip to content

kcl import fails to process multiple CRDs separated by "---" in a single YAML file #1598

@suin

Description

@suin

Bug Report

1. Minimal reproduce step (Required)

  1. Create a file named crds.yaml with the following content:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: foo.example.com
spec:
  group: example.com
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                field1:
                  type: string
  scope: Namespaced
  names:
    plural: foo
    singular: foo
    kind: Foo
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: bar.example.com
spec:
  group: example.com
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                field2:
                  type: string
  scope: Namespaced
  names:
    plural: bar
    singular: bar
    kind: Bar
  1. Run the following command:
kcl import -m crd -s crds.yaml

2. What did you expect to see? (Required)

The models directory should be generated with the following structure:

models
├── k8s
│  └── apimachinery
│     └── pkg
│        └── apis
│           └── meta
│              └── v1
│                 ├── managed_fields_entry.k
│                 ├── object_meta.k
│                 └── owner_reference.k
├── kcl.mod
├── kcl.mod.lock
└── v1
   ├── example_com_v1_bar.k
   └── example_com_v1_foo.k

3. What did you see instead (Required)

The command produces the following error:

2024/08/23 10:44:49 import.go:82: [ERROR] could not generate swagger spec: crds.yaml, err: couldn't get version/kind; json parse error: json: cannot unmarshal string into Go value of type struct { APIVersion string "json:\"apiVersion,omitempty\""; Kind string "json:\"kind,omitempty\"" }

The models directory is generated, but it's incomplete.

4. What is your KCL components version? (Required)

0.10.0-beta.2

Additional Information:

  1. If the crds.yaml file contains only one CRD without the --- separator, the import works correctly.
  2. Even with a single CRD, if the --- separator is present, the same error occurs.
  3. This issue prevents users from easily importing multiple CRDs from a single file, which is a common format provided by CRD repositories and APIs. For example, it hinders the ability to directly import CRDs from sources like https://doc.crds.dev/raw/github.com/crossplane/crossplane, which returns multiple CRDs separated by ---.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions