Skip to content

Commit

Permalink
Updates the odo dev output for logging (redhat-developer#5609)
Browse files Browse the repository at this point in the history
<!--
Thank you for opening a PR! Here are some things you need to know before submitting:

1. Please read our developer guideline: https://github.com/redhat-developer/odo/wiki/Dev:-odo-Dev-Guidelines
2. Label this PR accordingly with the '/kind' line
3. Ensure you have written and ran the appropriate tests: https://github.com/redhat-developer/odo/wiki/Dev:-Writing-and-running-tests
4. Read how we approve and LGTM each PR: https://github.com/redhat-developer/odo/wiki/Pull-Requests:-Review-guideline

Documentation:

If you are pushing a change to documentation, please read: https://github.com/redhat-developer/odo/wiki/Documentation:-Contributing
-->

**What type of PR is this:**

<!--
Add one of the following kinds:
/kind bug
/kind feature
/kind tests
/kind documentation

Feel free to use other [labels](https://github.com/redhat-developer/odo/labels) as needed. However one of the above labels must be present or the PR will not be reviewed. This instruction is for reviewers as well.
-->

/kind cleanup

**What does this PR do / why we need it:**

This PR does the following:
- Removes the references to `odo log`
- Changes the output to last 100 lines and puts the number in a constant
- Reworks the output of `odo dev` a bit to be bolded / more consise with
  out outputs.

See below for the changes:
```sh
▶ copy-odo && devfiles && cd deploy-no-knative && odo dev
go build -mod=vendor -ldflags="-X github.com/redhat-developer/odo/pkg/version.GITCOMMIT=4669bc9dc" cmd/odo/odo.go
  __
 /  \__     Developing using the deploy-no-knative Devfile
 \__/  \    Namespace: default
 /  \__/    odo version: v2.5.0
 \__/

↪ Deploying to the cluster in developer mode
 ✓  Waiting for Kubernetes resources [360ms]
 ✓  Syncing files into the container [1ms]
 ✓  Building your application in container on cluster [1s]
 ✓  Executing the application [1s]
 ⚠  Devfile command "run" exited with an error status in 1 seconds
 ⚠  Last 100 lines of log:
time="2022-03-30T16:47:25Z" level=info msg="try to start program" program=devrun
time="2022-03-30T16:47:25Z" level=info msg="success to start program" program=devrun
ODO_COMMAND_RUN is npm start
Changing directory to /project
Executing command cd /project && npm start
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /project/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/project/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
...
time="2022-03-30T16:50:30Z" level=info msg="Don't start the stopped program because its retry times 0 is greater than start retries 0" program=devrun
Your application is now running on the cluster.

- Port 3000 from "runtime" container forwarded to localhost:40001

Watching for changes in the current directory /Users/cdrage/syncthing/dev/k8s/devfiles/deploy-no-knative

Press Ctrl+c to exit.
```

**Which issue(s) this PR fixes:**
<!--
Specifying the issue will automatically close it when this PR is merged
-->

Fixes redhat-developer#5606

**PR acceptance criteria:**

- [X] Unit test

- [X] Integration test

- [X] Documentation

**How to test changes / Special notes to the reviewer:**

Signed-off-by: Charlie Drage <[email protected]>
  • Loading branch information
cdrage committed Aug 31, 2022
1 parent f6fbb1f commit 41da730
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
13 changes: 6 additions & 7 deletions pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package component
import (
"fmt"
"io"
"os"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -38,6 +37,7 @@ import (
)

const supervisorDStatusWaitTimeInterval = 1
const numberOfLinesToOutputLog = 100

// New instantiates a component adapter
func New(adapterContext common.AdapterContext, client kclient.ClientInterface, prefClient preference.Client) Adapter {
Expand Down Expand Up @@ -382,21 +382,20 @@ func (a Adapter) CheckSupervisordCommandStatus(command devfilev1.Command) error
}

if !running {
numberOfLines := 20
log.Warningf("devfile command %q exited with error status within %d sec", command.Id, supervisorDStatusWaitTimeInterval)
log.Infof("Last %d lines of the component's log:", numberOfLines)
log.Warningf("Devfile command %q exited with an error status in %d sec", command.Id, supervisorDStatusWaitTimeInterval)
log.Warningf("Last %d lines of log:", numberOfLinesToOutputLog)

rd, err := component.Log(a.Client, a.ComponentName, a.AppName, false, command)
if err != nil {
return err
}

err = util.DisplayLog(false, rd, os.Stderr, a.ComponentName, numberOfLines)
// Use GetStderr in order to make sure that colour output is correct
// on non-TTY terminals
err = util.DisplayLog(false, rd, log.GetStderr(), a.ComponentName, numberOfLinesToOutputLog)
if err != nil {
return err
}

log.Info("To get the full log output, please run 'odo log'")
}
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/odo/cli/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func (o *DevOptions) Run(ctx context.Context) error {
if err != nil {
return err
}
fmt.Fprintf(o.out, "\nYour application is running on cluster.\n\n")

// get the endpoint/port information for containers in devfile and setup port-forwarding
containers, err := o.Context.EnvSpecificInfo.GetDevfileObj().Data.GetComponents(parsercommon.DevfileOptions{
Expand All @@ -210,7 +209,11 @@ func (o *DevOptions) Run(ctx context.Context) error {
if err != nil {
return err
}
portsBuf := NewPortWriter(os.Stdout, len(portPairsSlice))

// Output that the application is running, and then show the port-forwarding information
log.Info("\nYour application is now running on the cluster")

portsBuf := NewPortWriter(log.GetStdout(), len(portPairsSlice))
go func() {
err = o.clientset.KubernetesClient.SetupPortForwarding(pod, portPairsSlice, portsBuf, o.errOut)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/odo/cli/dev/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"io"
"strings"

"github.com/fatih/color"
)

type PortWriter struct {
Expand All @@ -23,6 +25,12 @@ func NewPortWriter(buffer io.Writer, len int) *PortWriter {
}

func (o *PortWriter) Write(buf []byte) (n int, err error) {

// Set the colours to green (to indicate that the port is OPEN)
// as well as bold. So it stands our that the application is currently
// being port forwarded.
color.Set(color.FgGreen, color.Bold)
defer color.Unset() // Use it in your function
s := string(buf)
if strings.HasPrefix(s, "Forwarding from 127.0.0.1") {
fmt.Fprintf(o.buffer, " - %s", s)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/devfile/cmd_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ var _ = Describe("odo dev command tests", func() {

It("should error out with some log", func() {
helper.MatchAllInOutput(string(initErr), []string{
"exited with error status within 1 sec",
"exited with an error status in 1 sec",
"Did you mean one of these?",
})
})
Expand Down

0 comments on commit 41da730

Please sign in to comment.