Fixes #7088 template command bug

The template command (and install/upgrade) will dump the content
of the problematic yaml content if it cannot be unmarshalled.

Signed-off-by: Laszlo Varadi <laszlo.varadi@gmail.com>
pull/7094/head
Laszlo Varadi 6 years ago
parent 593ea3fb12
commit 6c001f53ea

@ -33,6 +33,7 @@ import (
"helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/gates" "helm.sh/helm/v3/pkg/gates"
"helm.sh/helm/v3/pkg/logs"
) )
// FeatureGateOCI is the feature gate for checking if `helm chart` and `helm registry` commands should work // FeatureGateOCI is the feature gate for checking if `helm chart` and `helm registry` commands should work
@ -62,6 +63,8 @@ func initKubeLogs() {
} }
func main() { func main() {
logs.Init(settings)
initKubeLogs() initKubeLogs()
actionConfig := new(action.Configuration) actionConfig := new(action.Configuration)

@ -0,0 +1,39 @@
/*
Copyright The Helm Authors.
Licensed 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.
*/
package logs
import (
"fmt"
"log"
"helm.sh/helm/v3/pkg/cli"
)
var settings *cli.EnvSettings
//Init logs package with settings... used to get the Debug flag
func Init(s *cli.EnvSettings) {
settings = s
}
//Debug log if Debug is set in HELM_DEBUG or with --debug
func Debug(format string, v ...interface{}) {
if settings.Debug {
format = fmt.Sprintf("[debug] %s\n", format)
log.Output(2, fmt.Sprintf(format, v...))
}
}

@ -17,6 +17,7 @@ limitations under the License.
package releaseutil package releaseutil
import ( import (
"fmt"
"log" "log"
"path" "path"
"sort" "sort"
@ -27,6 +28,7 @@ import (
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
"helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/logs"
"helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/release"
) )
@ -140,9 +142,19 @@ func (file *manifestFile) sort(result *result) error {
for _, entryKey := range sortedEntryKeys { for _, entryKey := range sortedEntryKeys {
m := file.entries[entryKey] m := file.entries[entryKey]
var entry SimpleHead var entry SimpleHead
if err := yaml.Unmarshal([]byte(m), &entry); err != nil { if err := yaml.Unmarshal([]byte(m), &entry); err != nil {
mname := ""
if len(file.entries) > 1 {
index, err := strconv.Atoi(entryKey[9:])
if err != nil {
index = 0
} else {
index++
}
mname = fmt.Sprintf(" (in yaml document %d)", index)
}
logs.Debug("YAML parse error on %s%s\n---\n%s\n---\n", file.path, mname, m)
return errors.Wrapf(err, "YAML parse error on %s", file.path) return errors.Wrapf(err, "YAML parse error on %s", file.path)
} }

@ -17,15 +17,109 @@ limitations under the License.
package releaseutil package releaseutil
import ( import (
"bytes"
"log"
"reflect" "reflect"
"strings"
"testing" "testing"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
"helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/logs"
"helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/release"
) )
func TestSortManifestErrorLog(t *testing.T) {
logs.Init(&cli.EnvSettings{Debug: true})
yamlWithError := `kind: ReplicaSet
apiVersion: v1beta1
metadata:
name: second_with_error
#indentation on next line is invalid
namespace: second
`
data := []struct {
name []string
path string
kind []string
hooks map[string][]release.HookEvent
manifest string
}{
{
name: []string{"first"},
path: "one",
kind: []string{"Job"},
hooks: map[string][]release.HookEvent{},
manifest: `apiVersion: v1
kind: Job
metadata:
name: first
labels:
doesnot: matter
`,
},
{
name: []string{"second", "second_with_error", "last"},
path: "yaml_with_error",
kind: []string{"ReplicaSet", "ReplicaSet", "Job"},
hooks: map[string][]release.HookEvent{},
manifest: `kind: ReplicaSet
apiVersion: v1beta1
metadata:
name: second
namespace: second
---
` + yamlWithError + `---
apiVersion: v1
kind: Job
metadata:
name: last
labels:
doesnot: matter
`,
},
}
manifests := make(map[string]string, len(data))
for _, o := range data {
manifests[o.path] = o.manifest
}
origWriter := log.Writer()
var buf bytes.Buffer
log.SetOutput(&buf)
SortManifests(manifests, chartutil.VersionSet{"v1", "v1beta1"}, InstallOrder)
log.SetOutput(origWriter)
s := buf.String()
if !strings.Contains(s, "YAML parse error on yaml_with_error (in yaml document 2)") || !strings.Contains(s, yamlWithError) {
t.Log("Error log should contain the problematic yaml content")
t.Log(s)
t.FailNow()
}
buf.Reset()
logs.Init(&cli.EnvSettings{Debug: false})
SortManifests(manifests, chartutil.VersionSet{"v1", "v1beta1"}, InstallOrder)
log.SetOutput(origWriter)
s = buf.String()
if strings.Contains(s, "YAML parse error on yaml_with_error") || strings.Contains(s, yamlWithError) {
t.Log("Error log should NOT contain the problematic yaml content")
t.Log(s)
t.FailNow()
}
}
func TestSortManifests(t *testing.T) { func TestSortManifests(t *testing.T) {
data := []struct { data := []struct {

Loading…
Cancel
Save