mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-22 23:07:00 +00:00
Refactor the existing base, so that one of the overlays patches an already present field of the base resource. Previously, all overlays added a new field in the base deployment, which made this case easier to solve, with a merge of the produced YAMLs from each overlay. Also, fix a typo in the comments. Signed-off-by: Alex Pyrgiotis <apyrgio@arrikto.com> Signed-off-by: Ioannis Androulidakis <ioannis@arrikto.com>
214 lines
4.4 KiB
Go
214 lines
4.4 KiB
Go
// Copyright 2019 The Kubernetes Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package target_test
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
kusttest_test "sigs.k8s.io/kustomize/v3/pkg/kusttest"
|
|
)
|
|
|
|
const patchAddProbe = `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: my-deployment
|
|
spec:
|
|
template:
|
|
spec:
|
|
containers:
|
|
- name: my-deployment
|
|
livenessProbe:
|
|
httpGet:
|
|
path: /healthz
|
|
port: 8080
|
|
`
|
|
|
|
const patchDnsPolicy = `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: my-deployment
|
|
spec:
|
|
template:
|
|
spec:
|
|
dnsPolicy: ClusterFirst
|
|
`
|
|
|
|
const patchRestartPolicy = `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: my-deployment
|
|
spec:
|
|
template:
|
|
spec:
|
|
restartPolicy: Always
|
|
`
|
|
|
|
// Here's a composite kustomization, that combines multiple overlays
|
|
// (probes, dns and restart policies) which patch the same base resource.
|
|
//
|
|
// The base resource is a deployment and the overlays patch aspects
|
|
// of it, without using any of the `namePrefix`, `nameSuffix` or `namespace`
|
|
// kustomization keywords.
|
|
//
|
|
// composite
|
|
// / | \
|
|
// probe dns restart
|
|
// \ | /
|
|
// base
|
|
//
|
|
func writeDiamondCompositionBase(th *kusttest_test.KustTestHarness) {
|
|
th.WriteK("/app/base", `
|
|
resources:
|
|
- deployment.yaml
|
|
`)
|
|
|
|
th.WriteF("/app/base/deployment.yaml", `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: my-deployment
|
|
spec:
|
|
template:
|
|
spec:
|
|
dnsPolicy: "None"
|
|
containers:
|
|
- name: my-deployment
|
|
image: my-image
|
|
`)
|
|
}
|
|
|
|
func writeProbeOverlay(th *kusttest_test.KustTestHarness) {
|
|
th.WriteK("/app/probe", `
|
|
resources:
|
|
- ../base
|
|
patchesStrategicMerge:
|
|
- dep-patch.yaml
|
|
`)
|
|
th.WriteF("/app/probe/dep-patch.yaml", patchAddProbe)
|
|
}
|
|
|
|
func writeDNSOverlay(th *kusttest_test.KustTestHarness) {
|
|
th.WriteK("/app/dns", `
|
|
resources:
|
|
- ../base
|
|
patchesStrategicMerge:
|
|
- dep-patch.yaml
|
|
`)
|
|
th.WriteF("/app/dns/dep-patch.yaml", patchDnsPolicy)
|
|
}
|
|
|
|
func writeRestartOverlay(th *kusttest_test.KustTestHarness) {
|
|
th.WriteK("/app/restart", `
|
|
resources:
|
|
- ../base
|
|
patchesStrategicMerge:
|
|
- dep-patch.yaml
|
|
`)
|
|
th.WriteF("/app/restart/dep-patch.yaml", patchRestartPolicy)
|
|
}
|
|
|
|
func TestCompositeDiamond(t *testing.T) {
|
|
th := kusttest_test.NewKustTestHarness(t, "/app/composite")
|
|
writeDiamondCompositionBase(th)
|
|
writeProbeOverlay(th)
|
|
writeDNSOverlay(th)
|
|
writeRestartOverlay(th)
|
|
|
|
th.WriteK("/app/composite", `
|
|
resources:
|
|
- ../probe
|
|
- ../dns
|
|
- ../restart
|
|
`)
|
|
|
|
_, err := th.MakeKustTarget().MakeCustomizedResMap()
|
|
if err == nil {
|
|
t.Fatalf("Expected resource accumulation error")
|
|
}
|
|
if !strings.Contains(
|
|
err.Error(), "already registered id: apps_v1_Deployment|~X|my-deployment") {
|
|
t.Fatalf("Unexpected err: %v", err)
|
|
}
|
|
}
|
|
|
|
const expectedPatchedDeployment = `
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: my-deployment
|
|
spec:
|
|
template:
|
|
spec:
|
|
containers:
|
|
- image: my-image
|
|
livenessProbe:
|
|
httpGet:
|
|
path: /healthz
|
|
port: 8080
|
|
name: my-deployment
|
|
dnsPolicy: ClusterFirst
|
|
restartPolicy: Always
|
|
`
|
|
|
|
// This test reuses some methods from TestCompositeDiamond,
|
|
// but overwrites the kustomization files in the overlays.
|
|
func TestStackedOverlays(t *testing.T) {
|
|
th := kusttest_test.NewKustTestHarness(t, "/app/restart")
|
|
writeDiamondCompositionBase(th)
|
|
|
|
// probe overlays base.
|
|
writeProbeOverlay(th)
|
|
|
|
// dns overlays probe.
|
|
writeDNSOverlay(th)
|
|
th.WriteK("/app/dns", `
|
|
resources:
|
|
- ../probe
|
|
patchesStrategicMerge:
|
|
- dep-patch.yaml
|
|
`)
|
|
|
|
// restart overlays dns.
|
|
writeRestartOverlay(th)
|
|
th.WriteK("/app/restart", `
|
|
resources:
|
|
- ../dns
|
|
patchesStrategicMerge:
|
|
- dep-patch.yaml
|
|
`)
|
|
|
|
m, err := th.MakeKustTarget().MakeCustomizedResMap()
|
|
if err != nil {
|
|
t.Fatalf("Err: %v", err)
|
|
}
|
|
th.AssertActualEqualsExpected(m, expectedPatchedDeployment)
|
|
}
|
|
|
|
func TestMultiPatch(t *testing.T) {
|
|
th := kusttest_test.NewKustTestHarness(t, "/app/composite")
|
|
writeDiamondCompositionBase(th)
|
|
|
|
th.WriteK("/app/composite", `
|
|
resources:
|
|
- ../base
|
|
patchesStrategicMerge:
|
|
- patchAddProbe.yaml
|
|
- patchDnsPolicy.yaml
|
|
- patchRestartPolicy.yaml
|
|
`)
|
|
th.WriteF("/app/composite/patchRestartPolicy.yaml", patchRestartPolicy)
|
|
th.WriteF("/app/composite/patchDnsPolicy.yaml", patchDnsPolicy)
|
|
th.WriteF("/app/composite/patchAddProbe.yaml", patchAddProbe)
|
|
|
|
m, err := th.MakeKustTarget().MakeCustomizedResMap()
|
|
if err != nil {
|
|
t.Fatalf("Err: %v", err)
|
|
}
|
|
th.AssertActualEqualsExpected(m, expectedPatchedDeployment)
|
|
}
|