mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Add KustFactory as a wrapper of all factories
This commit is contained in:
29
internal/k8sdeps/factory.go
Normal file
29
internal/k8sdeps/factory.go
Normal 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(),
|
||||
)
|
||||
}
|
||||
@@ -20,19 +20,14 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"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/internal/k8sdeps"
|
||||
"sigs.k8s.io/kustomize/pkg/commands"
|
||||
)
|
||||
|
||||
func main() {
|
||||
defer glog.Flush()
|
||||
|
||||
if err := commands.NewDefaultCommand(
|
||||
kunstruct.NewKunstructuredFactoryImpl(),
|
||||
transformer.NewFactoryImpl(),
|
||||
validator.NewKustValidator()).Execute(); err != nil {
|
||||
if err := commands.NewDefaultCommand(k8sdeps.NewFactory()).Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
|
||||
@@ -24,11 +24,9 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/pkg/constants"
|
||||
"sigs.k8s.io/kustomize/pkg/fs"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||
"sigs.k8s.io/kustomize/pkg/ifc/transformer"
|
||||
"sigs.k8s.io/kustomize/pkg/loader"
|
||||
"sigs.k8s.io/kustomize/pkg/resmap"
|
||||
"sigs.k8s.io/kustomize/pkg/resource"
|
||||
"sigs.k8s.io/kustomize/pkg/target"
|
||||
"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.
|
||||
func NewCmdBuild(
|
||||
out io.Writer, fs fs.FileSystem,
|
||||
kf ifc.KunstructuredFactory,
|
||||
rf *resmap.Factory,
|
||||
ptf transformer.Factory) *cobra.Command {
|
||||
var o buildOptions
|
||||
var p string
|
||||
@@ -77,7 +75,7 @@ func NewCmdBuild(
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return o.RunBuild(out, fs, kf, ptf)
|
||||
return o.RunBuild(out, fs, rf, ptf)
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringVarP(
|
||||
@@ -122,7 +120,7 @@ func (o *buildOptions) Validate(args []string, p string, fs fs.FileSystem) error
|
||||
// RunBuild runs build command.
|
||||
func (o *buildOptions) RunBuild(
|
||||
out io.Writer, fSys fs.FileSystem,
|
||||
kf ifc.KunstructuredFactory,
|
||||
rf *resmap.Factory,
|
||||
ptf transformer.Factory) error {
|
||||
rootLoader, err := loader.NewLoader(o.kustomizationPath, "", fSys)
|
||||
if err != nil {
|
||||
@@ -135,7 +133,7 @@ func (o *buildOptions) RunBuild(
|
||||
defer rootLoader.Cleanup()
|
||||
kt, err := target.NewKustTarget(
|
||||
rootLoader, fSys,
|
||||
resmap.NewFactory(resource.NewFactory(kf)),
|
||||
rf,
|
||||
ptf, tc)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -26,8 +26,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
|
||||
"sigs.k8s.io/kustomize/internal/k8sdeps/transformer"
|
||||
"sigs.k8s.io/kustomize/internal/k8sdeps"
|
||||
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
|
||||
"sigs.k8s.io/kustomize/pkg/constants"
|
||||
"sigs.k8s.io/kustomize/pkg/fs"
|
||||
@@ -129,10 +128,11 @@ func runBuildTestCase(t *testing.T, testcaseName string, updateKustomizeExpected
|
||||
kustomizationPath: testcase.Filename,
|
||||
}
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
f := k8sdeps.NewFactory()
|
||||
err = ops.RunBuild(
|
||||
buf, fSys,
|
||||
kunstruct.NewKunstructuredFactoryImpl(),
|
||||
transformer.NewFactoryImpl())
|
||||
f.ResmapF,
|
||||
f.TransformerF)
|
||||
switch {
|
||||
case err != nil && len(testcase.ExpectedError) == 0:
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
|
||||
@@ -25,15 +25,12 @@ import (
|
||||
"sigs.k8s.io/kustomize/pkg/commands/build"
|
||||
"sigs.k8s.io/kustomize/pkg/commands/edit"
|
||||
"sigs.k8s.io/kustomize/pkg/commands/misc"
|
||||
"sigs.k8s.io/kustomize/pkg/factory"
|
||||
"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.
|
||||
func NewDefaultCommand(
|
||||
kf ifc.KunstructuredFactory, ptf transformer.Factory,
|
||||
validator ifc.Validator) *cobra.Command {
|
||||
func NewDefaultCommand(f *factory.KustFactory) *cobra.Command {
|
||||
fsys := fs.MakeRealFS()
|
||||
stdOut := os.Stdout
|
||||
|
||||
@@ -49,8 +46,8 @@ See https://sigs.k8s.io/kustomize
|
||||
|
||||
c.AddCommand(
|
||||
// TODO: Make consistent API for newCmd* functions.
|
||||
build.NewCmdBuild(stdOut, fsys, kf, ptf),
|
||||
edit.NewCmdEdit(fsys, validator, kf),
|
||||
build.NewCmdBuild(stdOut, fsys, f.ResmapF, f.TransformerF),
|
||||
edit.NewCmdEdit(fsys, f.ValidatorF, f.UnstructF),
|
||||
misc.NewCmdConfig(fsys),
|
||||
misc.NewCmdVersion(stdOut),
|
||||
)
|
||||
|
||||
39
pkg/factory/factory.go
Normal file
39
pkg/factory/factory.go
Normal 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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user