From 76a280ead07d8b3e6fbb69820f8fe37e9df1a1d5 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kalynovskyi Date: Tue, 25 Feb 2020 14:20:57 -0600 Subject: [PATCH] Add Kustomize document filesystem layer Add document filesystem object to avoid direct kustomize imports by other packages in airshipctl, document.filesystem also extends kustomize filesystem with temporary file methods. Relates-To: #11 Closes: #11 Change-Id: Ia8034048d80d79d5996dce0e283828644fbef906 --- pkg/bootstrap/isogen/command.go | 4 +--- pkg/cluster/initinfra/infra.go | 7 ++----- pkg/cluster/initinfra/infra_test.go | 3 +-- pkg/document/bundle.go | 13 ++++++------- pkg/{k8s/kubectl => document}/filesystem.go | 13 +++++++++---- pkg/k8s/kubectl/kubectl.go | 7 +++---- pkg/k8s/kubectl/kubectl_test.go | 18 +++++++++--------- pkg/remote/remote_direct.go | 4 +--- testutil/testdatafs.go | 4 ++-- 9 files changed, 34 insertions(+), 39 deletions(-) rename pkg/{k8s/kubectl => document}/filesystem.go (63%) diff --git a/pkg/bootstrap/isogen/command.go b/pkg/bootstrap/isogen/command.go index cf6d93d76..ded81ac32 100644 --- a/pkg/bootstrap/isogen/command.go +++ b/pkg/bootstrap/isogen/command.go @@ -14,8 +14,6 @@ import ( "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/util" - - "sigs.k8s.io/kustomize/v3/pkg/fs" ) const ( @@ -48,7 +46,7 @@ func GenerateBootstrapIso(settings *environment.AirshipCTLSettings, args []strin // TODO (dukov) replace with the appropriate function once it's available // in document module - docBundle, err := document.NewBundle(fs.MakeRealFS(), manifest.TargetPath, "") + docBundle, err := document.NewBundle(document.NewDocumentFs(), manifest.TargetPath, "") if err != nil { return err } diff --git a/pkg/cluster/initinfra/infra.go b/pkg/cluster/initinfra/infra.go index e2de5a880..292695735 100644 --- a/pkg/cluster/initinfra/infra.go +++ b/pkg/cluster/initinfra/infra.go @@ -1,18 +1,15 @@ package initinfra import ( - "sigs.k8s.io/kustomize/v3/pkg/fs" - "opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/k8s/client" - "opendev.org/airship/airshipctl/pkg/k8s/kubectl" ) // Infra is an abstraction used to initialize base infrastructure type Infra struct { - FileSystem fs.FileSystem + FileSystem document.FileSystem RootSettings *environment.AirshipCTLSettings Client client.Interface @@ -30,7 +27,7 @@ func NewInfra(rs *environment.AirshipCTLSettings) *Infra { // Run intinfra subcommand logic func (infra *Infra) Run() error { - infra.FileSystem = kubectl.Buffer{FileSystem: fs.MakeRealFS()} + infra.FileSystem = document.NewDocumentFs() var err error infra.Client, err = client.NewClient(infra.RootSettings) if err != nil { diff --git a/pkg/cluster/initinfra/infra_test.go b/pkg/cluster/initinfra/infra_test.go index 64bb43251..d2aee9616 100644 --- a/pkg/cluster/initinfra/infra_test.go +++ b/pkg/cluster/initinfra/infra_test.go @@ -9,7 +9,6 @@ import ( "github.com/stretchr/testify/require" "k8s.io/client-go/kubernetes" - "sigs.k8s.io/kustomize/v3/pkg/fs" "opendev.org/airship/airshipctl/pkg/cluster/initinfra" "opendev.org/airship/airshipctl/pkg/document" @@ -65,7 +64,7 @@ func TestDeploy(t *testing.T) { infra.ClusterType = "ephemeral" infra.DryRun = true - infra.FileSystem = kubectl.Buffer{FileSystem: fs.MakeRealFS()} + infra.FileSystem = document.NewDocumentFs() kctl := kubectl.NewKubectl(tf) tc := TestClient{ diff --git a/pkg/document/bundle.go b/pkg/document/bundle.go index 029031c83..239e64461 100644 --- a/pkg/document/bundle.go +++ b/pkg/document/bundle.go @@ -7,7 +7,6 @@ import ( "sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct" "sigs.k8s.io/kustomize/v3/k8sdeps/transformer" "sigs.k8s.io/kustomize/v3/k8sdeps/validator" - "sigs.k8s.io/kustomize/v3/pkg/fs" "sigs.k8s.io/kustomize/v3/pkg/loader" "sigs.k8s.io/kustomize/v3/pkg/plugins" "sigs.k8s.io/kustomize/v3/pkg/resmap" @@ -37,7 +36,7 @@ type KustomizeBuildOptions struct { type BundleFactory struct { KustomizeBuildOptions resmap.ResMap - fs.FileSystem + FileSystem } // Bundle interface provides the specification for a bundle implementation @@ -47,8 +46,8 @@ type Bundle interface { SetKustomizeResourceMap(resmap.ResMap) error GetKustomizeBuildOptions() KustomizeBuildOptions SetKustomizeBuildOptions(KustomizeBuildOptions) error - SetFileSystem(fs.FileSystem) error - GetFileSystem() fs.FileSystem + SetFileSystem(FileSystem) error + GetFileSystem() FileSystem Select(selector Selector) ([]Document, error) GetByGvk(string, string, string) ([]Document, error) GetByName(string) (Document, error) @@ -60,7 +59,7 @@ type Bundle interface { // NewBundle is a convenience function to create a new bundle // Over time, it will evolve to support allowing more control // for kustomize plugins -func NewBundle(fSys fs.FileSystem, kustomizePath string, outputPath string) (bundle Bundle, err error) { +func NewBundle(fSys FileSystem, kustomizePath string, outputPath string) (bundle Bundle, err error) { var options = KustomizeBuildOptions{ KustomizationPath: kustomizePath, OutputPath: outputPath, @@ -141,13 +140,13 @@ func (b *BundleFactory) SetKustomizeBuildOptions(k KustomizeBuildOptions) error } // SetFileSystem sets the filesystem that will be used by this bundle -func (b *BundleFactory) SetFileSystem(fSys fs.FileSystem) error { +func (b *BundleFactory) SetFileSystem(fSys FileSystem) error { b.FileSystem = fSys return nil } // GetFileSystem gets the filesystem that will be used by this bundle -func (b *BundleFactory) GetFileSystem() fs.FileSystem { +func (b *BundleFactory) GetFileSystem() FileSystem { return b.FileSystem } diff --git a/pkg/k8s/kubectl/filesystem.go b/pkg/document/filesystem.go similarity index 63% rename from pkg/k8s/kubectl/filesystem.go rename to pkg/document/filesystem.go index 36e62c843..3ad4169c5 100644 --- a/pkg/k8s/kubectl/filesystem.go +++ b/pkg/document/filesystem.go @@ -1,4 +1,4 @@ -package kubectl +package document import ( "io/ioutil" @@ -18,12 +18,17 @@ type FileSystem interface { TempFile(string, string) (File, error) } -// Buffer is adaptor to TempFile -type Buffer struct { +// DocumentFs is adaptor to TempFile +type DocumentFs struct { fs.FileSystem } +// NewDocumentFs returns an instalce of DocumentFs +func NewDocumentFs() FileSystem { + return &DocumentFs{FileSystem: fs.MakeRealFS()} +} + // TempFile creates file in temporary filesystem, at default os.TempDir -func (b Buffer) TempFile(tmpDir string, prefix string) (File, error) { +func (dfs DocumentFs) TempFile(tmpDir string, prefix string) (File, error) { return ioutil.TempFile(tmpDir, prefix) } diff --git a/pkg/k8s/kubectl/kubectl.go b/pkg/k8s/kubectl/kubectl.go index ca5f15378..ec5ef5248 100644 --- a/pkg/k8s/kubectl/kubectl.go +++ b/pkg/k8s/kubectl/kubectl.go @@ -5,7 +5,6 @@ import ( "k8s.io/cli-runtime/pkg/genericclioptions" cmdutil "k8s.io/kubectl/pkg/cmd/util" - "sigs.k8s.io/kustomize/v3/pkg/fs" "opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/log" @@ -17,7 +16,7 @@ import ( type Kubectl struct { cmdutil.Factory genericclioptions.IOStreams - FileSystem + document.FileSystem // Directory to buffer documents before passing them to kubectl commands // default is empty, this means that /tmp dir will be used bufferDir string @@ -33,7 +32,7 @@ func NewKubectl(f cmdutil.Factory) *Kubectl { Out: os.Stdout, ErrOut: os.Stderr, }, - FileSystem: Buffer{FileSystem: fs.MakeRealFS()}, + FileSystem: document.NewDocumentFs(), } } @@ -49,7 +48,7 @@ func (kubectl *Kubectl) Apply(docs []document.Document, ao *ApplyOptions) error return err } - defer func(f File) { + defer func(f document.File) { fName := f.Name() dErr := kubectl.RemoveAll(fName) if dErr != nil { diff --git a/pkg/k8s/kubectl/kubectl_test.go b/pkg/k8s/kubectl/kubectl_test.go index b2d33edf9..4356d7bea 100644 --- a/pkg/k8s/kubectl/kubectl_test.go +++ b/pkg/k8s/kubectl/kubectl_test.go @@ -6,8 +6,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "sigs.k8s.io/kustomize/v3/pkg/fs" + "opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/k8s/kubectl" k8sutils "opendev.org/airship/airshipctl/pkg/k8s/utils" "opendev.org/airship/airshipctl/testutil" @@ -24,17 +24,17 @@ var ( type MockFileSystem struct { MockRemoveAll func() error - MockTempFile func() (kubectl.File, error) - fs.FileSystem + MockTempFile func() (document.File, error) + document.FileSystem } func (fsys MockFileSystem) RemoveAll(name string) error { return fsys.MockRemoveAll() } -func (fsys MockFileSystem) TempFile(bufferDir string, prefix string) (kubectl.File, error) { +func (fsys MockFileSystem) TempFile(bufferDir string, prefix string) (document.File, error) { return fsys.MockTempFile() } type TestFile struct { - kubectl.File + document.File MockName func() string MockWrite func() (int, error) MockClose func() error @@ -69,13 +69,13 @@ func TestApply(t *testing.T) { tests := []struct { name string expectedErr error - fs kubectl.FileSystem + fs document.FileSystem }{ { expectedErr: nil, fs: MockFileSystem{ MockRemoveAll: func() error { return nil }, - MockTempFile: func() (kubectl.File, error) { + MockTempFile: func() (document.File, error) { return TestFile{ MockName: func() string { return filenameRC }, MockWrite: func() (int, error) { return 0, nil }, @@ -87,13 +87,13 @@ func TestApply(t *testing.T) { { expectedErr: writeOutError, fs: MockFileSystem{ - MockTempFile: func() (kubectl.File, error) { return nil, writeOutError }}, + MockTempFile: func() (document.File, error) { return nil, writeOutError }}, }, { expectedErr: TempFileError, fs: MockFileSystem{ MockRemoveAll: func() error { return nil }, - MockTempFile: func() (kubectl.File, error) { + MockTempFile: func() (document.File, error) { return TestFile{ MockWrite: func() (int, error) { return 0, TempFileError }, MockName: func() string { return filenameRC }, diff --git a/pkg/remote/remote_direct.go b/pkg/remote/remote_direct.go index 4313fe494..cee56536a 100644 --- a/pkg/remote/remote_direct.go +++ b/pkg/remote/remote_direct.go @@ -11,8 +11,6 @@ import ( "opendev.org/airship/airshipctl/pkg/environment" alog "opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/remote/redfish" - - "sigs.k8s.io/kustomize/v3/pkg/fs" ) const ( @@ -82,7 +80,7 @@ func getRemoteDirectConfig(settings *environment.AirshipCTLSettings) (*config.Re // TODO (dukov) replace with the appropriate function once it's available // in document module - docBundle, err := document.NewBundle(fs.MakeRealFS(), manifest.TargetPath, "") + docBundle, err := document.NewBundle(document.NewDocumentFs(), manifest.TargetPath, "") if err != nil { return nil, "", err } diff --git a/testutil/testdatafs.go b/testutil/testdatafs.go index ae7606b2d..7c1e4224f 100644 --- a/testutil/testdatafs.go +++ b/testutil/testdatafs.go @@ -17,10 +17,10 @@ import ( // will iterate over the files in fixtureDir, which is a directory relative // to the tests themselves, and will write each of those files (preserving // names) to an in-memory file system and return that fs -func SetupTestFs(t *testing.T, fixtureDir string) fs.FileSystem { +func SetupTestFs(t *testing.T, fixtureDir string) document.FileSystem { t.Helper() - x := fs.MakeFakeFS() + x := &document.DocumentFs{FileSystem: fs.MakeFakeFS()} files, err := ioutil.ReadDir(fixtureDir) require.NoErrorf(t, err, "Failed to read fixture directory %s", fixtureDir)