-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support git repo caching on package create (#785)
## Description Add the ability to locally cache large git repositories when pulling them with Zarf ## Related Issue Fixes #750 ## Type of change - [X] New feature (non-breaking change which adds functionality) ## Checklist before merging - [x] Tests have been added/updated as necessary (add the `needs-tests` label) - [x] Documentation has been updated as necessary (add the `needs-docs` label) - [x] An ADR has been written as necessary (add the `needs-adr` label) [ [1](https://github.com/joelparkerhenderson/architecture-decision-record) [2](https://cognitect.com/blog/2011/11/15/documenting-architecture-decisions) [3](https://adr.github.io/) ] - [x] (Optional) Changes have been linted locally with [golangci-lint](https://github.com/golangci/golangci-lint). (NOTE: We haven't turned on lint checks in the pipeline yet so linting may be hard if it shows a lot of lint errors in places that weren't touched by changes. Thus, linting is optional right now.) Co-authored-by: Jon Perry <[email protected]> Co-authored-by: Megamind <[email protected]>
- Loading branch information
Showing
33 changed files
with
360 additions
and
125 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
27 changes: 27 additions & 0 deletions
27
docs/4-user-guide/1-the-zarf-cli/100-cli-commands/zarf_tools_clear-cache.md
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,27 @@ | ||
## zarf tools clear-cache | ||
|
||
Clears the configured git and image cache directory | ||
|
||
``` | ||
zarf tools clear-cache [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for clear-cache | ||
--zarf-cache string Specify the location of the Zarf artifact cache (images and git repositories) (default "~/.zarf-cache") | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-a, --architecture string Architecture for OCI images | ||
-l, --log-level string Log level when running Zarf. Valid options are: warn, info, debug, trace | ||
--no-progress Disable fancy UI progress bars, spinners, logos, etc. | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [zarf tools](zarf_tools.md) - Collection of additional tools to make airgap easier | ||
|
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 |
---|---|---|
|
@@ -17,3 +17,5 @@ components: | |
- https://github.com/stefanprodan/podinfo.git | ||
# Clone an azure repo that breaks in go-git and has to fall back to the host git | ||
- https://[email protected]/me0515/zarf-public-test/_git/zarf-public-test | ||
# Clone an azure repo (w/SHA) that breaks in go-git and has to fall back to the host git | ||
- https://[email protected]/me0515/zarf-public-test/_git/zarf-public-test@524980951ff16e19dc25232e9aea8fd693989ba6 |
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,17 @@ | ||
# Terraform | ||
This example demonstrates how to use Zarf to execute Terraform code to create an S3 bucket. | ||
|
||
### Assumptions/Prereqs | ||
- The binaries in the Zarf package are for an M1 Mac only and will need to be changed for other architectures | ||
- The S3 bucket name will likely need to be changed as S3 bucket names must be globally unique | ||
- Your machine has a connection to an AWS instance and is authenticated with an AWS account | ||
|
||
### Steps | ||
|
||
No K8s cluster is necessary, just build with the package with: | ||
|
||
`zarf package create` | ||
|
||
And execute with: | ||
|
||
`zard package deploy <package_name>` |
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,17 @@ | ||
terraform { | ||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = "4.33.0" | ||
} | ||
} | ||
} | ||
|
||
provider "aws" { | ||
region = "us-east-1" | ||
} | ||
|
||
resource "aws_s3_bucket" "example-bucket" { | ||
# note this bucket name will need to be changed because s3 buckets must be globally unique | ||
bucket = "unclegedds-example-bucket-v3" | ||
} |
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,32 @@ | ||
kind: ZarfPackageConfig | ||
metadata: | ||
name: "terraform" | ||
description: "Run terraform/terragrunt code" | ||
|
||
components: | ||
- name: download-terraform | ||
required: true | ||
scripts: | ||
after: | ||
- "rm -f terraform" | ||
- "./zarf tools archiver decompress tmp/terraform_1.3.1_darwin_arm64.zip ." | ||
- "./zarf tools archiver decompress tmp/terraform-provider-aws_4.33.0_darwin_arm64.zip ./tf-plugins/registry.terraform.io/hashicorp/aws/4.33.0/darwin_arm64" | ||
- "rm -rf tmp" | ||
files: | ||
# terraform code | ||
- source: main.tf | ||
target: main.tf | ||
# mac m1 terraform aws provider binary | ||
- source: https://releases.hashicorp.com/terraform-provider-aws/4.33.0/terraform-provider-aws_4.33.0_darwin_arm64.zip | ||
target: tmp/terraform-provider-aws_4.33.0_darwin_arm64.zip | ||
# mac m1 terraform binary | ||
- source: https://releases.hashicorp.com/terraform/1.3.1/terraform_1.3.1_darwin_arm64.zip | ||
target: tmp/terraform_1.3.1_darwin_arm64.zip | ||
|
||
- name: execute-terraform | ||
scripts: | ||
timeoutSeconds: 60 | ||
showOutput: true | ||
before: | ||
- "./terraform init -plugin-dir=tf-plugins" | ||
- "./terraform apply -auto-approve" |
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,66 @@ | ||
package git | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/defenseunicorns/zarf/src/internal/message" | ||
"github.com/defenseunicorns/zarf/src/internal/utils" | ||
"github.com/go-git/go-git/v5" | ||
) | ||
|
||
// clone performs a `git clone` of a given repo. | ||
func clone(gitDirectory string, gitURL string, onlyFetchRef bool, spinner *message.Spinner) (*git.Repository, error) { | ||
cloneOptions := &git.CloneOptions{ | ||
URL: gitURL, | ||
Progress: spinner, | ||
RemoteName: onlineRemoteName, | ||
} | ||
|
||
if onlyFetchRef { | ||
cloneOptions.Tags = git.NoTags | ||
} | ||
|
||
gitCred := FindAuthForHost(gitURL) | ||
|
||
// Gracefully handle no git creds on the system (like our CI/CD) | ||
if gitCred.Auth.Username != "" { | ||
cloneOptions.Auth = &gitCred.Auth | ||
} | ||
|
||
// Clone the given repo | ||
repo, err := git.PlainClone(gitDirectory, false, cloneOptions) | ||
|
||
if errors.Is(err, git.ErrRepositoryAlreadyExists) { | ||
repo, err = git.PlainOpen(gitDirectory) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return repo, git.ErrRepositoryAlreadyExists | ||
} else if err != nil { | ||
spinner.Debugf("Failed to clone repo: %s", err) | ||
message.Infof("Falling back to host git for %s", gitURL) | ||
|
||
// If we can't clone with go-git, fallback to the host clone | ||
// Only support "all tags" due to the azure clone url format including a username | ||
cmdArgs := []string{"clone", "--origin", onlineRemoteName, gitURL, gitDirectory} | ||
|
||
if onlyFetchRef { | ||
cmdArgs = append(cmdArgs, "--no-tags") | ||
} | ||
|
||
stdOut, stdErr, err := utils.ExecCommandWithContext(context.TODO(), false, "git", cmdArgs...) | ||
spinner.Updatef(stdOut) | ||
spinner.Debugf(stdErr) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return git.PlainOpen(gitDirectory) | ||
} else { | ||
return repo, nil | ||
} | ||
} |
Oops, something went wrong.