| 1 |
// Copyright 2021 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package krusty_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 11 |
) |
| 12 |
|
| 13 |
// This is broken since kustomize v3.9.3. |
| 14 |
// See https://github.com/kubernetes-sigs/kustomize/issues/3609 for details. |
| 15 |
|
| 16 |
// Here is a structure of a kustomization of one resource inheriting from |
| 17 |
// two bases. One of those bases is shared between the canary base and the |
| 18 |
// final resource. This is named canary as it is a simple pattern to |
| 19 |
// duplicate a resource that can be used with canary deployments. |
| 20 |
// |
| 21 |
// base |
| 22 |
// | deployment.yaml |
| 23 |
// | kustomization.yaml |
| 24 |
// canary |
| 25 |
// | deployment-canary-patch.yaml |
| 26 |
// | kustomization.yaml |
| 27 |
// mango |
| 28 |
// | deployment-mango-patch.yaml |
| 29 |
// | deployment-mango-canary-patch.yaml |
| 30 |
// | kustomization.yaml |
| 31 |
func TestRepeatBase(t *testing.T) { |
| 32 |
th := kusttest_test.MakeHarness(t) |
| 33 |
th.WriteK("base", ` |
| 34 |
resources: |
| 35 |
- deployment.yaml |
| 36 |
`) |
| 37 |
th.WriteF("base/deployment.yaml", ` |
| 38 |
--- |
| 39 |
apiVersion: apps/v1 |
| 40 |
kind: Deployment |
| 41 |
metadata: |
| 42 |
name: banana |
| 43 |
spec: |
| 44 |
selector: |
| 45 |
matchLabels: |
| 46 |
component: banana |
| 47 |
template: |
| 48 |
metadata: |
| 49 |
labels: |
| 50 |
component: banana |
| 51 |
spec: |
| 52 |
containers: |
| 53 |
- name: banana |
| 54 |
image: image |
| 55 |
`) |
| 56 |
|
| 57 |
th.WriteK("canary", ` |
| 58 |
resources: |
| 59 |
- ../base |
| 60 |
patches: |
| 61 |
- patch: | |
| 62 |
- op: replace |
| 63 |
path: /metadata/name |
| 64 |
value: banana-canary |
| 65 |
target: |
| 66 |
kind: Deployment |
| 67 |
- path: deployment-canary-patch.yaml |
| 68 |
`) |
| 69 |
th.WriteF("canary/deployment-canary-patch.yaml", ` |
| 70 |
--- |
| 71 |
apiVersion: apps/v1 |
| 72 |
kind: Deployment |
| 73 |
metadata: |
| 74 |
name: banana-canary |
| 75 |
labels: |
| 76 |
type: canary |
| 77 |
spec: |
| 78 |
selector: |
| 79 |
matchLabels: |
| 80 |
component: banana |
| 81 |
type: canary |
| 82 |
template: |
| 83 |
metadata: |
| 84 |
labels: |
| 85 |
component: banana |
| 86 |
type: canary |
| 87 |
spec: |
| 88 |
containers: |
| 89 |
- name: banana |
| 90 |
image: image-canary |
| 91 |
`) |
| 92 |
|
| 93 |
th.WriteK("mango", ` |
| 94 |
nameSuffix: -mango |
| 95 |
resources: |
| 96 |
- ../base |
| 97 |
- ../canary |
| 98 |
patches: |
| 99 |
- path: deployment-mango-base-patch.yaml |
| 100 |
- path: deployment-mango-canary-patch.yaml |
| 101 |
`) |
| 102 |
th.WriteF("mango/deployment-mango-base-patch.yaml", ` |
| 103 |
--- |
| 104 |
apiVersion: apps/v1 |
| 105 |
kind: Deployment |
| 106 |
metadata: |
| 107 |
name: banana |
| 108 |
spec: |
| 109 |
template: |
| 110 |
spec: |
| 111 |
containers: |
| 112 |
- name: banana |
| 113 |
image: image-mango |
| 114 |
`) |
| 115 |
th.WriteF("mango/deployment-mango-canary-patch.yaml", ` |
| 116 |
--- |
| 117 |
apiVersion: apps/v1 |
| 118 |
kind: Deployment |
| 119 |
metadata: |
| 120 |
name: banana-canary |
| 121 |
spec: |
| 122 |
template: |
| 123 |
spec: |
| 124 |
containers: |
| 125 |
- name: banana |
| 126 |
image: image-canary-mango |
| 127 |
`) |
| 128 |
|
| 129 |
err := th.RunWithErr("mango", th.MakeDefaultOptions()) |
| 130 |
if !strings.Contains( |
| 131 |
err.Error(), "multiple matches for Id Deployment.v1.apps/banana.[noNs]; failed to find unique target for patch Deployment.v1.apps/banana.[noNs]") { |
| 132 |
t.Fatalf("Unexpected err: %v", err) |
| 133 |
} |
| 134 |
|
| 135 |
// Uncomenting the following makes it work with kustomize v3.9.2 and bellow. |
| 136 |
|
| 137 |
// m := th.Run("/app", th.MakeDefaultOptions()) |
| 138 |
// th.AssertActualEqualsExpected(m, ` |
| 139 |
//apiVersion: apps/v1 |
| 140 |
//kind: Deployment |
| 141 |
//metadata: |
| 142 |
// name: deployment-a |
| 143 |
//spec: |
| 144 |
// selector: |
| 145 |
// matchLabels: |
| 146 |
// component: deployment |
| 147 |
// template: |
| 148 |
// metadata: |
| 149 |
// labels: |
| 150 |
// component: deployment |
| 151 |
// spec: |
| 152 |
// containers: |
| 153 |
// - image: image-a |
| 154 |
// name: container-a |
| 155 |
//--- |
| 156 |
//apiVersion: apps/v1 |
| 157 |
//kind: Deployment |
| 158 |
//metadata: |
| 159 |
// labels: |
| 160 |
// type: canary |
| 161 |
// name: deployment-canary-a |
| 162 |
//spec: |
| 163 |
// selector: |
| 164 |
// matchLabels: |
| 165 |
// component: deployment |
| 166 |
// type: canary |
| 167 |
// template: |
| 168 |
// metadata: |
| 169 |
// labels: |
| 170 |
// component: deployment |
| 171 |
// spec: |
| 172 |
// containers: |
| 173 |
// - image: image-canary-a |
| 174 |
// name: container-a |
| 175 |
// `) |
| 176 |
} |