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

View File

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

View File

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

View File

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