From f9eb05b009d975dd55330da576e59dd8bbcac447 Mon Sep 17 00:00:00 2001 From: Carolyn Van Slyck Date: Tue, 5 Oct 2021 08:39:55 -0500 Subject: [PATCH] Print the applied file in debug mode When running porter * apply, print the contents of the applied file in debug mode so that we can troubleshoot issues in the porter operator, such as we didn't pass in a valid yaml file or converted it from a CRD improperly. Always print the name of the applied resource when completed. Signed-off-by: Carolyn Van Slyck --- pkg/credentials/credentialset.go | 4 ++++ pkg/credentials/credentialset_test.go | 12 ++++++++++++ pkg/parameters/parameterset.go | 5 +++++ pkg/parameters/parameterset_test.go | 12 ++++++++++++ pkg/porter/apply.go | 10 ++++++++++ pkg/porter/credentials.go | 18 +++++++++++++++++- pkg/porter/parameters.go | 18 +++++++++++++++++- 7 files changed, 77 insertions(+), 2 deletions(-) diff --git a/pkg/credentials/credentialset.go b/pkg/credentials/credentialset.go index b6744160a..d939a2833 100644 --- a/pkg/credentials/credentialset.go +++ b/pkg/credentials/credentialset.go @@ -69,6 +69,10 @@ func (s CredentialSet) Validate() error { return nil } +func (s CredentialSet) String() string { + return fmt.Sprintf("%s/%s", s.Namespace, s.Name) +} + // Validate compares the given credentials with the spec. // // This will result in an error only when the following conditions are true: diff --git a/pkg/credentials/credentialset_test.go b/pkg/credentials/credentialset_test.go index 526803e96..c38fab37d 100644 --- a/pkg/credentials/credentialset_test.go +++ b/pkg/credentials/credentialset_test.go @@ -104,3 +104,15 @@ func TestMarshal(t *testing.T) { }) } } + +func TestCredentialSet_String(t *testing.T) { + t.Run("global namespace", func(t *testing.T) { + cs := CredentialSet{Name: "mycreds"} + assert.Equal(t, "/mycreds", cs.String()) + }) + + t.Run("local namespace", func(t *testing.T) { + cs := CredentialSet{Namespace: "dev", Name: "mycreds"} + assert.Equal(t, "dev/mycreds", cs.String()) + }) +} diff --git a/pkg/parameters/parameterset.go b/pkg/parameters/parameterset.go index d2cfd784f..3e48e5389 100644 --- a/pkg/parameters/parameterset.go +++ b/pkg/parameters/parameterset.go @@ -1,6 +1,7 @@ package parameters import ( + "fmt" "time" "get.porter.sh/porter/pkg/secrets" @@ -61,3 +62,7 @@ func (s ParameterSet) Validate() error { } return nil } + +func (s ParameterSet) String() string { + return fmt.Sprintf("%s/%s", s.Namespace, s.Name) +} diff --git a/pkg/parameters/parameterset_test.go b/pkg/parameters/parameterset_test.go index d09302f4d..8aca7b2cd 100644 --- a/pkg/parameters/parameterset_test.go +++ b/pkg/parameters/parameterset_test.go @@ -25,3 +25,15 @@ func TestNewParameterSet(t *testing.T) { assert.Equal(t, SchemaVersion, cs.SchemaVersion, "SchemaVersion was not set") assert.Len(t, cs.Parameters, 1, "Parameters should be initialized with 1 value") } + +func TestParameterSet_String(t *testing.T) { + t.Run("global namespace", func(t *testing.T) { + ps := ParameterSet{Name: "myparams"} + assert.Equal(t, "/myparams", ps.String()) + }) + + t.Run("local namespace", func(t *testing.T) { + ps := ParameterSet{Namespace: "dev", Name: "myparams"} + assert.Equal(t, "dev/myparams", ps.String()) + }) +} diff --git a/pkg/porter/apply.go b/pkg/porter/apply.go index 7d9f9d66d..2af763aeb 100644 --- a/pkg/porter/apply.go +++ b/pkg/porter/apply.go @@ -48,11 +48,21 @@ func (o *ApplyOptions) Validate(cxt *context.Context, args []string) error { } func (p *Porter) InstallationApply(opts ApplyOptions) error { + if p.Debug { + fmt.Fprintf(p.Err, "Reading input file %s...\n", opts.File) + } + namespace, err := p.getNamespaceFromFile(opts) if err != nil { return err } + if p.Debug { + // ignoring any error here, printing debug info isn't critical + contents, _ := p.FileSystem.ReadFile(opts.File) + fmt.Fprintf(p.Err, "Input file contents:\n%s\n", contents) + } + var input claims.Installation if err := encoding.UnmarshalFile(p.FileSystem, opts.File, &input); err != nil { return errors.Wrapf(err, "unable to parse %s as an installation document", opts.File) diff --git a/pkg/porter/credentials.go b/pkg/porter/credentials.go index 0a18afc95..b18de04e8 100644 --- a/pkg/porter/credentials.go +++ b/pkg/porter/credentials.go @@ -301,11 +301,21 @@ func validateCredentialName(args []string) error { } func (p *Porter) CredentialsApply(o ApplyOptions) error { + if p.Debug { + fmt.Fprintf(p.Err, "Reading input file %s...\n", o.File) + } + namespace, err := p.getNamespaceFromFile(o) if err != nil { return err } + if p.Debug { + // ignoring any error here, printing debug info isn't critical + contents, _ := p.FileSystem.ReadFile(o.File) + fmt.Fprintf(p.Err, "Input file contents:\n%s\n", contents) + } + var creds credentials.CredentialSet err = encoding.UnmarshalFile(p.FileSystem, o.File, &creds) if err != nil { @@ -324,7 +334,13 @@ func (p *Porter) CredentialsApply(o ApplyOptions) error { return errors.Wrap(err, "credential set is invalid") } - return p.Credentials.UpsertCredentialSet(creds) + err = p.Credentials.UpsertCredentialSet(creds) + if err != nil { + return err + } + + fmt.Fprintf(p.Err, "Applied %s credential set\n", creds) + return nil } func (p *Porter) getNamespaceFromFile(o ApplyOptions) (string, error) { diff --git a/pkg/porter/parameters.go b/pkg/porter/parameters.go index 753b26f8d..63cc8d3e1 100644 --- a/pkg/porter/parameters.go +++ b/pkg/porter/parameters.go @@ -476,11 +476,21 @@ func (p *Porter) printDisplayValuesTable(values []DisplayValue) error { } func (p *Porter) ParametersApply(o ApplyOptions) error { + if p.Debug { + fmt.Fprintf(p.Err, "Reading input file %s...\n", o.File) + } + namespace, err := p.getNamespaceFromFile(o) if err != nil { return err } + if p.Debug { + // ignoring any error here, printing debug info isn't critical + contents, _ := p.FileSystem.ReadFile(o.File) + fmt.Fprintf(p.Err, "Input file contents:\n%s\n", contents) + } + var params parameters.ParameterSet err = encoding.UnmarshalFile(p.FileSystem, o.File, ¶ms) if err != nil { @@ -499,7 +509,13 @@ func (p *Porter) ParametersApply(o ApplyOptions) error { return errors.Wrap(err, "parameter set is invalid") } - return p.Parameters.UpsertParameterSet(params) + err = p.Parameters.UpsertParameterSet(params) + if err != nil { + return err + } + + fmt.Fprintf(p.Err, "Applied %s parameter set\n", params) + return nil } // resolveParameters accepts a set of parameter assignments and combines them