Skip to content

[previewctl] Improve logging #10436

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

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dev/preview/previewctl/cmd/get_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (
"fmt"

"github.com/gitpod-io/gitpod/previewctl/pkg/preview"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func getNameCmd() *cobra.Command {
func getNameCmd(logger *logrus.Logger) *cobra.Command {

cmd := &cobra.Command{
Use: "get-name",
Short: "Returns the name of the preview for the corresponding branch.",
Run: func(cmd *cobra.Command, args []string) {
p := preview.New(branch)
p := preview.New(branch, logger)

fmt.Println(p.GetPreviewName())
},
Expand Down
9 changes: 4 additions & 5 deletions dev/preview/previewctl/cmd/install_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@
package cmd

import (
"log"

"github.com/gitpod-io/gitpod/previewctl/pkg/preview"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
watch = false
)

func installContextCmd() *cobra.Command {
func installContextCmd(logger *logrus.Logger) *cobra.Command {

cmd := &cobra.Command{
Use: "install-context",
Short: "Installs the kubectl context of a preview environment.",
Run: func(cmd *cobra.Command, args []string) {
p := preview.New(branch)
p := preview.New(branch, logger)

err := p.InstallContext(watch)
if err != nil {
log.Fatalf("Couldn't install context for the '%s' preview", p.Branch)
logger.WithFields(logrus.Fields{"err": err}).Fatal("Failed to install context.")
}
},
}
Expand Down
7 changes: 4 additions & 3 deletions dev/preview/previewctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
package cmd

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
branch = ""
)

func RootCmd() *cobra.Command {
func RootCmd(logger *logrus.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "previewctl",
Short: "Your best friend when interacting with Preview Environments :)",
Expand All @@ -22,8 +23,8 @@ func RootCmd() *cobra.Command {
cmd.PersistentFlags().StringVar(&branch, "branch", "", "From which branch's preview previewctl should interact with. By default it will use the result of \"git rev-parse --abbrev-ref HEAD\"")

cmd.AddCommand(
installContextCmd(),
getNameCmd(),
installContextCmd(logger),
getNameCmd(logger),
)
return cmd
}
3 changes: 3 additions & 0 deletions dev/preview/previewctl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ go 1.18

require github.com/spf13/cobra v1.4.0

require golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions dev/preview/previewctl/go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
14 changes: 10 additions & 4 deletions dev/preview/previewctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
package main

import (
"log"

"github.com/gitpod-io/gitpod/previewctl/cmd"
"github.com/sirupsen/logrus"
)

func main() {
root := cmd.RootCmd()
logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})

root := cmd.RootCmd(logger)
if err := root.Execute(); err != nil {
log.Fatal(err)
logger.WithFields(logrus.Fields{"err": err}).Fatal("command failed.")
}
}
23 changes: 16 additions & 7 deletions dev/preview/previewctl/pkg/preview/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,39 @@ package preview
import (
"crypto/sha256"
"encoding/hex"
"log"
"os/exec"
"regexp"
"strings"
"time"

"github.com/sirupsen/logrus"
)

type Preview struct {
Branch string

logger *logrus.Entry
}

func New(branch string) *Preview {
func New(branch string, logger *logrus.Logger) *Preview {
if branch == "" {
out, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output()
if err != nil {
log.Fatalf("Could not retrieve branch name, err: %v", err)
logger.WithFields(logrus.Fields{"err": err}).Fatal("Could not retrieve branch name.")
}
branch = string(out)
} else {
_, err := exec.Command("git", "rev-parse", "--verify", branch).Output()
if err != nil {
log.Fatalf("Branch '%s' does not exist", branch)
logger.WithFields(logrus.Fields{"branch": branch, "err": err}).Fatal("Branch does not exist.")
}
}

logEntry := logger.WithFields(logrus.Fields{"branch": branch})

return &Preview{
Branch: branch,
logger: logEntry,
}
}

Expand All @@ -48,10 +54,13 @@ func (p *Preview) InstallContext(watch bool) error {
for {
select {
case <-installTicker.C:
if err := installContext(p.Branch); err == nil {
// No error means successful context installation
return nil
err := installContext(p.Branch)
if err != nil {
p.logger.WithFields(logrus.Fields{"err": err}).Info("Failed to install context. Trying again in 30 seconds.")
continue
}
p.logger.Info("Context installed.")
return nil
}
}
}
Expand Down