Skip to content

Commit

Permalink
Have option to specify gitops path where to place generated files (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturo-skydio authored Mar 2, 2021
1 parent b7cd344 commit 3f0bfcb
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ When you run `bazel run ///helloworld:mynamespace.apply`, it applies this file i
| ***image_repository_prefix*** | `None` | Add a prefix to the image_repository. Can be used to upload the images in
| ***release_branch_prefix*** | `master` | A git branch name/prefix. Automatically run GitOps while building this branch. See [GitOps and Deployment](#gitops_and_deployment).
| ***deployment_branch*** | `None` | Automatic GitOps output will appear in a branch and PR with this name. See [GitOps and Deployment](#gitops_and_deployment).
| ***gitops_path*** | `cloud` | Path within the git repo where gitops files get generated into
| ***visibility*** | [Default_visibility](https://docs.bazel.build/versions/master/be/functions.html#package.default_visibility) | Changes the visibility of all rules generated by this macro. See [Bazel docs on visibility](https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes).

<a name="base-manifests-and-overlays"></a>
Expand Down
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
cloud/
custom_cloud/
7 changes: 6 additions & 1 deletion examples/e2e-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ rm -rf cloud

bazel run //helloworld:canary.gitops
bazel run //helloworld:release.gitops
bazel run //helloworld:gitops_custom_path.gitops
#the result of .gitops operation goes into /cloud directory and should be submitted back to the repo

#apply everything generated
kubectl apply -f cloud -R

#apply gitops_custom_path gen
kubectl apply -f custom_cloud -R

#wait for readiness
kubectl -n hwteam wait --timeout=60s --for=condition=Available deployment/helloworld deployment/helloworld-canary
kubectl -n hwteam wait --timeout=60s --for=condition=Available deployment/helloworld deployment/helloworld-canary deployment/helloworld-gitops-custom-path
20 changes: 20 additions & 0 deletions examples/helloworld/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ k8s_deploy(
user = USER,
)

k8s_deploy(
name = "gitops_custom_path",
cluster = CLUSTER,
deployment_branch = "helloworld-gitops-custom-path",
gitops_path = "custom_cloud",
image_digest_tag = True, # test optional image tagging
image_registry = REGISTRY, # override the default registry host for production
image_repository_prefix = "k8s",
images = {
"helloworld-image": ":image",
},
manifests = [
"deployment.yaml",
"service.yaml",
],
name_suffix = "-gitops-custom-path",
namespace = NAMESPACE,
user = USER,
)

sh_test(
name = "k8s_deploy_test",
srcs = ["k8s_deploy_test.sh"],
Expand Down
9 changes: 5 additions & 4 deletions gitops/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
// repo: https://[email protected]/scm/tm/repo.git
// dir: /tmp/cloudrepo
// mirrorDir: optional (if not empty) local mirror of the repository
func Clone(repo, dir, mirrorDir, primaryBranch string) (*Repo, error) {
func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error) {
if err := os.RemoveAll(dir); err != nil {
return nil, fmt.Errorf("Unable to clone repo: %w", err)
}
Expand All @@ -42,7 +42,8 @@ func Clone(repo, dir, mirrorDir, primaryBranch string) (*Repo, error) {
exec.Mustex("", "git", "clone", "-n", repo, dir)
}
exec.Mustex(dir, "git", "config", "--local", "core.sparsecheckout", "true")
if err := ioutil.WriteFile(filepath.Join(dir, ".git/info/sparse-checkout"), []byte("cloud/\n"), 0644); err != nil {
genPath := fmt.Sprintf("%s/\n", gitopsPath)
if err := ioutil.WriteFile(filepath.Join(dir, ".git/info/sparse-checkout"), []byte(genPath), 0644); err != nil {
return nil, fmt.Errorf("Unable to create .git/info/sparse-checkout: %w", err)
}
exec.Mustex(dir, "git", "checkout", primaryBranch)
Expand Down Expand Up @@ -93,8 +94,8 @@ func (r *Repo) GetLastCommitMessage() (msg string) {
}

// Commit all changes to the current branch. returns true if there were any changes
func (r *Repo) Commit(message string) bool {
exec.Mustex(r.Dir, "git", "add", "cloud")
func (r *Repo) Commit(message, gitopsPath string) bool {
exec.Mustex(r.Dir, "git", "add", gitopsPath)
if r.IsClean() {
return false
}
Expand Down
5 changes: 3 additions & 2 deletions gitops/prer/create_gitops_prs.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
workspace = flag.String("workspace", "", "path to workspace root")
repo = flag.String("git_repo", "https://bitbucket.tubemogul.info/scm/tm/repo.git", "git repo location")
gitMirror = flag.String("git_mirror", "", "git mirror location, like /mnt/mirror/bitbucket.tubemogul.info/tm/repo.git for jenkins")
gitopsPath = flag.String("gitops_path", "cloud", "location to store files in repo.")
gitopsTmpDir = flag.String("gitops_tmpdir", os.TempDir(), "location to check out git tree with /cloud.")
target = flag.String("target", "//... except //experimental/...", "target to scan. Useful for debugging only")
pushParallelism = flag.Int("push_parallelism", 5, "Number of image pushes to perform concurrently")
Expand Down Expand Up @@ -116,7 +117,7 @@ func main() {
log.Fatalf("Unable to create tempdir in %s: %v", *gitopsTmpDir, err)
}
defer os.RemoveAll(gitopsdir)
workdir, err := git.Clone(*repo, gitopsdir, *gitMirror, *prInto)
workdir, err := git.Clone(*repo, gitopsdir, *gitMirror, *prInto, *gitopsPath)
if err != nil {
log.Fatalf("Unable to clone repo: %v", err)
}
Expand Down Expand Up @@ -149,7 +150,7 @@ func main() {
bin := bazel.TargetToExecutable(target)
exec.Mustex("", bin, "--nopush", "--nobazel", "--deployment_root", gitopsdir)
}
if workdir.Commit(fmt.Sprintf("GitOps for release branch %s from %s commit %s\n%s", *releaseBranch, *branchName, *gitCommit, commitmsg.Generate(targets))) {
if workdir.Commit(fmt.Sprintf("GitOps for release branch %s from %s commit %s\n%s", *releaseBranch, *branchName, *gitCommit, commitmsg.Generate(targets)), *gitopsPath) {
log.Println("branch", branch, "has changes, push is required")
updatedGitopsTargets = append(updatedGitopsTargets, targets...)
updatedGitopsBranches = append(updatedGitopsBranches, branch)
Expand Down
2 changes: 2 additions & 0 deletions skylib/k8s.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def k8s_deploy(
image_repository_prefix = None, # Mutually exclusive with 'image_repository'. Add a prefix to the repository name generated from the image bazel path
objects = [],
gitops = True, # make sure to use gitops = False to work with individual namespace. This option will be turned False if namespace is '{BUILD_USER}'
gitops_path = "cloud",
deployment_branch = None,
release_branch_prefix = "master",
flatten_manifest_directories = False,
Expand Down Expand Up @@ -258,6 +259,7 @@ def k8s_deploy(
srcs = [name],
cluster = cluster,
namespace = namespace,
gitops_path = gitops_path,
strip_prefixes = [
namespace + "-",
cluster + "-",
Expand Down
10 changes: 6 additions & 4 deletions skylib/kustomize/kustomize.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,15 @@ fi
if "{" in namespace:
fail("unable to gitops namespace with placeholders %s" % inattr.label) #mynamespace should not be gitopsed
for infile in inattr.files.to_list():
statements += ("echo $TARGET_DIR/cloud/{namespace}/{cluster}/{file}\n" +
"mkdir -p $TARGET_DIR/cloud/{namespace}/{cluster}\n" +
"echo '# GENERATED BY {rulename} -> {gitopsrulename}' > $TARGET_DIR/cloud/{namespace}/{cluster}/{file}\n" +
"{template_engine} --template={infile} --variable=NAMESPACE={namespace} --stamp_info_file={info_file} >> $TARGET_DIR/cloud/{namespace}/{cluster}/{file}\n").format(
statements += ("echo $TARGET_DIR/{gitops_path}/{namespace}/{cluster}/{file}\n" +
"mkdir -p $TARGET_DIR/{gitops_path}/{namespace}/{cluster}\n" +
"echo '# GENERATED BY {rulename} -> {gitopsrulename}' > $TARGET_DIR/{gitops_path}/{namespace}/{cluster}/{file}\n" +
"{template_engine} --template={infile} --variable=NAMESPACE={namespace} --stamp_info_file={info_file} >> $TARGET_DIR/{gitops_path}/{namespace}/{cluster}/{file}\n").format(
infile = infile.path,
rulename = inattr.label,
gitopsrulename = ctx.label,
namespace = namespace,
gitops_path = ctx.attr.gitops_path,
cluster = cluster,
file = _remove_prefixes(infile.path.split("/")[-1], strip_prefixes),
template_engine = "${RUNFILES}/%s" % _get_runfile_path(ctx, ctx.executable._template_engine),
Expand Down Expand Up @@ -483,6 +484,7 @@ gitops = rule(
"cluster": attr.string(mandatory = True),
"namespace": attr.string(mandatory = True),
"deployment_branch": attr.string(),
"gitops_path": attr.string(),
"release_branch_prefix": attr.string(),
"strip_prefixes": attr.string_list(),
"_info_file": attr.label(
Expand Down

0 comments on commit 3f0bfcb

Please sign in to comment.