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

check if chart values parse as yaml before installing application #1194

Merged
merged 3 commits into from
Sep 18, 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
4 changes: 4 additions & 0 deletions cmd/embedded-cluster/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ var installCommand = &cli.Command{
return err // we want the user to see the error message without a prefix
}
}
if err := preflights.ValidateApp(); err != nil {
metrics.ReportApplyFinished(c, err)
return err
}
adminConsolePwd, err := maybeAskAdminConsolePassword(c)
if err != nil {
metrics.ReportApplyFinished(c, err)
Expand Down
31 changes: 31 additions & 0 deletions pkg/preflights/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package preflights

import (
"fmt"

"github.com/replicatedhq/embedded-cluster/pkg/release"
"github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
)

// ValidateApp runs some basic checks on the embedded cluster config.
func ValidateApp() error {
cfg, err := release.GetEmbeddedClusterConfig()
if err != nil {
return fmt.Errorf("unable to get embedded cluster config: %w", err)
}
if cfg == nil || cfg.Spec.Extensions.Helm == nil {
return nil
}

// for each addon, check to see if the values file parses as yaml
for _, addon := range cfg.Spec.Extensions.Helm.Charts {
genericUnmarshal := map[string]interface{}{}
err = yaml.Unmarshal([]byte(addon.Values), &genericUnmarshal)
if err != nil {
logrus.Debugf("failed to parse helm chart values for addon %s as yaml, values were %q: %v", addon.Name, addon.Values, err)
return fmt.Errorf("failed to parse helm chart values for addon %s as yaml: %w", addon.Name, err)
}
}
return nil
}
102 changes: 102 additions & 0 deletions pkg/preflights/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package preflights

import (
"github.com/replicatedhq/embedded-cluster/pkg/release"
"github.com/stretchr/testify/require"
"testing"
)

func TestValidateApp(t *testing.T) {
tests := []struct {
name string
releaseData map[string][]byte
wantErr string
}{
{
name: "valid",
releaseData: map[string][]byte{
"embedded-cluster-config.yaml": []byte(`
apiVersion: embeddedcluster.replicated.com/v1beta1
kind: Config
metadata:
name: "testconfig"`)},
},
{
name: "bad extension values",
releaseData: map[string][]byte{
"embedded-cluster-config.yaml": []byte(`
apiVersion: embeddedcluster.replicated.com/v1beta1
kind: Config
metadata:
name: "testconfig"
spec:
extensions:
helm:
charts:
- chartname: test
name: test
namespace: test
version: 1.0.0
values: |
badyaml: true
thisisnotavalidmapping: {{repl RandomString 10}}
`)},
wantErr: "failed to parse helm chart values for addon test as yaml: error converting YAML to JSON: yaml: invalid map key: map[interface {}]interface {}{\"repl RandomString 10\":interface {}(nil)}",
},
{
name: "good extension values",
releaseData: map[string][]byte{
"embedded-cluster-config.yaml": []byte(`
apiVersion: embeddedcluster.replicated.com/v1beta1
kind: Config
metadata:
name: "testconfig"
spec:
extensions:
helm:
charts:
- chartname: test
name: test
namespace: test
version: 1.0.0
values: |
goodyaml: true
`)},
wantErr: "",
},
{
name: "no extension values",
releaseData: map[string][]byte{
"embedded-cluster-config.yaml": []byte(`
apiVersion: embeddedcluster.replicated.com/v1beta1
kind: Config
metadata:
name: "testconfig"
spec:
extensions:
helm:
charts:
- chartname: test
name: test
namespace: test
version: 1.0.0
`)},
wantErr: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := require.New(t)

req.NoError(release.SetReleaseDataForTests(tt.releaseData))

err := ValidateApp()
if tt.wantErr != "" {
req.Error(err)
req.Equal(tt.wantErr, err.Error())
} else {
req.NoError(err)
}
})
}
}
Loading