Skip to content

Support allowNameChange and allowKindChange for patches #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/data-sources/overlay.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ Define [Kustomize patches](https://kubectl.docs.kubernetes.io/references/kustomi
- `path` path to a patch file on disk
- `patch` patch defined as an inline string
- `target` patch target, specified by: `group`, `version`, `kind`, `name`, `namespace`, `label_selector`, `annotation_selector`
- `options` - set `allow_kind_change` and/or `allow_name_change` to `true` to allow `kind` or `metadata.name` to be changed by the patch
(only relevant for strategic merge patches, JSON patches ignore this setting)

#### Example

Expand All @@ -341,6 +343,21 @@ data "kustomization_overlay" "example" {
}
}

patches {
target = {
kind = "Namespace"
name = "test-ns"
}
patch = <<-EOF
kind: Namespace
metadata:
name: new-ns
EOF
options {
allow_name_change = true
}
}

patches {
patch = <<-EOF
- op: replace
Expand Down
32 changes: 32 additions & 0 deletions kustomize/data_source_kustomization_overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ func getGeneratorOptions(o map[string]interface{}) *types.GeneratorOptions {
return g
}

func getPatchOptionsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"allow_kind_change": {
Type: schema.TypeBool,
Optional: true,
},
"allow_name_change": {
Type: schema.TypeBool,
Optional: true,
},
},
}
}

func getPatchOptions(o map[string]interface{}) map[string]bool {
po := make(map[string]bool)
po["allowKindChange"] = o["allow_kind_change"].(bool)
po["allowNameChange"] = o["allow_name_change"].(bool)
return po
}

func dataSourceKustomizationOverlay() *schema.Resource {
return &schema.Resource{
Read: kustomizationOverlay,
Expand Down Expand Up @@ -186,6 +208,12 @@ func dataSourceKustomizationOverlay() *schema.Resource {
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"options": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: getPatchOptionsSchema(),
},
"path": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -551,6 +579,10 @@ func getKustomization(d *schema.ResourceData) (k types.Kustomization) {
kp.Target.AnnotationSelector = t["annotation_selector"]
kp.Target.LabelSelector = t["label_selector"]
}
o := p["options"].([]interface{})
if len(o) == 1 && o[0] != nil {
kp.Options = getPatchOptions(o[0].(map[string]interface{}))
}

k.Patches = append(k.Patches, kp)
}
Expand Down
92 changes: 92 additions & 0 deletions kustomize/data_source_kustomization_overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,3 +907,95 @@ output "check_s" {
}
`, modulePath, modulePath)
}

//
//
// Test patch options attr
func TestDataSourceKustomizationOverlay_patchOptionsNoop(t *testing.T) {

resource.Test(t, resource.TestCase{
IsUnitTest: true,
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testKustomizationPatchOptionsConfigNoop(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("check_ns", `{"apiVersion":"v1","kind":"Namespace","metadata":{"name":"test-basic"}}`),
),
},
},
})
}

func testKustomizationPatchOptionsConfigNoop() string {
return `
data "kustomization_overlay" "test" {
resources = [
"test_kustomizations/basic/initial",
]
patches {
target = {
kind = "Namespace"
name = "test-basic"
}
patch = <<-EOF
kind: Namespace
metadata:
name: new-basic
EOF
options {
allow_name_change = false
}
}
}

output "check_ns" {
value = data.kustomization_overlay.test.manifests["_/Namespace/_/test-basic"]
}
`
}

// Test patch options attr(
func TestDataSourceKustomizationOverlay_patchOptions(t *testing.T) {

resource.Test(t, resource.TestCase{
IsUnitTest: true,
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testKustomizationPatchOptionsConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckOutput("check_ns", `{"apiVersion":"v1","kind":"Namespace","metadata":{"name":"new-basic"}}`),
),
},
},
})
}

func testKustomizationPatchOptionsConfig() string {
return `
data "kustomization_overlay" "test" {
resources = [
"test_kustomizations/basic/initial",
]
patches {
target = {
kind = "Namespace"
name = "test-basic"
}
patch = <<-EOF
kind: Namespace
metadata:
name: new-basic
EOF
options {
allow_name_change = true
}
}
}

output "check_ns" {
value = data.kustomization_overlay.test.manifests["_/Namespace/_/new-basic"]
}
`
}