Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.

Commit 0f25c42

Browse files
committed
add Jsonnet importer
1 parent 2eab4f4 commit 0f25c42

File tree

13 files changed

+280
-43
lines changed

13 files changed

+280
-43
lines changed

Gopkg.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@
6868

6969
[[constraint]]
7070
name = "github.com/google/go-jsonnet"
71-
version = "^0.10.0"
71+
version = "^0.11.2"

pkg/api/render.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func (app *App) decode(files []gh.File, vars templates.Variables) ([]runtime.Obj
3333
var objects []runtime.Object
3434

3535
for _, file := range files {
36+
log.WithFields(log.Fields{
37+
"Location": file.Location,
38+
}).Debug("decoding file")
39+
3640
switch {
3741
case strings.HasSuffix(file.Name(), ".yml"):
3842
fallthrough
@@ -97,19 +101,15 @@ func (app *App) decodeYAML(file gh.File, vars templates.Variables) ([]runtime.Ob
97101
func (app *App) decodeJsonnet(file gh.File, vars templates.Variables, all []gh.File) ([]runtime.Object, error) {
98102
var objects []runtime.Object
99103

100-
importer := new(jsonnet.MemoryImporter)
101-
importer.Data = make(map[string]string)
102-
for _, f := range all {
103-
importer.Data[f.Name()] = f.Content
104-
}
104+
importer := gh.NewJsonnetImporter(app.Clients.GitHub)
105105

106106
vm := jsonnet.MakeVM()
107107
vm.Importer(importer)
108108
for k, v := range vars {
109109
vm.ExtVar(k, v)
110110
}
111111

112-
docs, err := vm.EvaluateSnippetStream(file.Name(), file.Content)
112+
docs, err := vm.EvaluateSnippetStream(file.Location.String(), file.Content)
113113
if err != nil {
114114
return nil, errors.WithStack(err)
115115
}

pkg/api/render_test.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/rebuy-de/kubernetes-deployment/pkg/gh"
10+
fakeGH "github.com/rebuy-de/kubernetes-deployment/pkg/gh/fake"
1011
"github.com/rebuy-de/kubernetes-deployment/pkg/interceptors"
1112
"github.com/rebuy-de/kubernetes-deployment/pkg/templates"
1213
"github.com/rebuy-de/rebuy-go-sdk/testutil"
@@ -73,18 +74,43 @@ func TestDecode(t *testing.T) {
7374

7475
for _, tc := range cases {
7576
t.Run(tc.name, func(t *testing.T) {
76-
files := []gh.File{}
77+
files := fakeGH.Files{}
7778
for _, fname := range tc.files {
7879
files = append(files, gh.File{
79-
Path: fname,
80+
Location: &gh.Location{
81+
Owner: "rebuy-de",
82+
Repo: "test",
83+
Path: fname,
84+
Ref: "master",
85+
},
8086
Content: readFile(t, path.Join("test-fixtures", fname)),
8187
})
8288
}
8389

84-
app := App{Interceptors: &interceptors.Multi{}}
90+
github := &fakeGH.GitHub{
91+
"rebuy-de": fakeGH.Repos{
92+
"test": fakeGH.Branches{
93+
"master": fakeGH.Branch{
94+
Meta: gh.Branch{
95+
Name: "master",
96+
SHA: "5a5369823a2a9a6ad9c241b404be39f802d41d48",
97+
},
98+
Files: files,
99+
},
100+
},
101+
},
102+
}
103+
104+
app := App{
105+
Interceptors: &interceptors.Multi{},
106+
Clients: &Clients{
107+
GitHub: github,
108+
},
109+
}
110+
85111
objects, err := app.decode(files, vars)
86112
if err != nil {
87-
t.Fatal(err)
113+
t.Fatalf("%+v", err)
88114
}
89115
g := path.Join("test-fixtures", fmt.Sprintf("render-golden-%s.json", tc.name))
90116
testutil.AssertGoldenJSON(t, g, objects)

pkg/gh/client.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net/http"
77
"path"
8-
"regexp"
98
"strings"
109
"time"
1110

@@ -20,10 +19,6 @@ import (
2019
log "github.com/sirupsen/logrus"
2120
)
2221

23-
var (
24-
ContentLocationRE = regexp.MustCompile(`^github.com/([^/]+)/([^/]+)/(.*)$`)
25-
)
26-
2722
type Interface interface {
2823
GetBranch(location *Location) (*Branch, error)
2924
GetFile(location *Location) (File, error)
@@ -186,7 +181,7 @@ func (gh *API) GetFile(location *Location) (File, error) {
186181

187182
gh.statsd.Gauge("github.rate.remaining", resp.Rate.Remaining)
188183

189-
return File{location.Path, content}, nil
184+
return File{Location: location, Content: content}, nil
190185
}
191186

192187
func (gh *API) GetFiles(location *Location) ([]File, error) {
@@ -244,11 +239,16 @@ func (gh *API) GetFiles(location *Location) ([]File, error) {
244239
_, notAFile := err.(ErrNotAFile)
245240
if !notAFile {
246241
return nil, errors.Wrapf(err,
247-
"unable to decode file '%v'",
242+
"unable to download file '%v'",
248243
location)
249244
}
250245

246+
log.WithFields(log.Fields{
247+
"Location": file.Location,
248+
}).Debug("skipping path, because it is not a file")
249+
continue
251250
}
251+
252252
files = append(files, file)
253253
}
254254

pkg/gh/fake/impl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ func (d *GitHub) GetBranch(l *gh.Location) (*gh.Branch, error) {
5757

5858
func (d *GitHub) GetFile(l *gh.Location) (gh.File, error) {
5959
for _, file := range (*d)[l.Owner][l.Repo][l.Ref].Files {
60-
if file.Path == l.Path {
60+
if file.Location.Path == l.Path {
6161
return file, nil
6262
}
6363
}
64-
return gh.File{}, nil
64+
return gh.File{}, fmt.Errorf("File %s not found", l.String())
6565
}
6666

6767
func (d *GitHub) GetFiles(l *gh.Location) ([]gh.File, error) {
6868
var files []gh.File
6969

7070
for _, file := range (*d)[l.Owner][l.Repo][l.Ref].Files {
71-
dir, _ := path.Split("/" + file.Path)
71+
dir, _ := path.Split("/" + file.Location.Path)
7272
if path.Clean("/"+dir+"/") == path.Clean("/"+l.Path+"/") {
7373
files = append(files, file)
7474
}

pkg/gh/fake/package_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ var (
2222
"master": Branch{
2323
Meta: ExampleBranch,
2424
Files: Files{
25-
{Path: "deployments.yaml", Content: YAML([]string{"foo", "bar"})},
26-
{Path: "README.md", Content: "blubber"},
27-
{Path: "sub/foo.txt", Content: "bar"},
28-
{Path: "sub/bim.txt", Content: "baz"},
25+
{Location: &gh.Location{Path: "deployments.yaml"}, Content: YAML([]string{"foo", "bar"})},
26+
{Location: &gh.Location{Path: "README.md"}, Content: "blubber"},
27+
{Location: &gh.Location{Path: "sub/foo.txt"}, Content: "bar"},
28+
{Location: &gh.Location{Path: "sub/bim.txt"}, Content: "baz"},
2929
},
3030
},
3131
},
@@ -89,8 +89,8 @@ func TestGetFiles(t *testing.T) {
8989
}
9090

9191
expected := []gh.File{
92-
{Path: "deployments.yaml", Content: "- foo\n- bar\n"},
93-
{Path: "README.md", Content: "blubber"},
92+
{Location: &gh.Location{Path: "deployments.yaml"}, Content: "- foo\n- bar\n"},
93+
{Location: &gh.Location{Path: "README.md"}, Content: "blubber"},
9494
}
9595

9696
if !reflect.DeepEqual(files, expected) {
@@ -107,8 +107,8 @@ func TestGetSubdirectoryFiles(t *testing.T) {
107107
}
108108

109109
expected := []gh.File{
110-
{Path: "sub/foo.txt", Content: "bar"},
111-
{Path: "sub/bim.txt", Content: "baz"},
110+
{Location: &gh.Location{Path: "sub/foo.txt"}, Content: "bar"},
111+
{Location: &gh.Location{Path: "sub/bim.txt"}, Content: "baz"},
112112
}
113113

114114
if !reflect.DeepEqual(files, expected) {

pkg/gh/fake/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func ScanFiles(root string) Files {
3636
}
3737

3838
raw, err := ioutil.ReadFile(path)
39-
files = append(files, gh.File{Path: relPath, Content: string(raw)})
39+
files = append(files, gh.File{Location: &gh.Location{Path: relPath}, Content: string(raw)})
4040
return err
4141
})
4242
if err != nil {

pkg/gh/file.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package gh
33
import "path"
44

55
type File struct {
6-
Path string
7-
Content string
6+
Location *Location
7+
Content string
88
}
99

1010
func (f *File) Name() string {
11-
_, name := path.Split(f.Path)
11+
_, name := path.Split(f.Location.Path)
1212
return name
1313
}

pkg/gh/importer.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package gh
2+
3+
import (
4+
"path"
5+
"path/filepath"
6+
"strings"
7+
8+
jsonnet "github.com/google/go-jsonnet"
9+
)
10+
11+
type jsonnetImporter struct {
12+
client Interface
13+
}
14+
15+
func NewJsonnetImporter(client Interface) jsonnet.Importer {
16+
return &jsonnetImporter{
17+
client: client,
18+
}
19+
}
20+
21+
func (i *jsonnetImporter) Import(importedFrom, importedPath string) (contents jsonnet.Contents, foundAt string, err error) {
22+
fromLocation, err := NewLocation(importedFrom)
23+
if err != nil {
24+
return jsonnet.MakeContents(""), "", err
25+
}
26+
27+
var location *Location
28+
if strings.HasPrefix(importedPath, "github.com/") {
29+
// location constrution for absolute paths
30+
location, err = NewLocation(importedPath)
31+
if err != nil {
32+
return jsonnet.MakeContents(""), "", err
33+
}
34+
} else {
35+
// location constrution for relative paths
36+
fromDir := filepath.Dir(fromLocation.String())
37+
absolute := path.Clean(path.Join(fromDir, importedPath))
38+
39+
location, err = NewLocation(absolute)
40+
if err != nil {
41+
return jsonnet.MakeContents(""), "", err
42+
}
43+
}
44+
45+
// having no ref means same ref, if inside same repo and "master" if it is in another repo
46+
if location.Ref == "" {
47+
if location.Owner == fromLocation.Owner && location.Repo == fromLocation.Repo {
48+
location.Ref = fromLocation.Ref
49+
} else {
50+
location.Ref = "master"
51+
}
52+
53+
}
54+
55+
file, err := i.client.GetFile(location)
56+
if err != nil {
57+
return jsonnet.MakeContents(""), "", err
58+
}
59+
60+
return jsonnet.MakeContents(file.Content), location.String(), nil
61+
}

0 commit comments

Comments
 (0)