-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better support for dev takeover (#75)
Per #65 we want to make it easy to use a RepoConfig and deploy from a branch * Add repo mappings to RepoConfig . This is a set of repo URLs to rewrite the source branches in manifest sync this allows the application to be deployed from a branch. * Also add a Pause option which can be used for a takeover. * Fix #75 when matching globs against paths in a tarball we need to strip any leading "/" in the path. Add a version command to hydros * Update goreleaser to start setting the version
- Loading branch information
Showing
14 changed files
with
376 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/go-logr/zapr" | ||
"go.uber.org/zap" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// N.B these will get set by goreleaser | ||
// https://goreleaser.com/cookbooks/using-main.version/?h=using+main.version | ||
var ( | ||
version = "dev" | ||
commit = "none" | ||
date = "unknown" | ||
builtBy = "unknown" | ||
) | ||
|
||
func NewVersionCmd(name string, w io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "version", | ||
Short: "Return version", | ||
Example: fmt.Sprintf("%s version", name), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Fprintf(w, "%s %s, commit %s, built at %s by %s\n", name, version, commit, date, builtBy) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func logVersion() { | ||
log := zapr.NewLogger(zap.L()) | ||
log.Info("binary version", "version", version, "commit", commit, "date", date, "builtBy", builtBy) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package github | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/jlewi/hydros/api/v1alpha1" | ||
) | ||
|
||
// GitHubRepoToURI converts a GitHubRepo to a URI in the gogetter form. | ||
// It assumes the protocol is https. | ||
func GitHubRepoToURI(repo v1alpha1.GitHubRepo) url.URL { | ||
u := url.URL{ | ||
Scheme: "https", | ||
Host: "github.com", | ||
Path: "/" + repo.Org + "/" + repo.Repo + ".git", | ||
} | ||
|
||
if repo.Branch != "" { | ||
u.RawQuery = "ref=" + repo.Branch | ||
} | ||
|
||
return u | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package github | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/jlewi/hydros/api/v1alpha1" | ||
) | ||
|
||
func Test_GitHubRepoToURL(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
repo v1alpha1.GitHubRepo | ||
|
||
// Compare the actual URL to one parsed from a string | ||
expected string | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "main", | ||
repo: v1alpha1.GitHubRepo{ | ||
Org: "jlewi", | ||
Repo: "hydros", | ||
Branch: "jlewi/cicd", | ||
}, | ||
expected: "https://github.com/jlewi/hydros.git?ref=jlewi/cicd", | ||
}, | ||
} | ||
|
||
for _, c := range testCases { | ||
t.Run(c.name, func(t *testing.T) { | ||
actual := GitHubRepoToURI(c.repo) | ||
|
||
expectedU, err := url.Parse(c.expected) | ||
if err != nil { | ||
t.Fatalf("Error parsing expected URL %v", err) | ||
} | ||
|
||
if d := cmp.Diff(expectedU, &actual); d != "" { | ||
t.Errorf("Unexpected diff:\n%v", d) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package gitops | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/jlewi/hydros/api/v1alpha1" | ||
gh "github.com/jlewi/hydros/pkg/github" | ||
"github.com/jlewi/hydros/pkg/github/ghrepo" | ||
"github.com/jlewi/hydros/pkg/util" | ||
"github.com/pkg/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// rewriteRepos rewrites the repos in the manifest to the new repos if necessary. | ||
func rewriteRepos(ctx context.Context, m *v1alpha1.ManifestSync, mappings []v1alpha1.RepoMapping) error { | ||
log := util.LogFromContext(ctx) | ||
|
||
if m == nil { | ||
return errors.New("Manifest is nil") | ||
} | ||
|
||
if mappings == nil { | ||
return nil | ||
} | ||
|
||
srcRepoURL := gh.GitHubRepoToURI(m.Spec.SourceRepo) | ||
|
||
for _, mapping := range mappings { | ||
if srcRepoURL.String() != mapping.Input { | ||
continue | ||
} | ||
|
||
log.Info("Rewriting source repo", "old", srcRepoURL.String(), "new", mapping.Output, "manifestSync.Name", m.Metadata.Name) | ||
|
||
u, err := url.Parse(mapping.Output) | ||
if err != nil { | ||
return errors.Wrapf(err, "Could not parse URL %v", mapping.Output) | ||
} | ||
|
||
r, err := ghrepo.FromURL(u) | ||
if err != nil { | ||
return errors.Wrapf(err, "Could not parse URL %v", srcRepoURL.String()) | ||
} | ||
|
||
// ref parameter specifies the reference to checkout | ||
// https://github.com/hashicorp/go-getter#protocol-specific-options | ||
branch := u.Query().Get("ref") | ||
if branch == "" { | ||
return errors.Wrapf(err, "Branch is not specified in URL %v; it should be specified as a query argument e.g. ?ref=main", mapping.Output) | ||
} | ||
m.Spec.SourceRepo.Org = r.RepoOwner() | ||
m.Spec.SourceRepo.Repo = r.RepoName() | ||
m.Spec.SourceRepo.Branch = branch | ||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
// SetTakeOverAnnotations sets the takeover annotations on the manifest. | ||
func SetTakeOverAnnotations(m *v1alpha1.ManifestSync, pause time.Duration) error { | ||
tEnd := time.Now().Add(pause) | ||
|
||
k8sTime := metav1.NewTime(tEnd) | ||
v, err := k8sTime.MarshalJSON() | ||
if err != nil { | ||
return errors.Wrapf(err, "Failed to marshal time %v", tEnd) | ||
} | ||
m.Metadata.Annotations = map[string]string{ | ||
// We need to mark it as a takeover otherwise we won't override pauses. | ||
v1alpha1.TakeoverAnnotation: "true", | ||
v1alpha1.PauseAnnotation: string(v), | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.