Skip to content

Commit d3d1482

Browse files
committed
Update artifact tarball when spec.ignore changes
This commit adds a new field `status.artifact.excludedPatternsChecksum` 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 d425923 commit d3d1482

7 files changed

+62
-7
lines changed

api/v1beta2/artifact_types.go

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

1919
import (
20+
"crypto/sha256"
21+
"fmt"
22+
"io"
2023
"path"
2124
"strings"
2225

@@ -46,6 +49,11 @@ type Artifact struct {
4649
// +optional
4750
Checksum string `json:"checksum"`
4851

52+
// ExcludedPatternsChecksum is a SHA256 checksum of the excluded patterns
53+
// applied in the last reconciliation.
54+
// +optional
55+
ExcludedPatternsChecksum string `json:"excludedPatternsChecksum,omitempty"`
56+
4957
// LastUpdateTime is the timestamp corresponding to the last update of the
5058
// Artifact.
5159
// +required
@@ -74,6 +82,30 @@ func (in *Artifact) HasChecksum(checksum string) bool {
7482
return in.Checksum == checksum
7583
}
7684

85+
// EqualExcludedPatterns returns if the given excluded patterns match the
86+
// current Artifact excluded patterns.
87+
func (in *Artifact) EqualExcludedPatterns(artifact Artifact) bool {
88+
var ignored string
89+
if in != nil {
90+
ignored = in.ExcludedPatternsChecksum
91+
}
92+
return ignored == artifact.ExcludedPatternsChecksum
93+
}
94+
95+
// SetExcludedPatterns sets the SHA256 checksum of the given excluded patterns.
96+
func (in *Artifact) SetExcludedPatterns(ignore *string) {
97+
if in == nil {
98+
return
99+
}
100+
if ignore == nil {
101+
in.ExcludedPatternsChecksum = ""
102+
return
103+
}
104+
h := sha256.New()
105+
_, _ = io.Copy(h, strings.NewReader(*ignore))
106+
in.ExcludedPatternsChecksum = fmt.Sprintf("%x", h.Sum(nil))
107+
}
108+
77109
// ArtifactDir returns the artifact dir path in the form of
78110
// '<kind>/<namespace>/<name>'.
79111
func ArtifactDir(kind, namespace, name string) string {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ spec:
379379
checksum:
380380
description: Checksum is the SHA256 checksum of the Artifact file.
381381
type: string
382+
excludedPatternsChecksum:
383+
description: ExcludedPatternsChecksum is a SHA256 checksum of
384+
the excluded patterns applied in the last reconciliation.
385+
type: string
382386
lastUpdateTime:
383387
description: LastUpdateTime is the timestamp corresponding to
384388
the last update of the Artifact.

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ spec:
149149
secretRef:
150150
description: The secret name containing the Git credentials. For HTTPS
151151
repositories the secret must contain username and password fields.
152-
For SSH repositories the secret must contain identity and known_hosts
153-
fields.
152+
For SSH repositories the secret must contain identity, identity.pub
153+
and known_hosts fields.
154154
properties:
155155
name:
156156
description: Name of the referent.
@@ -554,6 +554,10 @@ spec:
554554
checksum:
555555
description: Checksum is the SHA256 checksum of the Artifact file.
556556
type: string
557+
excludedPatternsChecksum:
558+
description: ExcludedPatternsChecksum is a SHA256 checksum of
559+
the excluded patterns applied in the last reconciliation.
560+
type: string
557561
lastUpdateTime:
558562
description: LastUpdateTime is the timestamp corresponding to
559563
the last update of the Artifact.
@@ -663,6 +667,10 @@ spec:
663667
description: Checksum is the SHA256 checksum of the Artifact
664668
file.
665669
type: string
670+
excludedPatternsChecksum:
671+
description: ExcludedPatternsChecksum is a SHA256 checksum of
672+
the excluded patterns applied in the last reconciliation.
673+
type: string
666674
lastUpdateTime:
667675
description: LastUpdateTime is the timestamp corresponding to
668676
the last update of the Artifact.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ spec:
427427
checksum:
428428
description: Checksum is the SHA256 checksum of the Artifact file.
429429
type: string
430+
excludedPatternsChecksum:
431+
description: ExcludedPatternsChecksum is a SHA256 checksum of
432+
the excluded patterns applied in the last reconciliation.
433+
type: string
430434
lastUpdateTime:
431435
description: LastUpdateTime is the timestamp corresponding to
432436
the last update of the Artifact.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ spec:
350350
checksum:
351351
description: Checksum is the SHA256 checksum of the Artifact file.
352352
type: string
353+
excludedPatternsChecksum:
354+
description: ExcludedPatternsChecksum is a SHA256 checksum of
355+
the excluded patterns applied in the last reconciliation.
356+
type: string
353357
lastUpdateTime:
354358
description: LastUpdateTime is the timestamp corresponding to
355359
the last update of the Artifact.

controllers/bucket_controller.go

Lines changed: 5 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"
@@ -561,18 +562,19 @@ func (r *BucketReconciler) reconcileArtifact(ctx context.Context, obj *sourcev1.
561562

562563
// Create artifact
563564
artifact := r.Storage.NewArtifactFor(obj.Kind, obj, revision, fmt.Sprintf("%s.tar.gz", revision))
565+
artifact.SetExcludedPatterns(obj.Spec.Ignore)
564566

565567
// Set the ArtifactInStorageCondition if there's no drift.
566568
defer func() {
567-
if obj.GetArtifact().HasRevision(artifact.Revision) {
569+
if obj.GetArtifact().HasRevision(artifact.Revision) && obj.GetArtifact().EqualExcludedPatterns(artifact) {
568570
conditions.Delete(obj, sourcev1.ArtifactOutdatedCondition)
569571
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason,
570572
"stored artifact for revision '%s'", artifact.Revision)
571573
}
572574
}()
573575

574576
// The artifact is up-to-date
575-
if obj.GetArtifact().HasRevision(artifact.Revision) {
577+
if obj.GetArtifact().HasRevision(artifact.Revision) && obj.GetArtifact().EqualExcludedPatterns(artifact) {
576578
r.eventLogf(ctx, obj, events.EventTypeTrace, sourcev1.ArtifactUpToDateReason, "artifact up-to-date with remote revision: '%s'", artifact.Revision)
577579
return sreconcile.ResultSuccess, nil
578580
}

controllers/gitrepository_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,18 +497,19 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context,
497497
obj *sourcev1.GitRepository, commit *git.Commit, includes *artifactSet, dir string) (sreconcile.Result, error) {
498498
// Create potential new artifact with current available metadata
499499
artifact := r.Storage.NewArtifactFor(obj.Kind, obj.GetObjectMeta(), commit.String(), fmt.Sprintf("%s.tar.gz", commit.Hash.String()))
500+
artifact.SetExcludedPatterns(obj.Spec.Ignore)
500501

501502
// Set the ArtifactInStorageCondition if there's no drift.
502503
defer func() {
503-
if obj.GetArtifact().HasRevision(artifact.Revision) && !includes.Diff(obj.Status.IncludedArtifacts) {
504+
if obj.GetArtifact().HasRevision(artifact.Revision) && !includes.Diff(obj.Status.IncludedArtifacts) && obj.GetArtifact().EqualExcludedPatterns(artifact) {
504505
conditions.Delete(obj, sourcev1.ArtifactOutdatedCondition)
505506
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason,
506507
"stored artifact for revision '%s'", artifact.Revision)
507508
}
508509
}()
509510

510511
// The artifact is up-to-date
511-
if obj.GetArtifact().HasRevision(artifact.Revision) && !includes.Diff(obj.Status.IncludedArtifacts) {
512+
if obj.GetArtifact().HasRevision(artifact.Revision) && !includes.Diff(obj.Status.IncludedArtifacts) && obj.GetArtifact().EqualExcludedPatterns(artifact) {
512513
r.eventLogf(ctx, obj, events.EventTypeTrace, sourcev1.ArtifactUpToDateReason, "artifact up-to-date with remote revision: '%s'", artifact.Revision)
513514
return sreconcile.ResultSuccess, nil
514515
}

0 commit comments

Comments
 (0)