From 0f2de89ce567037362a58cad6527426a95a40ad0 Mon Sep 17 00:00:00 2001 From: Dex Date: Wed, 9 Jan 2019 16:11:17 -0800 Subject: [PATCH 1/3] use built kube path, add debug logging --- pkg/lifecycle/kubectl/daemonless.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/lifecycle/kubectl/daemonless.go b/pkg/lifecycle/kubectl/daemonless.go index 5454c6df0..d63716f89 100644 --- a/pkg/lifecycle/kubectl/daemonless.go +++ b/pkg/lifecycle/kubectl/daemonless.go @@ -47,8 +47,14 @@ func (d *DaemonlessKubectl) Execute(ctx context.Context, release api.Release, st return errors.Wrap(err, "get builder") } - builtPath, _ := builder.String(step.Path) - builtKubePath, _ := builder.String(step.Kubeconfig) + builtPath, err := builder.String(step.Path) + if err != nil { + return errors.Wrapf(err, "build apply path %s", step.Path) + } + builtKubePath, err := builder.String(step.Kubeconfig) + if err != nil { + return errors.Wrapf(err, "build kubeconfig path %s", step.Kubeconfig) + } debug := level.Debug(log.With(d.Logger, "step.type", "kubectl")) @@ -58,10 +64,11 @@ func (d *DaemonlessKubectl) Execute(ctx context.Context, release api.Release, st cmd := exec.Command("kubectl") cmd.Dir = release.FindRenderRoot() - cmd.Args = append(cmd.Args, "apply", "-f", step.Path) + cmd.Args = append(cmd.Args, "apply", "-f", builtPath) if step.Kubeconfig != "" { cmd.Args = append(cmd.Args, "--kubeconfig", builtKubePath) } + debug.Log("event", "kubectl.execute", "args", fmt.Sprintf("%+v", cmd.Args)) var stderr bytes.Buffer cmd.Stderr = &stderr @@ -72,6 +79,7 @@ func (d *DaemonlessKubectl) Execute(ctx context.Context, release api.Release, st doneCh := make(chan struct{}) messageCh := make(chan daemontypes.Message) go d.Status.PushStreamStep(ctx, messageCh) + debug.Log("event", "kubectl.streamStep.pushed") stderrString := "" stdoutString := "" @@ -93,8 +101,10 @@ func (d *DaemonlessKubectl) Execute(ctx context.Context, release api.Release, st Contents: ansiToHTML(stdoutString, stderrString), TrustedHTML: true, } + debug.Log("event", "kubectl.message.pushed") } case <-doneCh: + debug.Log("event", "kubectl.doneCh") stderrString = stderr.String() stdoutString = stdout.String() close(messageCh) @@ -113,6 +123,7 @@ func (d *DaemonlessKubectl) Execute(ctx context.Context, release api.Release, st debug.Log("stderr", stderrString) if err != nil { + debug.Log("event", "kubectl.run.error", "err", err) stderrString = fmt.Sprintf(`Error: %s stderr: %s`, err.Error(), stderrString) } @@ -125,6 +136,7 @@ stderr: %s`, err.Error(), stderrString) }, confirmActions(), ) + debug.Log("event", "kubectl.outputs.pushed", "next", "confirmed.await") return d.awaitMessageConfirmed(ctx, confirmedChan) } From 86b3bbd8e745111ab88d42ae9bc48847963f8b7c Mon Sep 17 00:00:00 2001 From: Dex Date: Wed, 9 Jan 2019 16:25:15 -0800 Subject: [PATCH 2/3] use full builder --- pkg/lifecycle/kubectl/daemonless.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/lifecycle/kubectl/daemonless.go b/pkg/lifecycle/kubectl/daemonless.go index d63716f89..7798155ba 100644 --- a/pkg/lifecycle/kubectl/daemonless.go +++ b/pkg/lifecycle/kubectl/daemonless.go @@ -14,22 +14,26 @@ import ( "github.com/replicatedhq/ship/pkg/api" "github.com/replicatedhq/ship/pkg/lifecycle" "github.com/replicatedhq/ship/pkg/lifecycle/daemon/daemontypes" + "github.com/replicatedhq/ship/pkg/state" "github.com/replicatedhq/ship/pkg/templates" ) type DaemonlessKubectl struct { Logger log.Logger Status daemontypes.StatusReceiver + StateManager state.Manager BuilderBuilder *templates.BuilderBuilder } func NewDaemonlessKubectl( logger log.Logger, builderBuilder *templates.BuilderBuilder, + statemanager state.Manager, ) lifecycle.KubectlApply { return &DaemonlessKubectl{ Logger: logger, BuilderBuilder: builderBuilder, + StateManager: statemanager, } } @@ -37,12 +41,19 @@ func (d *DaemonlessKubectl) WithStatusReceiver(statusReceiver daemontypes.Status return &DaemonlessKubectl{ Logger: d.Logger, BuilderBuilder: d.BuilderBuilder, + StateManager: d.StateManager, Status: statusReceiver, } } +// TODO I need tests func (d *DaemonlessKubectl) Execute(ctx context.Context, release api.Release, step api.KubectlApply, confirmedChan chan bool) error { - builder, err := d.BuilderBuilder.BaseBuilder(release.Metadata) + currState, err := d.StateManager.TryLoad() + if err != nil { + return errors.Wrap(err, "load state") + } + + builder, err := d.BuilderBuilder.FullBuilder(release.Metadata, release.Spec.Config.V1, currState.CurrentConfig()) if err != nil { return errors.Wrap(err, "get builder") } From ac56c7a19cbff5c14d5716820495fa7d78d9ff0a Mon Sep 17 00:00:00 2001 From: Dex Date: Wed, 9 Jan 2019 16:30:30 -0800 Subject: [PATCH 3/3] refactor cmd prep to separate method --- pkg/lifecycle/kubectl/daemonless.go | 65 ++++++++++++++++------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/pkg/lifecycle/kubectl/daemonless.go b/pkg/lifecycle/kubectl/daemonless.go index 7798155ba..1929a7545 100644 --- a/pkg/lifecycle/kubectl/daemonless.go +++ b/pkg/lifecycle/kubectl/daemonless.go @@ -48,37 +48,9 @@ func (d *DaemonlessKubectl) WithStatusReceiver(statusReceiver daemontypes.Status // TODO I need tests func (d *DaemonlessKubectl) Execute(ctx context.Context, release api.Release, step api.KubectlApply, confirmedChan chan bool) error { - currState, err := d.StateManager.TryLoad() - if err != nil { - return errors.Wrap(err, "load state") - } - - builder, err := d.BuilderBuilder.FullBuilder(release.Metadata, release.Spec.Config.V1, currState.CurrentConfig()) - if err != nil { - return errors.Wrap(err, "get builder") - } - - builtPath, err := builder.String(step.Path) - if err != nil { - return errors.Wrapf(err, "build apply path %s", step.Path) - } - builtKubePath, err := builder.String(step.Kubeconfig) - if err != nil { - return errors.Wrapf(err, "build kubeconfig path %s", step.Kubeconfig) - } - debug := level.Debug(log.With(d.Logger, "step.type", "kubectl")) - if builtPath == "" { - return errors.New("A path to apply is required") - } - - cmd := exec.Command("kubectl") - cmd.Dir = release.FindRenderRoot() - cmd.Args = append(cmd.Args, "apply", "-f", builtPath) - if step.Kubeconfig != "" { - cmd.Args = append(cmd.Args, "--kubeconfig", builtKubePath) - } + cmd, err := d.prepareCmd(release, step) debug.Log("event", "kubectl.execute", "args", fmt.Sprintf("%+v", cmd.Args)) var stderr bytes.Buffer @@ -136,7 +108,7 @@ func (d *DaemonlessKubectl) Execute(ctx context.Context, release api.Release, st if err != nil { debug.Log("event", "kubectl.run.error", "err", err) stderrString = fmt.Sprintf(`Error: %s -stderr: %s`, err.Error(), stderrString) + stderr: %s`, err.Error(), stderrString) } d.Status.PushMessageStep( @@ -152,6 +124,39 @@ stderr: %s`, err.Error(), stderrString) return d.awaitMessageConfirmed(ctx, confirmedChan) } +func (d *DaemonlessKubectl) prepareCmd(release api.Release, step api.KubectlApply) (*exec.Cmd, error) { + currState, err := d.StateManager.TryLoad() + if err != nil { + return nil, errors.Wrap(err, "load state") + } + + builder, err := d.BuilderBuilder.FullBuilder(release.Metadata, release.Spec.Config.V1, currState.CurrentConfig()) + if err != nil { + return nil, errors.Wrap(err, "get builder") + } + + builtPath, err := builder.String(step.Path) + if err != nil { + return nil, errors.Wrapf(err, "build apply path %s", step.Path) + } + builtKubePath, err := builder.String(step.Kubeconfig) + if err != nil { + return nil, errors.Wrapf(err, "build kubeconfig path %s", step.Kubeconfig) + } + + if builtPath == "" { + return nil, errors.New("A path to apply is required") + } + + cmd := exec.Command("kubectl") + cmd.Dir = release.FindRenderRoot() + cmd.Args = append(cmd.Args, "apply", "-f", builtPath) + if step.Kubeconfig != "" { + cmd.Args = append(cmd.Args, "--kubeconfig", builtKubePath) + } + return cmd, nil +} + func (d *DaemonlessKubectl) awaitMessageConfirmed(ctx context.Context, confirmedChan chan bool) error { debug := level.Debug(log.With(d.Logger, "struct", "daemonlesskubectl", "method", "awaitMessageConfirmed")) for {