Skip to content

Commit 3048457

Browse files
authored
Merge pull request #104 from SomtochiAma/git-repository
Base git repository working
2 parents 1d1fd97 + 238d579 commit 3048457

File tree

6 files changed

+222
-2
lines changed

6 files changed

+222
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.13
55
require (
66
github.com/blang/semver v3.5.0+incompatible
77
github.com/evanphx/json-patch v4.5.0+incompatible
8+
github.com/go-git/go-git/v5 v5.1.0
89
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
910
github.com/pkg/errors v0.8.1
1011
github.com/prometheus/client_golang v1.0.0

go.sum

Lines changed: 48 additions & 0 deletions
Large diffs are not rendered by default.

pkg/patterns/addon/pkg/loaders/fs.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import (
2020
"context"
2121
"flag"
2222
"fmt"
23-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2423
"strings"
2524

25+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26+
2627
"k8s.io/apimachinery/pkg/runtime"
2728
"sigs.k8s.io/controller-runtime/pkg/log"
2829
addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1"
@@ -47,6 +48,11 @@ func NewManifestLoader(channel string) (*ManifestLoader, error) {
4748
return &ManifestLoader{repo: repo}, nil
4849
}
4950

51+
if strings.Contains(channel, "git//") || strings.Contains(channel, ".git") {
52+
repo := NewGitRepository(channel)
53+
return &ManifestLoader{repo: repo}, nil
54+
}
55+
5056
repo := NewFSRepository(channel)
5157
return &ManifestLoader{repo: repo}, nil
5258
}

pkg/patterns/addon/pkg/loaders/git.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package loaders
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io/ioutil"
7+
"path"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/go-git/go-git/v5"
12+
"sigs.k8s.io/controller-runtime/pkg/log"
13+
"sigs.k8s.io/yaml"
14+
)
15+
16+
type GitRepository struct {
17+
baseURL string
18+
subDir string
19+
branch string
20+
}
21+
22+
var _ Repository = &GitRepository{}
23+
24+
// NewGitRepository constructs an GitRepository
25+
func NewGitRepository(baseurl string) *GitRepository {
26+
repo := parseGitURL(baseurl)
27+
return &repo
28+
}
29+
30+
func (r *GitRepository) LoadChannel(ctx context.Context, name string) (*Channel, error) {
31+
if !allowedChannelName(name) {
32+
return nil, fmt.Errorf("invalid channel name: %q", name)
33+
}
34+
35+
log := log.Log
36+
log.WithValues("baseURL", r.baseURL).Info("loading channel")
37+
log.WithValues("baseURL", r.baseURL).Info("cloning git repository")
38+
39+
if r.subDir != "" {
40+
name = r.subDir + "/" + name
41+
}
42+
b, err := r.readURL(name)
43+
if err != nil {
44+
log.WithValues("path", name).Error(err, "error reading channel")
45+
return nil, err
46+
}
47+
48+
channel := &Channel{}
49+
if err := yaml.Unmarshal(b, channel); err != nil {
50+
return nil, fmt.Errorf("error parsing channel bytes %s: %v", string(b), err)
51+
}
52+
53+
return channel, nil
54+
}
55+
56+
func (r *GitRepository) LoadManifest(ctx context.Context, packageName string, id string) (map[string]string, error) {
57+
if !allowedManifestId(packageName) {
58+
return nil, fmt.Errorf("invalid package name: %q", id)
59+
}
60+
61+
if !allowedManifestId(id) {
62+
return nil, fmt.Errorf("invalid manifest id: %q", id)
63+
}
64+
65+
log := log.Log
66+
log.WithValues("package", packageName).Info("loading package")
67+
68+
var filePath string
69+
if r.subDir == "" {
70+
filePath = path.Join("packages", packageName, id, "manifest.yaml")
71+
} else {
72+
filePath = path.Join(r.subDir, "packages", packageName, id, "manifest.yaml")
73+
}
74+
75+
b, err := r.readURL(filePath)
76+
77+
if err != nil {
78+
return nil, fmt.Errorf("error reading package %s: %v", filePath, err)
79+
}
80+
result := map[string]string{
81+
filePath: string(b),
82+
}
83+
84+
return result, nil
85+
}
86+
87+
func (r *GitRepository) readURL(url string) ([]byte, error) {
88+
repoDir := "/tmp/repo"
89+
filePath := filepath.Join(repoDir, url)
90+
fmt.Println(r.baseURL)
91+
_, err := git.PlainClone(repoDir, false, &git.CloneOptions{
92+
URL: r.baseURL,
93+
})
94+
if err != nil && err != git.ErrRepositoryAlreadyExists {
95+
return nil, err
96+
}
97+
98+
b, err := ioutil.ReadFile(filePath)
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
return b, nil
104+
}
105+
106+
func parseGitURL(url string) GitRepository {
107+
// checks for git:: suffix
108+
var subdir string
109+
if strings.HasPrefix(url, "git::") {
110+
url = strings.TrimPrefix(url, "git::")
111+
}
112+
113+
// checks for subdirectories
114+
if strings.Contains(url, ".git//") {
115+
urlComponent := strings.SplitN(url, ".git//", 2)
116+
url = urlComponent[0] + ".git"
117+
subdir = urlComponent[1]
118+
}
119+
120+
return GitRepository{
121+
baseURL: url,
122+
subDir: subdir,
123+
}
124+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package loaders
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestParseGitURL(t *testing.T) {
8+
tests := []struct {
9+
rawURL string
10+
baseURL string
11+
subDir string
12+
}{
13+
{
14+
rawURL: "https://github.com/testRepository.git",
15+
baseURL: "https://github.com/testRepository.git",
16+
subDir: "",
17+
},
18+
{
19+
rawURL: "git::https://github.com/testRepository.git",
20+
baseURL: "https://github.com/testRepository.git",
21+
subDir: "",
22+
},
23+
{
24+
rawURL: "git::https://github.com/testRepository.git//subDir/package",
25+
baseURL: "https://github.com/testRepository.git",
26+
subDir: "subDir/package",
27+
},
28+
}
29+
30+
for _, tt := range tests {
31+
gitRepo := parseGitURL(tt.rawURL)
32+
if gitRepo.baseURL != tt.baseURL {
33+
t.Errorf("Expected base url: %v, got %v", tt.baseURL, gitRepo.baseURL)
34+
}
35+
36+
if gitRepo.subDir != tt.subDir {
37+
t.Errorf("Expected base url: %v, got %v", tt.subDir, gitRepo.subDir)
38+
}
39+
}
40+
}

pkg/patterns/addon/pkg/status/aggregate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ package status
1919
import (
2020
"context"
2121
"fmt"
22-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2322
"reflect"
2423

24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
2526
appsv1 "k8s.io/api/apps/v1"
2627
corev1 "k8s.io/api/core/v1"
2728
"sigs.k8s.io/controller-runtime/pkg/client"

0 commit comments

Comments
 (0)