Skip to content

Commit dcd5dd3

Browse files
committed
internal/helm: various nitpicks
- Add some more documentation around chart builders - Ensure correct indentation in some doc comments - Provide example of using `errors.Is` for typed `BuildError` - Mention "bytes" in file size limit errors - Add missing copyright header Signed-off-by: Hidde Beydals <hello@hidde.co>
1 parent 4de8f1f commit dcd5dd3

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

internal/helm/chart/builder_local.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,34 @@ type localChartBuilder struct {
3434
dm *DependencyManager
3535
}
3636

37-
// NewLocalBuilder returns a Builder capable of building a Helm
38-
// chart with a LocalReference. For chart references pointing to a
39-
// directory, the DependencyManager is used to resolve missing local and
40-
// remote dependencies.
37+
// NewLocalBuilder returns a Builder capable of building a Helm chart with a
38+
// LocalReference. For chart references pointing to a directory, the
39+
// DependencyManager is used to resolve missing local and remote dependencies.
4140
func NewLocalBuilder(dm *DependencyManager) Builder {
4241
return &localChartBuilder{
4342
dm: dm,
4443
}
4544
}
4645

46+
// Build attempts to build a Helm chart with the given LocalReference and
47+
// BuildOptions, writing it to p.
48+
// It returns a Build describing the produced (or from cache observed) chart
49+
// written to p, or a BuildError.
50+
//
51+
// The chart is loaded from the LocalReference.Path, and only packaged if the
52+
// version (including BuildOptions.VersionMetadata modifications) differs from
53+
// the current BuildOptions.CachedChart.
54+
//
55+
// BuildOptions.ValuesFiles changes are in this case not taken into account,
56+
// and BuildOptions.Force should be used to enforce a rebuild.
57+
//
58+
// If the LocalReference.Path refers to an already packaged chart, and no
59+
// packaging is required due to BuildOptions modifying the chart,
60+
// LocalReference.Path is copied to p.
61+
//
62+
// If the LocalReference.Path refers to a chart directory, dependencies are
63+
// confirmed to be present using the DependencyManager, while attempting to
64+
// resolve any missing.
4765
func (b *localChartBuilder) Build(ctx context.Context, ref Reference, p string, opts BuildOptions) (*Build, error) {
4866
localRef, ok := ref.(LocalReference)
4967
if !ok {
@@ -80,8 +98,8 @@ func (b *localChartBuilder) Build(ctx context.Context, ref Reference, p string,
8098
}
8199

82100
// If all the following is true, we do not need to package the chart:
83-
// Chart version from metadata matches chart version for ref
84-
// BuildOptions.Force is False
101+
// - Chart version from current metadata matches calculated version
102+
// - BuildOptions.Force is False
85103
if opts.CachedChart != "" && !opts.Force {
86104
if curMeta, err = LoadChartMetadataFromArchive(opts.CachedChart); err == nil && result.Version == curMeta.Version {
87105
result.Path = opts.CachedChart

internal/helm/chart/builder_remote.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,27 @@ type remoteChartBuilder struct {
4040
}
4141

4242
// NewRemoteBuilder returns a Builder capable of building a Helm
43-
// chart with a RemoteReference from the given Index.
43+
// chart with a RemoteReference in the given repository.ChartRepository.
4444
func NewRemoteBuilder(repository *repository.ChartRepository) Builder {
4545
return &remoteChartBuilder{
4646
remote: repository,
4747
}
4848
}
4949

50+
// Build attempts to build a Helm chart with the given RemoteReference and
51+
// BuildOptions, writing it to p.
52+
// It returns a Build describing the produced (or from cache observed) chart
53+
// written to p, or a BuildError.
54+
//
55+
// The latest version for the RemoteReference.Version is determined in the
56+
// repository.ChartRepository, only downloading it if the version (including
57+
// BuildOptions.VersionMetadata) differs from the current BuildOptions.CachedChart.
58+
// BuildOptions.ValuesFiles changes are in this case not taken into account,
59+
// and BuildOptions.Force should be used to enforce a rebuild.
60+
//
61+
// After downloading the chart, it is only packaged if required due to BuildOptions
62+
// modifying the chart, otherwise the exact data as retrieved from the repository
63+
// is written to p, after validating it to be a chart.
5064
func (b *remoteChartBuilder) Build(_ context.Context, ref Reference, p string, opts BuildOptions) (*Build, error) {
5165
remoteRef, ok := ref.(RemoteReference)
5266
if !ok {
@@ -88,8 +102,8 @@ func (b *remoteChartBuilder) Build(_ context.Context, ref Reference, p string, o
88102
}
89103

90104
// If all the following is true, we do not need to download and/or build the chart:
91-
// Chart version from metadata matches chart version for ref
92-
// BuildOptions.Force is False
105+
// - Chart version from current metadata matches calculated version
106+
// - BuildOptions.Force is False
93107
if opts.CachedChart != "" && !opts.Force {
94108
if curMeta, err := LoadChartMetadataFromArchive(opts.CachedChart); err == nil && result.Version == curMeta.Version {
95109
result.Path = opts.CachedChart

internal/helm/chart/errors.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,19 @@ type BuildError struct {
3535
Err error
3636
}
3737

38-
// Error returns Err as a string, prefixed with the Reason, if any.
38+
// Error returns Err as a string, prefixed with the Reason to provide context.
3939
func (e *BuildError) Error() string {
4040
if e.Reason == nil {
4141
return e.Err.Error()
4242
}
4343
return fmt.Sprintf("%s: %s", e.Reason.Error(), e.Err.Error())
4444
}
4545

46-
// Is returns true of the Reason or Err equals target.
46+
// Is returns true if the Reason or Err equals target.
47+
// It can be used to programmatically place an arbitrary Err in the
48+
// context of the Builder:
49+
// err := &BuildError{Reason: ErrChartPull, Err: errors.New("arbitrary transport error")}
50+
// errors.Is(err, ErrChartPull)
4751
func (e *BuildError) Is(target error) bool {
4852
if e.Reason != nil && e.Reason == target {
4953
return true

internal/helm/chart/metadata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func LoadChartMetadataFromDir(dir string) (*helmchart.Metadata, error) {
118118
return nil, fmt.Errorf("'%s' is a directory", stat.Name())
119119
}
120120
if stat.Size() > helm.MaxChartFileSize {
121-
return nil, fmt.Errorf("size of '%s' exceeds '%d' limit", stat.Name(), helm.MaxChartFileSize)
121+
return nil, fmt.Errorf("size of '%s' exceeds '%d' bytes limit", stat.Name(), helm.MaxChartFileSize)
122122
}
123123
}
124124

@@ -145,7 +145,7 @@ func LoadChartMetadataFromArchive(archive string) (*helmchart.Metadata, error) {
145145
return nil, err
146146
}
147147
if stat.Size() > helm.MaxChartSize {
148-
return nil, fmt.Errorf("size of chart '%s' exceeds '%d' limit", stat.Name(), helm.MaxChartSize)
148+
return nil, fmt.Errorf("size of chart '%s' exceeds '%d' bytes limit", stat.Name(), helm.MaxChartSize)
149149
}
150150

151151
f, err := os.Open(archive)

internal/helm/repository/chart_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func (r *ChartRepository) LoadFromFile(path string) error {
244244
return err
245245
}
246246
if stat.Size() > helm.MaxIndexSize {
247-
return fmt.Errorf("size of index '%s' exceeds '%d' limit", stat.Name(), helm.MaxIndexSize)
247+
return fmt.Errorf("size of index '%s' exceeds '%d' bytes limit", stat.Name(), helm.MaxIndexSize)
248248
}
249249
b, err := os.ReadFile(path)
250250
if err != nil {

internal/helm/repository/utils_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2021 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package repository
218

319
import (

0 commit comments

Comments
 (0)