-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support various target and resource with go-getter
- Loading branch information
1 parent
3a2635b
commit 51b29d7
Showing
9 changed files
with
228 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2020 The Kubernetes Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package loader | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
|
||
"github.com/yujunz/go-getter" | ||
"sigs.k8s.io/kustomize/api/filesys" | ||
"sigs.k8s.io/kustomize/api/ifc" | ||
"sigs.k8s.io/kustomize/api/internal/git" | ||
) | ||
|
||
type resourceSpec struct { | ||
// Raw is the original resource in kustomization.yaml | ||
Raw string | ||
|
||
// Dir is where the resource is saved | ||
Dir filesys.ConfirmedDir | ||
} | ||
|
||
// Getter is a function that can gets resource | ||
type resourceGetter func(rs *resourceSpec) error | ||
|
||
func newResourceGetter(raw string, fSys filesys.FileSystem, referrer *fileLoader, cloner git.Cloner, getter resourceGetter) (ifc.Loader, error) { | ||
rs := &resourceSpec{ | ||
Raw: raw, | ||
} | ||
|
||
cleaner := func() error { | ||
return fSys.RemoveAll(rs.Dir.String()) | ||
} | ||
|
||
if err := getter(rs); err != nil { | ||
cleaner() | ||
return nil, err | ||
} | ||
|
||
// TODO(yujunz): check file or directory | ||
|
||
return &fileLoader{ | ||
loadRestrictor: RestrictionRootOnly, | ||
// TODO(yujunz): limit to getter root | ||
root: rs.Dir, | ||
referrer: referrer, | ||
fSys: fSys, | ||
cloner: cloner, | ||
rscSpec: rs, | ||
getter: getter, | ||
cleaner: cleaner, | ||
}, nil | ||
} | ||
|
||
func getResource(rs *resourceSpec) error { | ||
var err error | ||
|
||
rs.Dir, err = filesys.NewTmpConfirmedDir() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get the pwd | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
log.Fatalf("Error getting wd: %s", err) | ||
} | ||
|
||
opts := []getter.ClientOption{} | ||
client := &getter.Client{ | ||
Ctx: context.TODO(), | ||
Src: rs.Raw, | ||
Dst: rs.Dir.String(), | ||
Pwd: pwd, | ||
Mode: getter.ClientModeAny, | ||
Detectors: []getter.Detector{ | ||
new(getter.GitHubDetector), | ||
new(getter.GitDetector), | ||
new(getter.BitBucketDetector), | ||
}, | ||
Options: opts, | ||
} | ||
return client.Get() | ||
} | ||
|
||
func getNothing(rs *resourceSpec) error { | ||
var err error | ||
rs.Dir, err = filesys.NewTmpConfirmedDir() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get the pwd | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
log.Fatalf("Error getting wd: %s", err) | ||
} | ||
|
||
_, err = getter.Detect(rs.Raw, pwd, []getter.Detector{}) | ||
return err | ||
} |
Oops, something went wrong.