Repository: kinvolk/lokomotive
Pull request #787 (2020-08-10 18:56:24)

Move walkers to pkg/assets
The walkers deal with extracting arbitrary assets from memory to disk, therefore it makes sense to manage them under `pkg/assets`.


Patch:
diff --git a/pkg/assets/assets.go b/pkg/assets/assets.go
index 7c253eef..0703bfff 100644
--- a/pkg/assets/assets.go
+++ b/pkg/assets/assets.go
@@ -12,6 +12,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+// Package assets handles Lokomotive assets. Operations such as storing files as binary data and
+// extracting files from memory to disk belong in this package.
 package assets
 
 import (
diff --git a/pkg/util/walkers/copying.go b/pkg/assets/walkers.go
similarity index 56%
rename from pkg/util/walkers/copying.go
rename to pkg/assets/walkers.go
index dd82612b..09e23d96 100644
--- a/pkg/util/walkers/copying.go
+++ b/pkg/assets/walkers.go
@@ -12,35 +12,66 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package walkers
+package assets
 
 import (
 	"fmt"
 	"io"
+	"io/ioutil"
 	"os"
 	"path/filepath"
-
-	"github.com/pkg/errors"
-
-	"github.com/kinvolk/lokomotive/pkg/assets"
 )
 
-func CopyingWalker(path string, newDirPerms os.FileMode) assets.WalkFunc {
+// CopyingWalker returns a WalkFunc which writes the given file to disk.
+func CopyingWalker(path string, newDirPerms os.FileMode) WalkFunc {
 	return func(fileName string, fileInfo os.FileInfo, r io.ReadSeeker, err error) error {
 		if err != nil {
-			return errors.Wrapf(err, "error during walking at %q", fileName)
+			return fmt.Errorf("error while walking at %q: %w", fileName, err)
 		}
 
 		fileName = filepath.Join(path, fileName)
 
 		if err := os.MkdirAll(filepath.Dir(fileName), newDirPerms); err != nil {
-			return errors.Wrap(err, "failed to create dir")
+			return fmt.Errorf("failed to create dir: %w", err)
 		}
 
 		return writeFile(fileName, r)
 	}
 }
 
+// DumpingWalker returns a WalkFunc which sets the contents of the given file in a map.
+func DumpingWalker(contentsMap map[string]string, allowedExts ...string) WalkFunc {
+	var extsMap map[string]struct{}
+
+	if len(allowedExts) > 0 {
+		extsMap = make(map[string]struct{}, len(allowedExts))
+		for _, ext := range allowedExts {
+			extsMap[ext] = struct{}{}
+		}
+	}
+
+	return func(fileName string, fileInfo os.FileInfo, r io.ReadSeeker, err error) error {
+		if err != nil {
+			return fmt.Errorf("error while walking at %q: %w", fileName, err)
+		}
+
+		if extsMap != nil {
+			if _, ok := extsMap[filepath.Ext(fileName)]; !ok {
+				return nil
+			}
+		}
+
+		contents, err := ioutil.ReadAll(r)
+		if err != nil {
+			return fmt.Errorf("failed to read %q: %w", fileName, err)
+		}
+
+		contentsMap[fileName] = string(contents)
+
+		return nil
+	}
+}
+
 // writeFile writes data from given io.Reader to the file and makes sure, that
 // this is the only content stored in the file.
 func writeFile(p string, r io.Reader) error {
diff --git a/pkg/components/flatcar-linux-update-operator/component.go b/pkg/components/flatcar-linux-update-operator/component.go
index 08fcc73f..ae496fcf 100644
--- a/pkg/components/flatcar-linux-update-operator/component.go
+++ b/pkg/components/flatcar-linux-update-operator/component.go
@@ -24,7 +24,6 @@ import (
 	"github.com/kinvolk/lokomotive/pkg/assets"
 	"github.com/kinvolk/lokomotive/pkg/components"
 	"github.com/kinvolk/lokomotive/pkg/k8sutil"
-	"github.com/kinvolk/lokomotive/pkg/util/walkers"
 )
 
 const componentName = "flatcar-linux-update-operator"
@@ -45,7 +44,7 @@ func (c *component) LoadConfig(configBody *hcl.Body, evalContext *hcl.EvalContex
 
 func (c *component) RenderManifests() (map[string]string, error) {
 	ret := make(map[string]string)
-	walk := walkers.DumpingWalker(ret, ".yaml")
+	walk := assets.DumpingWalker(ret, ".yaml")
 	if err := assets.Assets.WalkFiles(fmt.Sprintf("/components/%s/manifests", componentName), walk); err != nil {
 		return nil, errors.Wrap(err, "failed to walk assets")
 	}
diff --git a/pkg/components/util/helm.go b/pkg/components/util/helm.go
index d8448731..e785d1de 100644
--- a/pkg/components/util/helm.go
+++ b/pkg/components/util/helm.go
@@ -32,7 +32,6 @@ import (
 	"github.com/kinvolk/lokomotive/pkg/assets"
 	"github.com/kinvolk/lokomotive/pkg/components"
 	"github.com/kinvolk/lokomotive/pkg/k8sutil"
-	"github.com/kinvolk/lokomotive/pkg/util/walkers"
 )
 
 // LoadChartFromAssets takes in an asset location and returns a Helm
@@ -46,7 +45,7 @@ func LoadChartFromAssets(location string) (*chart.Chart, error) {
 
 	// Rendered files could contain secret data, only allow the
 	// current user but not others
-	walk := walkers.CopyingWalker(tmpDir, 0700)
+	walk := assets.CopyingWalker(tmpDir, 0700)
 	if err := assets.Assets.WalkFiles(location, walk); err != nil {
 		return nil, errors.Wrap(err, "walking assets")
 	}
diff --git a/pkg/install/install.go b/pkg/install/install.go
index f22d5441..5448f936 100644
--- a/pkg/install/install.go
+++ b/pkg/install/install.go
@@ -21,7 +21,6 @@ import (
 
 	"github.com/kinvolk/lokomotive/pkg/assets"
 	"github.com/kinvolk/lokomotive/pkg/util"
-	"github.com/kinvolk/lokomotive/pkg/util/walkers"
 )
 
 // PrepareTerraformRootDir creates a directory named path including all
@@ -50,7 +49,7 @@ func PrepareTerraformRootDir(path string) error {
 // lokoctl binary or from the filesystem, depending on whether the
 // LOKOCTL_USE_FS_ASSETS environment variable was specified.
 func PrepareLokomotiveTerraformModuleAt(path string) error {
-	walk := walkers.CopyingWalker(path, 0755)
+	walk := assets.CopyingWalker(path, 0755)
 	if err := assets.Assets.WalkFiles("/lokomotive-kubernetes", walk); err != nil {
 		return errors.Wrap(err, "failed to walk assets")
 	}
diff --git a/pkg/util/walkers/dumping.go b/pkg/util/walkers/dumping.go
deleted file mode 100644
index e0074ebc..00000000
--- a/pkg/util/walkers/dumping.go
+++ /dev/null
@@ -1,56 +0,0 @@
-// Copyright 2020 The Lokomotive 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 walkers
-
-import (
-	"io"
-	"io/ioutil"
-	"os"
-	"path/filepath"
-
-	"github.com/pkg/errors"
-
-	"github.com/kinvolk/lokomotive/pkg/assets"
-)
-
-func DumpingWalker(contentsMap map[string]string, allowedExts ...string) assets.WalkFunc {
-	var extsMap map[string]struct{}
-
-	if len(allowedExts) > 0 {
-		extsMap = make(map[string]struct{}, len(allowedExts))
-		for _, ext := range allowedExts {
-			extsMap[ext] = struct{}{}
-		}
-	}
-	return func(fileName string, fileInfo os.FileInfo, r io.ReadSeeker, err error) error {
-		if err != nil {
-			return errors.Wrapf(err, "error during walking at %q", fileName)
-		}
-
-		if extsMap != nil {
-			if _, ok := extsMap[filepath.Ext(fileName)]; !ok {
-				return nil
-			}
-		}
-
-		contents, err := ioutil.ReadAll(r)
-		if err != nil {
-			return errors.Wrapf(err, "failed to read %q", fileName)
-		}
-
-		contentsMap[fileName] = string(contents)
-		return nil
-	}
-}


Test patch:
diff --git a/pkg/util/walkers/copying_test.go b/pkg/assets/walkers_internal_test.go
similarity index 99%
rename from pkg/util/walkers/copying_test.go
rename to pkg/assets/walkers_internal_test.go
index b4b2555d..24aea1ef 100644
--- a/pkg/util/walkers/copying_test.go
+++ b/pkg/assets/walkers_internal_test.go
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package walkers
+package assets
 
 import (
 	"io/ioutil"