Skip to content

Commit 9de63bc

Browse files
committed
chore: kbagent log stdout on error
1 parent 1d711bd commit 9de63bc

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

pkg/kbagent/service/action.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"sync"
2929

3030
"github.com/go-logr/logr"
31-
"github.com/pkg/errors"
3231
"golang.org/x/exp/maps"
3332

3433
"github.com/apecloud/kubeblocks/pkg/kbagent/proto"
@@ -85,17 +84,14 @@ func (s *actionService) HandleRequest(ctx context.Context, payload []byte) ([]by
8584
}
8685
resp, err := s.handleRequest(ctx, req)
8786
result := string(resp)
88-
if err != nil {
89-
result = err.Error()
90-
}
91-
s.logger.Info("Action Executed", "action", req.Action, "result", result)
87+
s.logger.Info("Action Executed", "action", req.Action, "result", result, "err", err)
9288
return s.encode(resp, err), nil
9389
}
9490

9591
func (s *actionService) decode(payload []byte) (*proto.ActionRequest, error) {
9692
req := &proto.ActionRequest{}
9793
if err := json.Unmarshal(payload, req); err != nil {
98-
return nil, errors.Wrapf(proto.ErrBadRequest, "unmarshal action request error: %s", err.Error())
94+
return nil, fmt.Errorf("%w: unmarshal action request error: %w", proto.ErrBadRequest, err)
9995
}
10096
return req, nil
10197
}
@@ -114,11 +110,11 @@ func (s *actionService) encode(out []byte, err error) []byte {
114110

115111
func (s *actionService) handleRequest(ctx context.Context, req *proto.ActionRequest) ([]byte, error) {
116112
if _, ok := s.actions[req.Action]; !ok {
117-
return nil, errors.Wrapf(proto.ErrNotDefined, "%s is not defined", req.Action)
113+
return nil, fmt.Errorf("%w: %s is not defined", proto.ErrNotDefined, req.Action)
118114
}
119115
action := s.actions[req.Action]
120116
if action.Exec == nil {
121-
return nil, errors.Wrap(proto.ErrNotImplemented, "only exec action is supported")
117+
return nil, fmt.Errorf("%w: only exec action is supported", proto.ErrNotImplemented)
122118
}
123119
// HACK: pre-check for the reconfigure action
124120
if err := checkReconfigure(ctx, req); err != nil {
@@ -154,8 +150,5 @@ func (s *actionService) handleExecActionNonBlocking(ctx context.Context, req *pr
154150
return nil, proto.ErrInProgress
155151
}
156152
delete(s.runningActions, req.Action)
157-
if (*result).err != nil {
158-
return nil, (*result).err
159-
}
160-
return (*result).stdout.Bytes(), nil
153+
return (*result).stdout.Bytes(), wrapExecError((*result).err, (*result).stderr)
161154
}

pkg/kbagent/service/command.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ package service
2222
import (
2323
"bytes"
2424
"context"
25+
"errors"
2526
"fmt"
2627
"io"
2728
"os"
2829
"os/exec"
2930
"strings"
3031
"time"
3132

32-
"github.com/pkg/errors"
33-
3433
"github.com/apecloud/kubeblocks/pkg/kbagent/proto"
3534
"github.com/apecloud/kubeblocks/pkg/kbagent/util"
3635
)
@@ -63,19 +62,15 @@ func runCommand(ctx context.Context, action *proto.ExecAction, parameters map[st
6362
return nil, err
6463
}
6564
result := <-resultChan
66-
err = result.err
67-
if err != nil {
68-
var exitErr *exec.ExitError
69-
if errors.As(err, &exitErr) {
70-
errMsg := fmt.Sprintf("exit code: %d", exitErr.ExitCode())
71-
if stderrMsg := result.stderr.String(); len(stderrMsg) > 0 {
72-
errMsg += fmt.Sprintf(", stderr: %s", stderrMsg)
73-
}
74-
return nil, errors.Wrapf(proto.ErrFailed, errMsg)
75-
}
76-
return nil, err
65+
return result.stdout.Bytes(), wrapExecError(result.err, result.stderr)
66+
}
67+
68+
func wrapExecError(err error, stderr *bytes.Buffer) error {
69+
var exitErr *exec.ExitError
70+
if errors.As(err, &exitErr) {
71+
return fmt.Errorf("%w: exit code: %d, stderr: %s", proto.ErrFailed, exitErr.ExitCode(), stderr.String())
7772
}
78-
return result.stdout.Bytes(), nil
73+
return err
7974
}
8075

8176
func runCommandNonBlocking(ctx context.Context, action *proto.ExecAction, parameters map[string]string, timeout *int32) (chan *commandResult, error) {
@@ -159,7 +154,7 @@ func runCommandX(ctx context.Context, action *proto.ExecAction, parameters map[s
159154
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
160155
errChan <- proto.ErrTimedOut
161156
} else {
162-
errChan <- errors.Wrapf(proto.ErrFailed, "failed to start command: %v", err)
157+
errChan <- fmt.Errorf("%w: failed to start command: %w", proto.ErrFailed, err)
163158
}
164159
return
165160
}

0 commit comments

Comments
 (0)