-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rpaas debug feature with custom image #136
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2a408b7
api/web/client: add initial instance debug support
morpheu 71faf79
api: proper attach and wait for status for debug
morpheu cb72d14
api: add tests for debug // return debug container logs when unable t…
morpheu ce477bc
integration: add integration tests for debug plugin cmd
morpheu d92137c
integration: bump rpaas-api chart
morpheu 1baa779
k8s: minor refactor on debug and add tests
morpheu fb6e55d
k8s: fix debugPodWithContainerStatus signature and add debug test wit…
morpheu 029f7f3
Update internal/pkg/rpaas/k8s.go
morpheu 584dec5
k8s: minor refactor on patchEphemeralContainers
morpheu 9d19ad3
api: add missing license header to transport
morpheu 14c8228
plugin: fix typo for debug/exec cmds
morpheu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd force the interactive mode (tty + interactive) in this sub-command... I think there's no other user case to use it without an interactive session.