From a0699c2c7af81c69d71f55a3d6a92ea7d7008d78 Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Wed, 21 Feb 2018 10:22:00 -0800 Subject: [PATCH] Move ManifestLoader under tree package --- pkg/kinflate/commands/addresource.go | 4 +-- pkg/kinflate/commands/configmap.go | 4 +-- pkg/kinflate/commands/inflate.go | 2 +- pkg/kinflate/{util => tree}/adjust_path.go | 2 +- pkg/kinflate/tree/builder.go | 36 +++++++------------ pkg/kinflate/tree/builder_test.go | 10 +++--- pkg/kinflate/{util => tree}/manifestloader.go | 9 ++++- .../{util => tree}/manifestloader_test.go | 10 +++--- 8 files changed, 36 insertions(+), 41 deletions(-) rename pkg/kinflate/{util => tree}/adjust_path.go (99%) rename pkg/kinflate/{util => tree}/manifestloader.go (94%) rename pkg/kinflate/{util => tree}/manifestloader_test.go (92%) diff --git a/pkg/kinflate/commands/addresource.go b/pkg/kinflate/commands/addresource.go index 4e34f5bfa..e2fc56855 100644 --- a/pkg/kinflate/commands/addresource.go +++ b/pkg/kinflate/commands/addresource.go @@ -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 diff --git a/pkg/kinflate/commands/configmap.go b/pkg/kinflate/commands/configmap.go index 06b78f44f..41bb366cf 100644 --- a/pkg/kinflate/commands/configmap.go +++ b/pkg/kinflate/commands/configmap.go @@ -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 diff --git a/pkg/kinflate/commands/inflate.go b/pkg/kinflate/commands/inflate.go index 03731ffcf..8a72b98b4 100644 --- a/pkg/kinflate/commands/inflate.go +++ b/pkg/kinflate/commands/inflate.go @@ -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 diff --git a/pkg/kinflate/util/adjust_path.go b/pkg/kinflate/tree/adjust_path.go similarity index 99% rename from pkg/kinflate/util/adjust_path.go rename to pkg/kinflate/tree/adjust_path.go index b96997f4f..cf0e92931 100644 --- a/pkg/kinflate/util/adjust_path.go +++ b/pkg/kinflate/tree/adjust_path.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package util +package tree import ( "path" diff --git a/pkg/kinflate/tree/builder.go b/pkg/kinflate/tree/builder.go index 7ec34d2cf..d0c445147 100644 --- a/pkg/kinflate/tree/builder.go +++ b/pkg/kinflate/tree/builder.go @@ -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 diff --git a/pkg/kinflate/tree/builder_test.go b/pkg/kinflate/tree/builder_test.go index c84e9d06d..d2b5dd82b 100644 --- a/pkg/kinflate/tree/builder_test.go +++ b/pkg/kinflate/tree/builder_test.go @@ -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 { diff --git a/pkg/kinflate/util/manifestloader.go b/pkg/kinflate/tree/manifestloader.go similarity index 94% rename from pkg/kinflate/util/manifestloader.go rename to pkg/kinflate/tree/manifestloader.go index d4bcaf2ee..40c82e1e4 100644 --- a/pkg/kinflate/util/manifestloader.go +++ b/pkg/kinflate/tree/manifestloader.go @@ -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. diff --git a/pkg/kinflate/util/manifestloader_test.go b/pkg/kinflate/tree/manifestloader_test.go similarity index 92% rename from pkg/kinflate/util/manifestloader_test.go rename to pkg/kinflate/tree/manifestloader_test.go index 9f2e9353e..8051ba10f 100644 --- a/pkg/kinflate/util/manifestloader_test.go +++ b/pkg/kinflate/tree/manifestloader_test.go @@ -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")