Skip to content

Commit 42706a3

Browse files
committed
Calculate checksums during file writes
1 parent 1ab1286 commit 42706a3

File tree

10 files changed

+230
-254
lines changed

10 files changed

+230
-254
lines changed

api/v1alpha1/gitrepository_types.go

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -122,62 +122,45 @@ const (
122122
GitOperationFailedReason string = "GitOperationFailed"
123123
)
124124

125-
// GitRepositoryReady sets the given artifact and url on the
126-
// GitRepository and resets the conditions to SourceCondition of
127-
// type Ready with status true and the given reason and message.
128-
// It returns the modified GitRepository.
129-
func GitRepositoryReady(repository GitRepository, artifact Artifact, url, reason, message string) GitRepository {
130-
repository.Status.Conditions = []SourceCondition{
131-
{
132-
Type: ReadyCondition,
133-
Status: corev1.ConditionTrue,
134-
LastTransitionTime: metav1.Now(),
135-
Reason: reason,
136-
Message: message,
137-
},
138-
}
139-
repository.Status.URL = url
140-
141-
if repository.Status.Artifact != nil {
142-
if repository.Status.Artifact.Path != artifact.Path {
143-
repository.Status.Artifact = &artifact
144-
}
145-
} else {
146-
repository.Status.Artifact = &artifact
147-
}
148-
149-
return repository
150-
}
151-
152125
// GitRepositoryProgressing resets the conditions of the GitRepository
153126
// to SourceCondition of type Ready with status unknown and
154127
// progressing reason and message. It returns the modified GitRepository.
155128
func GitRepositoryProgressing(repository GitRepository) GitRepository {
156-
repository.Status.Conditions = []SourceCondition{
157-
{
158-
Type: ReadyCondition,
159-
Status: corev1.ConditionUnknown,
160-
LastTransitionTime: metav1.Now(),
161-
Reason: ProgressingReason,
162-
Message: "reconciliation in progress",
163-
},
164-
}
129+
repository.Status.URL = ""
130+
repository.Status.Artifact = nil
131+
repository.Status.Conditions = []SourceCondition{}
132+
SetGitRepositoryCondition(&repository, ReadyCondition, corev1.ConditionUnknown, ProgressingReason, "reconciliation in progress")
165133
return repository
166134
}
167135

168-
// GitRepositoryNotReady resets the conditions of the GitRepository
169-
// to SourceCondition of type Ready with status false and the given
170-
// reason and message. It returns the modified GitRepository.
136+
// SetGitRepositoryCondition sets the given condition with the given status, reason and message
137+
// on the GitRepository.
138+
func SetGitRepositoryCondition(repository *GitRepository, condition string, status corev1.ConditionStatus, reason, message string) {
139+
repository.Status.Conditions = filterOutSourceCondition(repository.Status.Conditions, condition)
140+
repository.Status.Conditions = append(repository.Status.Conditions, SourceCondition{
141+
Type: condition,
142+
Status: status,
143+
LastTransitionTime: metav1.Now(),
144+
Reason: reason,
145+
Message: message,
146+
})
147+
}
148+
149+
// GitRepositoryReady sets the given artifact and url on the GitRepository
150+
// and sets the ReadyCondition to True, with the given reason and
151+
// message. It returns the modified GitRepository.
152+
func GitRepositoryReady(repository GitRepository, artifact Artifact, url, reason, message string) GitRepository {
153+
repository.Status.Artifact = &artifact
154+
repository.Status.URL = url
155+
SetGitRepositoryCondition(&repository, ReadyCondition, corev1.ConditionTrue, reason, message)
156+
return repository
157+
}
158+
159+
// GitRepositoryNotReady sets the ReadyCondition on the given GitRepository
160+
// to False, with the given reason and message. It returns the modified
161+
// GitRepository.
171162
func GitRepositoryNotReady(repository GitRepository, reason, message string) GitRepository {
172-
repository.Status.Conditions = []SourceCondition{
173-
{
174-
Type: ReadyCondition,
175-
Status: corev1.ConditionFalse,
176-
LastTransitionTime: metav1.Now(),
177-
Reason: reason,
178-
Message: message,
179-
},
180-
}
163+
SetGitRepositoryCondition(&repository, ReadyCondition, corev1.ConditionFalse, reason, message)
181164
return repository
182165
}
183166

api/v1alpha1/helmchart_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const (
9292
ChartPackageSucceededReason string = "ChartPackageSucceeded"
9393
)
9494

95-
// HelmReleaseProgressing resets any failures and registers progress toward reconciling the given HelmRelease
95+
// HelmChartProgressing resets any failures and registers progress toward reconciling the given HelmChart
9696
// by setting the ReadyCondition to ConditionUnknown for ProgressingReason.
9797
func HelmChartProgressing(chart HelmChart) HelmChart {
9898
chart.Status.URL = ""

api/v1alpha1/helmrepository_types.go

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -76,62 +76,45 @@ const (
7676
IndexationSucceededReason string = "IndexationSucceed"
7777
)
7878

79-
// HelmRepositoryReady sets the given artifact and url on the
80-
// HelmRepository and resets the conditions to SourceCondition of
81-
// type Ready with status true and the given reason and message.
82-
// It returns the modified HelmRepository.
83-
func HelmRepositoryReady(repository HelmRepository, artifact Artifact, url, reason, message string) HelmRepository {
84-
repository.Status.Conditions = []SourceCondition{
85-
{
86-
Type: ReadyCondition,
87-
Status: corev1.ConditionTrue,
88-
LastTransitionTime: metav1.Now(),
89-
Reason: reason,
90-
Message: message,
91-
},
92-
}
93-
repository.Status.URL = url
94-
95-
if repository.Status.Artifact != nil {
96-
if repository.Status.Artifact.Path != artifact.Path {
97-
repository.Status.Artifact = &artifact
98-
}
99-
} else {
100-
repository.Status.Artifact = &artifact
101-
}
102-
103-
return repository
104-
}
105-
10679
// HelmRepositoryProgressing resets the conditions of the HelmRepository
10780
// to SourceCondition of type Ready with status unknown and
10881
// progressing reason and message. It returns the modified HelmRepository.
10982
func HelmRepositoryProgressing(repository HelmRepository) HelmRepository {
110-
repository.Status.Conditions = []SourceCondition{
111-
{
112-
Type: ReadyCondition,
113-
Status: corev1.ConditionUnknown,
114-
LastTransitionTime: metav1.Now(),
115-
Reason: ProgressingReason,
116-
Message: "reconciliation in progress",
117-
},
118-
}
83+
repository.Status.URL = ""
84+
repository.Status.Artifact = nil
85+
repository.Status.Conditions = []SourceCondition{}
86+
SetHelmRepositoryCondition(&repository, ReadyCondition, corev1.ConditionUnknown, ProgressingReason, "reconciliation in progress")
11987
return repository
12088
}
12189

122-
// HelmRepositoryNotReady resets the conditions of the HelmRepository
123-
// to SourceCondition of type Ready with status false and the given
124-
// reason and message. It returns the modified HelmRepository.
90+
// SetHelmRepositoryCondition sets the given condition with the given status,
91+
// reason and message on the HelmRepository.
92+
func SetHelmRepositoryCondition(repository *HelmRepository, condition string, status corev1.ConditionStatus, reason, message string) {
93+
repository.Status.Conditions = filterOutSourceCondition(repository.Status.Conditions, condition)
94+
repository.Status.Conditions = append(repository.Status.Conditions, SourceCondition{
95+
Type: condition,
96+
Status: status,
97+
LastTransitionTime: metav1.Now(),
98+
Reason: reason,
99+
Message: message,
100+
})
101+
}
102+
103+
// HelmRepositoryReady sets the given artifact and url on the HelmRepository
104+
// and sets the ReadyCondition to True, with the given reason and
105+
// message. It returns the modified HelmRepository.
106+
func HelmRepositoryReady(repository HelmRepository, artifact Artifact, url, reason, message string) HelmRepository {
107+
repository.Status.Artifact = &artifact
108+
repository.Status.URL = url
109+
SetHelmRepositoryCondition(&repository, ReadyCondition, corev1.ConditionTrue, reason, message)
110+
return repository
111+
}
112+
113+
// HelmRepositoryNotReady sets the ReadyCondition on the given HelmRepository
114+
// to False, with the given reason and message. It returns the modified
115+
// HelmRepository.
125116
func HelmRepositoryNotReady(repository HelmRepository, reason, message string) HelmRepository {
126-
repository.Status.Conditions = []SourceCondition{
127-
{
128-
Type: ReadyCondition,
129-
Status: corev1.ConditionFalse,
130-
LastTransitionTime: metav1.Now(),
131-
Reason: reason,
132-
Message: message,
133-
},
134-
}
117+
SetHelmRepositoryCondition(&repository, ReadyCondition, corev1.ConditionFalse, reason, message)
135118
return repository
136119
}
137120

controllers/gitrepository_controller.go

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/go-git/go-git/v5/plumbing/transport"
2929
"github.com/go-logr/logr"
3030
corev1 "k8s.io/api/core/v1"
31-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3231
"k8s.io/apimachinery/pkg/runtime"
3332
"k8s.io/apimachinery/pkg/types"
3433
kuberecorder "k8s.io/client-go/tools/record"
@@ -100,13 +99,7 @@ func (r *GitRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
10099
}
101100

102101
// set initial status
103-
if reset, status := r.shouldResetStatus(repository); reset {
104-
repository.Status = status
105-
if err := r.Status().Update(ctx, &repository); err != nil {
106-
log.Error(err, "unable to update status")
107-
return ctrl.Result{Requeue: true}, err
108-
}
109-
} else {
102+
if repository.Generation == 0 || repository.GetArtifact() != nil && !r.Storage.ArtifactExist(*repository.GetArtifact()) {
110103
repository = sourcev1.GitRepositoryProgressing(repository)
111104
if err := r.Status().Update(ctx, &repository); err != nil {
112105
log.Error(err, "unable to update status")
@@ -202,6 +195,12 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
202195
return sourcev1.GitRepositoryNotReady(repository, sourcev1.GitOperationFailedReason, err.Error()), err
203196
}
204197

198+
// return early on unchanged revision
199+
artifact := r.Storage.NewArtifactFor(repository.Kind, repository.GetObjectMeta(), revision, fmt.Sprintf("%s.tar.gz", commit.Hash.String()))
200+
if repository.GetArtifact() != nil && repository.GetArtifact().Revision == revision {
201+
return repository, nil
202+
}
203+
205204
// verify PGP signature
206205
if repository.Spec.Verification != nil {
207206
err := r.verify(ctx, types.NamespacedName{
@@ -213,11 +212,6 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
213212
}
214213
}
215214

216-
// TODO(hidde): implement checksum when https://github.com/fluxcd/source-controller/pull/133
217-
// has been merged.
218-
artifact := r.Storage.ArtifactFor(repository.Kind, repository.ObjectMeta.GetObjectMeta(),
219-
fmt.Sprintf("%s.tar.gz", commit.Hash.String()), revision, "")
220-
221215
// create artifact dir
222216
err = r.Storage.MkdirAll(artifact)
223217
if err != nil {
@@ -234,7 +228,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
234228
defer unlock()
235229

236230
// archive artifact and check integrity
237-
if err := r.Storage.Archive(artifact, tmpGit, repository.Spec); err != nil {
231+
if err := r.Storage.Archive(&artifact, tmpGit, repository.Spec); err != nil {
238232
err = fmt.Errorf("storage archive error: %w", err)
239233
return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err
240234
}
@@ -250,32 +244,6 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
250244
return sourcev1.GitRepositoryReady(repository, artifact, url, sourcev1.GitOperationSucceedReason, message), nil
251245
}
252246

253-
// shouldResetStatus returns a boolean indicating if the status of the
254-
// given repository should be reset.
255-
func (r *GitRepositoryReconciler) shouldResetStatus(repository sourcev1.GitRepository) (bool, sourcev1.GitRepositoryStatus) {
256-
resetStatus := false
257-
if repository.Status.Artifact != nil {
258-
if !r.Storage.ArtifactExist(*repository.Status.Artifact) {
259-
resetStatus = true
260-
}
261-
}
262-
263-
if len(repository.Status.Conditions) == 0 || resetStatus {
264-
resetStatus = true
265-
}
266-
267-
return resetStatus, sourcev1.GitRepositoryStatus{
268-
Conditions: []sourcev1.SourceCondition{
269-
{
270-
Type: sourcev1.ReadyCondition,
271-
Status: corev1.ConditionUnknown,
272-
Reason: sourcev1.InitializingReason,
273-
LastTransitionTime: metav1.Now(),
274-
},
275-
},
276-
}
277-
}
278-
279247
// verify returns an error if the PGP signature can't be verified
280248
func (r *GitRepositoryReconciler) verify(ctx context.Context, publicKeySecret types.NamespacedName, commit *object.Commit) error {
281249
if commit.PGPSignature == "" {
@@ -304,10 +272,10 @@ func (r *GitRepositoryReconciler) verify(ctx context.Context, publicKeySecret ty
304272
// the given repository.
305273
func (r *GitRepositoryReconciler) gc(repository sourcev1.GitRepository, all bool) error {
306274
if all {
307-
return r.Storage.RemoveAll(r.Storage.ArtifactFor(repository.Kind, repository.GetObjectMeta(), "", "", ""))
275+
return r.Storage.RemoveAll(r.Storage.NewArtifactFor(repository.Kind, repository.GetObjectMeta(), "", ""))
308276
}
309-
if repository.Status.Artifact != nil {
310-
return r.Storage.RemoveAllButCurrent(*repository.Status.Artifact)
277+
if repository.GetArtifact() != nil {
278+
return r.Storage.RemoveAllButCurrent(*repository.GetArtifact())
311279
}
312280
return nil
313281
}

0 commit comments

Comments
 (0)