Skip to content

Commit

Permalink
fix(manifests): split YAML using reader
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Sep 30, 2024
1 parent c6d32f4 commit 3f57bab
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
21 changes: 16 additions & 5 deletions internal/manifests/manifests.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package manifests

import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strings"

"sigs.k8s.io/yaml"
"k8s.io/apimachinery/pkg/util/yaml"
libyaml "sigs.k8s.io/yaml"
)

func JSONStringsToYAMLBytes(jsonManifests []string) ([][]byte, error) {
Expand All @@ -15,7 +18,7 @@ func JSONStringsToYAMLBytes(jsonManifests []string) ([][]byte, error) {
for i, jsonManifest := range jsonManifests {
var err error
if yamlManifests[i], err =
yaml.JSONToYAML([]byte(jsonManifest)); err != nil {
libyaml.JSONToYAML([]byte(jsonManifest)); err != nil {
return nil,
fmt.Errorf("error converting JSON manifest to YAML: %w", err)
}
Expand All @@ -28,16 +31,24 @@ func CombineYAML(manifests [][]byte) []byte {
}

func SplitYAML(manifest []byte) (map[string][]byte, error) {
manifests := bytes.Split(manifest, []byte("---\n"))
dec := yaml.NewYAMLReader(bufio.NewReader(bytes.NewReader(manifest)))
manifestsByResourceTypeAndName := map[string][]byte{}
for _, manifest = range manifests {
for {
manifest, err := dec.Read()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, fmt.Errorf("error reading YAML document: %w", err)

Check warning on line 42 in internal/manifests/manifests.go

View check run for this annotation

Codecov / codecov/patch

internal/manifests/manifests.go#L42

Added line #L42 was not covered by tests
}

resource := struct {
Kind string `json:"kind"`
Metadata struct {
Name string `json:"name"`
} `json:"metadata"`
}{}
if err := yaml.Unmarshal(manifest, &resource); err != nil {
if err := libyaml.Unmarshal(manifest, &resource); err != nil {
return nil, fmt.Errorf("error unmarshaling resource: %w", err)
}
if resource.Kind == "" {
Expand Down
38 changes: 38 additions & 0 deletions internal/manifests/manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,44 @@ func TestSplitYAML(t *testing.T) {
require.Equal(t, "resource is missing metadata.name field", err.Error())
},
},
{
name: "YAML containing separators within the spec",
manifests: []byte(`apiVersion: v1
data:
mappings.yml: |-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
---
mappings:
some: mappings
kind: ConfigMap
metadata:
labels:
chart: airflow-1.11.0
component: config
heritage: Helm
release: airflow
tier: airflow
name: airflow-statsd`),
assertions: func(t *testing.T, manifests map[string][]byte, err error) {
require.NoError(t, err)
require.Len(t, manifests, 1)
},
},
{
name: "success",
manifests: []byte(`kind: foo
Expand Down

0 comments on commit 3f57bab

Please sign in to comment.