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

Commit

Permalink
Adapt sync command to sync latest valid revision
Browse files Browse the repository at this point in the history
In case git verification is enabled and the HEAD revision of the
configured branch could not be verified, we still want to synchronize
up till the latest valid commit and report back to the user that;

1. we found a commit with an invalid signature
2. we synchronized up till the latest valid commit we found

On all other errors we still fail.
  • Loading branch information
hiddeco committed Mar 25, 2019
1 parent 0c61ac8 commit 82a1957
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/fluxctl/await.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func awaitJob(ctx context.Context, client api.Server, jobID job.ID) (job.Result,
}
switch j.StatusString {
case job.StatusFailed:
result = j.Result
return false, j
case job.StatusSucceeded:
if j.Err != "" {
Expand Down
18 changes: 15 additions & 3 deletions cmd/fluxctl/sync_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -60,12 +61,17 @@ func (opts *syncOpts) RunE(cmd *cobra.Command, args []string) error {
}
result, err := awaitJob(ctx, opts.API, jobID)
if err != nil {
fmt.Fprintf(cmd.OutOrStderr(), "Failed to complete sync job (ID %q)\n", jobID)
return err
switch isUnverifiedHead(err) {
case true:
fmt.Fprintf(cmd.OutOrStderr(), "Warning: %s\n", err)
default:
fmt.Fprintf(cmd.OutOrStderr(), "Failed to complete sync job (ID %q)\n", jobID)
return err
}
}

rev := result.Revision[:7]
fmt.Fprintf(cmd.OutOrStderr(), "HEAD of %s is %s\n", gitConfig.Remote.Branch, rev)
fmt.Fprintf(cmd.OutOrStderr(), "Revision of %s to apply is %s\n", gitConfig.Remote.Branch, rev)
fmt.Fprintf(cmd.OutOrStderr(), "Waiting for %s to be applied ...\n", rev)
err = awaitSync(ctx, opts.API, rev)
if err != nil {
Expand All @@ -74,3 +80,9 @@ func (opts *syncOpts) RunE(cmd *cobra.Command, args []string) error {
fmt.Fprintln(cmd.OutOrStderr(), "Done.")
return nil
}

func isUnverifiedHead(err error) bool {
return err != nil &&
(strings.Contains(err.Error(), "branch HEAD in the git repo is not verified") &&
strings.Contains(err.Error(), "last verified commit was"))
}
22 changes: 15 additions & 7 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (d *Daemon) executeJob(id job.ID, do jobFunc, logger log.Logger) (job.Resul
d.JobStatusCache.SetStatus(id, job.Status{StatusString: job.StatusRunning})
result, err := do(ctx, id, logger)
if err != nil {
d.JobStatusCache.SetStatus(id, job.Status{StatusString: job.StatusFailed, Err: err.Error()})
d.JobStatusCache.SetStatus(id, job.Status{StatusString: job.StatusFailed, Err: err.Error(), Result: result})
return result, err
}
d.JobStatusCache.SetStatus(id, job.Status{StatusString: job.StatusSucceeded, Result: result})
Expand Down Expand Up @@ -353,17 +353,25 @@ func (d *Daemon) sync() jobFunc {
if err != nil {
return result, err
}
head, err := d.Repo.Revision(ctx, d.GitConfig.Branch)
syncHead, err := d.Repo.Revision(ctx, d.GitConfig.SyncTag)
if err != nil {
return result, err
}
if latestVerifiedRev, _, err := latestValidRevision(ctx, d.Repo, d.GitConfig, head); err != nil {
var latestVerifiedRev string
if latestVerifiedRev, _, err = latestValidRevision(ctx, d.Repo, d.GitConfig, syncHead); err != nil {
return result, err
} else if head, err := d.Repo.Revision(ctx, d.GitConfig.Branch); err != nil {
return result, err
} else if head != latestVerifiedRev {
return result, fmt.Errorf("unable to sync to invalid HEAD revision (%s) latest valid revision is: %s", head, latestVerifiedRev)
}
result.Revision = head
return result, nil
result.Revision = latestVerifiedRev
return result, fmt.Errorf(
"The branch HEAD in the git repo is not verified, and fluxd is unable to sync to it. The last verified commit was %.8s. HEAD is %.8s.",
latestVerifiedRev,
head,
)
}
result.Revision = latestVerifiedRev
return result, err
}
}

Expand Down

0 comments on commit 82a1957

Please sign in to comment.