Skip to content

Commit

Permalink
provisioner: improve empty manifest detection
Browse files Browse the repository at this point in the history
Reuse existing YAML marshaler to detect empty manifests.

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov committed Sep 19, 2023
1 parent 7eaf1c9 commit e88798a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
25 changes: 7 additions & 18 deletions provisioner/clusterpy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/url"
"strings"
"time"
"unicode"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
Expand Down Expand Up @@ -1042,7 +1041,7 @@ func remarshalYAML(contents string) (string, error) {
}
}

return string(result.Bytes()), nil
return result.String(), nil
}

func renderManifests(config channel.Config, cluster *api.Cluster, values map[string]interface{}, adapter *awsAdapter) ([]manifestPackage, error) {
Expand All @@ -1068,19 +1067,18 @@ func renderManifests(config channel.Config, cluster *api.Cluster, values map[str
return nil, fmt.Errorf("error rendering template %s: %v", manifest.Path, err)
}

// If there's no content we skip the file.
if stripWhitespace(rendered) == "" {
log.Debugf("Skipping empty file: %s", manifest.Path)
continue
}

// We remarshal the manifest here to get rid of references
remarshaled, err := remarshalYAML(rendered)
if err != nil {
return nil, fmt.Errorf("error remarshaling manifest %s: %v", manifest.Path, err)
}

renderedManifests = append(renderedManifests, remarshaled)
// If there's no content we skip the file.
if remarshaled == "" {
log.Debugf("Skipping empty file: %s\n%s\n", manifest.Path, rendered)
} else {
renderedManifests = append(renderedManifests, remarshaled)
}
}

if len(renderedManifests) > 0 {
Expand Down Expand Up @@ -1127,15 +1125,6 @@ func (p *clusterpyProvisioner) apply(ctx context.Context, logger *log.Entry, clu
return nil
}

func stripWhitespace(content string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, content)
}

func int32Ptr(i int32) *int32 { return &i }

func int32Value(v *int32) int32 {
Expand Down
66 changes: 66 additions & 0 deletions provisioner/clusterpy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,72 @@ first: 1
second: 2
`,
},
{
name: "empty manifest",
source: "",
expected: "",
},
{
name: "empty manifest with newline",
source: "\n",
expected: "",
},
{
name: "empty manifest with newlines",
source: "\n\n",
expected: "",
},
{
name: "empty manifest with comments",
source: `
# this manifest consists only
# of comments and
# whitespace lines
`,
expected: "",
},
{
name: "empty manifest with multiple documents",
source: "---",
expected: "",
},
{
name: "empty manifest with multiple documents",
source: "\n---",
expected: "",
},
{
name: "empty manifest with multiple documents",
source: "---\n",
expected: "",
},
{
name: "empty manifest with multiple documents",
source: "\n---\n",
expected: "",
},
{
name: "empty manifest with multiple documents and comments",
source: `
# this multidoc manifest
# consists only
---
# of comments and
---
# whitespace lines
---
---
---
`,
expected: "",
},
} {
t.Run(tc.name, func(t *testing.T) {
remarshaled, err := remarshalYAML(tc.source)
Expand Down

0 comments on commit e88798a

Please sign in to comment.