Skip to content

Commit 6ee9864

Browse files
committed
improve conditions and fix temp file storage
Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent a6bb5f0 commit 6ee9864

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

Diff for: api/v1beta2/helmchart_types.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ const (
172172
// chart succeeded.
173173
ChartPackageSucceededReason string = "ChartPackageSucceeded"
174174

175-
// ChartVerifiedSucceededReason signals that the Helm chart's signature
175+
// ChartVerificationSucceededReason signals that the Helm chart's signature
176176
// has been verified using it's provenance file.
177-
ChartVerifiedSucceededReason string = "ChartVerifiedSucceeded"
177+
ChartVerificationSucceededReason string = "ChartVerificationSucceeded"
178178
)
179179

180180
// GetConditions returns the status conditions of the object.

Diff for: controllers/helmchart_controller.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -809,11 +809,14 @@ func (r *HelmChartReconciler) garbageCollect(ctx context.Context, obj *sourcev1.
809809
}
810810
return false
811811
}
812-
if _, err := r.Storage.RemoveConditionally(dir, callback); err != nil {
812+
if deleted, err := r.Storage.RemoveConditionally(dir, callback); err != nil {
813813
return &serror.Event{
814814
Err: fmt.Errorf("garbage collection of old artifacts failed: %w", err),
815815
Reason: "GarbageCollectionFailed",
816816
}
817+
} else if len(deleted) > 0 {
818+
r.eventLogf(ctx, obj, events.EventTypeTrace, "GarbageCollectionSucceeded",
819+
"garbage collected old artifacts")
817820
}
818821
}
819822
return nil
@@ -1039,7 +1042,7 @@ func observeChartBuild(obj *sourcev1.HelmChart, build *chart.Build, err error) {
10391042
sigVerMsg.WriteString(fmt.Sprintf(" using key with fingeprint: '%X'", build.VerificationSignature.KeyFingerprint))
10401043
sigVerMsg.WriteString(fmt.Sprintf(" and hash verified: '%s'", build.VerificationSignature.FileHash))
10411044

1042-
conditions.MarkTrue(obj, sourcev1.SourceVerifiedCondition, sourcev1.ChartVerifiedSucceededReason, sigVerMsg.String())
1045+
conditions.MarkTrue(obj, sourcev1.SourceVerifiedCondition, sourcev1.ChartVerificationSucceededReason, sigVerMsg.String())
10431046
} else {
10441047
conditions.Delete(obj, sourcev1.SourceVerifiedCondition)
10451048
}

Diff for: controllers/helmchart_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ func TestHelmChartReconciler_reconcileSource(t *testing.T) {
551551
g.Expect(obj.Status.ObservedSourceArtifactRevision).To(Equal(gitArtifact.Revision))
552552
g.Expect(obj.Status.Conditions).To(conditions.MatchConditions([]metav1.Condition{
553553
*conditions.TrueCondition(sourcev1.ArtifactOutdatedCondition, "NewChart", "pulled 'helmchart' chart with version '0.1.0'"),
554-
*conditions.TrueCondition(sourcev1.SourceVerifiedCondition, sourcev1.ChartVerifiedSucceededReason, "chart signed by: 'TestUser' using key with fingeprint: '943CB5929ECDA2B5B5EC88BC7035BA97D32A87C1' and hash verified: 'sha256:007c7b7446eebcb18caeffe9898a3356ba1795f54df40ad39cfcc7382874a10a'"),
554+
*conditions.TrueCondition(sourcev1.SourceVerifiedCondition, sourcev1.ChartVerificationSucceededReason, "chart signed by: 'TestUser' using key with fingeprint: '943CB5929ECDA2B5B5EC88BC7035BA97D32A87C1' and hash verified: 'sha256:007c7b7446eebcb18caeffe9898a3356ba1795f54df40ad39cfcc7382874a10a'"),
555555
}))
556556
},
557557
cleanFunc: func(g *WithT, build *chart.Build) {

Diff for: internal/helm/chart/builder_remote.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,12 @@ func (b *remoteChartBuilder) Build(_ context.Context, ref Reference, p string, o
158158
// This is needed, since the verification will work only if the .tgz file is untampered.
159159
// But we write the packaged chart to disk under a different name, so the provenance file
160160
// will not be valid for this _new_ packaged chart.
161-
chart, err := util.WriteToFile(chartBuf, fmt.Sprintf("%s-%s.tgz", cv.Name, cv.Version))
162-
defer os.Remove(chart.Name())
161+
chart, err := util.WriteToTempFile(chartBuf, fmt.Sprintf("%s-%s.tgz", cv.Name, cv.Version), true)
163162
if err != nil {
164163
return nil, err
165164
}
165+
defer os.Remove(chart.Name())
166166
ver, err := verifyChartWithProvFile(bytes.NewReader(opts.Keyring), chart.Name(), provFilePath)
167-
168167
if err != nil {
169168
return nil, err
170169
}
@@ -245,7 +244,7 @@ func mergeChartValues(chart *helmchart.Chart, paths []string) (map[string]interf
245244
// validatePackageAndWriteToPath atomically writes the packaged chart from reader
246245
// to out while validating it by loading the chart metadata from the archive.
247246
func validatePackageAndWriteToPath(b []byte, out string) error {
248-
tmpFile, err := util.WriteToTempFile(b, out)
247+
tmpFile, err := util.WriteToTempFile(b, out, false)
249248
defer os.Remove(tmpFile.Name())
250249

251250
if err != nil {

Diff for: internal/helm/repository/chart_repository.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (r *ChartRepository) DownloadProvenanceFile(chart *repo.ChartVersion, path
216216
if err != nil {
217217
return err
218218
}
219-
tmpFile, err := util.WriteToTempFile(res.Bytes(), path)
219+
tmpFile, err := util.WriteToTempFile(res.Bytes(), path, false)
220220
defer os.Remove(tmpFile.Name())
221221

222222
if err != nil {

Diff for: internal/util/file.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"fmt"
2121
"os"
2222
"path/filepath"
23+
24+
"github.com/fluxcd/source-controller/internal/fs"
2325
)
2426

2527
func writeBytesToFile(bytes []byte, file *os.File) error {
@@ -33,21 +35,24 @@ func writeBytesToFile(bytes []byte, file *os.File) error {
3335
return nil
3436
}
3537

36-
// Writes the provided bytes to a file at the given path and returns the file handle.
37-
func WriteToFile(bytes []byte, path string) (*os.File, error) {
38-
file, err := os.Create(path)
39-
if err != nil {
40-
return nil, fmt.Errorf("failed to create temporary file for chart %s: %w", path, err)
41-
}
42-
return file, writeBytesToFile(bytes, file)
43-
}
44-
4538
// Writes the provided bytes to a temp file with the name provided in the path and
46-
// returns the file handle.
47-
func WriteToTempFile(bytes []byte, out string) (*os.File, error) {
39+
// returns the file handle. If renameToOriginal is true, it renames the temp file to
40+
// the intended file name (since temp file names have random bytes as suffix).
41+
func WriteToTempFile(bytes []byte, out string, renameToOriginal bool) (*os.File, error) {
4842
file, err := os.CreateTemp("", filepath.Base(out))
4943
if err != nil {
5044
return nil, fmt.Errorf("failed to create temporary file %s: %w", filepath.Base(out), err)
5145
}
52-
return file, writeBytesToFile(bytes, file)
46+
err = writeBytesToFile(bytes, file)
47+
if err != nil {
48+
return nil, err
49+
}
50+
if renameToOriginal {
51+
err = fs.RenameWithFallback(file.Name(), filepath.Join("/tmp", filepath.Base(out)))
52+
file, err = os.Open(filepath.Join("/tmp", filepath.Base(out)))
53+
if err != nil {
54+
return nil, fmt.Errorf("failed to rename temporary file %s: %w", filepath.Base(out), err)
55+
}
56+
}
57+
return file, nil
5358
}

0 commit comments

Comments
 (0)