Skip to content

Commit 7318ecb

Browse files
authored
Merge pull request #23 from fluxcd/use-kyaml-setters2
Implement automation via kyaml setters2
2 parents 6976581 + 76da207 commit 7318ecb

File tree

33 files changed

+827
-136
lines changed

33 files changed

+827
-136
lines changed

README.md

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,34 @@ the GitRepository kind, and doesn't need the source-controller itself.
3232
If you're not already using the [GitOps toolkit][gotk], you can just
3333
install the custom resource definition for GitRepository:
3434

35-
kubectl apply -f https://raw.githubusercontent.com/fluxcd/source-controller/master/config/crd/bases/source.toolkit.fluxcd.io_gitrepositories.yaml
35+
kubectl apply -f https://raw.githubusercontent.com/fluxcd/source-controller/v0.0.18/config/crd/bases/source.toolkit.fluxcd.io_gitrepositories.yaml
3636

3737
**To install the image reflector controller**
3838

3939
This controller relies on the image reflector controller. A working
4040
configuration for the latter can be applied straight from the GitHub
41-
repository (NB `-k`):
41+
repository:
4242

43-
kubectl apply -k github.com/fluxcd/image-reflector-controller/config/default
43+
kustomize build github.com/fluxcd/image-reflector-controller//config/default/?ref=main | kubectl apply -f-
4444

4545
### Installing the automation controller
4646

4747
You can apply a working configuration directly from GitHub:
4848

49-
kubectl apply -k github.com/fluxcd/image-automation-controller/config/default
49+
kustomize build github.com/fluxcd/image-automation-controller//config/default?ref=main | kubectl apply -f-
5050

5151
or, in a clone of this repository,
5252

5353
make docker-build deploy
5454

55+
You will need to do the latter if you're trying the controller on a
56+
branch other than `main`.
57+
5558
## How to use it
5659

5760
Here is a quick example of configuring an automation. I'm going to use
58-
[cuttlefacts-app][cuttlefacts-app-repo] because it's minimal and
59-
thereby, easy to follow.
61+
[cuttlefacts-app][cuttlefacts-app-repo] because it's minimal and easy
62+
to follow.
6063

6164
### Image policy
6265

@@ -92,7 +95,7 @@ kind: ImagePolicy
9295
metadata:
9396
name: app-policy
9497
spec:
95-
imageRepository:
98+
imageRepositoryRef:
9699
name: app-image
97100
policy:
98101
semver:
@@ -111,7 +114,7 @@ NAME LATESTIMAGE
111114
app-policy cuttlefacts/cuttlefacts-app:1.0.0
112115
```
113116

114-
### Git repository and automation
117+
### Creating the git repository object
115118

116119
You need a writable git repository, so fork
117120
[`cuttlefacts-app`][cuttlefacts-app-repo] to your own account, and
@@ -176,9 +179,9 @@ EOF
176179
$ $EDITOR repo.yaml
177180
```
178181

179-
Create the repository; be aware that unless you're running the full
180-
GitOps toolkit suite, there will be no controller acting on it (and
181-
doesn't need to be, for the purpose of this run-through).
182+
Create the repository object; be aware that unless you're running the
183+
full GitOps toolkit suite, there will be no controller acting on it
184+
(and doesn't need to be, for the purpose of this run-through).
182185

183186
```bash
184187
$ kubectl apply -f repo.yaml
@@ -188,9 +191,50 @@ NAME URL READY STATU
188191
cuttlefacts-repo ssh://[email protected]/squaremo/cuttlefacts-app 9s
189192
```
190193

194+
### Adding a marker to the YAML to update
195+
196+
To tell the controller what to update, you add some markers to the
197+
files to be updated. Each marker says which field to update, and which
198+
image policy to use for the new value.
199+
200+
In this case, it's the image in the deployment that needs to be
201+
updated, with the latest image from the image policy made
202+
earlier. Edit the file either locally or through GitHub, and add a
203+
marker to the file `deploy/deployment.yaml` at the line with the image
204+
field, `image: cuttlefacts/cuttlefacts-app`. The surrounding lines
205+
look like this:
206+
207+
```
208+
containers:
209+
- name: server
210+
image: cuttlefacts/cuttlefacts-app
211+
imagePullPolicy: IfNotPresent
212+
```
213+
214+
With the marker, they look like this:
215+
216+
```
217+
containers:
218+
- name: server
219+
image: cuttlefacts/cuttlefacts-app # {"$imagepolicy": "default:app-policy"}
220+
imagePullPolicy: IfNotPresent
221+
```
222+
223+
The marker is a comment at the end of the `image:` line, with a JSON
224+
value (so remember the double quotes), naming the image policy object
225+
to use for the value. A `:` character separates the namespace from the
226+
name of the `ImagePolicy` object. (The namespace is default because it
227+
wasn't specified in the manifest (`policy.yaml`) or when it was
228+
applied.)
229+
230+
Commit that change, and push it if you made the commit locally.
231+
232+
### Creating the automation object
233+
191234
Now we have an image policy, which calculates the most recent image,
192-
and a git repository to update -- the last ingredient is to tie them
193-
together with an `ImageUpdateAutomation` resource:
235+
and a git repository to update, and we've marked the field to update,
236+
in a file. The last ingredient is to tie these together with an
237+
`ImageUpdateAutomation` resource:
194238

195239
```
196240
$ cat > update.yaml <<EOF
@@ -199,19 +243,23 @@ kind: ImageUpdateAutomation
199243
metadata:
200244
name: update-app
201245
spec:
202-
gitRepository:
203-
name: cuttlefacts-repo
246+
checkout:
247+
gitRepositoryRef:
248+
name: cuttlefacts-repo
204249
update:
205-
imagePolicy:
206-
name: app-policy
250+
setters:
251+
paths:
252+
- .
207253
commit:
208254
authorName: UpdateBot
209255
authorEmail: [email protected]
210256
EOF
211257
```
212258

213-
Note that the image policy you created earlier, and the git
214-
repository, are both mentioned.
259+
The git repository object is mentioned, and the `setters` value gives
260+
the paths to apply updates under.
261+
262+
Apply the file to create the automation object:
215263

216264
kubectl apply -f update.yaml
217265

@@ -226,4 +274,4 @@ repository][squaremo-auto-commit].
226274
[cuttlefacts-app-repo]: https://github.com/cuttlefacts/cuttlefacts-app
227275
[github-fingerprints]: https://docs.github.com/en/github/authenticating-to-github/githubs-ssh-key-fingerprints
228276
[cuttlefacts-app-deployment]: https://github.com/cuttlefacts/cuttlefacts-app/blob/master/deploy/deployment.yaml
229-
[squaremo-auto-commit]: https://github.com/squaremo/cuttlefacts-app-automated/commit/ad445a6cbd938be4b93116990954104f5730177e
277+
[squaremo-auto-commit]: https://github.com/squaremo/cuttlefacts-app-auto-setters/commit/edb6e7c0724bcc2226dc3077558f747e7adfb8e8

api/v1alpha1/imageupdateautomation_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ type UpdateStrategy struct {
6262
// given policy's image, to the policy's latest image reference.
6363
// +optional
6464
ImagePolicyRef *corev1.LocalObjectReference `json:"imagePolicyRef,omitempty"`
65+
// Setters if present means update workloads using setters, via
66+
// fields marked in the files themselves.
67+
// +optional
68+
Setters *SettersStrategy `json:"setters,omitempty"`
69+
}
70+
71+
// SettersStrategy specifies how to use kyaml setters to update the
72+
// git repository.
73+
type SettersStrategy struct {
74+
// Paths gives all paths that should be subject to updates using
75+
// setters. If missing, the path `.` (the root of the git
76+
// repository) is assumed.
77+
// +optional
78+
Paths []string `json:"paths,omitempty"`
6579
}
6680

6781
// CommitSpec specifies how to commit changes to the git repository

api/v1alpha1/zz_generated.deepcopy.go

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

config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ spec:
9999
TODO: Add other useful fields. apiVersion, kind, uid?'
100100
type: string
101101
type: object
102+
setters:
103+
description: Setters if present means update workloads using setters,
104+
via fields marked in the files themselves.
105+
properties:
106+
paths:
107+
description: Paths gives all paths that should be subject to
108+
updates using setters. If missing, the path `.` (the root
109+
of the git repository) is assumed.
110+
items:
111+
type: string
112+
type: array
113+
type: object
102114
type: object
103115
required:
104116
- checkout

controllers/imageupdateautomation_controller.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(req ctrl.Request) (ctrl.Resu
143143
}
144144
return ctrl.Result{}, err
145145
}
146+
case updateStrat.Setters != nil:
147+
// For setters we first want to compile a list of _all_ the
148+
// policies in the same namespace (maybe in the future this
149+
// could be filtered by the automation object).
150+
var policies imagev1alpha1_reflect.ImagePolicyList
151+
if err := r.List(ctx, &policies, &client.ListOptions{Namespace: req.NamespacedName.Namespace}); err != nil {
152+
return ctrl.Result{}, err
153+
}
154+
155+
if err := updateAccordingToSetters(ctx, tmp, policies.Items); err != nil {
156+
return ctrl.Result{}, err
157+
}
146158
default:
147159
log.Info("no update strategy given in the spec")
148160
// no sense rescheduling until this resource changes
@@ -392,3 +404,9 @@ func updateAccordingToImagePolicy(ctx context.Context, path string, policy *imag
392404
}
393405
return update.UpdateImageEverywhere(path, path, latestRef, latestRef)
394406
}
407+
408+
// updateAccordingToSetters updates files under the root by treating
409+
// the given image policies as kyaml setters.
410+
func updateAccordingToSetters(ctx context.Context, path string, policies []imagev1alpha1_reflect.ImagePolicy) error {
411+
return update.UpdateWithSetters(path, path, policies)
412+
}

controllers/testdata/appconfig-expected/deploy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ spec:
77
spec:
88
containers:
99
- name: hello
10-
image: helloworld:1.0.1
10+
image: helloworld:1.0.1 # SETTER_SITE

controllers/testdata/appconfig-expected2/deploy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ spec:
77
spec:
88
containers:
99
- name: hello
10-
image: helloworld:1.2.0
10+
image: helloworld:1.2.0 # SETTER_SITE
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: test
5+
spec:
6+
template:
7+
spec:
8+
containers:
9+
- name: hello
10+
image: helloworld:1.0.1 # SETTER_SITE

controllers/testdata/appconfig/deploy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ spec:
77
spec:
88
containers:
99
- name: hello
10-
image: helloworld:1.0.0
10+
image: helloworld:1.0.0 # SETTER_SITE

0 commit comments

Comments
 (0)