Skip to content

Commit

Permalink
Support references to specific commit SHAs (#1)
Browse files Browse the repository at this point in the history
In addition of using a tag name to reference a specific version of an operator package, a combination of branch and SHA of a commit can be used as well.

Signed-off-by: Jan Schlicht <[email protected]>
  • Loading branch information
Jan Schlicht authored Jul 14, 2020
1 parent ebc79e5 commit 752e0bc
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
11 changes: 8 additions & 3 deletions pkg/apis/operator/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ type Git struct {
// cloned and the specified tag is checked out.
Source string `yaml:"source"`

// Tag of the KUDO operator version.
Tag string `yaml:"tag"`

// Directory where the KUDO operator is defined in the Git repository.
Directory string `yaml:"directory"`

// Tag (or branch) of the KUDO operator version.
Tag string `yaml:"tag,omitempty"`

// Optional SHA of the KUDO operator version if a branch is used instead of
// a tag. If this isn't set, the latest commit of the referenced branch will
// be used.
SHA string `yaml:"sha,omitempty"`
}
3 changes: 2 additions & 1 deletion pkg/internal/apis/operator/encode/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func convertV1Alpha1Version(in v1alpha1.Version) operator.Version {
func convertV1Alpha1Git(in v1alpha1.Git) operator.Git {
out := operator.Git{
Source: in.Source,
Tag: in.Tag,
Directory: in.Directory,
Tag: in.Tag,
SHA: in.SHA,
}

return out
Expand Down
11 changes: 8 additions & 3 deletions pkg/internal/apis/operator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ type Git struct {
// cloned and the specified tag is checked out.
Source string

// Tag of the KUDO operator version.
Tag string

// Directory where the KUDO operator is defined in the Git repository.
Directory string

// Tag (or branch) of the KUDO operator version.
Tag string

// Optional SHA of the KUDO operator version if a branch is used instead of
// a tag. If this isn't set, the latest commit of the referenced branch will
// be used.
SHA string
}
35 changes: 27 additions & 8 deletions pkg/internal/resolvers/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ import (
type Resolver struct {
URL string
Branch string
SHA string
OperatorDirectory string

// Extracted function to simplify testing.
gitClone func(ctx context.Context, url, branch, tempDir string) error
gitClone func(ctx context.Context, tempDir, url, branch, sha string) error
}

// NewResolver creates a new Resolver for a Git repository at the specified URL.
func NewResolver(url, branch, operatorDirectory string) Resolver {
func NewResolver(url, branch, sha string, operatorDirectory string) Resolver {
return Resolver{
URL: url,
Branch: branch,
SHA: sha,
OperatorDirectory: operatorDirectory,

gitClone: gitClone,
Expand All @@ -50,9 +52,10 @@ func (r Resolver) Resolve(ctx context.Context) (afero.Fs, resolvers.Remover, err

log.WithField("url", r.URL).
WithField("branch", r.Branch).
WithField("sha", r.SHA).
Info("Cloning Git repository")

if err := r.gitClone(ctx, r.URL, r.Branch, tempDir); err != nil {
if err := r.gitClone(ctx, tempDir, r.URL, r.Branch, r.SHA); err != nil {
return nil, nil, err
}

Expand All @@ -65,9 +68,27 @@ func (r Resolver) Resolve(ctx context.Context) (afero.Fs, resolvers.Remover, err
return afero.NewBasePathFs(fs, path.Join(tempDir, r.OperatorDirectory)), remover, nil
}

func gitClone(ctx context.Context, url, branch, tempDir string) error {
func gitClone(ctx context.Context, tempDir, url, branch, sha string) error {
logger := log.WithField("url", url).
WithField("branch", branch).
WithField("sha", sha)

if err := runAndLog(ctx, logger, "git", "clone", "--branch", branch, "--single-branch", url, tempDir); err != nil {
return err
}

if sha != "" {
if err := runAndLog(ctx, logger, "git", "-C", tempDir, "reset", "--hard", sha); err != nil {
return err
}
}

return nil
}

func runAndLog(ctx context.Context, logger *log.Entry, name string, args ...string) error {
//nolint:gosec
cmd := exec.CommandContext(ctx, "git", "clone", "--branch", branch, "--single-branch", url, tempDir)
cmd := exec.CommandContext(ctx, name, args...)

stderr, err := cmd.StderrPipe()
if err != nil {
Expand All @@ -83,9 +104,7 @@ func gitClone(ctx context.Context, url, branch, tempDir string) error {

for scanner.Scan() {
t := scanner.Text()
log.WithField("url", url).
WithField("branch", branch).
Debug(t)
logger.Debug(t)
}

if err := cmd.Wait(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/internal/resolvers/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func TestResolve(t *testing.T) {
testClone := func(ctx context.Context, url, branch, tempDir string) error {
if url == "example.org" && branch == "test" {
testClone := func(ctx context.Context, tempDir, url, branch, sha string) error {
if url == "example.org" && branch == "test" && sha == "" {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func getResolver(o operator.Operator, version operator.Version) (resolvers.Resol
}

// TODO: cache git sources to ensure that repositories are only cloned once per source
resolver := git.NewResolver(source.URL, version.Git.Tag, version.Git.Directory)
resolver := git.NewResolver(source.URL, version.Git.Tag, version.Git.SHA, version.Git.Directory)

return resolver, nil
}
Expand Down

0 comments on commit 752e0bc

Please sign in to comment.