diff --git a/pkg/kube_resource/generator/generator.go b/pkg/kube_resource/generator/generator.go index 0c7e192..9a46f4e 100644 --- a/pkg/kube_resource/generator/generator.go +++ b/pkg/kube_resource/generator/generator.go @@ -161,13 +161,8 @@ func splitDocuments(s string) ([]string, error) { return nil, fmt.Errorf("invalid document separator: %s", strings.TrimSpace(separator)) } // Remove all whitespace - result := strings.Map(func(r rune) rune { - if unicode.IsSpace(r) { - return -1 - } - return r - }, s[prev:loc[0]]) - if len(result) > 0 { + result := s[prev:loc[0]] + if len(result) > 0 && !isAllWhitespace(result) { docs = append(docs, result) } prev = loc[1] @@ -177,6 +172,15 @@ func splitDocuments(s string) ([]string, error) { return docs, nil } +func isAllWhitespace(str string) bool { + for _, r := range str { + if !unicode.IsSpace(r) { + return false + } + } + return true +} + // generate swagger model based on crd func generate(crdYaml string) (*spec.Swagger, error) { crdObj, _, err := scheme.Codecs.UniversalDeserializer(). diff --git a/pkg/kube_resource/generator/generator_test.go b/pkg/kube_resource/generator/generator_test.go index 4864a2c..4783a27 100644 --- a/pkg/kube_resource/generator/generator_test.go +++ b/pkg/kube_resource/generator/generator_test.go @@ -631,3 +631,115 @@ func TestSplitDocuments(t *testing.T) { t.Errorf("splitDocuments failed. expected 2, got %d", len(files)) } } + +func TestSplitYamlStreamDocuments(t *testing.T) { + crds := ` +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 +` + files, _ := splitDocuments(crds) + if len(files) != 2 { + t.Errorf("splitDocuments failed. expected 2, got %d", len(files)) + } + if files[0] != ` +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` { + panic(files[0]) + } + if files[1] != `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 +` { + panic(files[1]) + } +}