Add KustFactory as a wrapper of all factories

This commit is contained in:
Jingfang Liu
2018-10-11 12:15:08 -07:00
parent 6fa110d4fd
commit 40b2bf76ae
6 changed files with 82 additions and 24 deletions

View File

@@ -0,0 +1,29 @@
/*
Copyright 2018 The Kubernetes 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 k8sdeps
import (
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/internal/k8sdeps/transformer"
"sigs.k8s.io/kustomize/internal/k8sdeps/validator"
"sigs.k8s.io/kustomize/pkg/factory"
)
// NewFactory creates an instance of KustFactory using k8sdeps factories
func NewFactory() *factory.KustFactory {
return factory.NewKustFactory(
kunstruct.NewKunstructuredFactoryImpl(),
validator.NewKustValidator(),
transformer.NewFactoryImpl(),
)
}

View File

@@ -20,19 +20,14 @@ import (
"os" "os"
"github.com/golang/glog" "github.com/golang/glog"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct" "sigs.k8s.io/kustomize/internal/k8sdeps"
"sigs.k8s.io/kustomize/internal/k8sdeps/transformer"
"sigs.k8s.io/kustomize/internal/k8sdeps/validator"
"sigs.k8s.io/kustomize/pkg/commands" "sigs.k8s.io/kustomize/pkg/commands"
) )
func main() { func main() {
defer glog.Flush() defer glog.Flush()
if err := commands.NewDefaultCommand( if err := commands.NewDefaultCommand(k8sdeps.NewFactory()).Execute(); err != nil {
kunstruct.NewKunstructuredFactoryImpl(),
transformer.NewFactoryImpl(),
validator.NewKustValidator()).Execute(); err != nil {
os.Exit(1) os.Exit(1)
} }
os.Exit(0) os.Exit(0)

View File

@@ -24,11 +24,9 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/constants" "sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/ifc/transformer" "sigs.k8s.io/kustomize/pkg/ifc/transformer"
"sigs.k8s.io/kustomize/pkg/loader" "sigs.k8s.io/kustomize/pkg/loader"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/target" "sigs.k8s.io/kustomize/pkg/target"
"sigs.k8s.io/kustomize/pkg/transformers/config" "sigs.k8s.io/kustomize/pkg/transformers/config"
) )
@@ -62,7 +60,7 @@ Use different transformer configurations by passing files to kustomize
// NewCmdBuild creates a new build command. // NewCmdBuild creates a new build command.
func NewCmdBuild( func NewCmdBuild(
out io.Writer, fs fs.FileSystem, out io.Writer, fs fs.FileSystem,
kf ifc.KunstructuredFactory, rf *resmap.Factory,
ptf transformer.Factory) *cobra.Command { ptf transformer.Factory) *cobra.Command {
var o buildOptions var o buildOptions
var p string var p string
@@ -77,7 +75,7 @@ func NewCmdBuild(
if err != nil { if err != nil {
return err return err
} }
return o.RunBuild(out, fs, kf, ptf) return o.RunBuild(out, fs, rf, ptf)
}, },
} }
cmd.Flags().StringVarP( cmd.Flags().StringVarP(
@@ -122,7 +120,7 @@ func (o *buildOptions) Validate(args []string, p string, fs fs.FileSystem) error
// RunBuild runs build command. // RunBuild runs build command.
func (o *buildOptions) RunBuild( func (o *buildOptions) RunBuild(
out io.Writer, fSys fs.FileSystem, out io.Writer, fSys fs.FileSystem,
kf ifc.KunstructuredFactory, rf *resmap.Factory,
ptf transformer.Factory) error { ptf transformer.Factory) error {
rootLoader, err := loader.NewLoader(o.kustomizationPath, "", fSys) rootLoader, err := loader.NewLoader(o.kustomizationPath, "", fSys)
if err != nil { if err != nil {
@@ -135,7 +133,7 @@ func (o *buildOptions) RunBuild(
defer rootLoader.Cleanup() defer rootLoader.Cleanup()
kt, err := target.NewKustTarget( kt, err := target.NewKustTarget(
rootLoader, fSys, rootLoader, fSys,
resmap.NewFactory(resource.NewFactory(kf)), rf,
ptf, tc) ptf, tc)
if err != nil { if err != nil {
return err return err

View File

@@ -26,8 +26,7 @@ import (
"testing" "testing"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct" "sigs.k8s.io/kustomize/internal/k8sdeps"
"sigs.k8s.io/kustomize/internal/k8sdeps/transformer"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/constants" "sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
@@ -129,10 +128,11 @@ func runBuildTestCase(t *testing.T, testcaseName string, updateKustomizeExpected
kustomizationPath: testcase.Filename, kustomizationPath: testcase.Filename,
} }
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
f := k8sdeps.NewFactory()
err = ops.RunBuild( err = ops.RunBuild(
buf, fSys, buf, fSys,
kunstruct.NewKunstructuredFactoryImpl(), f.ResmapF,
transformer.NewFactoryImpl()) f.TransformerF)
switch { switch {
case err != nil && len(testcase.ExpectedError) == 0: case err != nil && len(testcase.ExpectedError) == 0:
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)

View File

@@ -25,15 +25,12 @@ import (
"sigs.k8s.io/kustomize/pkg/commands/build" "sigs.k8s.io/kustomize/pkg/commands/build"
"sigs.k8s.io/kustomize/pkg/commands/edit" "sigs.k8s.io/kustomize/pkg/commands/edit"
"sigs.k8s.io/kustomize/pkg/commands/misc" "sigs.k8s.io/kustomize/pkg/commands/misc"
"sigs.k8s.io/kustomize/pkg/factory"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/ifc/transformer"
) )
// NewDefaultCommand returns the default (aka root) command for kustomize command. // NewDefaultCommand returns the default (aka root) command for kustomize command.
func NewDefaultCommand( func NewDefaultCommand(f *factory.KustFactory) *cobra.Command {
kf ifc.KunstructuredFactory, ptf transformer.Factory,
validator ifc.Validator) *cobra.Command {
fsys := fs.MakeRealFS() fsys := fs.MakeRealFS()
stdOut := os.Stdout stdOut := os.Stdout
@@ -49,8 +46,8 @@ See https://sigs.k8s.io/kustomize
c.AddCommand( c.AddCommand(
// TODO: Make consistent API for newCmd* functions. // TODO: Make consistent API for newCmd* functions.
build.NewCmdBuild(stdOut, fsys, kf, ptf), build.NewCmdBuild(stdOut, fsys, f.ResmapF, f.TransformerF),
edit.NewCmdEdit(fsys, validator, kf), edit.NewCmdEdit(fsys, f.ValidatorF, f.UnstructF),
misc.NewCmdConfig(fsys), misc.NewCmdConfig(fsys),
misc.NewCmdVersion(stdOut), misc.NewCmdVersion(stdOut),
) )

39
pkg/factory/factory.go Normal file
View File

@@ -0,0 +1,39 @@
/*
Copyright 2018 The Kubernetes 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 factory provides factories for kustomize.
package factory
import (
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/ifc/transformer"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
)
// KustFactory provides different factories for kustomize
type KustFactory struct {
ResmapF *resmap.Factory
TransformerF transformer.Factory
ValidatorF ifc.Validator
UnstructF ifc.KunstructuredFactory
}
// NewKustFactory creats a KustFactory instance
func NewKustFactory(u ifc.KunstructuredFactory, v ifc.Validator, t transformer.Factory) *KustFactory {
return &KustFactory{
ResmapF: resmap.NewFactory(resource.NewFactory(u)),
TransformerF: t,
ValidatorF: v,
UnstructF: u,
}
}