Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kustomize build {repoUrl} #260

Merged
merged 2 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions pkg/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package commands

import (
"io"
"path/filepath"

"github.com/spf13/cobra"

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

// RunBuild runs build command.
func (o *buildOptions) RunBuild(out io.Writer, fSys fs.FileSystem) error {
l := loader.NewFileLoader(fSys)

absPath, err := filepath.Abs(o.kustomizationPath)
if err != nil {
return err
}

rootLoader, err := l.New(absPath)
rootLoader, err := loader.NewLoader(o.kustomizationPath, "", fSys)
if err != nil {
return err
}
defer rootLoader.Cleanup()

application, err := app.NewApplication(rootLoader, fSys)
if err != nil {
Expand Down
11 changes: 2 additions & 9 deletions pkg/commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package commands
import (
"errors"
"io"
"path/filepath"

"github.com/spf13/cobra"

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

l := loader.NewFileLoader(fSys)

absPath, err := filepath.Abs(o.kustomizationPath)
if err != nil {
return err
}

rootLoader, err := l.New(absPath)
rootLoader, err := loader.NewLoader(o.kustomizationPath, "", fSys)
if err != nil {
return err
}
defer rootLoader.Cleanup()

application, err := app.NewApplication(rootLoader, fSys)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/internal/loadertest/fakeloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ func (f FakeLoader) New(newRoot string) (loader.Loader, error) {
func (f FakeLoader) Load(location string) ([]byte, error) {
return f.delegate.Load(location)
}

// Cleanup does nothing
func (f FakeLoader) Cleanup() error {
return nil
}
14 changes: 6 additions & 8 deletions pkg/loader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ func (l *fileLoader) Root() string {
// Example: "/home/seans/project" or "/home/seans/project/"
// NOT "/home/seans/project/file.yaml".
func (l *fileLoader) New(newRoot string) (Loader, error) {
if !l.IsAbsPath(l.root, newRoot) {
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
}
root, err := l.fullLocation(l.root, newRoot)
if err != nil {
return nil, err
}
return newFileLoaderAtRoot(root, l.fSys), nil
return NewLoader(newRoot, l.root, l.fSys)
}

// IsAbsPath return true if the location calculated with the root
Expand Down Expand Up @@ -109,3 +102,8 @@ func (l *fileLoader) Load(location string) ([]byte, error) {
}
return l.fSys.ReadFile(fullLocation)
}

// Cleanup does nothing
func (l *fileLoader) Cleanup() error {
return nil
}
2 changes: 1 addition & 1 deletion pkg/loader/fileloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestLoader_Root(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected in New(): %v\n", err)
}
if "/home/seans/project/" != loader.Root() {
if "/home/seans/project" != loader.Root() {
t.Fatalf("Incorrect Loader Root: %s\n", loader.Root())
}

Expand Down
94 changes: 94 additions & 0 deletions pkg/loader/githubloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package loader

import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/hashicorp/go-getter"

"github.com/kubernetes-sigs/kustomize/pkg/fs"
)

// githubLoader loads files from a checkout github repo
type githubLoader struct {
repo string
checkoutDir string
loader *fileLoader
}

// Root returns the root location for this Loader.
func (l *githubLoader) Root() string {
return l.checkoutDir
}

// New delegates to fileLoader.New
func (l *githubLoader) New(newRoot string) (Loader, error) {
return l.loader.New(newRoot)
}

// Load delegates to fileLoader.Load
func (l *githubLoader) Load(location string) ([]byte, error) {
return l.loader.Load(location)
}

// Cleanup removes the checked out repo
func (l *githubLoader) Cleanup() error {
return os.RemoveAll(l.checkoutDir)
}

// newGithubLoader returns a new fileLoader with given github Url.
func newGithubLoader(repoUrl string, fs fs.FileSystem) (*githubLoader, error) {
dir, err := ioutil.TempDir("", "kustomize-")
if err != nil {
return nil, err
}
target := filepath.Join(dir, "repo")
err = checkout(repoUrl, target)
if err != nil {
return nil, err
}
l := newFileLoaderAtRoot(target, fs)
return &githubLoader{
repo: repoUrl,
checkoutDir: target,
loader: l,
}, nil
}

// isRepoUrl checks if a string is a repo Url
func isRepoUrl(s string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add test coverage for this

return strings.Contains(s, ".com") || strings.Contains(s, ".org") || strings.Contains(s, "https://")
}

// Checkout clones a github repo with specified commit/tag/branch
func checkout(url, dir string) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
client := &getter.Client{
Src: url,
Dst: dir,
Pwd: pwd,
Mode: getter.ClientModeDir,
}
return client.Get()
}
47 changes: 47 additions & 0 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ limitations under the License.
// Package loader has a data loading interface and various implementations.
package loader

import (
"fmt"
"path/filepath"

"github.com/kubernetes-sigs/kustomize/pkg/fs"
)

// Loader interface exposes methods to read bytes.
type Loader interface {
// Root returns the root location for this Loader.
Expand All @@ -25,4 +32,44 @@ type Loader interface {
New(newRoot string) (Loader, error)
// Load returns the bytes read from the location or an error.
Load(location string) ([]byte, error)
// Cleanup cleans the loader
Cleanup() error
}

// NewLoader returns a Loader given a target
// The target can be a local disk directory or a github Url
func NewLoader(target, r string, fSys fs.FileSystem) (Loader, error) {
if !isValidLoaderPath(target, r) {
return nil, fmt.Errorf("Not valid path: root='%s', loc='%s'\n", r, target)
}

if isRepoUrl(target) {
return newGithubLoader(target, fSys)
}

l := newFileLoaderAtRoot(r, fSys)
if isRootLoaderPath(r) {
absPath, err := filepath.Abs(target)
if err != nil {
return nil, err
}
target = absPath
}

if !l.IsAbsPath(l.root, target) {
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, target)
}
root, err := l.fullLocation(l.root, target)
if err != nil {
return nil, err
}
return newFileLoaderAtRoot(root, l.fSys), nil
}

func isValidLoaderPath(target, root string) bool {
return target != "" || root != ""
}

func isRootLoaderPath(root string) bool {
return root == ""
}
2 changes: 1 addition & 1 deletion vendor/github.com/hashicorp/go-getter/get.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading