working commit
This commit is contained in:
+276
@@ -0,0 +1,276 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package repo // import "helm.sh/helm/v4/pkg/repo/v1"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"helm.sh/helm/v4/internal/fileutil"
|
||||
"helm.sh/helm/v4/pkg/getter"
|
||||
"helm.sh/helm/v4/pkg/helmpath"
|
||||
)
|
||||
|
||||
// Entry represents a collection of parameters for chart repository
|
||||
type Entry struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
CertFile string `json:"certFile"`
|
||||
KeyFile string `json:"keyFile"`
|
||||
CAFile string `json:"caFile"`
|
||||
InsecureSkipTLSVerify bool `json:"insecure_skip_tls_verify"`
|
||||
PassCredentialsAll bool `json:"pass_credentials_all"`
|
||||
}
|
||||
|
||||
// ChartRepository represents a chart repository
|
||||
type ChartRepository struct {
|
||||
Config *Entry
|
||||
IndexFile *IndexFile
|
||||
Client getter.Getter
|
||||
CachePath string
|
||||
}
|
||||
|
||||
// NewChartRepository constructs ChartRepository
|
||||
func NewChartRepository(cfg *Entry, getters getter.Providers) (*ChartRepository, error) {
|
||||
u, err := url.Parse(cfg.URL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid chart URL format: %s", cfg.URL)
|
||||
}
|
||||
|
||||
client, err := getters.ByScheme(u.Scheme)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not find protocol handler for: %s", u.Scheme)
|
||||
}
|
||||
|
||||
return &ChartRepository{
|
||||
Config: cfg,
|
||||
IndexFile: NewIndexFile(),
|
||||
Client: client,
|
||||
CachePath: helmpath.CachePath("repository"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DownloadIndexFile fetches the index from a repository.
|
||||
func (r *ChartRepository) DownloadIndexFile() (string, error) {
|
||||
indexURL, err := ResolveReferenceURL(r.Config.URL, "index.yaml")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resp, err := r.Client.Get(indexURL,
|
||||
getter.WithURL(r.Config.URL),
|
||||
getter.WithInsecureSkipVerifyTLS(r.Config.InsecureSkipTLSVerify),
|
||||
getter.WithTLSClientConfig(r.Config.CertFile, r.Config.KeyFile, r.Config.CAFile),
|
||||
getter.WithBasicAuth(r.Config.Username, r.Config.Password),
|
||||
getter.WithPassCredentialsAll(r.Config.PassCredentialsAll),
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
index, err := io.ReadAll(resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
indexFile, err := loadIndex(index, r.Config.URL)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Create the chart list file in the cache directory
|
||||
var charts strings.Builder
|
||||
for name := range indexFile.Entries {
|
||||
fmt.Fprintln(&charts, name)
|
||||
}
|
||||
chartsFile := filepath.Join(r.CachePath, helmpath.CacheChartsFile(r.Config.Name))
|
||||
os.MkdirAll(filepath.Dir(chartsFile), 0755)
|
||||
|
||||
fileutil.AtomicWriteFile(chartsFile, bytes.NewReader([]byte(charts.String())), 0644)
|
||||
|
||||
// Create the index file in the cache directory
|
||||
fname := filepath.Join(r.CachePath, helmpath.CacheIndexFile(r.Config.Name))
|
||||
os.MkdirAll(filepath.Dir(fname), 0755)
|
||||
return fname, fileutil.AtomicWriteFile(fname, bytes.NewReader(index), 0644)
|
||||
}
|
||||
|
||||
type findChartInRepoURLOptions struct {
|
||||
Username string
|
||||
Password string
|
||||
PassCredentialsAll bool
|
||||
InsecureSkipTLSVerify bool
|
||||
CertFile string
|
||||
KeyFile string
|
||||
CAFile string
|
||||
ChartVersion string
|
||||
}
|
||||
|
||||
type FindChartInRepoURLOption func(*findChartInRepoURLOptions)
|
||||
|
||||
// WithChartVersion specifies the chart version to find
|
||||
func WithChartVersion(chartVersion string) FindChartInRepoURLOption {
|
||||
return func(options *findChartInRepoURLOptions) {
|
||||
options.ChartVersion = chartVersion
|
||||
}
|
||||
}
|
||||
|
||||
// WithUsernamePassword specifies the username/password credntials for the repository
|
||||
func WithUsernamePassword(username, password string) FindChartInRepoURLOption {
|
||||
return func(options *findChartInRepoURLOptions) {
|
||||
options.Username = username
|
||||
options.Password = password
|
||||
}
|
||||
}
|
||||
|
||||
// WithPassCredentialsAll flags whether credentials should be passed on to other domains
|
||||
func WithPassCredentialsAll(passCredentialsAll bool) FindChartInRepoURLOption {
|
||||
return func(options *findChartInRepoURLOptions) {
|
||||
options.PassCredentialsAll = passCredentialsAll
|
||||
}
|
||||
}
|
||||
|
||||
// WithClientTLS species the cert, key, and CA files for client mTLS
|
||||
func WithClientTLS(certFile, keyFile, caFile string) FindChartInRepoURLOption {
|
||||
return func(options *findChartInRepoURLOptions) {
|
||||
options.CertFile = certFile
|
||||
options.KeyFile = keyFile
|
||||
options.CAFile = caFile
|
||||
}
|
||||
}
|
||||
|
||||
// WithInsecureSkipTLSVerify skips TLS verification for repository communication
|
||||
func WithInsecureSkipTLSVerify(insecureSkipTLSVerify bool) FindChartInRepoURLOption {
|
||||
return func(options *findChartInRepoURLOptions) {
|
||||
options.InsecureSkipTLSVerify = insecureSkipTLSVerify
|
||||
}
|
||||
}
|
||||
|
||||
// FindChartInRepoURL finds chart in chart repository pointed by repoURL
|
||||
// without adding repo to repositories
|
||||
func FindChartInRepoURL(repoURL string, chartName string, getters getter.Providers, options ...FindChartInRepoURLOption) (string, error) {
|
||||
|
||||
opts := findChartInRepoURLOptions{}
|
||||
for _, option := range options {
|
||||
option(&opts)
|
||||
}
|
||||
|
||||
// Download and write the index file to a temporary location
|
||||
buf := make([]byte, 20)
|
||||
rand.Read(buf)
|
||||
name := strings.ReplaceAll(base64.StdEncoding.EncodeToString(buf), "/", "-")
|
||||
|
||||
c := Entry{
|
||||
URL: repoURL,
|
||||
Username: opts.Username,
|
||||
Password: opts.Password,
|
||||
PassCredentialsAll: opts.PassCredentialsAll,
|
||||
CertFile: opts.CertFile,
|
||||
KeyFile: opts.KeyFile,
|
||||
CAFile: opts.CAFile,
|
||||
Name: name,
|
||||
InsecureSkipTLSVerify: opts.InsecureSkipTLSVerify,
|
||||
}
|
||||
r, err := NewChartRepository(&c, getters)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
idx, err := r.DownloadIndexFile()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("looks like %q is not a valid chart repository or cannot be reached: %w", repoURL, err)
|
||||
}
|
||||
defer func() {
|
||||
os.RemoveAll(filepath.Join(r.CachePath, helmpath.CacheChartsFile(r.Config.Name)))
|
||||
os.RemoveAll(filepath.Join(r.CachePath, helmpath.CacheIndexFile(r.Config.Name)))
|
||||
}()
|
||||
|
||||
// Read the index file for the repository to get chart information and return chart URL
|
||||
repoIndex, err := LoadIndexFile(idx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
errMsg := fmt.Sprintf("chart %q", chartName)
|
||||
if opts.ChartVersion != "" {
|
||||
errMsg = fmt.Sprintf("%s version %q", errMsg, opts.ChartVersion)
|
||||
}
|
||||
cv, err := repoIndex.Get(chartName, opts.ChartVersion)
|
||||
if err != nil {
|
||||
return "", ChartNotFoundError{
|
||||
Chart: errMsg,
|
||||
RepoURL: repoURL,
|
||||
}
|
||||
}
|
||||
|
||||
if len(cv.URLs) == 0 {
|
||||
return "", fmt.Errorf("%s has no downloadable URLs", errMsg)
|
||||
}
|
||||
|
||||
chartURL := cv.URLs[0]
|
||||
|
||||
absoluteChartURL, err := ResolveReferenceURL(repoURL, chartURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to make chart URL absolute: %w", err)
|
||||
}
|
||||
|
||||
return absoluteChartURL, nil
|
||||
}
|
||||
|
||||
// ResolveReferenceURL resolves refURL relative to baseURL.
|
||||
// If refURL is absolute, it simply returns refURL.
|
||||
func ResolveReferenceURL(baseURL, refURL string) (string, error) {
|
||||
parsedRefURL, err := url.Parse(refURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse %s as URL: %w", refURL, err)
|
||||
}
|
||||
|
||||
if parsedRefURL.IsAbs() {
|
||||
return refURL, nil
|
||||
}
|
||||
|
||||
parsedBaseURL, err := url.Parse(baseURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse %s as URL: %w", baseURL, err)
|
||||
}
|
||||
|
||||
// We need a trailing slash for ResolveReference to work, but make sure there isn't already one
|
||||
parsedBaseURL.RawPath = strings.TrimSuffix(parsedBaseURL.RawPath, "/") + "/"
|
||||
parsedBaseURL.Path = strings.TrimSuffix(parsedBaseURL.Path, "/") + "/"
|
||||
|
||||
resolvedURL := parsedBaseURL.ResolveReference(parsedRefURL)
|
||||
resolvedURL.RawQuery = parsedBaseURL.RawQuery
|
||||
return resolvedURL.String(), nil
|
||||
}
|
||||
|
||||
func (e *Entry) String() string {
|
||||
buf, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
slog.Error("failed to marshal entry", slog.Any("error", err))
|
||||
panic(err)
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
Package repo implements the Helm Chart Repository.
|
||||
|
||||
A chart repository is an HTTP server that provides information on charts. A local
|
||||
repository cache is an on-disk representation of a chart repository.
|
||||
|
||||
There are two important file formats for chart repositories.
|
||||
|
||||
The first is the 'index.yaml' format, which is expressed like this:
|
||||
|
||||
apiVersion: v1
|
||||
entries:
|
||||
frobnitz:
|
||||
- created: 2016-09-29T12:14:34.830161306-06:00
|
||||
description: This is a frobnitz.
|
||||
digest: 587bd19a9bd9d2bc4a6d25ab91c8c8e7042c47b4ac246e37bf8e1e74386190f4
|
||||
home: http://example.com
|
||||
keywords:
|
||||
- frobnitz
|
||||
- sprocket
|
||||
- dodad
|
||||
maintainers:
|
||||
- email: helm@example.com
|
||||
name: The Helm Team
|
||||
- email: nobody@example.com
|
||||
name: Someone Else
|
||||
name: frobnitz
|
||||
urls:
|
||||
- http://example-charts.com/testdata/repository/frobnitz-1.2.3.tgz
|
||||
version: 1.2.3
|
||||
sprocket:
|
||||
- created: 2016-09-29T12:14:34.830507606-06:00
|
||||
description: This is a sprocket"
|
||||
digest: 8505ff813c39502cc849a38e1e4a8ac24b8e6e1dcea88f4c34ad9b7439685ae6
|
||||
home: http://example.com
|
||||
keywords:
|
||||
- frobnitz
|
||||
- sprocket
|
||||
- dodad
|
||||
maintainers:
|
||||
- email: helm@example.com
|
||||
name: The Helm Team
|
||||
- email: nobody@example.com
|
||||
name: Someone Else
|
||||
name: sprocket
|
||||
urls:
|
||||
- http://example-charts.com/testdata/repository/sprocket-1.2.0.tgz
|
||||
version: 1.2.0
|
||||
generated: 2016-09-29T12:14:34.829721375-06:00
|
||||
|
||||
An index.yaml file contains the necessary descriptive information about what
|
||||
charts are available in a repository, and how to get them.
|
||||
|
||||
The second file format is the repositories.yaml file format. This file is for
|
||||
facilitating local cached copies of one or more chart repositories.
|
||||
|
||||
The format of a repository.yaml file is:
|
||||
|
||||
apiVersion: v1
|
||||
generated: TIMESTAMP
|
||||
repositories:
|
||||
- name: stable
|
||||
url: http://example.com/charts
|
||||
cache: stable-index.yaml
|
||||
- name: incubator
|
||||
url: http://example.com/incubator
|
||||
cache: incubator-index.yaml
|
||||
|
||||
This file maps three bits of information about a repository:
|
||||
|
||||
- The name the user uses to refer to it
|
||||
- The fully qualified URL to the repository (index.yaml will be appended)
|
||||
- The name of the local cachefile
|
||||
|
||||
The format for both files was changed after Helm v2.0.0-Alpha.4. Helm is not
|
||||
backwards compatible with those earlier versions.
|
||||
*/
|
||||
package repo
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package repo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type ChartNotFoundError struct {
|
||||
RepoURL string
|
||||
Chart string
|
||||
}
|
||||
|
||||
func (e ChartNotFoundError) Error() string {
|
||||
return fmt.Sprintf("%s not found in %s repository", e.Chart, e.RepoURL)
|
||||
}
|
||||
|
||||
func (e ChartNotFoundError) Is(err error) bool {
|
||||
_, ok := err.(ChartNotFoundError)
|
||||
return ok
|
||||
}
|
||||
+419
@@ -0,0 +1,419 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package repo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
"helm.sh/helm/v4/internal/fileutil"
|
||||
"helm.sh/helm/v4/internal/urlutil"
|
||||
chart "helm.sh/helm/v4/pkg/chart/v2"
|
||||
"helm.sh/helm/v4/pkg/chart/v2/loader"
|
||||
"helm.sh/helm/v4/pkg/provenance"
|
||||
)
|
||||
|
||||
// APIVersionV1 is the v1 API version for index and repository files.
|
||||
const APIVersionV1 = "v1"
|
||||
|
||||
var (
|
||||
// ErrNoAPIVersion indicates that an API version was not specified.
|
||||
ErrNoAPIVersion = errors.New("no API version specified")
|
||||
// ErrNoChartVersion indicates that a chart with the given version is not found.
|
||||
ErrNoChartVersion = errors.New("no chart version found")
|
||||
// ErrNoChartName indicates that a chart with the given name is not found.
|
||||
ErrNoChartName = errors.New("no chart name found")
|
||||
// ErrEmptyIndexYaml indicates that the content of index.yaml is empty.
|
||||
ErrEmptyIndexYaml = errors.New("empty index.yaml file")
|
||||
)
|
||||
|
||||
// ChartVersions is a list of versioned chart references.
|
||||
// Implements a sorter on Version.
|
||||
type ChartVersions []*ChartVersion
|
||||
|
||||
// Len returns the length.
|
||||
func (c ChartVersions) Len() int { return len(c) }
|
||||
|
||||
// Swap swaps the position of two items in the versions slice.
|
||||
func (c ChartVersions) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
||||
|
||||
// Less returns true if the version of entry a is less than the version of entry b.
|
||||
func (c ChartVersions) Less(a, b int) bool {
|
||||
// Failed parse pushes to the back.
|
||||
i, err := semver.NewVersion(c[a].Version)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
j, err := semver.NewVersion(c[b].Version)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return i.LessThan(j)
|
||||
}
|
||||
|
||||
// IndexFile represents the index file in a chart repository
|
||||
type IndexFile struct {
|
||||
// This is used ONLY for validation against chartmuseum's index files and is discarded after validation.
|
||||
ServerInfo map[string]interface{} `json:"serverInfo,omitempty"`
|
||||
APIVersion string `json:"apiVersion"`
|
||||
Generated time.Time `json:"generated"`
|
||||
Entries map[string]ChartVersions `json:"entries"`
|
||||
PublicKeys []string `json:"publicKeys,omitempty"`
|
||||
|
||||
// Annotations are additional mappings uninterpreted by Helm. They are made available for
|
||||
// other applications to add information to the index file.
|
||||
Annotations map[string]string `json:"annotations,omitempty"`
|
||||
}
|
||||
|
||||
// NewIndexFile initializes an index.
|
||||
func NewIndexFile() *IndexFile {
|
||||
return &IndexFile{
|
||||
APIVersion: APIVersionV1,
|
||||
Generated: time.Now(),
|
||||
Entries: map[string]ChartVersions{},
|
||||
PublicKeys: []string{},
|
||||
}
|
||||
}
|
||||
|
||||
// LoadIndexFile takes a file at the given path and returns an IndexFile object
|
||||
func LoadIndexFile(path string) (*IndexFile, error) {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
i, err := loadIndex(b, path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading %s: %w", path, err)
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// MustAdd adds a file to the index
|
||||
// This can leave the index in an unsorted state
|
||||
func (i IndexFile) MustAdd(md *chart.Metadata, filename, baseURL, digest string) error {
|
||||
if i.Entries == nil {
|
||||
return errors.New("entries not initialized")
|
||||
}
|
||||
|
||||
if md.APIVersion == "" {
|
||||
md.APIVersion = chart.APIVersionV1
|
||||
}
|
||||
if err := md.Validate(); err != nil {
|
||||
return fmt.Errorf("validate failed for %s: %w", filename, err)
|
||||
}
|
||||
|
||||
u := filename
|
||||
if baseURL != "" {
|
||||
_, file := filepath.Split(filename)
|
||||
var err error
|
||||
u, err = urlutil.URLJoin(baseURL, file)
|
||||
if err != nil {
|
||||
u = path.Join(baseURL, file)
|
||||
}
|
||||
}
|
||||
cr := &ChartVersion{
|
||||
URLs: []string{u},
|
||||
Metadata: md,
|
||||
Digest: digest,
|
||||
Created: time.Now(),
|
||||
}
|
||||
ee := i.Entries[md.Name]
|
||||
i.Entries[md.Name] = append(ee, cr)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Add adds a file to the index and logs an error.
|
||||
//
|
||||
// Deprecated: Use index.MustAdd instead.
|
||||
func (i IndexFile) Add(md *chart.Metadata, filename, baseURL, digest string) {
|
||||
if err := i.MustAdd(md, filename, baseURL, digest); err != nil {
|
||||
slog.Error("skipping loading invalid entry for chart %q %q from %s: %s", md.Name, md.Version, filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Has returns true if the index has an entry for a chart with the given name and exact version.
|
||||
func (i IndexFile) Has(name, version string) bool {
|
||||
_, err := i.Get(name, version)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// SortEntries sorts the entries by version in descending order.
|
||||
//
|
||||
// In canonical form, the individual version records should be sorted so that
|
||||
// the most recent release for every version is in the 0th slot in the
|
||||
// Entries.ChartVersions array. That way, tooling can predict the newest
|
||||
// version without needing to parse SemVers.
|
||||
func (i IndexFile) SortEntries() {
|
||||
for _, versions := range i.Entries {
|
||||
sort.Sort(sort.Reverse(versions))
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the ChartVersion for the given name.
|
||||
//
|
||||
// If version is empty, this will return the chart with the latest stable version,
|
||||
// prerelease versions will be skipped.
|
||||
func (i IndexFile) Get(name, version string) (*ChartVersion, error) {
|
||||
vs, ok := i.Entries[name]
|
||||
if !ok {
|
||||
return nil, ErrNoChartName
|
||||
}
|
||||
if len(vs) == 0 {
|
||||
return nil, ErrNoChartVersion
|
||||
}
|
||||
|
||||
var constraint *semver.Constraints
|
||||
if version == "" {
|
||||
constraint, _ = semver.NewConstraint("*")
|
||||
} else {
|
||||
var err error
|
||||
constraint, err = semver.NewConstraint(version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// when customer inputs specific version, check whether there's an exact match first
|
||||
if len(version) != 0 {
|
||||
for _, ver := range vs {
|
||||
if version == ver.Version {
|
||||
return ver, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, ver := range vs {
|
||||
test, err := semver.NewVersion(ver.Version)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if constraint.Check(test) {
|
||||
if len(version) != 0 {
|
||||
slog.Warn("unable to find exact version requested; falling back to closest available version", "chart", name, "requested", version, "selected", ver.Version)
|
||||
}
|
||||
return ver, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("no chart version found for %s-%s", name, version)
|
||||
}
|
||||
|
||||
// WriteFile writes an index file to the given destination path.
|
||||
//
|
||||
// The mode on the file is set to 'mode'.
|
||||
func (i IndexFile) WriteFile(dest string, mode os.FileMode) error {
|
||||
b, err := yaml.Marshal(i)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fileutil.AtomicWriteFile(dest, bytes.NewReader(b), mode)
|
||||
}
|
||||
|
||||
// WriteJSONFile writes an index file in JSON format to the given destination
|
||||
// path.
|
||||
//
|
||||
// The mode on the file is set to 'mode'.
|
||||
func (i IndexFile) WriteJSONFile(dest string, mode os.FileMode) error {
|
||||
b, err := json.MarshalIndent(i, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fileutil.AtomicWriteFile(dest, bytes.NewReader(b), mode)
|
||||
}
|
||||
|
||||
// Merge merges the given index file into this index.
|
||||
//
|
||||
// This merges by name and version.
|
||||
//
|
||||
// If one of the entries in the given index does _not_ already exist, it is added.
|
||||
// In all other cases, the existing record is preserved.
|
||||
//
|
||||
// This can leave the index in an unsorted state
|
||||
func (i *IndexFile) Merge(f *IndexFile) {
|
||||
for _, cvs := range f.Entries {
|
||||
for _, cv := range cvs {
|
||||
if !i.Has(cv.Name, cv.Version) {
|
||||
e := i.Entries[cv.Name]
|
||||
i.Entries[cv.Name] = append(e, cv)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ChartVersion represents a chart entry in the IndexFile
|
||||
type ChartVersion struct {
|
||||
*chart.Metadata
|
||||
URLs []string `json:"urls"`
|
||||
Created time.Time `json:"created,omitempty"`
|
||||
Removed bool `json:"removed,omitempty"`
|
||||
Digest string `json:"digest,omitempty"`
|
||||
|
||||
// ChecksumDeprecated is deprecated in Helm 3, and therefore ignored. Helm 3 replaced
|
||||
// this with Digest. However, with a strict YAML parser enabled, a field must be
|
||||
// present on the struct for backwards compatibility.
|
||||
ChecksumDeprecated string `json:"checksum,omitempty"`
|
||||
|
||||
// EngineDeprecated is deprecated in Helm 3, and therefore ignored. However, with a strict
|
||||
// YAML parser enabled, this field must be present.
|
||||
EngineDeprecated string `json:"engine,omitempty"`
|
||||
|
||||
// TillerVersionDeprecated is deprecated in Helm 3, and therefore ignored. However, with a strict
|
||||
// YAML parser enabled, this field must be present.
|
||||
TillerVersionDeprecated string `json:"tillerVersion,omitempty"`
|
||||
|
||||
// URLDeprecated is deprecated in Helm 3, superseded by URLs. It is ignored. However,
|
||||
// with a strict YAML parser enabled, this must be present on the struct.
|
||||
URLDeprecated string `json:"url,omitempty"`
|
||||
}
|
||||
|
||||
// IndexDirectory reads a (flat) directory and generates an index.
|
||||
//
|
||||
// It indexes only charts that have been packaged (*.tgz).
|
||||
//
|
||||
// The index returned will be in an unsorted state
|
||||
func IndexDirectory(dir, baseURL string) (*IndexFile, error) {
|
||||
archives, err := filepath.Glob(filepath.Join(dir, "*.tgz"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
moreArchives, err := filepath.Glob(filepath.Join(dir, "**/*.tgz"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
archives = append(archives, moreArchives...)
|
||||
|
||||
index := NewIndexFile()
|
||||
for _, arch := range archives {
|
||||
fname, err := filepath.Rel(dir, arch)
|
||||
if err != nil {
|
||||
return index, err
|
||||
}
|
||||
|
||||
var parentDir string
|
||||
parentDir, fname = filepath.Split(fname)
|
||||
// filepath.Split appends an extra slash to the end of parentDir. We want to strip that out.
|
||||
parentDir = strings.TrimSuffix(parentDir, string(os.PathSeparator))
|
||||
parentURL, err := urlutil.URLJoin(baseURL, parentDir)
|
||||
if err != nil {
|
||||
parentURL = path.Join(baseURL, parentDir)
|
||||
}
|
||||
|
||||
c, err := loader.Load(arch)
|
||||
if err != nil {
|
||||
// Assume this is not a chart.
|
||||
continue
|
||||
}
|
||||
hash, err := provenance.DigestFile(arch)
|
||||
if err != nil {
|
||||
return index, err
|
||||
}
|
||||
if err := index.MustAdd(c.Metadata, fname, parentURL, hash); err != nil {
|
||||
return index, fmt.Errorf("failed adding to %s to index: %w", fname, err)
|
||||
}
|
||||
}
|
||||
return index, nil
|
||||
}
|
||||
|
||||
// loadIndex loads an index file and does minimal validity checking.
|
||||
//
|
||||
// The source parameter is only used for logging.
|
||||
// This will fail if API Version is not set (ErrNoAPIVersion) or if the unmarshal fails.
|
||||
func loadIndex(data []byte, source string) (*IndexFile, error) {
|
||||
i := &IndexFile{}
|
||||
|
||||
if len(data) == 0 {
|
||||
return i, ErrEmptyIndexYaml
|
||||
}
|
||||
|
||||
if err := jsonOrYamlUnmarshal(data, i); err != nil {
|
||||
return i, err
|
||||
}
|
||||
|
||||
for name, cvs := range i.Entries {
|
||||
for idx := len(cvs) - 1; idx >= 0; idx-- {
|
||||
if cvs[idx] == nil {
|
||||
slog.Warn(fmt.Sprintf("skipping loading invalid entry for chart %q from %s: empty entry", name, source))
|
||||
cvs = append(cvs[:idx], cvs[idx+1:]...)
|
||||
continue
|
||||
}
|
||||
// When metadata section missing, initialize with no data
|
||||
if cvs[idx].Metadata == nil {
|
||||
cvs[idx].Metadata = &chart.Metadata{}
|
||||
}
|
||||
if cvs[idx].APIVersion == "" {
|
||||
cvs[idx].APIVersion = chart.APIVersionV1
|
||||
}
|
||||
if err := cvs[idx].Validate(); ignoreSkippableChartValidationError(err) != nil {
|
||||
slog.Warn(fmt.Sprintf("skipping loading invalid entry for chart %q %q from %s: %s", name, cvs[idx].Version, source, err))
|
||||
cvs = append(cvs[:idx], cvs[idx+1:]...)
|
||||
}
|
||||
}
|
||||
// adjust slice to only contain a set of valid versions
|
||||
i.Entries[name] = cvs
|
||||
}
|
||||
i.SortEntries()
|
||||
if i.APIVersion == "" {
|
||||
return i, ErrNoAPIVersion
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
// jsonOrYamlUnmarshal unmarshals the given byte slice containing JSON or YAML
|
||||
// into the provided interface.
|
||||
//
|
||||
// It automatically detects whether the data is in JSON or YAML format by
|
||||
// checking its validity as JSON. If the data is valid JSON, it will use the
|
||||
// `encoding/json` package to unmarshal it. Otherwise, it will use the
|
||||
// `sigs.k8s.io/yaml` package to unmarshal the YAML data.
|
||||
func jsonOrYamlUnmarshal(b []byte, i interface{}) error {
|
||||
if json.Valid(b) {
|
||||
return json.Unmarshal(b, i)
|
||||
}
|
||||
return yaml.UnmarshalStrict(b, i)
|
||||
}
|
||||
|
||||
// ignoreSkippableChartValidationError inspect the given error and returns nil if
|
||||
// the error isn't important for index loading
|
||||
//
|
||||
// In particular, charts may introduce validations that don't impact repository indexes
|
||||
// And repository indexes may be generated by older/non-compliant software, which doesn't
|
||||
// conform to all validations.
|
||||
func ignoreSkippableChartValidationError(err error) error {
|
||||
verr, ok := err.(chart.ValidationError)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
// https://github.com/helm/helm/issues/12748 (JFrog repository strips alias field)
|
||||
if strings.HasPrefix(verr.Error(), "validation: more than one dependency with name or alias") {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package repo // import "helm.sh/helm/v4/pkg/repo/v1"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// File represents the repositories.yaml file
|
||||
type File struct {
|
||||
APIVersion string `json:"apiVersion"`
|
||||
Generated time.Time `json:"generated"`
|
||||
Repositories []*Entry `json:"repositories"`
|
||||
}
|
||||
|
||||
// NewFile generates an empty repositories file.
|
||||
//
|
||||
// Generated and APIVersion are automatically set.
|
||||
func NewFile() *File {
|
||||
return &File{
|
||||
APIVersion: APIVersionV1,
|
||||
Generated: time.Now(),
|
||||
Repositories: []*Entry{},
|
||||
}
|
||||
}
|
||||
|
||||
// LoadFile takes a file at the given path and returns a File object
|
||||
func LoadFile(path string) (*File, error) {
|
||||
r := new(File)
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return r, fmt.Errorf("couldn't load repositories file (%s): %w", path, err)
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(b, r)
|
||||
return r, err
|
||||
}
|
||||
|
||||
// Add adds one or more repo entries to a repo file.
|
||||
func (r *File) Add(re ...*Entry) {
|
||||
r.Repositories = append(r.Repositories, re...)
|
||||
}
|
||||
|
||||
// Update attempts to replace one or more repo entries in a repo file. If an
|
||||
// entry with the same name doesn't exist in the repo file it will add it.
|
||||
func (r *File) Update(re ...*Entry) {
|
||||
for _, target := range re {
|
||||
r.update(target)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *File) update(e *Entry) {
|
||||
for j, repo := range r.Repositories {
|
||||
if repo.Name == e.Name {
|
||||
r.Repositories[j] = e
|
||||
return
|
||||
}
|
||||
}
|
||||
r.Add(e)
|
||||
}
|
||||
|
||||
// Has returns true if the given name is already a repository name.
|
||||
func (r *File) Has(name string) bool {
|
||||
entry := r.Get(name)
|
||||
return entry != nil
|
||||
}
|
||||
|
||||
// Get returns an entry with the given name if it exists, otherwise returns nil
|
||||
func (r *File) Get(name string) *Entry {
|
||||
for _, entry := range r.Repositories {
|
||||
if entry.Name == name {
|
||||
return entry
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove removes the entry from the list of repositories.
|
||||
func (r *File) Remove(name string) bool {
|
||||
cp := []*Entry{}
|
||||
found := false
|
||||
for _, rf := range r.Repositories {
|
||||
if rf == nil {
|
||||
continue
|
||||
}
|
||||
if rf.Name == name {
|
||||
found = true
|
||||
continue
|
||||
}
|
||||
cp = append(cp, rf)
|
||||
}
|
||||
r.Repositories = cp
|
||||
return found
|
||||
}
|
||||
|
||||
// WriteFile writes a repositories file to the given path.
|
||||
func (r *File) WriteFile(path string, perm os.FileMode) error {
|
||||
data, err := yaml.Marshal(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, perm)
|
||||
}
|
||||
Reference in New Issue
Block a user