Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 6604de7

Browse files
committed
adding more refactoring and unit tests
1 parent c64eeae commit 6604de7

File tree

6 files changed

+447
-255
lines changed

6 files changed

+447
-255
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ CAPI_KIND_CLUSTER_NAME ?= capi-test
231231
# It is set by Prow GIT_TAG, a git-based tag of the form vYYYYMMDD-hash, e.g., v20210120-v0.3.10-308-gc61521971
232232

233233
# Next release is: v0.3.2
234-
TAG ?= v0.3.2-preview.47
234+
TAG ?= v0.3.2-preview.53
235235
ARCH ?= $(shell go env GOARCH)
236236
ALL_ARCH = amd64 arm arm64
237237

config/default/manager_image_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ spec:
77
template:
88
spec:
99
containers:
10-
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v0.3.2-preview.47
10+
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v0.3.2-preview.53
1111
name: manager

config/default/manager_image_patch.yaml-e

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ spec:
77
template:
88
spec:
99
containers:
10-
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v0.3.2-preview.47
10+
- image: ghcr.io/patricklaabs/cluster-api-addon-provider-cdk8s/cluster-api-cdk8s-controller:v0.3.2-preview.53
1111
name: manager

controllers/cdk8sappproxy/cdk8sappproxy_git_operator.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ func (r *Reconciler) prepareSource(ctx context.Context, cdk8sAppProxy *addonsv1a
3030
return "", "", err
3131
}
3232

33-
retrieveCommitHash, err := gitImpl.Clone(gitSpec.URL, tempDir, &buf)
33+
err = gitImpl.Clone(gitSpec.URL, tempDir, &buf)
3434
if err != nil {
35-
logger.Error(err, addonsv1alpha1.GitCloneFailedCondition, "Failed to clone git repository", "operation", operation)
35+
logger.Error(err, addonsv1alpha1.GitCloneFailedCondition, "Failed to clone git repository")
36+
}
37+
38+
retrieveCommitHash, err := gitImpl.Hash(tempDir)
39+
if err != nil {
40+
logger.Error(err, addonsv1alpha1.GitHashFailureReason, "Failed to get local git hash")
3641
}
3742

3843
if operation == OperationNormal {

controllers/cdk8sappproxy/git/git.go

Lines changed: 85 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ import (
1010
"net/url"
1111
)
1212

13+
// GitOperator defines the interface for git operations.
1314
type GitOperator interface {
14-
Clone(repoUrl string, directory string, writer *bytes.Buffer) (currentCommitHash string, err error)
15-
Poll(repoUrl string, branch string, directory string, writer *bytes.Buffer) (detectedChanges bool, err error)
15+
Clone(repoUrl string, directory string, writer *bytes.Buffer) (err error)
16+
Poll(repo string, branch string, directory string, writer *bytes.Buffer) (changes bool, err error)
17+
Hash(repo string) (hash string, err error)
1618
}
1719

20+
// GitImplementer implements the GitOperator interface.
1821
type GitImplementer struct{}
1922

2023
// Clone clones the given repository to a local directory.
21-
func (g *GitImplementer) Clone(repoUrl string, directory string, writer *bytes.Buffer) (currentCommitHash string, err error) {
22-
validationBuffer := new(bytes.Buffer)
23-
currentCommitHash = "0"
24-
25-
err = validateRepoUrl(repoUrl, validationBuffer)
26-
if err != nil {
27-
fmt.Fprintf(writer, "%s", addonsv1alpha1.InvalidGitRepositoryReason)
24+
func (g *GitImplementer) Clone(repoUrl string, directory string, writer *bytes.Buffer) (err error) {
25+
// Check if repo and directory are empty.
26+
if empty(repoUrl, directory) {
27+
fmt.Fprintf(writer, "%s", addonsv1alpha1.EmptyGitRepositoryReason)
2828

29-
return currentCommitHash, err
29+
return fmt.Errorf("%s", addonsv1alpha1.EmptyGitRepositoryReason)
3030
}
3131

3232
_, err = git.PlainClone(directory, false, &git.CloneOptions{
@@ -35,136 +35,122 @@ func (g *GitImplementer) Clone(repoUrl string, directory string, writer *bytes.B
3535
if err != nil {
3636
fmt.Fprintf(writer, addonsv1alpha1.GitCloneFailedCondition)
3737

38-
return currentCommitHash, err
39-
}
40-
41-
localGitHashBuffer := new(bytes.Buffer)
42-
currentCommitHash, err = localGitHash(directory, localGitHashBuffer)
43-
if err != nil {
44-
fmt.Fprintf(writer, addonsv1alpha1.ClusterSelectorParseFailedReason)
45-
46-
return currentCommitHash, err
38+
return err
4739
}
4840

49-
fmt.Fprintf(writer, addonsv1alpha1.GitCloneSuccessCondition)
50-
51-
return currentCommitHash, err
41+
return err
5242
}
5343

54-
// Poll polls for changes for the given remote git repository.
55-
// returns true, if current local commit hash and remote hash are not equal.
56-
func (g *GitImplementer) Poll(repoUrl string, branch string, directory string, writer *bytes.Buffer) (detectedChanges bool, err error) {
44+
// Poll polls for changes for the given remote git repository. Returns true, if current local commit hash and remote hash are not equal.
45+
func (g *GitImplementer) Poll(repo string, branch string, directory string, writer *bytes.Buffer) (changes bool, err error) {
5746
// Defaults to false. We only change to true if there is a difference between the hashes.
58-
detectedChanges = false
59-
60-
err = validateRepoUrl(repoUrl, writer)
61-
if err != nil {
62-
fmt.Fprintf(writer, "%s", addonsv1alpha1.InvalidGitRepositoryReason)
47+
changes = false
6348

64-
return detectedChanges, err
65-
}
66-
if directory == "" {
49+
// Check if repo and directory are empty.
50+
if empty(repo, directory) {
6751
fmt.Fprintf(writer, "%s", addonsv1alpha1.EmptyGitRepositoryReason)
6852

69-
return detectedChanges, err
53+
return changes, fmt.Errorf("%s", addonsv1alpha1.EmptyGitRepositoryReason)
7054
}
7155

72-
// get hash from local repo
73-
localHash, err := localGitHash(directory, writer)
56+
// Get hash from local repo.
57+
localHash, err := g.Hash(directory)
7458
if err != nil {
75-
//fmt.Fprintf(writer, "%s", addonsv1alpha1.GitHashFailureReason)
7659
fmt.Fprintf(writer, "localGitHash error")
7760

78-
return detectedChanges, err
61+
return changes, err
7962
}
8063

8164
// Get Hash from remote repo
82-
remoteHash, err := remoteGitHash(repoUrl, branch, writer)
65+
remoteHash, err := g.Hash(repo)
8366
if err != nil {
84-
//fmt.Fprintf(writer, "%s", addonsv1alpha1.GitHashFailureReason)
8567
fmt.Fprintf(writer, "remoteGitHash error")
8668

87-
return detectedChanges, err
69+
return changes, err
8870
}
8971

9072
if localHash != remoteHash {
91-
detectedChanges = true
73+
changes = true
9274
}
9375

9476
fmt.Fprintf(writer, "%s", addonsv1alpha1.GitHashSuccessReason)
9577

96-
return detectedChanges, err
78+
return changes, err
9779
}
9880

99-
// validateRepoUrl validates the given repoUrl.
100-
func validateRepoUrl(repoUrl string, writer *bytes.Buffer) (err error) {
101-
// Checking, if repoUrl is empty
102-
if repoUrl == "" {
103-
fmt.Fprintf(writer, "%s", addonsv1alpha1.EmptyGitRepositoryReason)
104-
105-
return fmt.Errorf("%s", addonsv1alpha1.EmptyGitRepositoryReason)
106-
}
107-
108-
_, err = url.ParseRequestURI(repoUrl)
109-
if err != nil {
110-
fmt.Fprintf(writer, "%s", addonsv1alpha1.InvalidGitRepositoryReason)
111-
112-
return fmt.Errorf("%s", addonsv1alpha1.InvalidGitRepositoryReason)
113-
}
81+
// Hash retrieves the hash of the given repository.
82+
func (g *GitImplementer) Hash(repo string) (hash string, err error) {
83+
//cdk8sAppProxy := &addonsv1alpha1.Cdk8sAppProxy{}
84+
//branch := cdk8sAppProxy.Spec.GitRepository.Reference
85+
branch := "main"
86+
87+
switch {
88+
case isUrl(repo):
89+
remoterepo := git.NewRemote(nil, &config.RemoteConfig{
90+
URLs: []string{repo},
91+
Name: "origin",
92+
})
93+
94+
refs, err := remoterepo.List(&git.ListOptions{})
95+
if err != nil {
96+
//return hash, fmt.Errorf("%s", addonsv1alpha1.GitHashFailureReason)
97+
return hash, fmt.Errorf("failed to list remote refs: %v", err)
98+
}
11499

115-
return err
116-
}
100+
refName := plumbing.NewBranchReferenceName(branch)
101+
for _, ref := range refs {
102+
if ref.Name() == refName {
103+
return ref.Hash().String(), nil
104+
}
105+
}
117106

118-
// localGitHash checks the current git hash for the given local repository.
119-
func localGitHash(directory string, writer *bytes.Buffer) (hash string, err error) {
120-
hash = "0"
121-
repo, err := git.PlainOpen(directory)
122-
if err != nil {
123107
//return hash, fmt.Errorf("%s", addonsv1alpha1.GitHashFailureReason)
124-
return hash, fmt.Errorf("failed to open local git repository: %v", err)
125-
}
108+
return hash, fmt.Errorf("failed to find remote ref for branch %s: %v", branch, refs)
109+
case !isUrl(repo):
110+
localRepo, err := git.PlainOpen(repo)
111+
if err != nil {
112+
//return hash, fmt.Errorf("%s", addonsv1alpha1.GitHashFailureReason)
113+
return hash, fmt.Errorf("failed to open local git repository: %v", err)
114+
}
126115

127-
headRef, err := repo.Head()
128-
if err != nil {
129-
//return hash, fmt.Errorf("%s", addonsv1alpha1.GitHashFailureReason)
130-
return hash, fmt.Errorf("failed to get head for local git repo: %v", err)
131-
}
116+
headRef, err := localRepo.Head()
117+
if err != nil {
118+
//return hash, fmt.Errorf("%s", addonsv1alpha1.GitHashFailureReason)
119+
return hash, fmt.Errorf("failed to get head for local git repo: %v", err)
120+
}
132121

133-
hash = headRef.Hash().String()
134-
if hash == "" {
135-
//return hash, fmt.Errorf("%s", addonsv1alpha1.GitHashFailureReason)
136-
return hash, fmt.Errorf("failed to retrieve hash for local git repo")
137-
}
122+
hash = headRef.Hash().String()
123+
if hash == "" {
124+
//return hash, fmt.Errorf("%s", addonsv1alpha1.GitHashFailureReason)
125+
return hash, fmt.Errorf("failed to retrieve hash for local git repo")
126+
}
138127

128+
return hash, err
129+
}
139130
return hash, err
140131
}
141132

142-
// remoteGitHash checks the git hash for the given remote repository.
143-
func remoteGitHash(repoUrl string, branch string, writer *bytes.Buffer) (hash string, err error) {
144-
// validates the given remote repository url
145-
err = validateRepoUrl(repoUrl, nil)
146-
if err != nil {
147-
return hash, err
133+
// isUrl checks if the given string is a valid URL.
134+
func isUrl(repo string) bool {
135+
if repo == "" {
136+
return false
148137
}
149-
150-
repo := git.NewRemote(nil, &config.RemoteConfig{
151-
URLs: []string{repoUrl},
152-
Name: "origin",
153-
})
154-
155-
refs, err := repo.List(&git.ListOptions{})
138+
parsedUrl, err := url.ParseRequestURI(repo)
156139
if err != nil {
157-
//return hash, fmt.Errorf("%s", addonsv1alpha1.GitHashFailureReason)
158-
return hash, fmt.Errorf("failed to list remote refs: %v", err)
140+
return false
159141
}
160142

161-
refName := plumbing.NewBranchReferenceName(branch)
162-
for _, ref := range refs {
163-
if ref.Name() == refName {
164-
return ref.Hash().String(), nil
165-
}
143+
if parsedUrl.Scheme != "" {
144+
return true
145+
} else {
146+
return false
166147
}
148+
}
167149

168-
//return hash, fmt.Errorf("%s", addonsv1alpha1.GitHashFailureReason)
169-
return hash, fmt.Errorf("failed to find remote ref for branch %s: %v", branch, refs)
150+
// empty checks if the repo and directory strings are empty.
151+
func empty(repo string, directory string) bool {
152+
if repo == "" || directory == "" {
153+
return true
154+
}
155+
return false
170156
}

0 commit comments

Comments
 (0)