Skip to content

Commit c1fb9b7

Browse files
authored
Merge pull request #154 from fluxcd/small-refactors
2 parents 41e8f21 + ac84c70 commit c1fb9b7

File tree

9 files changed

+58
-54
lines changed

9 files changed

+58
-54
lines changed

.github/workflows/e2e.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ jobs:
6767
kubectl -n source-system wait helmchart/mariadb-git --for=condition=ready --timeout=5m
6868
kubectl -n source-system delete -f ./config/testdata/helmchart-valuesfile
6969
- name: Setup Minio
70+
env:
71+
MINIO_VER: ${{ 'v6.3.1' }}
7072
run: |
7173
kubectl create ns minio
7274
helm repo add minio https://helm.min.io/
7375
helm upgrade --wait -i minio minio/minio \
76+
--version $MINIO_VER \
7477
--namespace minio \
7578
--set accessKey=myaccesskey \
7679
--set secretKey=mysecretkey \

api/v1alpha1/artifact.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ type Artifact struct {
4949
LastUpdateTime metav1.Time `json:"lastUpdateTime,omitempty"`
5050
}
5151

52+
// HasRevision returns true if the given revision matches the current
53+
// Revision of the Artifact.
54+
func (in *Artifact) HasRevision(revision string) bool {
55+
if in == nil {
56+
return false
57+
}
58+
return in.Revision == revision
59+
}
60+
5261
// ArtifactDir returns the artifact dir path in the form of
5362
// <source-kind>/<source-namespace>/<source-name>.
5463
func ArtifactDir(kind, namespace, name string) string {

controllers/bucket_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (r *BucketReconciler) reconcile(ctx context.Context, bucket sourcev1.Bucket
211211

212212
// return early on unchanged revision
213213
artifact := r.Storage.NewArtifactFor(bucket.Kind, bucket.GetObjectMeta(), revision, fmt.Sprintf("%s.tar.gz", revision))
214-
if bucket.GetArtifact() != nil && bucket.GetArtifact().Revision == revision {
214+
if bucket.GetArtifact().HasRevision(artifact.Revision) {
215215
if artifact.URL != bucket.GetArtifact().URL {
216216
r.Storage.SetArtifactURL(bucket.GetArtifact())
217217
bucket.Status.URL = r.Storage.SetHostname(bucket.Status.URL)

controllers/gitrepository_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
198198

199199
// return early on unchanged revision
200200
artifact := r.Storage.NewArtifactFor(repository.Kind, repository.GetObjectMeta(), revision, fmt.Sprintf("%s.tar.gz", commit.Hash.String()))
201-
if repository.GetArtifact() != nil && repository.GetArtifact().Revision == revision {
201+
if repository.GetArtifact().HasRevision(artifact.Revision) {
202202
if artifact.URL != repository.GetArtifact().URL {
203203
r.Storage.SetArtifactURL(repository.GetArtifact())
204204
repository.Status.URL = r.Storage.SetHostname(repository.Status.URL)

controllers/helmchart_controller.go

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,12 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
238238
}
239239

240240
// Return early if the revision is still the same as the current artifact
241-
artifact := r.Storage.NewArtifactFor(chart.Kind, chart.GetObjectMeta(), cv.Version,
241+
newArtifact := r.Storage.NewArtifactFor(chart.Kind, chart.GetObjectMeta(), cv.Version,
242242
fmt.Sprintf("%s-%s.tgz", cv.Name, cv.Version))
243-
if !force && repository.GetArtifact() != nil && repository.GetArtifact().Revision == cv.Version {
244-
if artifact.URL != repository.GetArtifact().URL {
243+
if !force && repository.GetArtifact().HasRevision(newArtifact.Revision) {
244+
if newArtifact.URL != repository.GetArtifact().URL {
245245
r.Storage.SetArtifactURL(chart.GetArtifact())
246-
repository.Status.URL = r.Storage.SetHostname(chart.Status.URL)
246+
chart.Status.URL = r.Storage.SetHostname(chart.Status.URL)
247247
}
248248
return chart, nil
249249
}
@@ -296,21 +296,19 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
296296
err = fmt.Errorf("auth options error: %w", err)
297297
return sourcev1.HelmChartNotReady(chart, sourcev1.AuthenticationFailedReason, err.Error()), err
298298
}
299-
if cleanup != nil {
300-
defer cleanup()
301-
}
299+
defer cleanup()
302300
clientOpts = opts
303301
}
304302

305303
// Ensure artifact directory exists
306-
err = r.Storage.MkdirAll(artifact)
304+
err = r.Storage.MkdirAll(newArtifact)
307305
if err != nil {
308306
err = fmt.Errorf("unable to create chart directory: %w", err)
309307
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
310308
}
311309

312310
// Acquire a lock for the artifact
313-
unlock, err := r.Storage.Lock(artifact)
311+
unlock, err := r.Storage.Lock(newArtifact)
314312
if err != nil {
315313
err = fmt.Errorf("unable to acquire lock: %w", err)
316314
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
@@ -328,7 +326,7 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
328326
// or write the chart directly to storage.
329327
var (
330328
readyReason = sourcev1.ChartPullSucceededReason
331-
readyMessage = fmt.Sprintf("Fetched revision: %s", artifact.Revision)
329+
readyMessage = fmt.Sprintf("Fetched revision: %s", newArtifact.Revision)
332330
)
333331
switch {
334332
case chart.Spec.ValuesFile != "" && chart.Spec.ValuesFile != chartutil.ValuesfileName:
@@ -362,36 +360,29 @@ func (r *HelmChartReconciler) reconcileFromHelmRepository(ctx context.Context,
362360
}
363361

364362
// Copy the packaged chart to the artifact path
365-
cf, err := os.Open(pkgPath)
366-
if err != nil {
367-
err = fmt.Errorf("failed to open chart package: %w", err)
363+
if err := r.Storage.CopyFromPath(&newArtifact, pkgPath); err != nil {
364+
err = fmt.Errorf("failed to write chart package to storage: %w", err)
368365
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
369366
}
370-
if err := r.Storage.Copy(&artifact, cf); err != nil {
371-
cf.Close()
372-
err = fmt.Errorf("failed to copy chart package to storage: %w", err)
373-
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
374-
}
375-
cf.Close()
376367

377-
readyMessage = fmt.Sprintf("Fetched and packaged revision: %s", artifact.Revision)
368+
readyMessage = fmt.Sprintf("Fetched and packaged revision: %s", newArtifact.Revision)
378369
readyReason = sourcev1.ChartPackageSucceededReason
379370
default:
380371
// Write artifact to storage
381-
if err := r.Storage.AtomicWriteFile(&artifact, res, 0644); err != nil {
372+
if err := r.Storage.AtomicWriteFile(&newArtifact, res, 0644); err != nil {
382373
err = fmt.Errorf("unable to write chart file: %w", err)
383374
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
384375
}
385376
}
386377

387378
// Update symlink
388-
chartUrl, err := r.Storage.Symlink(artifact, fmt.Sprintf("%s-latest.tgz", cv.Name))
379+
chartUrl, err := r.Storage.Symlink(newArtifact, fmt.Sprintf("%s-latest.tgz", cv.Name))
389380
if err != nil {
390381
err = fmt.Errorf("storage error: %w", err)
391382
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
392383
}
393384

394-
return sourcev1.HelmChartReady(chart, artifact, chartUrl, readyReason, readyMessage), nil
385+
return sourcev1.HelmChartReady(chart, newArtifact, chartUrl, readyReason, readyMessage), nil
395386
}
396387

397388
func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
@@ -432,32 +423,30 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
432423
}
433424

434425
// Return early if the revision is still the same as the current chart artifact
435-
chartArtifact := r.Storage.NewArtifactFor(chart.Kind, chart.ObjectMeta.GetObjectMeta(), chartMetadata.Version,
426+
newArtifact := r.Storage.NewArtifactFor(chart.Kind, chart.ObjectMeta.GetObjectMeta(), chartMetadata.Version,
436427
fmt.Sprintf("%s-%s.tgz", chartMetadata.Name, chartMetadata.Version))
437-
if !force && chart.GetArtifact() != nil && chart.GetArtifact().Revision == chartMetadata.Version {
438-
if chartArtifact.URL != artifact.URL {
439-
r.Storage.SetArtifactURL(&chartArtifact)
428+
if !force && chart.GetArtifact().HasRevision(newArtifact.Revision) {
429+
if newArtifact.URL != artifact.URL {
430+
r.Storage.SetArtifactURL(&newArtifact)
440431
chart.Status.URL = r.Storage.SetHostname(chart.Status.URL)
441432
}
442433
return chart, nil
443434
}
444435

445-
// Overwrite default values if instructed to
446-
if chart.Spec.ValuesFile != "" {
447-
if err := helm.OverwriteChartDefaultValues(chartPath, chart.Spec.ValuesFile); err != nil {
448-
return sourcev1.HelmChartNotReady(chart, sourcev1.ChartPackageFailedReason, err.Error()), err
449-
}
436+
// Overwrite default values if configured
437+
if err := helm.OverwriteChartDefaultValues(chartPath, chart.Spec.ValuesFile); err != nil {
438+
return sourcev1.HelmChartNotReady(chart, sourcev1.ChartPackageFailedReason, err.Error()), err
450439
}
451440

452441
// Ensure artifact directory exists
453-
err = r.Storage.MkdirAll(chartArtifact)
442+
err = r.Storage.MkdirAll(newArtifact)
454443
if err != nil {
455444
err = fmt.Errorf("unable to create artifact directory: %w", err)
456445
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
457446
}
458447

459448
// Acquire a lock for the artifact
460-
unlock, err := r.Storage.Lock(chartArtifact)
449+
unlock, err := r.Storage.Lock(newArtifact)
461450
if err != nil {
462451
err = fmt.Errorf("unable to acquire lock: %w", err)
463452
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
@@ -475,27 +464,20 @@ func (r *HelmChartReconciler) reconcileFromTarballArtifact(ctx context.Context,
475464
}
476465

477466
// Copy the packaged chart to the artifact path
478-
cf, err := os.Open(pkgPath)
479-
if err != nil {
480-
err = fmt.Errorf("failed to open chart package: %w", err)
481-
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
482-
}
483-
if err := r.Storage.Copy(&chartArtifact, cf); err != nil {
484-
cf.Close()
485-
err = fmt.Errorf("failed to copy chart package to storage: %w", err)
467+
if err := r.Storage.CopyFromPath(&newArtifact, pkgPath); err != nil {
468+
err = fmt.Errorf("failed to write chart package to storage: %w", err)
486469
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
487470
}
488-
cf.Close()
489471

490472
// Update symlink
491-
cUrl, err := r.Storage.Symlink(chartArtifact, fmt.Sprintf("%s-latest.tgz", chartMetadata.Name))
473+
cUrl, err := r.Storage.Symlink(newArtifact, fmt.Sprintf("%s-latest.tgz", chartMetadata.Name))
492474
if err != nil {
493475
err = fmt.Errorf("storage error: %w", err)
494476
return sourcev1.HelmChartNotReady(chart, sourcev1.StorageOperationFailedReason, err.Error()), err
495477
}
496478

497-
message := fmt.Sprintf("Fetched and packaged revision: %s", chartArtifact.Revision)
498-
return sourcev1.HelmChartReady(chart, chartArtifact, cUrl, sourcev1.ChartPackageSucceededReason, message), nil
479+
message := fmt.Sprintf("Fetched and packaged revision: %s", newArtifact.Revision)
480+
return sourcev1.HelmChartReady(chart, newArtifact, cUrl, sourcev1.ChartPackageSucceededReason, message), nil
499481
}
500482

501483
// resetStatus returns a modified v1alpha1.HelmChart and a boolean indicating

controllers/helmrepository_controller.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ func (r *HelmRepositoryReconciler) reconcile(ctx context.Context, repository sou
195195
err = fmt.Errorf("auth options error: %w", err)
196196
return sourcev1.HelmRepositoryNotReady(repository, sourcev1.AuthenticationFailedReason, err.Error()), err
197197
}
198-
if cleanup != nil {
199-
defer cleanup()
200-
}
198+
defer cleanup()
201199
clientOpts = opts
202200
}
203201

@@ -219,7 +217,7 @@ func (r *HelmRepositoryReconciler) reconcile(ctx context.Context, repository sou
219217
// return early on unchanged generation
220218
artifact := r.Storage.NewArtifactFor(repository.Kind, repository.ObjectMeta.GetObjectMeta(), i.Generated.Format(time.RFC3339Nano),
221219
fmt.Sprintf("index-%s.yaml", url.PathEscape(i.Generated.Format(time.RFC3339Nano))))
222-
if repository.GetArtifact() != nil && repository.GetArtifact().Revision == i.Generated.Format(time.RFC3339Nano) {
220+
if repository.GetArtifact().HasRevision(artifact.Revision) {
223221
if artifact.URL != repository.GetArtifact().URL {
224222
r.Storage.SetArtifactURL(repository.GetArtifact())
225223
repository.Status.URL = r.Storage.SetHostname(repository.Status.URL)

controllers/storage.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,18 @@ func (s *Storage) Copy(artifact *sourcev1.Artifact, reader io.Reader) (err error
339339
return nil
340340
}
341341

342+
// CopyFromPath atomically copies the contents of the given path to the path of
343+
// the v1alpha1.Artifact. If successful, the checksum and last update time on the
344+
// artifact is set.
345+
func (s *Storage) CopyFromPath(artifact *sourcev1.Artifact, path string) (err error) {
346+
f, err := os.Open(path)
347+
if err != nil {
348+
return err
349+
}
350+
defer f.Close()
351+
return s.Copy(artifact, f)
352+
}
353+
342354
// Symlink creates or updates a symbolic link for the given v1alpha1.Artifact
343355
// and returns the URL for the symlink.
344356
func (s *Storage) Symlink(artifact sourcev1.Artifact, linkName string) (string, error) {

internal/helm/chart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
// OverwriteChartDefaultValues overwrites the chart default values file in the
2929
// given chartPath with the contents of the given valuesFile.
3030
func OverwriteChartDefaultValues(chartPath, valuesFile string) error {
31-
if valuesFile == chartutil.ValuesfileName {
31+
if valuesFile == "" || valuesFile == chartutil.ValuesfileName {
3232
return nil
3333
}
3434
srcPath := path.Join(chartPath, valuesFile)

internal/helm/getter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TLSClientConfigFromSecret(secret corev1.Secret) (getter.Option, func(), err
6060
certBytes, keyBytes, caBytes := secret.Data["certFile"], secret.Data["keyFile"], secret.Data["caFile"]
6161
switch {
6262
case len(certBytes)+len(keyBytes)+len(caBytes) == 0:
63-
return nil, nil, nil
63+
return nil, func() {}, nil
6464
case (len(certBytes) > 0 && len(keyBytes) == 0) || (len(keyBytes) > 0 && len(certBytes) == 0):
6565
return nil, nil, fmt.Errorf("invalid '%s' secret data: fields 'certFile' and 'keyFile' require each other's presence",
6666
secret.Name)

0 commit comments

Comments
 (0)