Skip to content

Commit b30bc0c

Browse files
committed
Update artifact tarball when spec.ignore changes
This commit adds a new field `status.ignoreChecksum` which is used to detect changes on `spec.ignore`, that controls the exclusions for the object's `status.artifact` Signed-off-by: Tiago Angelo <[email protected]>
1 parent 1a06b7a commit b30bc0c

File tree

6 files changed

+76
-5
lines changed

6 files changed

+76
-5
lines changed

api/v1beta2/bucket_types.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"crypto/sha256"
21+
"fmt"
2022
"time"
2123

2224
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -124,6 +126,11 @@ type BucketStatus struct {
124126
// +optional
125127
Artifact *Artifact `json:"artifact,omitempty"`
126128

129+
// IgnoreChecksum is a SHA256 checksum of the excluded patterns applied
130+
// in the last reconciliation.
131+
// +optional
132+
IgnoreChecksum string `json:"ignoreChecksum,omitempty"`
133+
127134
meta.ReconcileRequestStatus `json:",inline"`
128135
}
129136

@@ -157,6 +164,27 @@ func (in *Bucket) GetArtifact() *Artifact {
157164
return in.Status.Artifact
158165
}
159166

167+
// UpdateIgnoreChecksum updates the checksum of excluded patterns in the status sub-resource.
168+
func (in *Bucket) UpdateIgnoreChecksum() {
169+
var checksum string
170+
if in.Spec.Ignore != nil {
171+
ignore := []byte(*in.Spec.Ignore)
172+
checksum = fmt.Sprintf("%x", sha256.Sum256(ignore))
173+
}
174+
in.Status.IgnoreChecksum = checksum
175+
}
176+
177+
// IgnoreHasChanged compares the checksum of excluded patterns applied in the last reconciliation
178+
// with the current spec and returns true if values are different.
179+
func (in *Bucket) IgnoreHasChanged() bool {
180+
var checksum string
181+
if in.Spec.Ignore != nil {
182+
ignore := []byte(*in.Spec.Ignore)
183+
checksum = fmt.Sprintf("%x", sha256.Sum256(ignore))
184+
}
185+
return in.Status.IgnoreChecksum != checksum
186+
}
187+
160188
// +genclient
161189
// +genclient:Namespaced
162190
// +kubebuilder:storageversion

api/v1beta2/gitrepository_types.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"crypto/sha256"
21+
"fmt"
2022
"time"
2123

2224
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -211,6 +213,11 @@ type GitRepositoryStatus struct {
211213
// +optional
212214
IncludedArtifacts []*Artifact `json:"includedArtifacts,omitempty"`
213215

216+
// IgnoreChecksum is a SHA256 checksum of the excluded patterns applied
217+
// in the last reconciliation.
218+
// +optional
219+
IgnoreChecksum string `json:"ignoreChecksum,omitempty"`
220+
214221
meta.ReconcileRequestStatus `json:",inline"`
215222
}
216223

@@ -246,6 +253,27 @@ func (in *GitRepository) GetArtifact() *Artifact {
246253
return in.Status.Artifact
247254
}
248255

256+
// UpdateIgnoreChecksum updates the checksum of excluded patterns in the status sub-resource.
257+
func (in *GitRepository) UpdateIgnoreChecksum() {
258+
var checksum string
259+
if in.Spec.Ignore != nil {
260+
ignore := []byte(*in.Spec.Ignore)
261+
checksum = fmt.Sprintf("%x", sha256.Sum256(ignore))
262+
}
263+
in.Status.IgnoreChecksum = checksum
264+
}
265+
266+
// IgnoreHasChanged compares the checksum of excluded patterns applied in the last reconciliation
267+
// with the current spec and returns true if values are different.
268+
func (in *GitRepository) IgnoreHasChanged() bool {
269+
var checksum string
270+
if in.Spec.Ignore != nil {
271+
ignore := []byte(*in.Spec.Ignore)
272+
checksum = fmt.Sprintf("%x", sha256.Sum256(ignore))
273+
}
274+
return in.Status.IgnoreChecksum != checksum
275+
}
276+
249277
// +genclient
250278
// +genclient:Namespaced
251279
// +kubebuilder:storageversion

config/crd/bases/source.toolkit.fluxcd.io_buckets.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ spec:
478478
- type
479479
type: object
480480
type: array
481+
ignoreChecksum:
482+
description: IgnoreChecksum is a SHA256 checksum of the excluded patterns
483+
applied in the last reconciliation.
484+
type: string
481485
lastHandledReconcileAt:
482486
description: LastHandledReconcileAt holds the value of the most recent
483487
reconcile request value, so a change of the annotation value can

config/crd/bases/source.toolkit.fluxcd.io_gitrepositories.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,10 @@ spec:
653653
- type
654654
type: object
655655
type: array
656+
ignoreChecksum:
657+
description: IgnoreChecksum is a SHA256 checksum of the excluded patterns
658+
applied in the last reconciliation.
659+
type: string
656660
includedArtifacts:
657661
description: IncludedArtifacts contains a list of the last successfully
658662
included Artifacts as instructed by GitRepositorySpec.Include.

controllers/bucket_controller.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"sync"
2929
"time"
3030

31-
"github.com/fluxcd/source-controller/pkg/azure"
3231
"golang.org/x/sync/errgroup"
3332
"golang.org/x/sync/semaphore"
3433
corev1 "k8s.io/api/core/v1"
@@ -42,6 +41,8 @@ import (
4241
"sigs.k8s.io/controller-runtime/pkg/predicate"
4342
"sigs.k8s.io/controller-runtime/pkg/ratelimiter"
4443

44+
"github.com/fluxcd/source-controller/pkg/azure"
45+
4546
"github.com/fluxcd/pkg/apis/meta"
4647
"github.com/fluxcd/pkg/runtime/conditions"
4748
helper "github.com/fluxcd/pkg/runtime/controller"
@@ -564,19 +565,22 @@ func (r *BucketReconciler) reconcileArtifact(ctx context.Context, obj *sourcev1.
564565

565566
// Set the ArtifactInStorageCondition if there's no drift.
566567
defer func() {
567-
if obj.GetArtifact().HasRevision(artifact.Revision) {
568+
if obj.GetArtifact().HasRevision(artifact.Revision) && !obj.IgnoreHasChanged() {
568569
conditions.Delete(obj, sourcev1.ArtifactOutdatedCondition)
569570
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason,
570571
"stored artifact for revision '%s'", artifact.Revision)
571572
}
572573
}()
573574

574575
// The artifact is up-to-date
575-
if obj.GetArtifact().HasRevision(artifact.Revision) {
576+
if obj.GetArtifact().HasRevision(artifact.Revision) && !obj.IgnoreHasChanged() {
576577
r.eventLogf(ctx, obj, events.EventTypeTrace, sourcev1.ArtifactUpToDateReason, "artifact up-to-date with remote revision: '%s'", artifact.Revision)
577578
return sreconcile.ResultSuccess, nil
578579
}
579580

581+
// If needed, updates ignore checksum
582+
obj.UpdateIgnoreChecksum()
583+
580584
// Ensure target path exists and is a directory
581585
if f, err := os.Stat(dir); err != nil {
582586
e := &serror.Event{

controllers/gitrepository_controller.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,19 +500,22 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context,
500500

501501
// Set the ArtifactInStorageCondition if there's no drift.
502502
defer func() {
503-
if obj.GetArtifact().HasRevision(artifact.Revision) && !includes.Diff(obj.Status.IncludedArtifacts) {
503+
if obj.GetArtifact().HasRevision(artifact.Revision) && !includes.Diff(obj.Status.IncludedArtifacts) && !obj.IgnoreHasChanged() {
504504
conditions.Delete(obj, sourcev1.ArtifactOutdatedCondition)
505505
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason,
506506
"stored artifact for revision '%s'", artifact.Revision)
507507
}
508508
}()
509509

510510
// The artifact is up-to-date
511-
if obj.GetArtifact().HasRevision(artifact.Revision) && !includes.Diff(obj.Status.IncludedArtifacts) {
511+
if obj.GetArtifact().HasRevision(artifact.Revision) && !includes.Diff(obj.Status.IncludedArtifacts) && !obj.IgnoreHasChanged() {
512512
r.eventLogf(ctx, obj, events.EventTypeTrace, sourcev1.ArtifactUpToDateReason, "artifact up-to-date with remote revision: '%s'", artifact.Revision)
513513
return sreconcile.ResultSuccess, nil
514514
}
515515

516+
// If needed, updates ignore checksum
517+
obj.UpdateIgnoreChecksum()
518+
516519
// Ensure target path exists and is a directory
517520
if f, err := os.Stat(dir); err != nil {
518521
e := &serror.Event{

0 commit comments

Comments
 (0)