Skip to content

Commit

Permalink
feat(rpaas-api): streaming instance logs (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurcgc authored Sep 23, 2021
1 parent 6a33437 commit 083cac7
Show file tree
Hide file tree
Showing 25 changed files with 1,284 additions and 114 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.api
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.15-alpine AS builder
FROM golang:1.16-alpine AS builder
COPY . /go/src/github.com/tsuru/rpaas-operator
WORKDIR /go/src/github.com/tsuru/rpaas-operator
RUN apk add --update gcc git make musl-dev && \
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.operator
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.15-alpine AS builder
FROM golang:1.16-alpine AS builder
COPY . /go/src/github.com/tsuru/rpaas-operator
WORKDIR /go/src/github.com/tsuru/rpaas-operator
RUN apk add --update gcc git make musl-dev && \
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.purger
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.15-alpine AS builder
FROM golang:1.16-alpine AS builder
COPY . /go/src/github.com/tsuru/rpaas-operator
WORKDIR /go/src/github.com/tsuru/rpaas-operator
RUN apk add --update gcc git make musl-dev && \
Expand Down
1 change: 1 addition & 0 deletions cmd/plugin/rpaasv2/cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewApp(o, e io.Writer, client rpaasclient.Client) (app *cli.App) {
NewCmdAutoscale(),
NewCmdExec(),
NewCmdShell(),
NewCmdLogs(),
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Expand Down
9 changes: 4 additions & 5 deletions cmd/plugin/rpaasv2/cmd/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/tsuru/rpaas-operator/api/v1alpha1"
"github.com/tsuru/rpaas-operator/pkg/rpaas/client"
"github.com/tsuru/rpaas-operator/pkg/rpaas/client/fake"
"github.com/tsuru/rpaas-operator/pkg/rpaas/client/types"
clientTypes "github.com/tsuru/rpaas-operator/pkg/rpaas/client/types"
)

Expand Down Expand Up @@ -91,7 +90,7 @@ func TestInfo(t *testing.T) {
Content: "# some nginx config at server context",
},
},
Routes: []types.Route{
Routes: []clientTypes.Route{
{
Path: "/app1",
Destination: "app1.tsuru.example.com",
Expand Down Expand Up @@ -432,7 +431,7 @@ Routes:
Binds: []v1alpha1.Bind{},
Replicas: int32Ptr(3),
Blocks: []clientTypes.Block{},
Routes: []types.Route{},
Routes: []clientTypes.Route{},
Team: "some-team",
Cluster: "my-dedicated-cluster",
Description: "some description",
Expand Down Expand Up @@ -544,7 +543,7 @@ Ports:
Binds: []v1alpha1.Bind{},
Replicas: int32Ptr(3),
Blocks: []clientTypes.Block{},
Routes: []types.Route{},
Routes: []clientTypes.Route{},
Team: "some-team",
Cluster: "my-dedicated-cluster",
Description: "some description",
Expand Down Expand Up @@ -664,7 +663,7 @@ Pods: 3
},
},
Replicas: int32Ptr(5),
Routes: []types.Route{
Routes: []clientTypes.Route{
{
Path: "some-path",
Destination: "some-destination",
Expand Down
93 changes: 93 additions & 0 deletions cmd/plugin/rpaasv2/cmd/log.go
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"),
})
}
81 changes: 81 additions & 0 deletions cmd/plugin/rpaasv2/cmd/log_test.go
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())
})
}
}
7 changes: 7 additions & 0 deletions config/api/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ rules:
- patch
- update
- delete
- apiGroups:
- ""
resources:
- pods/log
verbs:
- get
- list
- apiGroups:
- ""
resources:
Expand Down
Loading

0 comments on commit 083cac7

Please sign in to comment.