mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
@@ -7,13 +7,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filters/namespace"
|
"sigs.k8s.io/kustomize/api/filters/namespace"
|
||||||
"sigs.k8s.io/kustomize/api/transform"
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
|
"sigs.k8s.io/kustomize/api/transform"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,8 +22,9 @@ type NamespaceTransformerPlugin struct {
|
|||||||
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||||
|
|
||||||
// YAMLSupport can be set to true to use the kyaml filter instead of the
|
// YAMLSupport can be set to true to use the kyaml filter instead of the
|
||||||
// kunstruct transformer
|
// kunstruct transformer.
|
||||||
YAMLSupport bool
|
// TODO: change the default to use kyaml when it is stable
|
||||||
|
YAMLSupport bool `json:"yamlSupport,omitempty" yaml:"yamlSupport,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *NamespaceTransformerPlugin) Config(
|
func (p *NamespaceTransformerPlugin) Config(
|
||||||
|
|||||||
@@ -8,13 +8,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/filters/namespace"
|
"sigs.k8s.io/kustomize/api/filters/namespace"
|
||||||
"sigs.k8s.io/kustomize/api/transform"
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/resid"
|
"sigs.k8s.io/kustomize/api/resid"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/resource"
|
"sigs.k8s.io/kustomize/api/resource"
|
||||||
|
"sigs.k8s.io/kustomize/api/transform"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/filtersutil"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,8 +23,9 @@ type plugin struct {
|
|||||||
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||||
|
|
||||||
// YAMLSupport can be set to true to use the kyaml filter instead of the
|
// YAMLSupport can be set to true to use the kyaml filter instead of the
|
||||||
// kunstruct transformer
|
// kunstruct transformer.
|
||||||
YAMLSupport bool
|
// TODO: change the default to use kyaml when it is stable
|
||||||
|
YAMLSupport bool `json:"yamlSupport,omitempty" yaml:"yamlSupport,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection GoUnusedGlobalVariable
|
//noinspection GoUnusedGlobalVariable
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package main_test
|
package main_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -11,16 +12,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestNamespaceTransformer1(t *testing.T) {
|
func TestNamespaceTransformer1(t *testing.T) {
|
||||||
th := kusttest_test.MakeEnhancedHarness(t).
|
for _, b := range []bool{true, false} {
|
||||||
PrepBuiltin("NamespaceTransformer")
|
t.Run(fmt.Sprintf("yaml-%v", b), func(t *testing.T) {
|
||||||
defer th.Reset()
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
|
PrepBuiltin("NamespaceTransformer")
|
||||||
rm := th.LoadAndRunTransformer(`
|
defer th.Reset()
|
||||||
|
rm := th.LoadAndRunTransformer(fmt.Sprintf(`
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: NamespaceTransformer
|
kind: NamespaceTransformer
|
||||||
metadata:
|
metadata:
|
||||||
name: notImportantHere
|
name: notImportantHere
|
||||||
namespace: test
|
namespace: test
|
||||||
|
yamlSupport: %v
|
||||||
fieldSpecs:
|
fieldSpecs:
|
||||||
- path: metadata/namespace
|
- path: metadata/namespace
|
||||||
create: true
|
create: true
|
||||||
@@ -30,7 +33,7 @@ fieldSpecs:
|
|||||||
- path: subjects
|
- path: subjects
|
||||||
kind: ClusterRoleBinding
|
kind: ClusterRoleBinding
|
||||||
group: rbac.authorization.k8s.io
|
group: rbac.authorization.k8s.io
|
||||||
`, `
|
`, b), `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
@@ -101,16 +104,16 @@ metadata:
|
|||||||
name: crd
|
name: crd
|
||||||
`)
|
`)
|
||||||
|
|
||||||
// Import note: The namespace transformer is in charge of
|
// Import note: The namespace transformer is in charge of
|
||||||
// the metadata.namespace field. The namespace transformer SHOULD
|
// the metadata.namespace field. The namespace transformer SHOULD
|
||||||
// NOT modify neither the "namespace" subfield within the
|
// NOT modify neither the "namespace" subfield within the
|
||||||
// ClusterRoleBinding.subjects field nor the "namespace"
|
// ClusterRoleBinding.subjects field nor the "namespace"
|
||||||
// subfield in the ValidatingWebhookConfiguration.webhooks field.
|
// subfield in the ValidatingWebhookConfiguration.webhooks field.
|
||||||
// This is the role of the namereference Transformer to handle
|
// This is the role of the namereference Transformer to handle
|
||||||
// object reference changes (prefix/suffix and namespace).
|
// object reference changes (prefix/suffix and namespace).
|
||||||
// For use cases involving simultaneous change of name and namespace,
|
// For use cases involving simultaneous change of name and namespace,
|
||||||
// refer to namespaces tests in pkg/target test suites.
|
// refer to namespaces tests in pkg/target test suites.
|
||||||
th.AssertActualEqualsExpected(rm, `
|
th.AssertActualEqualsExpected(rm, `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
@@ -182,14 +185,18 @@ kind: CustomResourceDefinition
|
|||||||
metadata:
|
metadata:
|
||||||
name: crd
|
name: crd
|
||||||
`)
|
`)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceTransformerClusterLevelKinds(t *testing.T) {
|
func TestNamespaceTransformerClusterLevelKinds(t *testing.T) {
|
||||||
th := kusttest_test.MakeEnhancedHarness(t).
|
for _, b := range []bool{true, false} {
|
||||||
PrepBuiltin("NamespaceTransformer")
|
t.Run(fmt.Sprintf("yaml-%v", b), func(t *testing.T) {
|
||||||
defer th.Reset()
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
|
PrepBuiltin("NamespaceTransformer")
|
||||||
|
defer th.Reset()
|
||||||
|
|
||||||
const noChangeExpected = `
|
const noChangeExpected = `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Namespace
|
kind: Namespace
|
||||||
metadata:
|
metadata:
|
||||||
@@ -211,12 +218,13 @@ kind: PersistentVolume
|
|||||||
metadata:
|
metadata:
|
||||||
name: pv1
|
name: pv1
|
||||||
`
|
`
|
||||||
rm := th.LoadAndRunTransformer(`
|
rm := th.LoadAndRunTransformer(fmt.Sprintf(`
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: NamespaceTransformer
|
kind: NamespaceTransformer
|
||||||
metadata:
|
metadata:
|
||||||
name: notImportantHere
|
name: notImportantHere
|
||||||
namespace: test
|
namespace: test
|
||||||
|
yamlSupport: %v
|
||||||
fieldSpecs:
|
fieldSpecs:
|
||||||
- path: metadata/namespace
|
- path: metadata/namespace
|
||||||
create: true
|
create: true
|
||||||
@@ -226,26 +234,32 @@ fieldSpecs:
|
|||||||
- path: subjects
|
- path: subjects
|
||||||
kind: ClusterRoleBinding
|
kind: ClusterRoleBinding
|
||||||
group: rbac.authorization.k8s.io
|
group: rbac.authorization.k8s.io
|
||||||
`, noChangeExpected)
|
`, b), noChangeExpected)
|
||||||
|
|
||||||
th.AssertActualEqualsExpected(rm, noChangeExpected)
|
th.AssertActualEqualsExpected(rm, noChangeExpected)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNamespaceTransformerObjectConflict(t *testing.T) {
|
func TestNamespaceTransformerObjectConflict(t *testing.T) {
|
||||||
th := kusttest_test.MakeEnhancedHarness(t).
|
for _, b := range []bool{true, false} {
|
||||||
PrepBuiltin("NamespaceTransformer")
|
t.Run(fmt.Sprintf("yaml-%v", b), func(t *testing.T) {
|
||||||
defer th.Reset()
|
|
||||||
|
|
||||||
err := th.ErrorFromLoadAndRunTransformer(`
|
th := kusttest_test.MakeEnhancedHarness(t).
|
||||||
|
PrepBuiltin("NamespaceTransformer")
|
||||||
|
defer th.Reset()
|
||||||
|
|
||||||
|
err := th.ErrorFromLoadAndRunTransformer(fmt.Sprintf(`
|
||||||
apiVersion: builtin
|
apiVersion: builtin
|
||||||
kind: NamespaceTransformer
|
kind: NamespaceTransformer
|
||||||
metadata:
|
metadata:
|
||||||
name: notImportantHere
|
name: notImportantHere
|
||||||
namespace: test
|
namespace: test
|
||||||
|
yamlSupport: %v
|
||||||
fieldSpecs:
|
fieldSpecs:
|
||||||
- path: metadata/namespace
|
- path: metadata/namespace
|
||||||
create: true
|
create: true
|
||||||
`, `
|
`, b), `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
@@ -259,10 +273,12 @@ metadata:
|
|||||||
namespace: bar
|
namespace: bar
|
||||||
`)
|
`)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error")
|
t.Fatal("expected error")
|
||||||
}
|
}
|
||||||
if !strings.Contains(err.Error(), "ID conflict") {
|
if !strings.Contains(err.Error(), "ID conflict") {
|
||||||
t.Fatalf("unexpected error: %s", err.Error())
|
t.Fatalf("unexpected error: %s", err.Error())
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user