Skip to content

Commit 0c3e73f

Browse files
authored
Merge pull request #264 from fluxcd/kustomize-patches
2 parents 975534b + d7cdeb6 commit 0c3e73f

15 files changed

Lines changed: 430 additions & 132 deletions

File tree

.github/workflows/e2e.yaml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,26 @@ jobs:
8282
kubectl -n impersonation wait kustomizations/podinfo --for=condition=ready --timeout=4m
8383
kubectl -n impersonation delete kustomizations/podinfo
8484
until kubectl -n impersonation get deploy/podinfo 2>&1 | grep NotFound ; do sleep 2; done
85-
- name: Run image overide tests
85+
- name: Run images override tests
8686
run: |
87-
kubectl -n override-test apply -f ./config/testdata/overrides
88-
kubectl -n override-test wait kustomizations/podinfo --for=condition=ready --timeout=1m
89-
ACTUAL_TAG=$(kubectl -n override-test get deployments podinfo -o jsonpath='{.spec.template.spec.containers[0].image}' | cut -f2 -d ":")
90-
if [[ $ACTUAL_TAG != "5.0.0" ]]; then echo "Image tag did not override" && exit 1; fi
87+
kubectl -n images-test apply -f ./config/testdata/overrides/images.yaml
88+
kubectl -n images-test wait kustomizations/podinfo --for=condition=ready --timeout=1m
89+
ACTUAL_TAG=$(kubectl -n images-test get deployments podinfo -o jsonpath='{.spec.template.spec.containers[0].image}' | cut -f2 -d ":")
90+
if [[ $ACTUAL_TAG != "5.0.0" ]]; then echo "Image tag was not overwritten" && exit 1; fi
91+
- name: Run patches override tests
92+
run: |
93+
kubectl -n patches-test apply -f ./config/testdata/overrides/patches.yaml
94+
kubectl -n patches-test wait kustomizations/podinfo --for=condition=ready --timeout=1m
95+
WANT="xxxx"
96+
RESULT=$(kubectl -n patches-test get deployment podinfo -o jsonpath='{.metadata.labels.yyyy}')
97+
if [ "$RESULT" != "$WANT" ]; then
98+
echo -e "$RESULT\n\ndoes not equal\n\n$WANT" && exit 1
99+
fi
100+
WANT="yyyy"
101+
RESULT=$(kubectl -n patches-test get deployment podinfo -o jsonpath='{.metadata.labels.xxxx}')
102+
if [ "$RESULT" != "$WANT" ]; then
103+
echo -e "$RESULT\n\ndoes not equal\n\$WANT" && exit 1
104+
fi
91105
- name: Logs
92106
run: |
93107
kubectl -n kustomize-system logs deploy/source-controller

api/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ module github.com/fluxcd/kustomize-controller/api
33
go 1.15
44

55
require (
6+
github.com/fluxcd/pkg/apis/kustomize v0.0.1
67
github.com/fluxcd/pkg/apis/meta v0.7.0
78
github.com/fluxcd/pkg/runtime v0.8.0
9+
k8s.io/apiextensions-apiserver v0.20.2
810
k8s.io/apimachinery v0.20.2
911
sigs.k8s.io/controller-runtime v0.8.0
1012
)

api/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
8888
github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
8989
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
9090
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
91+
github.com/fluxcd/pkg/apis/kustomize v0.0.1 h1:TkA80R0GopRY27VJqzKyS6ifiKIAfwBd7OHXtV3t2CI=
92+
github.com/fluxcd/pkg/apis/kustomize v0.0.1/go.mod h1:JAFPfnRmcrAoG1gNiA8kmEXsnOBuDyZ/F5X4DAQcVV0=
9193
github.com/fluxcd/pkg/apis/meta v0.7.0 h1:5e8gm4OLqjuKWdrOIY5DEEsjcwzyJFK8rCDesJ+V8IY=
9294
github.com/fluxcd/pkg/apis/meta v0.7.0/go.mod h1:yHuY8kyGHYz22I0jQzqMMGCcHViuzC/WPdo9Gisk8Po=
9395
github.com/fluxcd/pkg/runtime v0.8.0 h1:cnSBZJLcXlKgjXpFFFExu+4ZncIxmPgNIx+ErLcCLnA=
@@ -634,21 +636,27 @@ k8s.io/api v0.20.1/go.mod h1:KqwcCVogGxQY3nBlRpwt+wpAMF/KjaCc7RpywacvqUo=
634636
k8s.io/api v0.20.2 h1:y/HR22XDZY3pniu9hIFDLpUCPq2w5eQ6aV/VFQ7uJMw=
635637
k8s.io/api v0.20.2/go.mod h1:d7n6Ehyzx+S+cE3VhTGfVNNqtGc/oL9DCdYYahlurV8=
636638
k8s.io/apiextensions-apiserver v0.20.1/go.mod h1:ntnrZV+6a3dB504qwC5PN/Yg9PBiDNt1EVqbW2kORVk=
639+
k8s.io/apiextensions-apiserver v0.20.2 h1:rfrMWQ87lhd8EzQWRnbQ4gXrniL/yTRBgYH1x1+BLlo=
640+
k8s.io/apiextensions-apiserver v0.20.2/go.mod h1:F6TXp389Xntt+LUq3vw6HFOLttPa0V8821ogLGwb6Zs=
637641
k8s.io/apimachinery v0.20.1/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU=
638642
k8s.io/apimachinery v0.20.2 h1:hFx6Sbt1oG0n6DZ+g4bFt5f6BoMkOjKWsQFu077M3Vg=
639643
k8s.io/apimachinery v0.20.2/go.mod h1:WlLqWAHZGg07AeltaI0MV5uk1Omp8xaN0JGLY6gkRpU=
640644
k8s.io/apiserver v0.20.1/go.mod h1:ro5QHeQkgMS7ZGpvf4tSMx6bBOgPfE+f52KwvXfScaU=
645+
k8s.io/apiserver v0.20.2/go.mod h1:2nKd93WyMhZx4Hp3RfgH2K5PhwyTrprrkWYnI7id7jA=
641646
k8s.io/client-go v0.20.1/go.mod h1:/zcHdt1TeWSd5HoUe6elJmHSQ6uLLgp4bIJHVEuy+/Y=
642647
k8s.io/client-go v0.20.2/go.mod h1:kH5brqWqp7HDxUFKoEgiI4v8G1xzbe9giaCenUWJzgE=
643648
k8s.io/code-generator v0.20.1/go.mod h1:UsqdF+VX4PU2g46NC2JRs4gc+IfrctnwHb76RNbWHJg=
649+
k8s.io/code-generator v0.20.2/go.mod h1:UsqdF+VX4PU2g46NC2JRs4gc+IfrctnwHb76RNbWHJg=
644650
k8s.io/component-base v0.20.1/go.mod h1:guxkoJnNoh8LNrbtiQOlyp2Y2XFCZQmrcg2n/DeYNLk=
651+
k8s.io/component-base v0.20.2/go.mod h1:pzFtCiwe/ASD0iV7ySMu8SYVJjCapNM9bjvk7ptpKh0=
645652
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
646653
k8s.io/gengo v0.0.0-20201113003025-83324d819ded/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
647654
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
648655
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
649656
k8s.io/klog/v2 v2.4.0 h1:7+X0fUguPyrKEC4WjH8iGDg3laWgMo5tMnRTIGTTxGQ=
650657
k8s.io/klog/v2 v2.4.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
651658
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd/go.mod h1:WOJ3KddDSol4tAGcJo0Tvi+dK12EcqSLqcWsryKMpfM=
659+
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 h1:CbnUZsM497iRC5QMVkHwyl8s2tB3g7yaSHkYPkpgelw=
652660
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
653661
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
654662
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=

api/v1beta1/kustomization_types.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2021
apimeta "k8s.io/apimachinery/pkg/api/meta"
2122
"time"
2223

2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"k8s.io/apimachinery/pkg/types"
2526

27+
"github.com/fluxcd/pkg/apis/kustomize"
2628
"github.com/fluxcd/pkg/apis/meta"
2729
"github.com/fluxcd/pkg/runtime/dependency"
2830
)
@@ -74,9 +76,19 @@ type KustomizationSpec struct {
7476
// +optional
7577
HealthChecks []meta.NamespacedObjectKindReference `json:"healthChecks,omitempty"`
7678

77-
// A list of images used to override or set the name and tag for container images.
79+
// Strategic merge patches, defined as inline YAML objects.
7880
// +optional
79-
Images []Image `json:"images,omitempty"`
81+
PatchesStrategicMerge []apiextensionsv1.JSON `json:"patchesStrategicMerge,omitempty"`
82+
83+
// JSON 6902 patches, defined as inline YAML objects.
84+
// +optional
85+
PatchesJSON6902 []kustomize.JSON6902Patch `json:"patchesJson6902,omitempty"`
86+
87+
// Images is a list of (image name, new name, new tag or digest)
88+
// for changing image names, tags or digests. This can also be achieved with a
89+
// patch, but this operator is simpler to specify.
90+
// +optional
91+
Images []kustomize.Image `json:"images,omitempty"`
8092

8193
// The name of the Kubernetes service account to impersonate
8294
// when reconciling this Kustomization.
@@ -124,21 +136,6 @@ type Decryption struct {
124136
SecretRef *meta.LocalObjectReference `json:"secretRef,omitempty"`
125137
}
126138

127-
// Image contains the name, new name and new tag that will replace the original container image.
128-
type Image struct {
129-
// Name of the image to be replaced.
130-
// +required
131-
Name string `json:"name"`
132-
133-
// NewName is the name of the image used to replace the original one.
134-
// +required
135-
NewName string `json:"newName"`
136-
137-
// NewTag is the image tag used to replace the original tag.
138-
// +required
139-
NewTag string `json:"newTag"`
140-
}
141-
142139
// KubeConfig references a Kubernetes secret that contains a kubeconfig file.
143140
type KubeConfig struct {
144141
// SecretRef holds the name to a secret that contains a 'value' key with

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 18 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,30 @@ spec:
114114
type: object
115115
type: array
116116
images:
117-
description: A list of images used to override or set the name and
118-
tag for container images.
117+
description: Images is a list of (image name, new name, new tag or
118+
digest) for changing image names, tags or digests. This can also
119+
be achieved with a patch, but this operator is simpler to specify.
119120
items:
120-
description: Image contains the name, new name and new tag that
121-
will replace the original container image.
121+
description: Image contains an image name, a new name, a new tag
122+
or digest, which will replace the original name and tag.
122123
properties:
124+
digest:
125+
description: Digest is the value used to replace the original
126+
image tag. If digest is present NewTag value is ignored.
127+
type: string
123128
name:
124-
description: Name of the image to be replaced.
129+
description: Name is a tag-less image name.
125130
type: string
126131
newName:
127-
description: NewName is the name of the image used to replace
128-
the original one.
132+
description: NewName is the value used to replace the original
133+
name.
129134
type: string
130135
newTag:
131-
description: NewTag is the image tag used to replace the original
136+
description: NewTag is the value used to replace the original
132137
tag.
133138
type: string
134139
required:
135140
- name
136-
- newName
137-
- newTag
138141
type: object
139142
type: array
140143
interval:
@@ -162,6 +165,85 @@ spec:
162165
- name
163166
type: object
164167
type: object
168+
patchesJson6902:
169+
description: JSON 6902 patches, defined as inline YAML objects.
170+
items:
171+
description: JSON6902Patch contains a JSON6902 patch and the target
172+
the patch should be applied to.
173+
properties:
174+
patch:
175+
description: Patch contains the JSON6902 patch document with
176+
an array of operation objects.
177+
items:
178+
description: JSON6902 is a JSON6902 operation object. https://tools.ietf.org/html/rfc6902#section-4
179+
properties:
180+
from:
181+
type: string
182+
op:
183+
enum:
184+
- test
185+
- remove
186+
- add
187+
- replace
188+
- move
189+
- copy
190+
type: string
191+
path:
192+
type: string
193+
value:
194+
x-kubernetes-preserve-unknown-fields: true
195+
required:
196+
- op
197+
- path
198+
type: object
199+
type: array
200+
target:
201+
description: Target points to the resources that the patch document
202+
should be applied to.
203+
properties:
204+
annotationSelector:
205+
description: AnnotationSelector is a string that follows
206+
the label selection expression https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#api
207+
It matches with the resource annotations.
208+
type: string
209+
group:
210+
description: Group is the API group to select resources
211+
from. Together with Version and Kind it is capable of
212+
unambiguously identifying and/or selecting resources.
213+
https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/api-group.md
214+
type: string
215+
kind:
216+
description: Kind of the API Group to select resources from.
217+
Together with Group and Version it is capable of unambiguously
218+
identifying and/or selecting resources. https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/api-group.md
219+
type: string
220+
labelSelector:
221+
description: LabelSelector is a string that follows the
222+
label selection expression https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#api
223+
It matches with the resource labels.
224+
type: string
225+
name:
226+
description: Name to match resources with.
227+
type: string
228+
namespace:
229+
description: Namespace to select resources from.
230+
type: string
231+
version:
232+
description: Version of the API Group to select resources
233+
from. Together with Group and Kind it is capable of unambiguously
234+
identifying and/or selecting resources. https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/api-group.md
235+
type: string
236+
type: object
237+
required:
238+
- patch
239+
- target
240+
type: object
241+
type: array
242+
patchesStrategicMerge:
243+
description: Strategic merge patches, defined as inline YAML objects.
244+
items:
245+
x-kubernetes-preserve-unknown-fields: true
246+
type: array
165247
path:
166248
description: Path to the directory containing the kustomization.yaml
167249
file, or the set of plain YAMLs a kustomization.yaml should be generated
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apiVersion: v1
22
kind: Namespace
33
metadata:
4-
name: override-test
4+
name: images-test
55
---
66
apiVersion: source.toolkit.fluxcd.io/v1beta1
77
kind: GitRepository
88
metadata:
99
name: podinfo
10-
namespace: override-test
10+
namespace: images-test
1111
spec:
1212
interval: 5m
1313
url: https://github.com/stefanprodan/podinfo
@@ -18,9 +18,9 @@ apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
1818
kind: Kustomization
1919
metadata:
2020
name: podinfo
21-
namespace: override-test
21+
namespace: images-test
2222
spec:
23-
targetNamespace: override-test
23+
targetNamespace: images-test
2424
interval: 5m
2525
path: "./kustomize"
2626
prune: true

0 commit comments

Comments
 (0)