remove decoder interface since it is only used inside k8sdeps

This commit is contained in:
Jingfang Liu
2018-10-10 11:08:23 -07:00
parent cf4a1ba083
commit 96091dfcf5
22 changed files with 35 additions and 92 deletions

View File

@@ -1,47 +0,0 @@
/*
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 (
"bytes"
"errors"
"k8s.io/apimachinery/pkg/util/yaml"
)
// KustDecoder unmarshalls bytes to objects.
type KustDecoder struct {
d *yaml.YAMLOrJSONDecoder
}
// NewKustDecoder returns a new KustDecoder.
func NewKustDecoder() *KustDecoder {
return &KustDecoder{}
}
// SetInput initializes an apimachinery decoder.
func (k *KustDecoder) SetInput(in []byte) {
k.d = yaml.NewYAMLOrJSONDecoder(
bytes.NewReader(in), 1024)
}
// Decode delegates to the apimachinery decoder.
func (k *KustDecoder) Decode(into interface{}) error {
if k.d == nil {
return errors.New("no decoder")
}
return k.d.Decode(into)
}

View File

@@ -17,8 +17,11 @@ limitations under the License.
package k8sdeps package k8sdeps
import ( import (
"bytes"
"io" "io"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/kustomize/internal/k8sdeps/configmapandsecret" "sigs.k8s.io/kustomize/internal/k8sdeps/configmapandsecret"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/ifc"
@@ -27,7 +30,6 @@ import (
// KunstructurerFactoryImpl hides construction using apimachinery types. // KunstructurerFactoryImpl hides construction using apimachinery types.
type KunstructurerFactoryImpl struct { type KunstructurerFactoryImpl struct {
decoder ifc.Decoder
cmfactory *configmapandsecret.ConfigMapFactory cmfactory *configmapandsecret.ConfigMapFactory
secfactory *configmapandsecret.SecretFactory secfactory *configmapandsecret.SecretFactory
} }
@@ -35,19 +37,19 @@ type KunstructurerFactoryImpl struct {
var _ ifc.KunstructuredFactory = &KunstructurerFactoryImpl{} var _ ifc.KunstructuredFactory = &KunstructurerFactoryImpl{}
// NewKunstructuredFactoryImpl returns a factory. // NewKunstructuredFactoryImpl returns a factory.
func NewKunstructuredFactoryImpl(d ifc.Decoder) ifc.KunstructuredFactory { func NewKunstructuredFactoryImpl() ifc.KunstructuredFactory {
return &KunstructurerFactoryImpl{decoder: d} return &KunstructurerFactoryImpl{}
} }
// SliceFromBytes returns a slice of Kunstructured. // SliceFromBytes returns a slice of Kunstructured.
func (kf *KunstructurerFactoryImpl) SliceFromBytes( func (kf *KunstructurerFactoryImpl) SliceFromBytes(
in []byte) ([]ifc.Kunstructured, error) { in []byte) ([]ifc.Kunstructured, error) {
kf.decoder.SetInput(in) decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewReader(in), 1024)
var result []ifc.Kunstructured var result []ifc.Kunstructured
var err error var err error
for err == nil || isEmptyYamlError(err) { for err == nil || isEmptyYamlError(err) {
var out unstructured.Unstructured var out unstructured.Unstructured
err = kf.decoder.Decode(&out) err = decoder.Decode(&out)
if err == nil { if err == nil {
result = append(result, &UnstructAdapter{Unstructured: out}) result = append(result, &UnstructAdapter{Unstructured: out})
} }

View File

@@ -24,7 +24,7 @@ import (
) )
func TestSliceFromBytes(t *testing.T) { func TestSliceFromBytes(t *testing.T) {
factory := NewKunstructuredFactoryImpl(NewKustDecoder()) factory := NewKunstructuredFactoryImpl()
testConfigMap := factory.FromMap( testConfigMap := factory.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",

View File

@@ -29,7 +29,7 @@ import (
) )
var rf = resource.NewFactory( var rf = resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"} var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}
var foo = gvk.Gvk{Group: "example.com", Version: "v1", Kind: "Foo"} var foo = gvk.Gvk{Group: "example.com", Version: "v1", Kind: "Foo"}

View File

@@ -21,7 +21,7 @@ import (
) )
func TestGetFieldValue(t *testing.T) { func TestGetFieldValue(t *testing.T) {
factory := NewKunstructuredFactoryImpl(NewKustDecoder()) factory := NewKunstructuredFactoryImpl()
kunstructured := factory.FromMap(map[string]interface{}{ kunstructured := factory.FromMap(map[string]interface{}{
"Kind": "Service", "Kind": "Service",
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{

View File

@@ -29,9 +29,8 @@ func main() {
defer glog.Flush() defer glog.Flush()
if err := commands.NewDefaultCommand( if err := commands.NewDefaultCommand(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder()), k8sdeps.NewKunstructuredFactoryImpl(),
patch.NewPatchTransformerFactory(), patch.NewPatchTransformerFactory(),
k8sdeps.NewKustDecoder(),
k8sdeps.NewKustValidator(), k8sdeps.NewKustValidator(),
k8sdeps.NewKustHash()).Execute(); err != nil { k8sdeps.NewKustHash()).Execute(); err != nil {
os.Exit(1) os.Exit(1)

View File

@@ -65,7 +65,7 @@ func NewCmdBuild(
out io.Writer, fs fs.FileSystem, out io.Writer, fs fs.FileSystem,
kf ifc.KunstructuredFactory, kf ifc.KunstructuredFactory,
ptf patch.TransformerFactory, ptf patch.TransformerFactory,
decoder ifc.Decoder, hash ifc.Hash) *cobra.Command { hash ifc.Hash) *cobra.Command {
var o buildOptions var o buildOptions
var p string var p string
@@ -79,7 +79,7 @@ func NewCmdBuild(
if err != nil { if err != nil {
return err return err
} }
return o.RunBuild(out, fs, kf, ptf, decoder, hash) return o.RunBuild(out, fs, kf, ptf, hash)
}, },
} }
cmd.Flags().StringVarP( cmd.Flags().StringVarP(
@@ -126,7 +126,7 @@ func (o *buildOptions) RunBuild(
out io.Writer, fSys fs.FileSystem, out io.Writer, fSys fs.FileSystem,
kf ifc.KunstructuredFactory, kf ifc.KunstructuredFactory,
ptf patch.TransformerFactory, ptf patch.TransformerFactory,
decoder ifc.Decoder, hash ifc.Hash) error { hash ifc.Hash) error {
rootLoader, err := loader.NewLoader(o.kustomizationPath, "", fSys) rootLoader, err := loader.NewLoader(o.kustomizationPath, "", fSys)
if err != nil { if err != nil {
return err return err
@@ -137,7 +137,7 @@ func (o *buildOptions) RunBuild(
resmap.NewFactory(resource.NewFactory(kf)), resmap.NewFactory(resource.NewFactory(kf)),
ptf, ptf,
makeTransformerconfig(fSys, o.transformerconfigPaths), makeTransformerconfig(fSys, o.transformerconfigPaths),
decoder, hash) hash)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -131,9 +131,9 @@ func runBuildTestCase(t *testing.T, testcaseName string, updateKustomizeExpected
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
err = ops.RunBuild( err = ops.RunBuild(
buf, fSys, buf, fSys,
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder()), k8sdeps.NewKunstructuredFactoryImpl(),
patch.NewPatchTransformerFactory(), patch.NewPatchTransformerFactory(),
k8sdeps.NewKustDecoder(), k8sdeps.NewKustHash()) k8sdeps.NewKustHash())
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

@@ -33,7 +33,6 @@ import (
// NewDefaultCommand returns the default (aka root) command for kustomize command. // NewDefaultCommand returns the default (aka root) command for kustomize command.
func NewDefaultCommand( func NewDefaultCommand(
kf ifc.KunstructuredFactory, ptf patch.TransformerFactory, kf ifc.KunstructuredFactory, ptf patch.TransformerFactory,
decoder ifc.Decoder,
validator ifc.Validator, hash ifc.Hash) *cobra.Command { validator ifc.Validator, hash ifc.Hash) *cobra.Command {
fsys := fs.MakeRealFS() fsys := fs.MakeRealFS()
stdOut := os.Stdout stdOut := os.Stdout
@@ -50,7 +49,7 @@ 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, decoder, hash), build.NewCmdBuild(stdOut, fsys, kf, ptf, hash),
edit.NewCmdEdit(fsys, validator, kf), edit.NewCmdEdit(fsys, validator, kf),
misc.NewCmdConfig(fsys), misc.NewCmdConfig(fsys),
misc.NewCmdVersion(stdOut), misc.NewCmdVersion(stdOut),

View File

@@ -23,14 +23,6 @@ import (
"sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/kustomize/pkg/types"
) )
// Decoder unmarshalls byte input into an object.
type Decoder interface {
// SetInput accepts new input.
SetInput([]byte)
// Decode yields the next object from the input, else io.EOF
Decode(interface{}) error
}
// Validator provides functions to validate annotations and labels // Validator provides functions to validate annotations and labels
type Validator interface { type Validator interface {
MakeAnnotationValidator() func(map[string]string) error MakeAnnotationValidator() func(map[string]string) error

View File

@@ -32,7 +32,7 @@ import (
) )
var rf = resource.NewFactory( var rf = resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
func TestNewPatchJson6902FactoryNoTarget(t *testing.T) { func TestNewPatchJson6902FactoryNoTarget(t *testing.T) {
p := patch.Json6902{} p := patch.Json6902{}

View File

@@ -32,7 +32,7 @@ var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}
func TestJsonPatchJSONTransformer_Transform(t *testing.T) { func TestJsonPatchJSONTransformer_Transform(t *testing.T) {
rf := resource.NewFactory( rf := resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
id := resid.NewResId(deploy, "deploy1") id := resid.NewResId(deploy, "deploy1")
base := resmap.ResMap{ base := resmap.ResMap{
id: rf.FromMap( id: rf.FromMap(

View File

@@ -30,7 +30,7 @@ import (
var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"} var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}
var statefulset = gvk.Gvk{Group: "apps", Version: "v1", Kind: "StatefulSet"} var statefulset = gvk.Gvk{Group: "apps", Version: "v1", Kind: "StatefulSet"}
var rf = resource.NewFactory( var rf = resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
var rmF = NewFactory(rf) var rmF = NewFactory(rf)
func TestEncodeAsYaml(t *testing.T) { func TestEncodeAsYaml(t *testing.T) {

View File

@@ -23,7 +23,7 @@ import (
) )
var factory = NewFactory( var factory = NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
var testConfigMap = factory.FromMap( var testConfigMap = factory.FromMap(
map[string]interface{}{ map[string]interface{}{

View File

@@ -43,7 +43,6 @@ import (
// KustTarget encapsulates the entirety of a kustomization build. // KustTarget encapsulates the entirety of a kustomization build.
type KustTarget struct { type KustTarget struct {
kustomization *types.Kustomization kustomization *types.Kustomization
decoder ifc.Decoder
hash ifc.Hash hash ifc.Hash
ldr ifc.Loader ldr ifc.Loader
fSys fs.FileSystem fSys fs.FileSystem
@@ -58,7 +57,7 @@ func NewKustTarget(
rf *resmap.Factory, rf *resmap.Factory,
ptf patch.TransformerFactory, ptf patch.TransformerFactory,
tcfg *transformerconfig.TransformerConfig, tcfg *transformerconfig.TransformerConfig,
d ifc.Decoder, h ifc.Hash) (*KustTarget, error) { h ifc.Hash) (*KustTarget, error) {
content, err := ldr.Load(constants.KustomizationFileName) content, err := ldr.Load(constants.KustomizationFileName)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -76,7 +75,6 @@ func NewKustTarget(
fSys: fSys, fSys: fSys,
rf: rf, rf: rf,
tcfg: tcfg, tcfg: tcfg,
decoder: d,
hash: h, hash: h,
ptf: ptf, ptf: ptf,
}, nil }, nil
@@ -227,7 +225,7 @@ func (kt *KustTarget) loadCustomizedBases() (resmap.ResMap, *interror.Kustomizat
continue continue
} }
target, err := NewKustTarget( target, err := NewKustTarget(
ldr, kt.fSys, kt.rf, kt.ptf, kt.tcfg, kt.decoder, kt.hash) ldr, kt.fSys, kt.rf, kt.ptf, kt.tcfg, kt.hash)
if err != nil { if err != nil {
errs.Append(errors.Wrap(err, "couldn't make target for "+path)) errs.Append(errors.Wrap(err, "couldn't make target for "+path))
continue continue
@@ -257,7 +255,7 @@ func (kt *KustTarget) loadBasesAsFlatList() ([]*KustTarget, error) {
continue continue
} }
target, err := NewKustTarget( target, err := NewKustTarget(
ldr, kt.fSys, kt.rf, kt.ptf, kt.tcfg, kt.decoder, kt.hash) ldr, kt.fSys, kt.rf, kt.ptf, kt.tcfg, kt.hash)
if err != nil { if err != nil {
errs.Append(err) errs.Append(err)
continue continue

View File

@@ -89,7 +89,7 @@ metadata:
) )
var rf = resmap.NewFactory(resource.NewFactory( var rf = resmap.NewFactory(resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder()))) k8sdeps.NewKunstructuredFactoryImpl()))
func makeLoader1(t *testing.T) ifc.Loader { func makeLoader1(t *testing.T) ifc.Loader {
ldr := loadertest.NewFakeLoader("/testpath") ldr := loadertest.NewFakeLoader("/testpath")
@@ -212,7 +212,7 @@ func TestResources1(t *testing.T) {
kt, err := NewKustTarget( kt, err := NewKustTarget(
l, fakeFs, rf, patch.NewPatchTransformerFactory(), l, fakeFs, rf, patch.NewPatchTransformerFactory(),
transformerconfig.MakeDefaultTransformerConfig(), transformerconfig.MakeDefaultTransformerConfig(),
k8sdeps.NewKustDecoder(), k8sdeps.NewKustHash()) k8sdeps.NewKustHash())
if err != nil { if err != nil {
t.Fatalf("unexpected construction error %v", err) t.Fatalf("unexpected construction error %v", err)
} }
@@ -238,7 +238,7 @@ func TestResourceNotFound(t *testing.T) {
kt, err := NewKustTarget( kt, err := NewKustTarget(
l, fakeFs, rf, patch.NewPatchTransformerFactory(), l, fakeFs, rf, patch.NewPatchTransformerFactory(),
transformerconfig.MakeDefaultTransformerConfig(), transformerconfig.MakeDefaultTransformerConfig(),
k8sdeps.NewKustDecoder(), k8sdeps.NewKustHash()) k8sdeps.NewKustHash())
if err != nil { if err != nil {
t.Fatalf("Unexpected construction error %v", err) t.Fatalf("Unexpected construction error %v", err)
} }
@@ -262,7 +262,7 @@ func TestSecretTimeout(t *testing.T) {
kt, err := NewKustTarget( kt, err := NewKustTarget(
l, fakeFs, rf, patch.NewPatchTransformerFactory(), l, fakeFs, rf, patch.NewPatchTransformerFactory(),
transformerconfig.MakeDefaultTransformerConfig(), transformerconfig.MakeDefaultTransformerConfig(),
k8sdeps.NewKustDecoder(), k8sdeps.NewKustHash()) k8sdeps.NewKustHash())
if err != nil { if err != nil {
t.Fatalf("Unexpected construction error %v", err) t.Fatalf("Unexpected construction error %v", err)
} }

View File

@@ -30,7 +30,7 @@ import (
func TestImageTagTransformer(t *testing.T) { func TestImageTagTransformer(t *testing.T) {
var rf = resource.NewFactory( var rf = resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
m := resmap.ResMap{ m := resmap.ResMap{
resid.NewResId(deploy, "deploy1"): rf.FromMap( resid.NewResId(deploy, "deploy1"): rf.FromMap(

View File

@@ -42,7 +42,7 @@ var crb = gvk.Gvk{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "Clus
var sa = gvk.Gvk{Version: "v1", Kind: "ServiceAccount"} var sa = gvk.Gvk{Version: "v1", Kind: "ServiceAccount"}
var ingress = gvk.Gvk{Kind: "Ingress"} var ingress = gvk.Gvk{Kind: "Ingress"}
var rf = resource.NewFactory( var rf = resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
func TestLabelsRun(t *testing.T) { func TestLabelsRun(t *testing.T) {
m := resmap.ResMap{ m := resmap.ResMap{

View File

@@ -29,7 +29,7 @@ import (
func TestNameHashTransformer(t *testing.T) { func TestNameHashTransformer(t *testing.T) {
rf := resource.NewFactory( rf := resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
objs := resmap.ResMap{ objs := resmap.ResMap{
resid.NewResId(cmap, "cm1"): rf.FromMap( resid.NewResId(cmap, "cm1"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{

View File

@@ -29,7 +29,7 @@ import (
func TestNameReferenceRun(t *testing.T) { func TestNameReferenceRun(t *testing.T) {
rf := resource.NewFactory( rf := resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
m := resmap.ResMap{ m := resmap.ResMap{
resid.NewResId(cmap, "cm1"): rf.FromMap( resid.NewResId(cmap, "cm1"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{

View File

@@ -29,7 +29,7 @@ import (
func TestNamespaceRun(t *testing.T) { func TestNamespaceRun(t *testing.T) {
rf := resource.NewFactory( rf := resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
m := resmap.ResMap{ m := resmap.ResMap{
resid.NewResId(cmap, "cm1"): rf.FromMap( resid.NewResId(cmap, "cm1"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{

View File

@@ -29,7 +29,7 @@ import (
func TestPrefixNameRun(t *testing.T) { func TestPrefixNameRun(t *testing.T) {
rf := resource.NewFactory( rf := resource.NewFactory(
k8sdeps.NewKunstructuredFactoryImpl(k8sdeps.NewKustDecoder())) k8sdeps.NewKunstructuredFactoryImpl())
m := resmap.ResMap{ m := resmap.ResMap{
resid.NewResId(cmap, "cm1"): rf.FromMap( resid.NewResId(cmap, "cm1"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{