Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Add detailed error message in fluxctl sync
Browse files Browse the repository at this point in the history
* Extends v6.GitConfig to include `Error` field.

* In case of `fluxctl sync` error, prints the `Error` field
  in the output.

* Bumps api version.

Signed-off-by: Dinos Kousidis <[email protected]>
  • Loading branch information
Dinos Kousidis committed Jan 26, 2020
1 parent 212d5e1 commit c61ec9f
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cmd/fluxctl/sync_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (opts *syncOpts) RunE(cmd *cobra.Command, args []string) error {

ctx := context.Background()

gitConfig, err := opts.API.GitRepoConfig(ctx, false)
gitConfig, err := opts.API.GitRepoConfigWithError(ctx, false)
if err != nil {
return err
}
Expand All @@ -46,7 +46,7 @@ func (opts *syncOpts) RunE(cmd *cobra.Command, args []string) error {
case git.RepoReady:
break
default:
return fmt.Errorf("git repository %s is not ready to sync (status: %s)", gitConfig.Remote.URL, string(gitConfig.Status))
return fmt.Errorf("git repository %s is not ready to sync\n(Full error: %v)", gitConfig.Remote.URL, gitConfig.Error)
}

fmt.Fprintf(cmd.OutOrStderr(), "Synchronizing with %s\n", gitConfig.Remote.URL)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package api

import "github.com/fluxcd/flux/pkg/api/v11"
import v12 "github.com/fluxcd/flux/pkg/api/v12"

// Server defines the minimal interface a Flux must satisfy to adequately serve a
// connecting fluxctl. This interface specifically does not facilitate connecting
// to Weave Cloud.
type Server interface {
v11.Server
v12.Server
}
22 changes: 22 additions & 0 deletions pkg/api/v12/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// This package defines the types for Flux API version 12.
package v12

import (
"context"

v11 "github.com/fluxcd/flux/pkg/api/v11"
v6 "github.com/fluxcd/flux/pkg/api/v6"
)

// GitConfig extends the v6.GitConfig with Error
type GitConfig struct {
v6.GitConfig
Error string `json:"errors"`
}

// Server in version 12
type Server interface {
v11.Server

GitRepoConfigWithError(ctx context.Context, regenerate bool) (GitConfig, error)
}
1 change: 1 addition & 0 deletions pkg/api/v6/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type GitConfig struct {
Remote GitRemoteConfig `json:"remote"`
PublicSSHKey ssh.PublicKey `json:"publicSSHKey"`
Status git.GitRepoStatus `json:"status"`
Error string `json:"errors"`
}

type Deprecated interface {
Expand Down
14 changes: 14 additions & 0 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/fluxcd/flux/pkg/api"
"github.com/fluxcd/flux/pkg/api/v10"
"github.com/fluxcd/flux/pkg/api/v11"
"github.com/fluxcd/flux/pkg/api/v12"
"github.com/fluxcd/flux/pkg/api/v6"
"github.com/fluxcd/flux/pkg/api/v9"
"github.com/fluxcd/flux/pkg/cluster"
Expand Down Expand Up @@ -640,6 +641,19 @@ func (d *Daemon) GitRepoConfig(ctx context.Context, regenerate bool) (v6.GitConf
}, nil
}

func (d *Daemon) GitRepoConfigWithError(ctx context.Context, regenerate bool) (v12.GitConfig, error) {
gitConfig, err := d.GitRepoConfig(ctx, regenerate)
if err != nil {
return v12.GitConfig{}, err
}
_, errorField := d.Repo.Status()

return v12.GitConfig{
gitConfig,
errorField.Error(),
}, nil
}

// Non-api.Server methods

// WithWorkingClone applies the given func to a fresh, writable clone
Expand Down
7 changes: 7 additions & 0 deletions pkg/http/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/fluxcd/flux/pkg/api"
"github.com/fluxcd/flux/pkg/api/v10"
"github.com/fluxcd/flux/pkg/api/v11"
"github.com/fluxcd/flux/pkg/api/v12"
"github.com/fluxcd/flux/pkg/api/v6"
"github.com/fluxcd/flux/pkg/api/v9"
fluxerr "github.com/fluxcd/flux/pkg/errors"
Expand Down Expand Up @@ -130,6 +131,12 @@ func (c *Client) GitRepoConfig(ctx context.Context, regenerate bool) (v6.GitConf
return res, err
}

func (c *Client) GitRepoConfigWithError(ctx context.Context, regenerate bool) (v12.GitConfig, error) {
var res v12.GitConfig
err := c.methodWithResp(ctx, "POST", &res, transport.GitRepoConfigWithError, regenerate)
return res, err
}

// --- Request helpers

// Post is a simple query-param only post request
Expand Down
13 changes: 13 additions & 0 deletions pkg/http/daemon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func NewHandler(s api.Server, r *mux.Router) http.Handler {
r.Get(transport.SyncStatus).HandlerFunc(handle.SyncStatus)
r.Get(transport.Export).HandlerFunc(handle.Export)
r.Get(transport.GitRepoConfig).HandlerFunc(handle.GitRepoConfig)
r.Get(transport.GitRepoConfigWithError).HandlerFunc(handle.GitRepoConfigWithError)

// These handlers persist to support requests from older fluxctls. In general we
// should avoid adding references to them so that they can eventually be removed.
Expand Down Expand Up @@ -231,6 +232,18 @@ func (s HTTPServer) GitRepoConfig(w http.ResponseWriter, r *http.Request) {
transport.JSONResponse(w, r, res)
}

func (s HTTPServer) GitRepoConfigWithError(w http.ResponseWriter, r *http.Request) {
var regenerate bool
if err := json.NewDecoder(r.Body).Decode(&regenerate); err != nil {
transport.WriteError(w, r, http.StatusBadRequest, err)
}
res, err := s.server.GitRepoConfigWithError(r.Context(), regenerate)
if err != nil {
transport.ErrorResponse(w, r, err)
}
transport.JSONResponse(w, r, res)
}

// --- handlers supporting deprecated requests

func (s HTTPServer) UpdateImages(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
SyncStatus = "SyncStatus"
Export = "Export"
GitRepoConfig = "GitRepoConfig"
GitRepoConfigWithError = "GitRepoConfigWithError"

UpdateImages = "UpdateImages"
UpdatePolicies = "UpdatePolicies"
Expand All @@ -38,4 +39,5 @@ const (
RegisterDaemonV9 = "RegisterDaemonV9"
RegisterDaemonV10 = "RegisterDaemonV10"
RegisterDaemonV11 = "RegisterDaemonV11"
RegisterDaemonV12 = "RegisterDaemonV12"
)
1 change: 1 addition & 0 deletions pkg/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewAPIRouter() *mux.Router {
r.NewRoute().Name(SyncStatus).Methods("GET").Path("/v6/sync").Queries("ref", "{ref}")
r.NewRoute().Name(Export).Methods("HEAD", "GET").Path("/v6/export")
r.NewRoute().Name(GitRepoConfig).Methods("POST").Path("/v9/git-repo-config")
r.NewRoute().Name(GitRepoConfigWithError).Methods("POST").Path("/v12/git-repo-config-with-error")

// These routes persist to support requests from older fluxctls. In general we
// should avoid adding references to them so that they can eventually be removed.
Expand Down
10 changes: 10 additions & 0 deletions pkg/remote/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/fluxcd/flux/pkg/api"
"github.com/fluxcd/flux/pkg/api/v10"
"github.com/fluxcd/flux/pkg/api/v11"
"github.com/fluxcd/flux/pkg/api/v12"
"github.com/fluxcd/flux/pkg/api/v6"
"github.com/fluxcd/flux/pkg/api/v9"
"github.com/fluxcd/flux/pkg/job"
Expand Down Expand Up @@ -107,6 +108,15 @@ func (p *ErrorLoggingServer) GitRepoConfig(ctx context.Context, regenerate bool)
return p.server.GitRepoConfig(ctx, regenerate)
}

func (p *ErrorLoggingServer) GitRepoConfigWithError(ctx context.Context, regenerate bool) (_ v12.GitConfig, err error) {
defer func() {
if err != nil {
p.logger.Log("method", "GitRepoConfigWithError", "error", err)
}
}()
return p.server.GitRepoConfigWithError(ctx, regenerate)
}

func (p *ErrorLoggingServer) Ping(ctx context.Context) (err error) {
defer func() {
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions pkg/remote/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/fluxcd/flux/pkg/api"
"github.com/fluxcd/flux/pkg/api/v10"
"github.com/fluxcd/flux/pkg/api/v11"
"github.com/fluxcd/flux/pkg/api/v12"
"github.com/fluxcd/flux/pkg/api/v6"
"github.com/fluxcd/flux/pkg/api/v9"
"github.com/fluxcd/flux/pkg/job"
Expand Down Expand Up @@ -118,6 +119,16 @@ func (i *instrumentedServer) SyncStatus(ctx context.Context, cursor string) (_ [
return i.s.SyncStatus(ctx, cursor)
}

func (i *instrumentedServer) GitRepoConfigWithError(ctx context.Context, regenerate bool) (_ v12.GitConfig, err error) {
defer func(begin time.Time) {
requestDuration.With(
fluxmetrics.LabelMethod, "GitRepoConfigWithError",
fluxmetrics.LabelSuccess, fmt.Sprint(err == nil),
).Observe(time.Since(begin).Seconds())
}(time.Now())
return i.s.GitRepoConfigWithError(ctx, regenerate)
}

func (i *instrumentedServer) GitRepoConfig(ctx context.Context, regenerate bool) (_ v6.GitConfig, err error) {
defer func(begin time.Time) {
requestDuration.With(
Expand Down
8 changes: 8 additions & 0 deletions pkg/remote/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/fluxcd/flux/pkg/api"
"github.com/fluxcd/flux/pkg/api/v10"
"github.com/fluxcd/flux/pkg/api/v11"
"github.com/fluxcd/flux/pkg/api/v12"
"github.com/fluxcd/flux/pkg/api/v6"
"github.com/fluxcd/flux/pkg/api/v9"
"github.com/fluxcd/flux/pkg/guid"
Expand Down Expand Up @@ -49,6 +50,9 @@ type MockServer struct {

GitRepoConfigAnswer v6.GitConfig
GitRepoConfigError error

GitRepoConfigWithErrorAnswer v12.GitConfig
GitRepoConfigWithErrorError error
}

func (p *MockServer) Ping(ctx context.Context) error {
Expand Down Expand Up @@ -104,6 +108,10 @@ func (p *MockServer) GitRepoConfig(ctx context.Context, regenerate bool) (v6.Git
return p.GitRepoConfigAnswer, p.GitRepoConfigError
}

func (p *MockServer) GitRepoConfigWithError(ctx context.Context, regenerate bool) (v12.GitConfig, error) {
return p.GitRepoConfigWithErrorAnswer, p.GitRepoConfigWithErrorError
}

var _ api.Server = &MockServer{}

// -- Battery of tests for an api.Server implementation. Since these
Expand Down
5 changes: 5 additions & 0 deletions pkg/remote/rpc/baseclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/fluxcd/flux/pkg/api"
"github.com/fluxcd/flux/pkg/api/v10"
"github.com/fluxcd/flux/pkg/api/v11"
"github.com/fluxcd/flux/pkg/api/v12"
"github.com/fluxcd/flux/pkg/api/v6"
"github.com/fluxcd/flux/pkg/api/v9"
"github.com/fluxcd/flux/pkg/job"
Expand Down Expand Up @@ -67,3 +68,7 @@ func (bc baseClient) SyncStatus(context.Context, string) ([]string, error) {
func (bc baseClient) GitRepoConfig(context.Context, bool) (v6.GitConfig, error) {
return v6.GitConfig{}, remote.UpgradeNeededError(errors.New("GitRepoConfig method not implemented"))
}

func (bc baseClient) GitRepoConfigWithError(context.Context, bool) (v12.GitConfig, error) {
return v12.GitConfig{}, remote.UpgradeNeededError(errors.New("GitRepoConfigWithError method not implemented"))
}
20 changes: 20 additions & 0 deletions pkg/remote/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/fluxcd/flux/pkg/api/v10"
"github.com/fluxcd/flux/pkg/api/v12"

"github.com/pkg/errors"

Expand Down Expand Up @@ -207,6 +208,11 @@ type GitRepoConfigResponse struct {
ApplicationError *fluxerr.Error
}

type GitRepoConfigWithErrorResponse struct {
Result v12.GitConfig
ApplicationError *fluxerr.Error
}

func (p *RPCServer) GitRepoConfig(regenerate bool, resp *GitRepoConfigResponse) error {
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()
Expand All @@ -220,3 +226,17 @@ func (p *RPCServer) GitRepoConfig(regenerate bool, resp *GitRepoConfigResponse)
}
return err
}

func (p *RPCServer) GitRepoConfigWithError(regenerate bool, resp *GitRepoConfigWithErrorResponse) error {
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()
v, err := p.s.GitRepoConfigWithError(ctx, regenerate)
resp.Result = v
if err != nil {
if err, ok := errors.Cause(err).(*fluxerr.Error); ok {
resp.ApplicationError = err
return nil
}
}
return err
}

0 comments on commit c61ec9f

Please sign in to comment.