-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* api/web/client: add initial instance debug support * api: proper attach and wait for status for debug * api: add tests for debug // return debug container logs when unable to attach to it * integration: add integration tests for debug plugin cmd * integration: bump rpaas-api chart * k8s: minor refactor on debug and add tests * k8s: fix debugPodWithContainerStatus signature and add debug test with no pod defined * Update internal/pkg/rpaas/k8s.go Co-authored-by: Claudio Netto <[email protected]> * k8s: minor refactor on patchEphemeralContainers Co-authored-by: Claudio Netto <[email protected]> * api: add missing license header to transport * plugin: fix typo for debug/exec cmds --------- Co-authored-by: Claudio Netto <[email protected]>
- Loading branch information
1 parent
3ac2590
commit cdbed04
Showing
25 changed files
with
1,468 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright 2023 tsuru authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/urfave/cli/v2" | ||
"k8s.io/kubectl/pkg/util/term" | ||
|
||
rpaasclient "github.com/tsuru/rpaas-operator/pkg/rpaas/client" | ||
) | ||
|
||
func NewCmdDebug() *cli.Command { | ||
return &cli.Command{ | ||
Name: "debug", | ||
Usage: "Run debug in an pod from instance", | ||
ArgsUsage: "[-p POD] [-c CONTAINER] [--debug-image image] [--] COMMAND [args...]", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "service", | ||
Aliases: []string{"tsuru-service", "s"}, | ||
Usage: "the Tsuru service name", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "instance", | ||
Aliases: []string{"tsuru-service-instance", "i"}, | ||
Usage: "the reverse proxy instance name", | ||
Required: true, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "pod", | ||
Aliases: []string{"p"}, | ||
Usage: "pod name - if omitted, the first pod will be chosen", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "container", | ||
Aliases: []string{"c"}, | ||
Usage: "container name - if omitted, the \"nginx\" container will be chosen", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "debug-image", | ||
Aliases: []string{"d"}, | ||
Usage: "debug image name - if omitted, service default defined debug image will be chosen", | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "interactive", | ||
Aliases: []string{"I", "stdin"}, | ||
Usage: "pass STDIN to container", | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "tty", | ||
Aliases: []string{"t"}, | ||
Usage: "allocate a pseudo-TTY", | ||
}, | ||
}, | ||
Before: setupClient, | ||
Action: runDebug, | ||
} | ||
} | ||
|
||
func runDebug(c *cli.Context) error { | ||
client, err := getClient(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var width, height uint16 | ||
if ts := term.GetSize(os.Stdin.Fd()); ts != nil { | ||
width, height = ts.Width, ts.Height | ||
} | ||
|
||
args := rpaasclient.DebugArgs{ | ||
Command: c.Args().Slice(), | ||
Instance: c.String("instance"), | ||
Pod: c.String("pod"), | ||
Container: c.String("container"), | ||
Interactive: c.Bool("interactive"), | ||
Image: c.String("debug-image"), | ||
TTY: c.Bool("tty"), | ||
TerminalWidth: width, | ||
TerminalHeight: height, | ||
} | ||
|
||
if args.Interactive { | ||
args.In = os.Stdin | ||
} | ||
|
||
tty := &term.TTY{ | ||
In: args.In, | ||
Out: c.App.Writer, | ||
Raw: args.TTY, | ||
} | ||
return tty.Safe(func() error { | ||
conn, err := client.Debug(c.Context, args) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
|
||
done := make(chan error, 1) | ||
go func() { | ||
defer close(done) | ||
for { | ||
mtype, message, nerr := conn.ReadMessage() | ||
if nerr != nil { | ||
closeErr, ok := nerr.(*websocket.CloseError) | ||
if !ok { | ||
done <- fmt.Errorf("ERROR: received an unexpected error while reading messages: %w", err) | ||
return | ||
} | ||
|
||
switch closeErr.Code { | ||
case websocket.CloseNormalClosure: | ||
case websocket.CloseInternalServerErr: | ||
done <- fmt.Errorf("ERROR: the command may not be executed as expected - reason: %s", closeErr.Text) | ||
default: | ||
done <- fmt.Errorf("ERROR: unexpected close error: %s", closeErr.Error()) | ||
} | ||
|
||
return | ||
} | ||
|
||
switch mtype { | ||
case websocket.TextMessage, websocket.BinaryMessage: | ||
c.App.Writer.Write(message) | ||
} | ||
} | ||
}() | ||
err = <-done | ||
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) | ||
return err | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2023 tsuru authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/tsuru/rpaas-operator/pkg/rpaas/client" | ||
"github.com/tsuru/rpaas-operator/pkg/rpaas/client/fake" | ||
) | ||
|
||
func TestDebug(t *testing.T) { | ||
var called bool | ||
|
||
tests := []struct { | ||
name string | ||
args []string | ||
expected string | ||
expectedError string | ||
expectedCalled bool | ||
client client.Client | ||
}{ | ||
{ | ||
name: "with command and arguments", | ||
args: []string{"rpaasv2", "debug", "-s", "rpaasv2", "-i", "my-instance", "--", "my-command", "-arg1", "--arg2"}, | ||
client: &fake.FakeClient{ | ||
FakeDebug: func(ctx context.Context, args client.DebugArgs) (*websocket.Conn, error) { | ||
called = true | ||
expected := client.DebugArgs{ | ||
Command: []string{"my-command", "-arg1", "--arg2"}, | ||
Instance: "my-instance", | ||
} | ||
assert.Equal(t, expected, args) | ||
return nil, fmt.Errorf("some error") | ||
}, | ||
}, | ||
expectedCalled: true, | ||
expectedError: "some error", | ||
}, | ||
{ | ||
name: "with all options activated", | ||
args: []string{"rpaasv2", "debug", "-s", "rpaasv2", "-i", "my-instance", "--tty", "--interactive", "-p", "pod-1", "-c", "container-1", "--", "my-shell"}, | ||
client: &fake.FakeClient{ | ||
FakeDebug: func(ctx context.Context, args client.DebugArgs) (*websocket.Conn, error) { | ||
called = true | ||
expected := client.DebugArgs{ | ||
In: os.Stdin, | ||
Command: []string{"my-shell"}, | ||
Instance: "my-instance", | ||
Pod: "pod-1", | ||
Container: "container-1", | ||
TTY: true, | ||
Interactive: true, | ||
} | ||
assert.Equal(t, expected, args) | ||
return nil, fmt.Errorf("another error") | ||
}, | ||
}, | ||
expectedCalled: true, | ||
expectedError: "another error", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
called = false | ||
stdout := &bytes.Buffer{} | ||
stderr := &bytes.Buffer{} | ||
app := NewApp(stdout, stderr, tt.client) | ||
err := app.Run(tt.args) | ||
if tt.expectedError != "" { | ||
assert.EqualError(t, err, tt.expectedError) | ||
return | ||
} | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.expectedCalled, called) | ||
assert.Equal(t, tt.expected, stdout.String()) | ||
assert.Empty(t, stderr.String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.