Skip to content

Commit

Permalink
Context plumbing (getporter#2242)
Browse files Browse the repository at this point in the history
* Add context to PullBundle

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Add context to Copy

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Use context in build

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Use context when working with shared cnab options

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Remove unhelpful debug statement

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Use context in version command

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Add context to porter storage permissions command

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Use context when printing debug info for a bundle

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Use context in IsBundleUpToDate

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Use context in schema command

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Add context to mixins commands

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Add context to plugins uninstall command

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Add context to the exec mixin commands (but not the mixins runtime)

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Use context in lint command

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Use context in remaining credentials commands

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Add context to pkgmgmt commands

Signed-off-by: Carolyn Van Slyck <[email protected]>

* use context in random places

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Rest of the fixes

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Use context in dependencies

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Review feedback

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Do not print applied parameter/credential set

Signed-off-by: Carolyn Van Slyck <[email protected]>
  • Loading branch information
carolynvs authored Jul 20, 2022
1 parent 5488a8b commit 3d66a3f
Show file tree
Hide file tree
Showing 63 changed files with 469 additions and 376 deletions.
2 changes: 1 addition & 1 deletion cmd/exec/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func buildInstallCommand(m *exec.Mixin) *cobra.Command {
Use: "install",
Short: "Execute the install functionality of this mixin",
RunE: func(cmd *cobra.Command, args []string) error {
return m.Execute(opts)
return m.Execute(cmd.Context(), opts)
},
}
flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cmd/exec/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func buildInvokeCommand(m *exec.Mixin) *cobra.Command {
Use: "invoke",
Short: "Execute the invoke functionality of this mixin",
RunE: func(cmd *cobra.Command, args []string) error {
return m.Execute(opts)
return m.Execute(cmd.Context(), opts)
},
}
flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cmd/exec/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func buildLintCommand(m *exec.Mixin) *cobra.Command {
Use: "lint",
Short: "Check sections of the bundle associated with this mixin for problems and adherence to best practices",
RunE: func(cmd *cobra.Command, args []string) error {
return m.PrintLintResults()
return m.PrintLintResults(cmd.Context())
},
}
return cmd
Expand Down
2 changes: 1 addition & 1 deletion cmd/exec/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func buildUninstallCommand(m *exec.Mixin) *cobra.Command {
Use: "uninstall",
Short: "Execute the uninstall functionality of this mixin",
RunE: func(cmd *cobra.Command, args []string) error {
return m.Execute(opts)
return m.Execute(cmd.Context(), opts)
},
}
flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cmd/exec/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func buildUpgradeCommand(m *exec.Mixin) *cobra.Command {
Use: "upgrade",
Short: "Execute the upgrade functionality of this mixin",
RunE: func(cmd *cobra.Command, args []string) error {
return m.Execute(opts)
return m.Execute(cmd.Context(), opts)
},
}
flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cmd/porter/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ If the source bundle is a digest reference, destination must be a tagged referen
return opts.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.CopyBundle(opts)
return p.CopyBundle(cmd.Context(), opts)
},
}
f := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cmd/porter/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func buildCredentialsCreateCommand(p *porter.Porter) *cobra.Command {
return opts.Validate(args)
},
RunE: func(cmd *cobra.Command, argrs []string) error {
return p.CreateCredential(opts)
return p.CreateCredential(cmd.Context(), opts)
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/porter/mixins.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func BuildMixinUninstallCommand(p *porter.Porter) *cobra.Command {
return opts.Validate(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.UninstallMixin(opts)
return p.UninstallMixin(cmd.Context(), opts)
},
}

Expand Down Expand Up @@ -172,7 +172,7 @@ See https://getporter.org/mixin-dev-guide/distribution more details.
return opts.Validate(p.Context)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.GenerateMixinFeed(opts)
return p.GenerateMixinFeed(cmd.Context(), opts)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/porter/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func BuildPluginUninstallCommand(p *porter.Porter) *cobra.Command {
return opts.Validate(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
return p.UninstallPlugin(opts)
return p.UninstallPlugin(cmd.Context(), opts)
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/porter/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func buildStorageFixPermissionsCommand(p *porter.Porter) *cobra.Command {
Short: "Fix the permissions on your PORTER_HOME directory",
Long: `This will reset the permissions on your PORTER_HOME directory to the least permissions required, where only the current user has permissions.`,
RunE: func(cmd *cobra.Command, args []string) error {
return p.FixPermissions()
return p.FixPermissions(cmd.Context())
},
}
}
2 changes: 1 addition & 1 deletion pkg/cnab/cnab-to-oci/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewTestRegistry() *TestRegistry {
return &TestRegistry{}
}

func (t TestRegistry) PullBundle(ref cnab.OCIReference, insecureRegistry bool) (cnab.BundleReference, error) {
func (t TestRegistry) PullBundle(ctx context.Context, ref cnab.OCIReference, insecureRegistry bool) (cnab.BundleReference, error) {
if t.MockPullBundle != nil {
return t.MockPullBundle(ref, insecureRegistry)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cnab/cnab-to-oci/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// RegistryProvider handles talking with an OCI registry.
type RegistryProvider interface {
// PullBundle pulls a bundle from an OCI registry.
PullBundle(ref cnab.OCIReference, insecureRegistry bool) (cnab.BundleReference, error)
PullBundle(ctx context.Context, ref cnab.OCIReference, insecureRegistry bool) (cnab.BundleReference, error)

// PushBundle pushes a bundle to an OCI registry.
PushBundle(ctx context.Context, bundleRef cnab.BundleReference, insecureRegistry bool) (cnab.BundleReference, error)
Expand Down
16 changes: 12 additions & 4 deletions pkg/cnab/cnab-to-oci/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/google/go-containerregistry/pkg/crane"
"github.com/moby/term"
"github.com/opencontainers/go-digest"
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap/zapcore"
)

// ErrNoContentDigest represents an error due to an image not having a
Expand All @@ -45,24 +47,30 @@ func NewRegistry(c *portercontext.Context) *Registry {
}

// PullBundle pulls a bundle from an OCI registry. Returns the bundle, and an optional image relocation mapping, if applicable.
func (r *Registry) PullBundle(ref cnab.OCIReference, insecureRegistry bool) (cnab.BundleReference, error) {
func (r *Registry) PullBundle(ctx context.Context, ref cnab.OCIReference, insecureRegistry bool) (cnab.BundleReference, error) {
ctx, span := tracing.StartSpan(ctx,
attribute.String("reference", ref.String()),
attribute.Bool("insecure", insecureRegistry),
)
defer span.EndSpan()

var insecureRegistries []string
if insecureRegistry {
reg := ref.Registry()
insecureRegistries = append(insecureRegistries, reg)
}

if r.Debug {
if span.ShouldLog(zapcore.DebugLevel) {
msg := strings.Builder{}
msg.WriteString("Pulling bundle ")
msg.WriteString(ref.String())
if insecureRegistry {
msg.WriteString(" with --insecure-registry")
}
fmt.Fprintln(r.Err, msg.String())
span.Debug(msg.String())
}

bun, reloMap, digest, err := remotes.Pull(context.Background(), ref.Named, r.createResolver(insecureRegistries))
bun, reloMap, digest, err := remotes.Pull(ctx, ref.Named, r.createResolver(insecureRegistries))
if err != nil {
return cnab.BundleReference{}, fmt.Errorf("unable to pull bundle: %w", err)
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/cnab/provider/action.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package cnabprovider

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"strings"

"get.porter.sh/porter/pkg/cnab"
"get.porter.sh/porter/pkg/config"
Expand All @@ -17,6 +17,7 @@ import (
"github.com/cnabio/cnab-go/driver"
"github.com/hashicorp/go-multierror"
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap/zapcore"
)

// Shared arguments for all CNAB actions
Expand Down Expand Up @@ -170,7 +171,7 @@ func (r *Runtime) Execute(ctx context.Context, args ActionArguments) error {
}
}

r.printDebugInfo(b, creds, args.Params)
r.printDebugInfo(ctx, b, creds, args.Params)

opResult, result, err := a.Run(currentRun.ToCNAB(), creds.ToCNAB(), r.ApplyConfig(ctx, args)...)

Expand Down Expand Up @@ -301,28 +302,30 @@ func (r *Runtime) appendFailedResult(ctx context.Context, opErr error, run stora
return multierror.Append(opErr, resultErr).ErrorOrNil()
}

func (r *Runtime) printDebugInfo(b cnab.ExtendedBundle, creds secrets.Set, params map[string]interface{}) {
if r.Debug {
dump := &bytes.Buffer{}
func (r *Runtime) printDebugInfo(ctx context.Context, b cnab.ExtendedBundle, creds secrets.Set, params map[string]interface{}) {
log := tracing.LoggerFromContext(ctx)

if log.ShouldLog(zapcore.DebugLevel) {
var dump strings.Builder
secrets := make([]string, 0, len(params)+len(creds))

fmt.Fprintf(dump, "params:\n")
dump.WriteString("params:\n")
for k, v := range params {
if b.IsSensitiveParameter(k) {
// TODO(carolynvs): When we consolidate our conversion logic of parameters into strings, let's use it here.
// https://github.com/cnabio/cnab-go/issues/270
secrets = append(secrets, fmt.Sprintf("%v", v))
}
fmt.Fprintf(dump, " - %s: %v\n", k, v)
dump.WriteString(fmt.Sprintf(" - %s: %v\n", k, v))
}

fmt.Fprintf(dump, "creds:\n")
dump.WriteString("creds:\n")
for k, v := range creds {
secrets = append(secrets, fmt.Sprintf("%v", v))
fmt.Fprintf(dump, " - %s: %v\n", k, v)
dump.WriteString(fmt.Sprintf(" - %s: %v\n", k, v))
}

r.Context.SetSensitiveValues(secrets)
fmt.Fprintln(r.Err, dump.String())
log.Debug(dump.String())
}
}
11 changes: 5 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,24 +220,23 @@ func (c *Config) SetPorterPath(path string) {
c.porterPath = path
}

func (c *Config) GetPorterPath() (string, error) {
func (c *Config) GetPorterPath(ctx context.Context) (string, error) {
if c.porterPath != "" {
return c.porterPath, nil
}

log := tracing.LoggerFromContext(ctx)
porterPath, err := getExecutable()
if err != nil {
return "", fmt.Errorf("could not get path to the executing porter binary: %w", err)
return "", log.Error(fmt.Errorf("could not get path to the executing porter binary: %w", err))
}

// We try to resolve back to the original location
hardPath, err := evalSymlinks(porterPath)
if err != nil { // if we have trouble resolving symlinks, skip trying to help people who used symlinks
fmt.Fprintln(c.Err, fmt.Errorf("WARNING could not resolve %s for symbolic links\n: %w", porterPath, err))
log.Error(fmt.Errorf("WARNING could not resolve %s for symbolic links: %w", porterPath, err))
} else if hardPath != porterPath {
if c.Debug {
fmt.Fprintf(c.Err, "Resolved porter binary from %s to %s\n", porterPath, hardPath)
}
log.Debugf("Resolved porter binary from %s to %s", porterPath, hardPath)
porterPath = hardPath
}

Expand Down
19 changes: 11 additions & 8 deletions pkg/exec/builder/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package builder

import (
"bufio"
"context"
"fmt"
"io/ioutil"

"get.porter.sh/porter/pkg/portercontext"
"get.porter.sh/porter/pkg/tracing"
"get.porter.sh/porter/pkg/yaml"
)

Expand Down Expand Up @@ -73,18 +75,19 @@ func unmarshalActionMap(actionMap map[string][]interface{}, builder BuildableAct
// err := yaml.Unmarshal(contents, &action)
// return &action, err
// })
func LoadAction(cxt *portercontext.Context, commandFile string, unmarshal func([]byte) (interface{}, error)) error {
contents, err := readInputFromStdinOrFile(cxt, commandFile)
func LoadAction(ctx context.Context, porterCtx *portercontext.Context, commandFile string, unmarshal func([]byte) (interface{}, error)) error {
//lint:ignore SA4006 ignore unused ctx for now
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()

contents, err := readInputFromStdinOrFile(porterCtx, commandFile)
if err != nil {
return err
return span.Error(err)
}

result, err := unmarshal(contents)
if cxt.Debug {
fmt.Fprintf(cxt.Err, "DEBUG Parsed Input:\n%#v\n", result)
}
_, err = unmarshal(contents)
if err != nil {
return fmt.Errorf("could not unmarshal input:\n %s: %w", string(contents), err)
return span.Error(fmt.Errorf("could not unmarshal input:\n %s: %w", string(contents), err))
}

return nil
Expand Down
Loading

0 comments on commit 3d66a3f

Please sign in to comment.