mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Move crd package to transformerconfig.factory.
This commit is contained in:
@@ -28,7 +28,6 @@ import (
|
|||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/crds"
|
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/pkg/ifc/patch"
|
"sigs.k8s.io/kustomize/pkg/ifc/patch"
|
||||||
interror "sigs.k8s.io/kustomize/pkg/internal/error"
|
interror "sigs.k8s.io/kustomize/pkg/internal/error"
|
||||||
@@ -136,11 +135,11 @@ func (kt *KustTarget) loadCustomizedResMap() (resmap.ResMap, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
errs.Append(errors.Wrap(err, "loadResMapFromBasesAndResources"))
|
errs.Append(errors.Wrap(err, "loadResMapFromBasesAndResources"))
|
||||||
}
|
}
|
||||||
crdPathConfigs, err := crds.RegisterCRDs(
|
crdPathConfigs, err := transformerconfig.
|
||||||
transformerconfig.NewFactory(kt.ldr), kt.kustomization.Crds)
|
NewFactory(kt.ldr).LoadCRDs(kt.kustomization.Crds)
|
||||||
kt.tcfg = kt.tcfg.Merge(crdPathConfigs)
|
kt.tcfg = kt.tcfg.Merge(crdPathConfigs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs.Append(errors.Wrap(err, "RegisterCRDs"))
|
errs.Append(errors.Wrap(err, "LoadCRDs"))
|
||||||
}
|
}
|
||||||
resMap, err := kt.generateConfigMapsAndSecrets(errs)
|
resMap, err := kt.generateConfigMapsAndSecrets(errs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package crds
|
package transformerconfig
|
||||||
|
|
||||||
// Annotation is to mark a field as annotations.
|
// Annotation is to mark a field as annotations.
|
||||||
// "x-kubernetes-annotation": ""
|
// "x-kubernetes-annotation": ""
|
||||||
@@ -26,18 +26,18 @@ import (
|
|||||||
|
|
||||||
// Factory makes instances of TransformerConfig.
|
// Factory makes instances of TransformerConfig.
|
||||||
type Factory struct {
|
type Factory struct {
|
||||||
loader ifc.Loader
|
ldr ifc.Loader
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFactory(l ifc.Loader) *Factory {
|
func NewFactory(l ifc.Loader) *Factory {
|
||||||
return &Factory{loader: l}
|
return &Factory{ldr: l}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tf *Factory) Loader() ifc.Loader {
|
func (tf *Factory) loader() ifc.Loader {
|
||||||
if tf.loader.(ifc.Loader) == nil {
|
if tf.ldr.(ifc.Loader) == nil {
|
||||||
log.Fatal("no loader")
|
log.Fatal("no loader")
|
||||||
}
|
}
|
||||||
return tf.loader
|
return tf.ldr
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromFiles returns a TranformerConfig object from a list of files
|
// FromFiles returns a TranformerConfig object from a list of files
|
||||||
@@ -45,7 +45,7 @@ func (tf *Factory) FromFiles(
|
|||||||
paths []string) (*TransformerConfig, error) {
|
paths []string) (*TransformerConfig, error) {
|
||||||
result := &TransformerConfig{}
|
result := &TransformerConfig{}
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
data, err := tf.Loader().Load(path)
|
data, err := tf.loader().Load(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Package crds read in files for CRD schemas and parse annotations from it
|
package transformerconfig
|
||||||
package crds
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -24,15 +23,13 @@ import (
|
|||||||
"github.com/ghodss/yaml"
|
"github.com/ghodss/yaml"
|
||||||
"k8s.io/kube-openapi/pkg/common"
|
"k8s.io/kube-openapi/pkg/common"
|
||||||
"sigs.k8s.io/kustomize/pkg/gvk"
|
"sigs.k8s.io/kustomize/pkg/gvk"
|
||||||
"sigs.k8s.io/kustomize/pkg/transformerconfig"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// RegisterCRDs parse CRD schemas from paths and update various pathConfigs
|
// LoadCRDs parse CRD schemas from paths into a TransformerConfig
|
||||||
func RegisterCRDs(
|
func (tf *Factory) LoadCRDs(paths []string) (*TransformerConfig, error) {
|
||||||
tf *transformerconfig.Factory, paths []string) (*transformerconfig.TransformerConfig, error) {
|
|
||||||
pathConfigs := tf.EmptyConfig()
|
pathConfigs := tf.EmptyConfig()
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
pathConfig, err := registerCRD(tf, path)
|
pathConfig, err := tf.loadCRD(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -41,10 +38,9 @@ func RegisterCRDs(
|
|||||||
return pathConfigs, nil
|
return pathConfigs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// register CRD from one path
|
func (tf *Factory) loadCRD(path string) (*TransformerConfig, error) {
|
||||||
func registerCRD(tf *transformerconfig.Factory, path string) (*transformerconfig.TransformerConfig, error) {
|
|
||||||
result := tf.EmptyConfig()
|
result := tf.EmptyConfig()
|
||||||
content, err := tf.Loader().Load(path)
|
content, err := tf.loader().Load(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return result, err
|
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
|
// getCRDPathConfig gets pathConfigs for one CRD recursively
|
||||||
func getCRDPathConfig(
|
func getCRDPathConfig(
|
||||||
types map[string]common.OpenAPIDefinition, atype string, crd string, in gvk.Gvk,
|
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 {
|
if _, ok := types[crd]; !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -104,7 +100,7 @@ func getCRDPathConfig(
|
|||||||
_, annotate := property.Extensions.GetString(Annotation)
|
_, annotate := property.Extensions.GetString(Annotation)
|
||||||
if annotate {
|
if annotate {
|
||||||
configs.AddAnnotationPathConfig(
|
configs.AddAnnotationPathConfig(
|
||||||
transformerconfig.PathConfig{
|
PathConfig{
|
||||||
CreateIfNotPresent: false,
|
CreateIfNotPresent: false,
|
||||||
Gvk: in,
|
Gvk: in,
|
||||||
Path: strings.Join(append(path, propname), "/"),
|
Path: strings.Join(append(path, propname), "/"),
|
||||||
@@ -114,7 +110,7 @@ func getCRDPathConfig(
|
|||||||
_, label := property.Extensions.GetString(LabelSelector)
|
_, label := property.Extensions.GetString(LabelSelector)
|
||||||
if label {
|
if label {
|
||||||
configs.AddLabelPathConfig(
|
configs.AddLabelPathConfig(
|
||||||
transformerconfig.PathConfig{
|
PathConfig{
|
||||||
CreateIfNotPresent: false,
|
CreateIfNotPresent: false,
|
||||||
Gvk: in,
|
Gvk: in,
|
||||||
Path: strings.Join(append(path, propname), "/"),
|
Path: strings.Join(append(path, propname), "/"),
|
||||||
@@ -124,7 +120,7 @@ func getCRDPathConfig(
|
|||||||
_, identity := property.Extensions.GetString(Identity)
|
_, identity := property.Extensions.GetString(Identity)
|
||||||
if identity {
|
if identity {
|
||||||
configs.AddPrefixPathConfig(
|
configs.AddPrefixPathConfig(
|
||||||
transformerconfig.PathConfig{
|
PathConfig{
|
||||||
CreateIfNotPresent: false,
|
CreateIfNotPresent: false,
|
||||||
Gvk: in,
|
Gvk: in,
|
||||||
Path: strings.Join(append(path, propname), "/"),
|
Path: strings.Join(append(path, propname), "/"),
|
||||||
@@ -139,9 +135,9 @@ func getCRDPathConfig(
|
|||||||
if !ok {
|
if !ok {
|
||||||
nameKey = "name"
|
nameKey = "name"
|
||||||
}
|
}
|
||||||
configs.AddNamereferencePathConfig(transformerconfig.ReferencePathConfig{
|
configs.AddNamereferencePathConfig(ReferencePathConfig{
|
||||||
Gvk: gvk.Gvk{Kind: kind, Version: version},
|
Gvk: gvk.Gvk{Kind: kind, Version: version},
|
||||||
PathConfigs: []transformerconfig.PathConfig{
|
PathConfigs: []PathConfig{
|
||||||
{
|
{
|
||||||
CreateIfNotPresent: false,
|
CreateIfNotPresent: false,
|
||||||
Gvk: in,
|
Gvk: in,
|
||||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package crds
|
package transformerconfig
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -23,7 +23,6 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/pkg/gvk"
|
"sigs.k8s.io/kustomize/pkg/gvk"
|
||||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
|
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
|
||||||
"sigs.k8s.io/kustomize/pkg/transformerconfig"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -149,11 +148,11 @@ func makeLoader(t *testing.T) ifc.Loader {
|
|||||||
return ldr
|
return ldr
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRegisterCRD(t *testing.T) {
|
func TestLoadCRDs(t *testing.T) {
|
||||||
refpathconfigs := []transformerconfig.ReferencePathConfig{
|
refpathconfigs := []ReferencePathConfig{
|
||||||
{
|
{
|
||||||
Gvk: gvk.Gvk{Kind: "Secret", Version: "v1"},
|
Gvk: gvk.Gvk{Kind: "Secret", Version: "v1"},
|
||||||
PathConfigs: []transformerconfig.PathConfig{
|
PathConfigs: []PathConfig{
|
||||||
{
|
{
|
||||||
CreateIfNotPresent: false,
|
CreateIfNotPresent: false,
|
||||||
Gvk: gvk.Gvk{Kind: "MyKind"},
|
Gvk: gvk.Gvk{Kind: "MyKind"},
|
||||||
@@ -163,7 +162,7 @@ func TestRegisterCRD(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
Gvk: gvk.Gvk{Kind: "Bee", Version: "v1beta1"},
|
Gvk: gvk.Gvk{Kind: "Bee", Version: "v1beta1"},
|
||||||
PathConfigs: []transformerconfig.PathConfig{
|
PathConfigs: []PathConfig{
|
||||||
{
|
{
|
||||||
CreateIfNotPresent: false,
|
CreateIfNotPresent: false,
|
||||||
Gvk: gvk.Gvk{Kind: "MyKind"},
|
Gvk: gvk.Gvk{Kind: "MyKind"},
|
||||||
@@ -173,12 +172,12 @@ func TestRegisterCRD(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := &transformerconfig.TransformerConfig{
|
expected := &TransformerConfig{
|
||||||
NameReference: refpathconfigs,
|
NameReference: refpathconfigs,
|
||||||
}
|
}
|
||||||
|
|
||||||
pathconfig, _ := RegisterCRDs(
|
pathconfig, _ := NewFactory(makeLoader(t)).LoadCRDs(
|
||||||
transformerconfig.NewFactory(makeLoader(t)), []string{"/testpath/crd.json"})
|
[]string{"/testpath/crd.json"})
|
||||||
|
|
||||||
if !reflect.DeepEqual(pathconfig, expected) {
|
if !reflect.DeepEqual(pathconfig, expected) {
|
||||||
t.Fatalf("expected\n %v\n but got\n %v\n", expected, pathconfig)
|
t.Fatalf("expected\n %v\n but got\n %v\n", expected, pathconfig)
|
||||||
Reference in New Issue
Block a user