Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(manifests): split YAML using reader #315

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"strings"

"github.com/ghodss/yaml"
"sigs.k8s.io/yaml"

libExec "github.com/akuity/kargo-render/internal/exec"
"github.com/akuity/kargo-render/internal/file"
Expand Down
2 changes: 1 addition & 1 deletion cmd/kargo-render/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"strings"

"github.com/ghodss/yaml"
"sigs.k8s.io/yaml"
)

func output(obj any, out io.Writer, format string) error {
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"regexp"
"strings"

"github.com/ghodss/yaml"
"github.com/xeipuuv/gojsonschema"
"sigs.k8s.io/yaml"

"github.com/akuity/kargo-render/internal/argocd"
"github.com/akuity/kargo-render/internal/file"
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ require (
github.com/fatih/camelcase v1.0.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fvbommel/sortorder v1.0.1 // indirect
github.com/ghodss/yaml v1.0.0
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
Expand Down Expand Up @@ -158,7 +157,7 @@ require (
sigs.k8s.io/kustomize/api v0.12.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
sigs.k8s.io/yaml v1.4.0
)

replace (
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/fvbommel/sortorder v1.0.1 h1:dSnXLt4mJYH25uDDGa3biZNQsozaUWDSWeKJ0qqFfzE=
github.com/fvbommel/sortorder v1.0.1/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY=
github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4=
Expand Down
22 changes: 17 additions & 5 deletions internal/manifests/manifests.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package manifests

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

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

func JSONStringsToYAMLBytes(jsonManifests []string) ([][]byte, error) {
yamlManifests := make([][]byte, len(jsonManifests))

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 @@ -27,16 +31,24 @@
}

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
Loading