Skip to content

Commit 7321579

Browse files
niladrihgjenkins8
authored andcommittedDec 27, 2024·
Add annotations and dependencies to get metadata output
The output of helm get metadata includes a subset of the fields contained in the chart.Metadata struct. This change adds the values of the annotations field and the dependencies field to the output. Signed-off-by: Niladri Halder <niladri.halder26@gmail.com>

File tree

7 files changed

+84
-26
lines changed

7 files changed

+84
-26
lines changed
 

‎cmd/helm/get_metadata.go

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"log"
2323

2424
"github.com/spf13/cobra"
25+
k8sLabels "k8s.io/apimachinery/pkg/labels"
2526

2627
"helm.sh/helm/v3/cmd/helm/require"
2728
"helm.sh/helm/v3/pkg/action"
@@ -78,10 +79,13 @@ func (w metadataWriter) WriteTable(out io.Writer) error {
7879
_, _ = fmt.Fprintf(out, "CHART: %v\n", w.metadata.Chart)
7980
_, _ = fmt.Fprintf(out, "VERSION: %v\n", w.metadata.Version)
8081
_, _ = fmt.Fprintf(out, "APP_VERSION: %v\n", w.metadata.AppVersion)
82+
_, _ = fmt.Fprintf(out, "ANNOTATIONS: %v\n", k8sLabels.Set(w.metadata.Annotations).String())
83+
_, _ = fmt.Fprintf(out, "DEPENDENCIES: %v\n", w.metadata.FormattedDepNames())
8184
_, _ = fmt.Fprintf(out, "NAMESPACE: %v\n", w.metadata.Namespace)
8285
_, _ = fmt.Fprintf(out, "REVISION: %v\n", w.metadata.Revision)
8386
_, _ = fmt.Fprintf(out, "STATUS: %v\n", w.metadata.Status)
8487
_, _ = fmt.Fprintf(out, "DEPLOYED_AT: %v\n", w.metadata.DeployedAt)
88+
8589
return nil
8690
}
8791

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"thomas-guide","chart":"foo","version":"0.1.0-beta.1","appVersion":"1.0","namespace":"default","revision":1,"status":"deployed","deployedAt":"1977-09-02T22:04:05Z"}
1+
{"name":"thomas-guide","chart":"foo","version":"0.1.0-beta.1","appVersion":"1.0","annotations":{"category":"web-apps","supported":"true"},"dependencies":[{"name":"cool-plugin","version":"1.0.0","repository":"https://coolplugin.io/charts","condition":"coolPlugin.enabled","enabled":true},{"name":"crds","version":"2.7.1","repository":"","condition":"crds.enabled"}],"namespace":"default","revision":1,"status":"deployed","deployedAt":"1977-09-02T22:04:05Z"}

‎cmd/helm/testdata/output/get-metadata.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ NAME: thomas-guide
22
CHART: foo
33
VERSION: 0.1.0-beta.1
44
APP_VERSION: 1.0
5+
ANNOTATIONS: category=web-apps,supported=true
6+
DEPENDENCIES: cool-plugin,crds
57
NAMESPACE: default
68
REVISION: 1
79
STATUS: deployed

‎cmd/helm/testdata/output/get-metadata.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
annotations:
2+
category: web-apps
3+
supported: "true"
14
appVersion: "1.0"
25
chart: foo
6+
dependencies:
7+
- condition: coolPlugin.enabled
8+
enabled: true
9+
name: cool-plugin
10+
repository: https://coolplugin.io/charts
11+
version: 1.0.0
12+
- condition: crds.enabled
13+
name: crds
14+
repository: ""
15+
version: 2.7.1
316
deployedAt: "1977-09-02T22:04:05Z"
417
name: thomas-guide
518
namespace: default

‎pkg/action/get_metadata.go

+38-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ limitations under the License.
1616

1717
package action
1818

19-
import "time"
19+
import (
20+
"sort"
21+
"strings"
22+
"time"
23+
24+
"helm.sh/helm/v3/pkg/chart"
25+
)
2026

2127
// GetMetadata is the action for checking a given release's metadata.
2228
//
@@ -28,14 +34,16 @@ type GetMetadata struct {
2834
}
2935

3036
type Metadata struct {
31-
Name string `json:"name" yaml:"name"`
32-
Chart string `json:"chart" yaml:"chart"`
33-
Version string `json:"version" yaml:"version"`
34-
AppVersion string `json:"appVersion" yaml:"appVersion"`
35-
Namespace string `json:"namespace" yaml:"namespace"`
36-
Revision int `json:"revision" yaml:"revision"`
37-
Status string `json:"status" yaml:"status"`
38-
DeployedAt string `json:"deployedAt" yaml:"deployedAt"`
37+
Name string `json:"name" yaml:"name"`
38+
Chart string `json:"chart" yaml:"chart"`
39+
Version string `json:"version" yaml:"version"`
40+
AppVersion string `json:"appVersion" yaml:"appVersion"`
41+
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
42+
Dependencies []*chart.Dependency `json:"dependencies,omitempty" yaml:"dependencies,omitempty"`
43+
Namespace string `json:"namespace" yaml:"namespace"`
44+
Revision int `json:"revision" yaml:"revision"`
45+
Status string `json:"status" yaml:"status"`
46+
DeployedAt string `json:"deployedAt" yaml:"deployedAt"`
3947
}
4048

4149
// NewGetMetadata creates a new GetMetadata object with the given configuration.
@@ -57,13 +65,26 @@ func (g *GetMetadata) Run(name string) (*Metadata, error) {
5765
}
5866

5967
return &Metadata{
60-
Name: rel.Name,
61-
Chart: rel.Chart.Metadata.Name,
62-
Version: rel.Chart.Metadata.Version,
63-
AppVersion: rel.Chart.Metadata.AppVersion,
64-
Namespace: rel.Namespace,
65-
Revision: rel.Version,
66-
Status: rel.Info.Status.String(),
67-
DeployedAt: rel.Info.LastDeployed.Format(time.RFC3339),
68+
Name: rel.Name,
69+
Chart: rel.Chart.Metadata.Name,
70+
Version: rel.Chart.Metadata.Version,
71+
AppVersion: rel.Chart.Metadata.AppVersion,
72+
Dependencies: rel.Chart.Metadata.Dependencies,
73+
Annotations: rel.Chart.Metadata.Annotations,
74+
Namespace: rel.Namespace,
75+
Revision: rel.Version,
76+
Status: rel.Info.Status.String(),
77+
DeployedAt: rel.Info.LastDeployed.Format(time.RFC3339),
6878
}, nil
6979
}
80+
81+
// FormattedDepNames formats metadata.dependencies names into a comma-separated list.
82+
func (m *Metadata) FormattedDepNames() string {
83+
depsNames := make([]string, 0, len(m.Dependencies))
84+
for _, dep := range m.Dependencies {
85+
depsNames = append(depsNames, dep.Name)
86+
}
87+
sort.StringSlice(depsNames).Sort()
88+
89+
return strings.Join(depsNames, ",")
90+
}

‎pkg/chart/dependency.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,28 @@ type Dependency struct {
2525
// Name is the name of the dependency.
2626
//
2727
// This must mach the name in the dependency's Chart.yaml.
28-
Name string `json:"name"`
28+
Name string `json:"name" yaml:"name"`
2929
// Version is the version (range) of this chart.
3030
//
3131
// A lock file will always produce a single version, while a dependency
3232
// may contain a semantic version range.
33-
Version string `json:"version,omitempty"`
33+
Version string `json:"version,omitempty" yaml:"version,omitempty"`
3434
// The URL to the repository.
3535
//
3636
// Appending `index.yaml` to this string should result in a URL that can be
3737
// used to fetch the repository index.
38-
Repository string `json:"repository"`
38+
Repository string `json:"repository" yaml:"repository"`
3939
// A yaml path that resolves to a boolean, used for enabling/disabling charts (e.g. subchart1.enabled )
40-
Condition string `json:"condition,omitempty"`
40+
Condition string `json:"condition,omitempty" yaml:"condition,omitempty"`
4141
// Tags can be used to group charts for enabling/disabling together
42-
Tags []string `json:"tags,omitempty"`
42+
Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"`
4343
// Enabled bool determines if chart should be loaded
44-
Enabled bool `json:"enabled,omitempty"`
44+
Enabled bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
4545
// ImportValues holds the mapping of source values to parent key to be imported. Each item can be a
4646
// string or pair of child/parent sublist items.
47-
ImportValues []interface{} `json:"import-values,omitempty"`
47+
ImportValues []interface{} `json:"import-values,omitempty" yaml:"import-values,omitempty"`
4848
// Alias usable alias to be used for the chart
49-
Alias string `json:"alias,omitempty"`
49+
Alias string `json:"alias,omitempty" yaml:"alias,omitempty"`
5050
}
5151

5252
// Validate checks for common problems with the dependency datastructure in

‎pkg/release/mock.go

+18
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ func Mock(opts *MockReleaseOptions) *Release {
7474
Name: "foo",
7575
Version: "0.1.0-beta.1",
7676
AppVersion: "1.0",
77+
Annotations: map[string]string{
78+
"category": "web-apps",
79+
"supported": "true",
80+
},
81+
Dependencies: []*chart.Dependency{
82+
{
83+
Name: "cool-plugin",
84+
Version: "1.0.0",
85+
Repository: "https://coolplugin.io/charts",
86+
Condition: "coolPlugin.enabled",
87+
Enabled: true,
88+
},
89+
{
90+
Name: "crds",
91+
Version: "2.7.1",
92+
Condition: "crds.enabled",
93+
},
94+
},
7795
},
7896
Templates: []*chart.File{
7997
{Name: "templates/foo.tpl", Data: []byte(MockManifest)},

0 commit comments

Comments
 (0)
Please sign in to comment.