-
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.
feat(rpaas-api): streaming instance logs (#106)
- Loading branch information
Showing
25 changed files
with
1,284 additions
and
114 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
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,93 @@ | ||
// Copyright 2021 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 ( | ||
rpaasclient "github.com/tsuru/rpaas-operator/pkg/rpaas/client" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func NewCmdLogs() *cli.Command { | ||
return &cli.Command{ | ||
Name: "logs", | ||
Usage: "Fetches and prints logs from instance pods", | ||
Aliases: []string{"log"}, | ||
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: "specific pod to log from", | ||
Required: false, | ||
}, | ||
&cli.PathFlag{ | ||
Name: "container", | ||
Aliases: []string{"c"}, | ||
Usage: "specific container to log from", | ||
Required: false, | ||
}, | ||
&cli.IntFlag{ | ||
Name: "lines", | ||
Aliases: []string{"l"}, | ||
Usage: "number of earlier log lines to show", | ||
Required: false, | ||
}, | ||
&cli.DurationFlag{ | ||
Name: "since", | ||
Usage: "only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to last 24 hours.", | ||
Required: false, | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "follow", | ||
Aliases: []string{"f"}, | ||
Usage: "specify if the logs should be streamed", | ||
Required: false, | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "timestamp", | ||
Aliases: []string{"with-timestamp"}, | ||
Usage: "include timestamps on each line in the log output", | ||
Required: false, | ||
Value: true, | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "color", | ||
Usage: "defines whether or not to display colorful output. Defaults to true.", | ||
Required: false, | ||
Value: true, | ||
}, | ||
}, | ||
Before: setupClient, | ||
Action: runLogRpaas, | ||
} | ||
} | ||
|
||
func runLogRpaas(c *cli.Context) error { | ||
client, err := getClient(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return client.Log(c.Context, rpaasclient.LogArgs{ | ||
Instance: c.String("instance"), | ||
Lines: c.Int("lines"), | ||
Since: c.Duration("since"), | ||
Follow: c.Bool("follow"), | ||
WithTimestamp: c.Bool("timestamp"), | ||
Pod: c.String("pod"), | ||
Container: c.String("container"), | ||
Color: c.Bool("color"), | ||
}) | ||
} |
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,81 @@ | ||
// Copyright 2021 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" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
rpaasclient "github.com/tsuru/rpaas-operator/pkg/rpaas/client" | ||
"github.com/tsuru/rpaas-operator/pkg/rpaas/client/fake" | ||
) | ||
|
||
func TestLog(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []string | ||
expected string | ||
expectedError string | ||
client rpaasclient.Client | ||
}{ | ||
{ | ||
name: "when Log returns an error", | ||
args: []string{"./rpaasv2", "log", "-i", "my-instance"}, | ||
expectedError: "some error", | ||
client: &fake.FakeClient{ | ||
FakeLog: func(args rpaasclient.LogArgs) error { | ||
expected := rpaasclient.LogArgs{ | ||
Instance: "my-instance", | ||
Color: true, | ||
WithTimestamp: true, | ||
} | ||
assert.Equal(t, expected, args) | ||
return fmt.Errorf("some error") | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "when Log returns no error", | ||
args: []string{"./rpaasv2", "logs", "-i", "my-instance", "--since", "2s", "--follow", "--pod", "some-pod", "--container", "some-container", "--with-timestamp", "--lines", "15"}, | ||
client: &fake.FakeClient{ | ||
FakeLog: func(args rpaasclient.LogArgs) error { | ||
expected := rpaasclient.LogArgs{ | ||
Instance: "my-instance", | ||
Since: time.Second * 2, | ||
Follow: true, | ||
Pod: "some-pod", | ||
Container: "some-container", | ||
WithTimestamp: true, | ||
Lines: 15, | ||
Color: true, | ||
} | ||
assert.Equal(t, expected, args) | ||
return nil | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
stdout := &bytes.Buffer{} | ||
stderr := &bytes.Buffer{} | ||
app := NewApp(stdout, stderr, tt.client) | ||
err := app.Run(tt.args) | ||
if tt.expectedError != "" { | ||
assert.Error(t, err) | ||
assert.EqualError(t, err, tt.expectedError) | ||
return | ||
} | ||
require.NoError(t, err) | ||
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
Oops, something went wrong.