Move crd package to transformerconfig.factory.

This commit is contained in:
Jeffrey Regan
2018-10-10 12:53:53 -07:00
parent 681e2bf213
commit 8534107fc8
5 changed files with 30 additions and 36 deletions

View File

@@ -28,7 +28,6 @@ import (
"github.com/golang/glog"
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/crds"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc/patch"
interror "sigs.k8s.io/kustomize/pkg/internal/error"
@@ -136,11 +135,11 @@ func (kt *KustTarget) loadCustomizedResMap() (resmap.ResMap, error) {
if err != nil {
errs.Append(errors.Wrap(err, "loadResMapFromBasesAndResources"))
}
crdPathConfigs, err := crds.RegisterCRDs(
transformerconfig.NewFactory(kt.ldr), kt.kustomization.Crds)
crdPathConfigs, err := transformerconfig.
NewFactory(kt.ldr).LoadCRDs(kt.kustomization.Crds)
kt.tcfg = kt.tcfg.Merge(crdPathConfigs)
if err != nil {
errs.Append(errors.Wrap(err, "RegisterCRDs"))
errs.Append(errors.Wrap(err, "LoadCRDs"))
}
resMap, err := kt.generateConfigMapsAndSecrets(errs)
if err != nil {

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package crds
package transformerconfig
// Annotation is to mark a field as annotations.
// "x-kubernetes-annotation": ""

View File

@@ -26,18 +26,18 @@ import (
// Factory makes instances of TransformerConfig.
type Factory struct {
loader ifc.Loader
ldr ifc.Loader
}
func NewFactory(l ifc.Loader) *Factory {
return &Factory{loader: l}
return &Factory{ldr: l}
}
func (tf *Factory) Loader() ifc.Loader {
if tf.loader.(ifc.Loader) == nil {
func (tf *Factory) loader() ifc.Loader {
if tf.ldr.(ifc.Loader) == nil {
log.Fatal("no loader")
}
return tf.loader
return tf.ldr
}
// FromFiles returns a TranformerConfig object from a list of files
@@ -45,7 +45,7 @@ func (tf *Factory) FromFiles(
paths []string) (*TransformerConfig, error) {
result := &TransformerConfig{}
for _, path := range paths {
data, err := tf.Loader().Load(path)
data, err := tf.loader().Load(path)
if err != nil {
return nil, err
}

View File

@@ -14,8 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Package crds read in files for CRD schemas and parse annotations from it
package crds
package transformerconfig
import (
"encoding/json"
@@ -24,15 +23,13 @@ import (
"github.com/ghodss/yaml"
"k8s.io/kube-openapi/pkg/common"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
)
// RegisterCRDs parse CRD schemas from paths and update various pathConfigs
func RegisterCRDs(
tf *transformerconfig.Factory, paths []string) (*transformerconfig.TransformerConfig, error) {
// LoadCRDs parse CRD schemas from paths into a TransformerConfig
func (tf *Factory) LoadCRDs(paths []string) (*TransformerConfig, error) {
pathConfigs := tf.EmptyConfig()
for _, path := range paths {
pathConfig, err := registerCRD(tf, path)
pathConfig, err := tf.loadCRD(path)
if err != nil {
return nil, err
}
@@ -41,10 +38,9 @@ func RegisterCRDs(
return pathConfigs, nil
}
// register CRD from one path
func registerCRD(tf *transformerconfig.Factory, path string) (*transformerconfig.TransformerConfig, error) {
func (tf *Factory) loadCRD(path string) (*TransformerConfig, error) {
result := tf.EmptyConfig()
content, err := tf.Loader().Load(path)
content, err := tf.loader().Load(path)
if err != nil {
return result, err
}
@@ -95,7 +91,7 @@ func getCRDs(types map[string]common.OpenAPIDefinition) map[string]gvk.Gvk {
// getCRDPathConfig gets pathConfigs for one CRD recursively
func getCRDPathConfig(
types map[string]common.OpenAPIDefinition, atype string, crd string, in gvk.Gvk,
path []string, configs *transformerconfig.TransformerConfig) error {
path []string, configs *TransformerConfig) error {
if _, ok := types[crd]; !ok {
return nil
}
@@ -104,7 +100,7 @@ func getCRDPathConfig(
_, annotate := property.Extensions.GetString(Annotation)
if annotate {
configs.AddAnnotationPathConfig(
transformerconfig.PathConfig{
PathConfig{
CreateIfNotPresent: false,
Gvk: in,
Path: strings.Join(append(path, propname), "/"),
@@ -114,7 +110,7 @@ func getCRDPathConfig(
_, label := property.Extensions.GetString(LabelSelector)
if label {
configs.AddLabelPathConfig(
transformerconfig.PathConfig{
PathConfig{
CreateIfNotPresent: false,
Gvk: in,
Path: strings.Join(append(path, propname), "/"),
@@ -124,7 +120,7 @@ func getCRDPathConfig(
_, identity := property.Extensions.GetString(Identity)
if identity {
configs.AddPrefixPathConfig(
transformerconfig.PathConfig{
PathConfig{
CreateIfNotPresent: false,
Gvk: in,
Path: strings.Join(append(path, propname), "/"),
@@ -139,9 +135,9 @@ func getCRDPathConfig(
if !ok {
nameKey = "name"
}
configs.AddNamereferencePathConfig(transformerconfig.ReferencePathConfig{
configs.AddNamereferencePathConfig(ReferencePathConfig{
Gvk: gvk.Gvk{Kind: kind, Version: version},
PathConfigs: []transformerconfig.PathConfig{
PathConfigs: []PathConfig{
{
CreateIfNotPresent: false,
Gvk: in,

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package crds
package transformerconfig
import (
"reflect"
@@ -23,7 +23,6 @@ import (
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
"sigs.k8s.io/kustomize/pkg/transformerconfig"
)
const (
@@ -149,11 +148,11 @@ func makeLoader(t *testing.T) ifc.Loader {
return ldr
}
func TestRegisterCRD(t *testing.T) {
refpathconfigs := []transformerconfig.ReferencePathConfig{
func TestLoadCRDs(t *testing.T) {
refpathconfigs := []ReferencePathConfig{
{
Gvk: gvk.Gvk{Kind: "Secret", Version: "v1"},
PathConfigs: []transformerconfig.PathConfig{
PathConfigs: []PathConfig{
{
CreateIfNotPresent: false,
Gvk: gvk.Gvk{Kind: "MyKind"},
@@ -163,7 +162,7 @@ func TestRegisterCRD(t *testing.T) {
},
{
Gvk: gvk.Gvk{Kind: "Bee", Version: "v1beta1"},
PathConfigs: []transformerconfig.PathConfig{
PathConfigs: []PathConfig{
{
CreateIfNotPresent: false,
Gvk: gvk.Gvk{Kind: "MyKind"},
@@ -173,12 +172,12 @@ func TestRegisterCRD(t *testing.T) {
},
}
expected := &transformerconfig.TransformerConfig{
expected := &TransformerConfig{
NameReference: refpathconfigs,
}
pathconfig, _ := RegisterCRDs(
transformerconfig.NewFactory(makeLoader(t)), []string{"/testpath/crd.json"})
pathconfig, _ := NewFactory(makeLoader(t)).LoadCRDs(
[]string{"/testpath/crd.json"})
if !reflect.DeepEqual(pathconfig, expected) {
t.Fatalf("expected\n %v\n but got\n %v\n", expected, pathconfig)