Skip to content

Commit bddc6b5

Browse files
authored
Merge pull request #75 from fluxcd/test-push-status
Record last pushed commit in status
2 parents a45a216 + 50a4e91 commit bddc6b5

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

api/v1alpha1/imageupdateautomation_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ type ImageUpdateAutomationStatus struct {
9494
// made).
9595
// +optional
9696
LastAutomationRunTime *metav1.Time `json:"lastAutomationRunTime,omitempty"`
97+
// LastPushCommit records the SHA1 of the last commit made by the
98+
// controller, for this automation object
99+
// +optional
100+
LastPushCommit string `json:"lastPushCommit,omitempty"`
101+
// LastPushTime records the time of the last pushed change.
102+
// +optional
103+
LastPushTime *metav1.Time `json:"lastPushTime,omitempty"`
97104
// +optional
98105
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
99106
// +optional

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 4 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ spec:
187187
description: LastHandledReconcileAt holds the value of the most recent
188188
reconcile request value, so a change can be detected.
189189
type: string
190+
lastPushCommit:
191+
description: LastPushCommit records the SHA1 of the last commit made
192+
by the controller, for this automation object
193+
type: string
194+
lastPushTime:
195+
description: LastPushTime records the time of the last pushed change.
196+
format: date-time
197+
type: string
190198
observedGeneration:
191199
format: int64
192200
type: integer

controllers/imageupdateautomation_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,17 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(req ctrl.Request) (ctrl.Resu
199199
r.event(auto, events.EventSeverityInfo, "no updates made")
200200
log.V(debug).Info("no changes made in working directory; no commit")
201201
statusMessage = "no updates made"
202+
if lastCommit, lastTime := auto.Status.LastPushCommit, auto.Status.LastPushTime; lastCommit != "" {
203+
statusMessage = fmt.Sprintf("%s; last commit %s at %s", statusMessage, lastCommit[:7], lastTime.Format(time.RFC3339))
204+
}
202205
} else {
203206
return failWithError(err)
204207
}
205208
} else {
206209
r.event(auto, events.EventSeverityInfo, "committed and pushed change "+rev)
207210
log.Info("pushed commit to origin", "revision", rev)
211+
auto.Status.LastPushCommit = rev
212+
auto.Status.LastPushTime = &metav1.Time{Time: now}
208213
statusMessage = "committed and pushed " + rev
209214
}
210215

controllers/update_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
. "github.com/onsi/gomega"
3737
"github.com/otiai10/copy"
3838
corev1 "k8s.io/api/core/v1"
39-
// apimeta "k8s.io/apimachinery/pkg/api/meta"
39+
apimeta "k8s.io/apimachinery/pkg/api/meta"
4040
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4141
"k8s.io/apimachinery/pkg/types"
4242
ctrl "sigs.k8s.io/controller-runtime"
@@ -241,6 +241,11 @@ var _ = Describe("ImageUpdateAutomation", func() {
241241
Expect(err).ToNot(HaveOccurred())
242242
Expect(commit.Message).To(Equal(commitMessage))
243243

244+
var newObj imagev1.ImageUpdateAutomation
245+
Expect(k8sClient.Get(context.Background(), updateKey, &newObj)).To(Succeed())
246+
Expect(newObj.Status.LastPushCommit).To(Equal(head.Hash().String()))
247+
Expect(newObj.Status.LastPushTime).ToNot(BeNil())
248+
244249
compareRepoWithExpected(repoURL, branch, "testdata/appconfig-setters-expected", func(tmp string) {
245250
replaceMarker(tmp, policyKey)
246251
})
@@ -319,6 +324,7 @@ var _ = Describe("ImageUpdateAutomation", func() {
319324
}, timeout, time.Second).Should(BeTrue())
320325
// check that the annotation was recorded as seen
321326
Expect(newUpdate.Status.LastHandledReconcileAt).To(Equal(ts))
327+
expectCommittedAndPushed(newUpdate.Status.Conditions)
322328

323329
// check that a new commit was made
324330
compareRepoWithExpected(repoURL, branch, "testdata/appconfig-setters-expected", func(tmp string) {
@@ -329,6 +335,12 @@ var _ = Describe("ImageUpdateAutomation", func() {
329335
})
330336
})
331337

338+
func expectCommittedAndPushed(conditions []metav1.Condition) {
339+
rc := apimeta.FindStatusCondition(conditions, meta.ReadyCondition)
340+
Expect(rc).ToNot(BeNil())
341+
Expect(rc.Message).To(ContainSubstring("committed and pushed"))
342+
}
343+
332344
func replaceMarker(path string, policyKey types.NamespacedName) {
333345
// NB this requires knowledge of what's in the git
334346
// repo, so a little brittle

0 commit comments

Comments
 (0)