1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package helm
import (
"archive/tar"
"compress/gzip"
"io"
"strings"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/validation"
"github.com/hashicorp/go-version"
"gopkg.in/yaml.v3"
)
var (
// ErrMissingChartFile indicates a missing Chart.yaml file
ErrMissingChartFile = util.NewInvalidArgumentErrorf("Chart.yaml file is missing")
// ErrInvalidName indicates an invalid package name
ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid")
// ErrInvalidVersion indicates an invalid package version
ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid")
// ErrInvalidChart indicates an invalid chart
ErrInvalidChart = util.NewInvalidArgumentErrorf("chart is invalid")
)
// Metadata for a Chart file. This models the structure of a Chart.yaml file.
type Metadata struct {
APIVersion string `json:"api_version" yaml:"apiVersion"`
Type string `json:"type,omitempty" yaml:"type,omitempty"`
Name string `json:"name" yaml:"name"`
Version string `json:"version" yaml:"version"`
AppVersion string `json:"app_version,omitempty" yaml:"appVersion,omitempty"`
Home string `json:"home,omitempty" yaml:"home,omitempty"`
Sources []string `json:"sources,omitempty" yaml:"sources,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Keywords []string `json:"keywords,omitempty" yaml:"keywords,omitempty"`
Maintainers []*Maintainer `json:"maintainers,omitempty" yaml:"maintainers,omitempty"`
Icon string `json:"icon,omitempty" yaml:"icon,omitempty"`
Condition string `json:"condition,omitempty" yaml:"condition,omitempty"`
Tags string `json:"tags,omitempty" yaml:"tags,omitempty"`
Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
KubeVersion string `json:"kube_version,omitempty" yaml:"kubeVersion,omitempty"`
Dependencies []*Dependency `json:"dependencies,omitempty" yaml:"dependencies,omitempty"`
}
type Maintainer struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Email string `json:"email,omitempty" yaml:"email,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
}
type Dependency struct {
Name string `json:"name" yaml:"name"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Repository string `json:"repository" yaml:"repository"`
Condition string `json:"condition,omitempty" yaml:"condition,omitempty"`
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
Enabled bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
ImportValues []any `json:"import_values,omitempty" yaml:"import-values,omitempty"`
Alias string `json:"alias,omitempty" yaml:"alias,omitempty"`
}
// ParseChartArchive parses the metadata of a Helm archive
func ParseChartArchive(r io.Reader) (*Metadata, error) {
gzr, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
hd, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if hd.Typeflag != tar.TypeReg {
continue
}
if hd.FileInfo().Name() == "Chart.yaml" {
if strings.Count(hd.Name, "/") != 1 {
continue
}
return ParseChartFile(tr)
}
}
return nil, ErrMissingChartFile
}
// ParseChartFile parses a Chart.yaml file to retrieve the metadata of a Helm chart
func ParseChartFile(r io.Reader) (*Metadata, error) {
var metadata *Metadata
if err := yaml.NewDecoder(r).Decode(&metadata); err != nil {
return nil, err
}
if metadata.APIVersion == "" {
return nil, ErrInvalidChart
}
if metadata.Type != "" && metadata.Type != "application" && metadata.Type != "library" {
return nil, ErrInvalidChart
}
if metadata.Name == "" {
return nil, ErrInvalidName
}
if _, err := version.NewSemver(metadata.Version); err != nil {
return nil, ErrInvalidVersion
}
if !validation.IsValidURL(metadata.Home) {
metadata.Home = ""
}
return metadata, nil
}
|