Skip to content

Commit f018370

Browse files
committed
Add kustomize build {repoUrl}
1 parent c9a8bc1 commit f018370

File tree

9 files changed

+146
-539
lines changed

9 files changed

+146
-539
lines changed

pkg/commands/build.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package commands
1818

1919
import (
2020
"io"
21-
"path/filepath"
2221

2322
"github.com/spf13/cobra"
2423

@@ -75,17 +74,11 @@ func (o *buildOptions) Validate(args []string) error {
7574

7675
// RunBuild runs build command.
7776
func (o *buildOptions) RunBuild(out io.Writer, fSys fs.FileSystem) error {
78-
l := loader.NewFileLoader(fSys)
79-
80-
absPath, err := filepath.Abs(o.kustomizationPath)
81-
if err != nil {
82-
return err
83-
}
84-
85-
rootLoader, err := l.New(absPath)
77+
rootLoader, err := loader.NewLoader(o.kustomizationPath, fSys)
8678
if err != nil {
8779
return err
8880
}
81+
defer rootLoader.Cleanup()
8982

9083
application, err := app.NewApplication(rootLoader, fSys)
9184
if err != nil {

pkg/commands/diff.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package commands
1919
import (
2020
"errors"
2121
"io"
22-
"path/filepath"
2322

2423
"github.com/spf13/cobra"
2524

@@ -68,17 +67,11 @@ func (o *diffOptions) Validate(args []string) error {
6867
// RunDiff gets the differences between Application.MakeCustomizedResMap() and Application.MakeUncustomizedResMap().
6968
func (o *diffOptions) RunDiff(out, errOut io.Writer, fSys fs.FileSystem) error {
7069

71-
l := loader.NewFileLoader(fSys)
72-
73-
absPath, err := filepath.Abs(o.kustomizationPath)
74-
if err != nil {
75-
return err
76-
}
77-
78-
rootLoader, err := l.New(absPath)
70+
rootLoader, err := loader.NewLoader(o.kustomizationPath, fSys)
7971
if err != nil {
8072
return err
8173
}
74+
defer rootLoader.Cleanup()
8275

8376
application, err := app.NewApplication(rootLoader, fSys)
8477
if err != nil {

pkg/internal/loadertest/fakeloader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,8 @@ func (f FakeLoader) New(newRoot string) (loader.Loader, error) {
6363
func (f FakeLoader) Load(location string) ([]byte, error) {
6464
return f.delegate.Load(location)
6565
}
66+
67+
// Cleanup does nothing
68+
func (f FakeLoader) Cleanup() error {
69+
return nil
70+
}

pkg/loader/fileloader.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func (l *fileLoader) Root() string {
5353
// Example: "/home/seans/project" or "/home/seans/project/"
5454
// NOT "/home/seans/project/file.yaml".
5555
func (l *fileLoader) New(newRoot string) (Loader, error) {
56+
if isRepoUrl(newRoot) {
57+
return newGithubLoader(newRoot, l.fSys)
58+
}
5659
if !l.IsAbsPath(l.root, newRoot) {
5760
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
5861
}
@@ -109,3 +112,8 @@ func (l *fileLoader) Load(location string) ([]byte, error) {
109112
}
110113
return l.fSys.ReadFile(fullLocation)
111114
}
115+
116+
// Cleanup does nothing
117+
func (l *fileLoader) Cleanup() error {
118+
return nil
119+
}

pkg/loader/githubloader.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package loader
18+
19+
import (
20+
"io/ioutil"
21+
"os"
22+
"path/filepath"
23+
"strings"
24+
25+
"github.com/hashicorp/go-getter"
26+
27+
"github.com/kubernetes-sigs/kustomize/pkg/fs"
28+
)
29+
30+
// githubLoader loads files from a checkout github repo
31+
type githubLoader struct {
32+
repo string
33+
checkoutDir string
34+
fSys fs.FileSystem
35+
loader *fileLoader
36+
}
37+
38+
// Root returns the root location for this Loader.
39+
func (l *githubLoader) Root() string {
40+
return l.checkoutDir
41+
}
42+
43+
// New delegates to fileLoader.New
44+
func (l *githubLoader) New(newRoot string) (Loader, error) {
45+
return l.loader.New(newRoot)
46+
}
47+
48+
// Load delegates to fileLoader.Load
49+
func (l *githubLoader) Load(location string) ([]byte, error) {
50+
return l.loader.Load(location)
51+
}
52+
53+
// Cleanup removes the checked out repo
54+
func (l *githubLoader) Cleanup() error {
55+
return os.RemoveAll(l.checkoutDir)
56+
}
57+
58+
// newGithubLoader returns a new fileLoader with given github Url.
59+
func newGithubLoader(repoUrl string, fs fs.FileSystem) (*githubLoader, error) {
60+
dir, err := ioutil.TempDir("", "kustomize-")
61+
if err != nil {
62+
return nil, err
63+
}
64+
target := filepath.Join(dir, "repo")
65+
err = checkout(repoUrl, target)
66+
if err != nil {
67+
return nil, err
68+
}
69+
l := newFileLoaderAtRoot(target, fs)
70+
return &githubLoader{
71+
repo: repoUrl,
72+
checkoutDir: target,
73+
fSys: fs,
74+
loader: l,
75+
}, nil
76+
}
77+
78+
// isRepoUrl checks if a string is a repo Url
79+
func isRepoUrl(s string) bool {
80+
return strings.Contains(s, ".com") || strings.Contains(s, ".org") || strings.Contains(s, "https://")
81+
}
82+
83+
// Checkout clones a github repo with specified commit/tag/branch
84+
func checkout(url, dir string) error {
85+
pwd, err := os.Getwd()
86+
if err != nil {
87+
return err
88+
}
89+
client := &getter.Client{
90+
Src: url,
91+
Dst: dir,
92+
Pwd: pwd,
93+
Mode: getter.ClientModeDir,
94+
}
95+
return client.Get()
96+
}

pkg/loader/loader.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ limitations under the License.
1717
// Package loader has a data loading interface and various implementations.
1818
package loader
1919

20+
import (
21+
"fmt"
22+
"path/filepath"
23+
24+
"github.com/kubernetes-sigs/kustomize/pkg/fs"
25+
)
26+
2027
// Loader interface exposes methods to read bytes.
2128
type Loader interface {
2229
// Root returns the root location for this Loader.
@@ -25,4 +32,29 @@ type Loader interface {
2532
New(newRoot string) (Loader, error)
2633
// Load returns the bytes read from the location or an error.
2734
Load(location string) ([]byte, error)
35+
// Cleanup cleans the loader
36+
Cleanup() error
37+
}
38+
39+
// NewLoader returns a Loader given a target
40+
// The target can be a local disk directory or a github Url
41+
func NewLoader(target string, fSys fs.FileSystem) (Loader, error) {
42+
if isRepoUrl(target) {
43+
return newGithubLoader(target, fSys)
44+
}
45+
46+
l := NewFileLoader(fSys)
47+
absPath, err := filepath.Abs(target)
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
if !l.IsAbsPath(l.root, absPath) {
53+
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, absPath)
54+
}
55+
root, err := l.fullLocation(l.root, absPath)
56+
if err != nil {
57+
return nil, err
58+
}
59+
return newFileLoaderAtRoot(root, l.fSys), nil
2860
}

vendor/github.com/hashicorp/go-getter/get.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)