Skip to content

Commit 9f9de1d

Browse files
Support targeting the root folder of the target GitOps directory using the prer tool (#190)
* make tests ran on macs where port 5000 is taken * gitops path root support * keep gitopsPath to cloud for backward compatibility --------- Co-authored-by: csinghal <[email protected]>
1 parent 83e80a5 commit 9f9de1d

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

create_kind_cluster.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@ set -o errexit
1313
# desired cluster name; default is "kind"
1414
KIND_CLUSTER_NAME="${KIND_CLUSTER_NAME:-kind}"
1515

16+
# kind executable
17+
go install sigs.k8s.io/[email protected]
18+
kind="$(go env GOPATH)/bin/kind"
19+
which "${kind}"
20+
1621
# create registry container unless it already exists
1722
reg_name='kind-registry'
18-
reg_port='5000'
23+
reg_port='15000'
1924
running="$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)"
2025
if [ "${running}" != 'true' ]; then
2126
docker container rm "${reg_name}" 2>/dev/null || true
2227
docker run \
23-
-d --restart=always -p "${reg_port}:5000" --name "${reg_name}" \
24-
registry:2
28+
-d --restart=always -e "REGISTRY_HTTP_ADDR=0.0.0.0:${reg_port}" -p "${reg_port}:${reg_port}" --name "${reg_name}" \
29+
registry:3
2530
fi
2631

2732
# create a cluster with the local registry enabled in containerd
28-
cat <<EOF | kind create cluster --name "${KIND_CLUSTER_NAME}" --image "kindest/node:v1.30.13" --config=-
33+
cat <<EOF | "${kind}" create cluster --name "${KIND_CLUSTER_NAME}" --image "kindest/node:v1.30.13" --config=-
2934
kind: Cluster
3035
apiVersion: kind.x-k8s.io/v1alpha4
3136
containerdConfigPatches:

e2e_test.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ bindir=$(cd `dirname "$0"` && pwd)
1717
repo_path=$bindir
1818
cd $repo_path
1919

20-
#check installs
20+
# Check prerequisites
2121
bazel version
2222
docker version
2323
which kubectl
2424
go version
2525

26-
go get sigs.k8s.io/[email protected]
27-
2826
cluster_running="$(docker inspect -f '{{.State.Running}}' kind-control-plane 2>/dev/null || true)"
2927
if [ "${cluster_running}" != 'true' ]; then
3028
./create_kind_cluster.sh

examples/helloworld/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ CLUSTER = "kind-kind"
4545

4646
USER = "kind-kind"
4747

48-
REGISTRY = "localhost:5000"
48+
REGISTRY = "localhost:15000"
4949

5050
k8s_deploy(
5151
name = "mynamespace",

examples/helloworld/k8s_deploy_test.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cat mynamespace.show
3131
grep -F "kind: Deployment" mynamespace.show
3232
grep -F "kind: Service" mynamespace.show
3333
grep -F "name: helloworld" mynamespace.show
34-
grep -E "image: localhost:5000/.*/helloworld/image@sha256" mynamespace.show
34+
grep -E "image: localhost:15000/.*/helloworld/image@sha256" mynamespace.show
3535
grep -E "app_label_image_short_digest" mynamespace.show | grep -v -F 'image.short-digest'
3636

3737
$(rlocation examples/helloworld/canary.show) > canary.show
@@ -42,7 +42,7 @@ grep -F "kind: Deployment" canary.show
4242
grep -F "kind: Service" canary.show
4343
grep -F "namespace: $NAMESPACE" canary.show
4444
grep -F "name: helloworld-canary" canary.show
45-
grep -E "image: localhost:5000/k8s/helloworld/image@sha256" canary.show
45+
grep -E "image: localhost:15000/k8s/helloworld/image@sha256" canary.show
4646

4747
$(rlocation examples/helloworld/release.show) > release.show
4848
echo "DEBUG: release.show:"
@@ -52,4 +52,4 @@ grep -F "kind: Deployment" release.show
5252
grep -F "kind: Service" release.show
5353
grep -F "namespace: $NAMESPACE" canary.show
5454
grep -F "name: helloworld" mynamespace.show
55-
grep -E "image: localhost:5000/k8s/helloworld/image@sha256" release.show
55+
grep -E "image: localhost:15000/k8s/helloworld/image@sha256" release.show

gitops/git/git.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ func Clone(repo, dir, mirrorDir, primaryBranch, gitopsPath string) (*Repo, error
4545
}
4646
args = append(args, repo, dir)
4747
exec.Mustex("", "git", args...)
48-
exec.Mustex(dir, "git", "config", "--local", "core.sparsecheckout", "true")
49-
genPath := fmt.Sprintf("%s/\n", gitopsPath)
50-
if err := ioutil.WriteFile(filepath.Join(dir, ".git/info/sparse-checkout"), []byte(genPath), 0644); err != nil {
51-
return nil, fmt.Errorf("Unable to create .git/info/sparse-checkout: %w", err)
48+
// Enable sparse-checkout when restricting to a subdir
49+
if !isRootPath(gitopsPath) {
50+
exec.Mustex(dir, "git", "config", "--local", "core.sparsecheckout", "true")
51+
genPath := fmt.Sprintf("%s/\n", gitopsPath)
52+
if err := ioutil.WriteFile(filepath.Join(dir, ".git/info/sparse-checkout"), []byte(genPath), 0644); err != nil {
53+
return nil, fmt.Errorf("Unable to create .git/info/sparse-checkout: %w", err)
54+
}
5255
}
5356
exec.Mustex(dir, "git", "checkout", primaryBranch)
5457
return &Repo{
@@ -108,7 +111,11 @@ func (r *Repo) GetLastCommitMessage() (msg string) {
108111

109112
// Commit all changes to the current branch. returns true if there were any changes
110113
func (r *Repo) Commit(message, gitopsPath string) bool {
111-
exec.Mustex(r.Dir, "git", "add", gitopsPath)
114+
if isRootPath(gitopsPath) {
115+
exec.Mustex(r.Dir, "git", "add", ".")
116+
} else {
117+
exec.Mustex(r.Dir, "git", "add", gitopsPath)
118+
}
112119
if r.IsClean() {
113120
return false
114121
}
@@ -155,3 +162,8 @@ func (r *Repo) Push(branches []string) {
155162
args := append([]string{"push", r.RemoteName, "-f", "--set-upstream"}, branches...)
156163
exec.Mustex(r.Dir, "git", args...)
157164
}
165+
166+
// isRootPath is an internal helper to detect "full repo" case.
167+
func isRootPath(gitopsPath string) bool {
168+
return gitopsPath == "" || gitopsPath == "."
169+
}

0 commit comments

Comments
 (0)