Move ManifestLoader under tree package

This commit is contained in:
Jingfang Liu 2018-02-21 10:22:00 -08:00
parent 6fdf4d7ab3
commit a0699c2c7a
8 changed files with 36 additions and 41 deletions

View File

@ -24,7 +24,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/kinflate/constants"
kutil "k8s.io/kubectl/pkg/kinflate/util"
"k8s.io/kubectl/pkg/kinflate/tree"
"k8s.io/kubectl/pkg/kinflate/util/fs"
)
@ -87,7 +87,7 @@ func (o *addResourceOptions) RunAddResource(out, errOut io.Writer, fsys fs.FileS
return err
}
loader := kutil.ManifestLoader{FS: fsys}
loader := tree.ManifestLoader{FS: fsys}
m, err := loader.Read(constants.KubeManifestFileName)
if err != nil {
return err

View File

@ -23,7 +23,7 @@ import (
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
"k8s.io/kubectl/pkg/kinflate/configmapandsecret"
"k8s.io/kubectl/pkg/kinflate/constants"
kutil "k8s.io/kubectl/pkg/kinflate/util"
"k8s.io/kubectl/pkg/kinflate/tree"
"k8s.io/kubectl/pkg/kinflate/util/fs"
"github.com/spf13/cobra"
@ -52,7 +52,7 @@ func newCmdAddConfigMap(errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
}
// Load in the manifest file.
loader := kutil.ManifestLoader{FS: fsys}
loader := tree.ManifestLoader{FS: fsys}
m, err := loader.Read(constants.KubeManifestFileName)
if err != nil {
return err

View File

@ -84,7 +84,7 @@ func (o *inflateOptions) Complete(cmd *cobra.Command, args []string) error {
// runInflate does the real transformation.
func (o *inflateOptions) runInflate(fs fs.FileSystem) (types.KObject, error) {
// Build a tree of ManifestData.
loader := tree.Loader{FS: fs, InitialPath: o.manifestPath}
loader := tree.ManifestLoader{FS: fs, InitialPath: o.manifestPath}
root, err := loader.LoadManifestDataFromPath()
if err != nil {
return nil, err

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package util
package tree
import (
"path"

View File

@ -25,23 +25,11 @@ import (
cutil "k8s.io/kubectl/pkg/kinflate/configmapandsecret"
"k8s.io/kubectl/pkg/kinflate/types"
kutil "k8s.io/kubectl/pkg/kinflate/util"
"k8s.io/kubectl/pkg/kinflate/util/fs"
)
type Loader struct {
// Allows unit tests with fake filesystem.
FS fs.FileSystem
// Unexpanded manifest directory or manifest filename.
// Examples: "." or "sean-manifest.yaml"
InitialPath string
// Full expanded manifest file path.
// Examples: "/usr/local/Kube-manifest.yaml" or "/home/seans/project/sean-manifest.yaml"
FullFilePath string
}
// LoadManifestDataFromPath takes a path to a Kube-manifest.yaml or a dir that has a Kube-manifest.yaml.
// It returns a tree of ManifestData.
func (l *Loader) LoadManifestDataFromPath() (*ManifestData, error) {
func (l *ManifestLoader) LoadManifestDataFromPath() (*ManifestData, error) {
m, err := l.loadManifestFileFromPath()
if err != nil {
return nil, err
@ -50,14 +38,14 @@ func (l *Loader) LoadManifestDataFromPath() (*ManifestData, error) {
}
// loadManifestFileFromPath loads a manifest object from file.
func (l *Loader) loadManifestFileFromPath() (*manifest.Manifest, error) {
mLoader := kutil.ManifestLoader{FS: l.FS}
func (l *ManifestLoader) loadManifestFileFromPath() (*manifest.Manifest, error) {
mLoader := ManifestLoader{FS: l.FS}
// Expand the initial directory or file path into the full manifest file path.
fullFilepath, err := mLoader.MakeValidManifestPath(l.InitialPath)
if err != nil {
return nil, err
}
l.FullFilePath = fullFilepath
l.fullFilePath = fullFilepath
m, err := mLoader.Read(fullFilepath)
if err != nil {
return nil, err
@ -67,7 +55,7 @@ func (l *Loader) loadManifestFileFromPath() (*manifest.Manifest, error) {
}
// manifestToManifestData make a ManifestData given an Manifest object
func (l *Loader) manifestToManifestData(m *manifest.Manifest) (*ManifestData, error) {
func (l *ManifestLoader) manifestToManifestData(m *manifest.Manifest) (*ManifestData, error) {
mdata, err := l.loadManifestDataFromManifestFileAndResources(m)
if err != nil {
return nil, err
@ -75,7 +63,7 @@ func (l *Loader) manifestToManifestData(m *manifest.Manifest) (*ManifestData, er
pkgs := []*ManifestData{}
for _, pkg := range m.Packages {
loader := &Loader{FS: l.FS, InitialPath: pkg}
loader := &ManifestLoader{FS: l.FS, InitialPath: pkg}
pkgNode, err := loader.LoadManifestDataFromPath()
if err != nil {
return nil, err
@ -86,7 +74,7 @@ func (l *Loader) manifestToManifestData(m *manifest.Manifest) (*ManifestData, er
return mdata, nil
}
func (l *Loader) loadManifestDataFromManifestFileAndResources(m *manifest.Manifest) (*ManifestData, error) {
func (l *ManifestLoader) loadManifestDataFromManifestFileAndResources(m *manifest.Manifest) (*ManifestData, error) {
mdata := &ManifestData{}
var err error
mdata.Name = m.Name
@ -97,7 +85,7 @@ func (l *Loader) loadManifestDataFromManifestFileAndResources(m *manifest.Manife
res, err := l.loadKObjectFromPaths(m.Resources)
if err != nil {
errorMsg := fmt.Sprintf("Resource from Manifest (%s) couldn't be loaded properly.\n%v\n"+
"Please check the Resource subsection in (%s).", l.FullFilePath, err, l.FullFilePath)
"Please check the Resource subsection in (%s).", l.fullFilePath, err, l.fullFilePath)
return nil, fmt.Errorf(errorMsg)
}
mdata.Resources = ResourcesType(res)
@ -114,7 +102,7 @@ func (l *Loader) loadManifestDataFromManifestFileAndResources(m *manifest.Manife
}
mdata.Configmaps = ConfigmapsType(cms)
sec, err := cutil.MakeSecretsKObject(m.SecretGenerators, l.FullFilePath)
sec, err := cutil.MakeSecretsKObject(m.SecretGenerators, l.fullFilePath)
if err != nil {
return nil, err
}
@ -122,7 +110,7 @@ func (l *Loader) loadManifestDataFromManifestFileAndResources(m *manifest.Manife
return mdata, nil
}
func (l *Loader) loadKObjectFromPaths(paths []string) (types.KObject, error) {
func (l *ManifestLoader) loadKObjectFromPaths(paths []string) (types.KObject, error) {
res := types.KObject{}
for _, path := range paths {
err := l.loadKObjectFromPath(path, res)
@ -133,7 +121,7 @@ func (l *Loader) loadKObjectFromPaths(paths []string) (types.KObject, error) {
return res, nil
}
func (l *Loader) loadKObjectFromPath(path string, into types.KObject) error {
func (l *ManifestLoader) loadKObjectFromPath(path string, into types.KObject) error {
_, err := l.FS.Stat(path)
if err != nil {
return err
@ -159,7 +147,7 @@ func (l *Loader) loadKObjectFromPath(path string, into types.KObject) error {
return e
}
func (l *Loader) loadKObjectFromFile(filename string, into types.KObject) error {
func (l *ManifestLoader) loadKObjectFromFile(filename string, into types.KObject) error {
f, err := l.FS.Stat(filename)
if err != nil {
return err

View File

@ -131,7 +131,7 @@ func TestFileToMap(t *testing.T) {
}
// TODO: Convert to a fake filesystem instead of using test files.
loader := Loader{FS: fs.MakeRealFS()}
loader := ManifestLoader{FS: fs.MakeRealFS()}
for _, tc := range testcases {
actual := types.KObject{}
@ -184,7 +184,7 @@ func TestPathToMap(t *testing.T) {
}
// TODO: Convert to a fake filesystem instead of using test files.
loader := Loader{FS: fs.MakeRealFS()}
loader := ManifestLoader{FS: fs.MakeRealFS()}
for _, tc := range testcases {
actual := types.KObject{}
@ -248,7 +248,7 @@ func TestPathsToMap(t *testing.T) {
}
// TODO: Convert to a fake filesystem instead of using test files.
loader := Loader{FS: fs.MakeRealFS()}
loader := ManifestLoader{FS: fs.MakeRealFS()}
for _, tc := range testcases {
actual, err := loader.loadKObjectFromPaths(tc.filenames)
@ -312,7 +312,7 @@ func TestManifestToManifestData(t *testing.T) {
}
// TODO: Convert to a fake filesystem instead of using test files.
loader := Loader{FS: fs.MakeRealFS()}
loader := ManifestLoader{FS: fs.MakeRealFS()}
actual, err := loader.loadManifestDataFromManifestFileAndResources(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@ -333,7 +333,7 @@ func TestLoadManifestDataFromPath(t *testing.T) {
parent1.Packages = []*ManifestData{child1}
parent2.Packages = []*ManifestData{child2}
loader := Loader{FS: fs.MakeRealFS(), InitialPath: "testdata/hierarchy"}
loader := ManifestLoader{FS: fs.MakeRealFS(), InitialPath: "testdata/hierarchy"}
expected := grandparent
actual, err := loader.LoadManifestDataFromPath()
if err != nil {

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package util
package tree
import (
"errors"
@ -30,7 +30,14 @@ import (
)
type ManifestLoader struct {
// Allows unit tests with fake filesystem.
FS fs.FileSystem
// Initial path to manifest directory or manifest filename.
InitialPath string
// Full expanded manifest file path.
fullFilePath string
}
// First pass to encapsulate fields for more informative error messages.

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package util_test
package tree_test
import (
"reflect"
@ -23,7 +23,7 @@ import (
"strings"
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
kutil "k8s.io/kubectl/pkg/kinflate/util"
"k8s.io/kubectl/pkg/kinflate/tree"
"k8s.io/kubectl/pkg/kinflate/util/fs"
)
@ -31,7 +31,7 @@ func TestManifestLoader(t *testing.T) {
manifest := &manifest.Manifest{
NamePrefix: "prefix",
}
loader := kutil.ManifestLoader{FS: fs.MakeFakeFS()}
loader := tree.ManifestLoader{FS: fs.MakeFakeFS()}
if err := loader.Write("Kube-manifest.yaml", manifest); err != nil {
t.Fatalf("Couldn't write manifest file: %v\n", err)
@ -50,7 +50,7 @@ func TestManifestLoaderEmptyFile(t *testing.T) {
manifest := &manifest.Manifest{
NamePrefix: "prefix",
}
loader := kutil.ManifestLoader{}
loader := tree.ManifestLoader{}
if loader.Write("", manifest) == nil {
t.Fatalf("Write to empty filename should fail")
}
@ -61,7 +61,7 @@ func TestLoadNotExist(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.Mkdir(".", 0644)
fakeFS.Create(badSuffix)
loader := kutil.ManifestLoader{FS: fakeFS}
loader := tree.ManifestLoader{FS: fakeFS}
_, err := loader.MakeValidManifestPath("Kube-manifest.yaml")
if err == nil {
t.Fatalf("expect an error")