Skip to content

Commit

Permalink
Add kustomize build {repoUrl}
Browse files Browse the repository at this point in the history
  • Loading branch information
Liujingfang1 committed Aug 14, 2018
1 parent c9a8bc1 commit cf72eaf
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 18 deletions.
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
}
8 changes: 8 additions & 0 deletions pkg/loader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ 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 isRepoUrl(newRoot) {
return newGithubLoader(newRoot, l.fSys)
}
if !l.IsAbsPath(l.root, newRoot) {
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
}
Expand Down Expand Up @@ -109,3 +112,8 @@ func (l *fileLoader) Load(location string) ([]byte, error) {
}
return l.fSys.ReadFile(fullLocation)
}

// Cleanup does nothing
func (l *fileLoader) Cleanup() error {
return nil
}
96 changes: 96 additions & 0 deletions pkg/loader/githubloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
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
fSys fs.FileSystem
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,
fSys: fs,
loader: l,
}, nil
}

// isRepoUrl checks if a string is a repo Url
func isRepoUrl(s string) bool {
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()
}
32 changes: 32 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,29 @@ 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 string, fSys fs.FileSystem) (Loader, error) {
if isRepoUrl(target) {
return newGithubLoader(target, fSys)
}

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

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

0 comments on commit cf72eaf

Please sign in to comment.