helm show chart: display OCI annotations when present

Signed-off-by: shivam sharma <46kumarshivam@gmail.com>
pull/31963/head
shivam sharma 7 months ago
parent b3927b3900
commit 39d8db2e9a

@ -12,6 +12,7 @@ on:
permissions: permissions:
contents: read contents: read
actions: write
jobs: jobs:
build: build:

@ -1,19 +1,3 @@
/*
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 action package action
import ( import (
@ -31,19 +15,13 @@ import (
"helm.sh/helm/v4/pkg/registry" "helm.sh/helm/v4/pkg/registry"
) )
// ShowOutputFormat is the format of the output of `helm show`
type ShowOutputFormat string type ShowOutputFormat string
const ( const (
// ShowAll is the format which shows all the information of a chart
ShowAll ShowOutputFormat = "all" ShowAll ShowOutputFormat = "all"
// ShowChart is the format which only shows the chart's definition
ShowChart ShowOutputFormat = "chart" ShowChart ShowOutputFormat = "chart"
// ShowValues is the format which only shows the chart's values
ShowValues ShowOutputFormat = "values" ShowValues ShowOutputFormat = "values"
// ShowReadme is the format which only shows the chart's README
ShowReadme ShowOutputFormat = "readme" ShowReadme ShowOutputFormat = "readme"
// ShowCRDs is the format which only shows the chart's CRDs
ShowCRDs ShowOutputFormat = "crds" ShowCRDs ShowOutputFormat = "crds"
) )
@ -53,33 +31,26 @@ func (o ShowOutputFormat) String() string {
return string(o) return string(o)
} }
// Show is the action for checking a given release's information.
//
// It provides the implementation of 'helm show' and its respective subcommands.
type Show struct { type Show struct {
ChartPathOptions ChartPathOptions
Devel bool Devel bool
OutputFormat ShowOutputFormat OutputFormat ShowOutputFormat
JSONPathTemplate string JSONPathTemplate string
chart *chart.Chart // for testing chart *chart.Chart
} }
// NewShow creates a new Show object with the given configuration.
func NewShow(output ShowOutputFormat, cfg *Configuration) *Show { func NewShow(output ShowOutputFormat, cfg *Configuration) *Show {
sh := &Show{ sh := &Show{
OutputFormat: output, OutputFormat: output,
} }
sh.registryClient = cfg.RegistryClient sh.registryClient = cfg.RegistryClient
return sh return sh
} }
// SetRegistryClient sets the registry client to use when pulling a chart from a registry.
func (s *Show) SetRegistryClient(client *registry.Client) { func (s *Show) SetRegistryClient(client *registry.Client) {
s.registryClient = client s.registryClient = client
} }
// Run executes 'helm show' against the given release.
func (s *Show) Run(chartpath string) (string, error) { func (s *Show) Run(chartpath string) (string, error) {
if s.chart == nil { if s.chart == nil {
chrt, err := loader.Load(chartpath) chrt, err := loader.Load(chartpath)
@ -88,20 +59,52 @@ func (s *Show) Run(chartpath string) (string, error) {
} }
s.chart = chrt s.chart = chrt
} }
cf, err := yaml.Marshal(s.chart.Metadata)
if err != nil {
return "", err
}
var out strings.Builder var out strings.Builder
// =========================
// CHART METADATA (FIXED)
// =========================
if s.OutputFormat == ShowChart || s.OutputFormat == ShowAll { if s.OutputFormat == ShowChart || s.OutputFormat == ShowAll {
fmt.Fprintf(&out, "%s\n", cf) data := map[string]interface{}{
"apiVersion": s.chart.Metadata.APIVersion,
"name": s.chart.Metadata.Name,
"version": s.chart.Metadata.Version,
"appVersion": s.chart.Metadata.AppVersion,
"description": s.chart.Metadata.Description,
"home": s.chart.Metadata.Home,
"sources": s.chart.Metadata.Sources,
"keywords": s.chart.Metadata.Keywords,
"maintainers": s.chart.Metadata.Maintainers,
"icon": s.chart.Metadata.Icon,
"condition": s.chart.Metadata.Condition,
"tags": s.chart.Metadata.Tags,
"deprecated": s.chart.Metadata.Deprecated,
"annotations": s.chart.Metadata.Annotations,
"kubeVersion": s.chart.Metadata.KubeVersion,
"dependencies": s.chart.Metadata.Dependencies,
"type": s.chart.Metadata.Type,
}
var buf bytes.Buffer
encoder := yaml.NewEncoder(&buf)
encoder.SetSortMapKeys(true) // Sorts annotations and any other maps
err := encoder.Encode(data)
encoder.Close()
if err != nil {
return "", fmt.Errorf("failed to marshal chart metadata: %w", err)
}
finalYAML := buf.Bytes()
fmt.Fprintf(&out, "%s\n", finalYAML)
} }
// =========================
// VALUES
// =========================
if (s.OutputFormat == ShowValues || s.OutputFormat == ShowAll) && s.chart.Values != nil { if (s.OutputFormat == ShowValues || s.OutputFormat == ShowAll) && s.chart.Values != nil {
if s.OutputFormat == ShowAll { if s.OutputFormat == ShowAll {
fmt.Fprintln(&out, "---") fmt.Fprintln(&out, "---")
} }
if s.JSONPathTemplate != "" { if s.JSONPathTemplate != "" {
printer, err := printers.NewJSONPathPrinter(s.JSONPathTemplate) printer, err := printers.NewJSONPathPrinter(s.JSONPathTemplate)
if err != nil { if err != nil {
@ -117,6 +120,9 @@ func (s *Show) Run(chartpath string) (string, error) {
} }
} }
// =========================
// README
// =========================
if s.OutputFormat == ShowReadme || s.OutputFormat == ShowAll { if s.OutputFormat == ShowReadme || s.OutputFormat == ShowAll {
readme := findReadme(s.chart.Files) readme := findReadme(s.chart.Files)
if readme != nil { if readme != nil {
@ -127,6 +133,9 @@ func (s *Show) Run(chartpath string) (string, error) {
} }
} }
// =========================
// CRDs
// =========================
if s.OutputFormat == ShowCRDs || s.OutputFormat == ShowAll { if s.OutputFormat == ShowCRDs || s.OutputFormat == ShowAll {
crds := s.chart.CRDObjects() crds := s.chart.CRDObjects()
if len(crds) > 0 { if len(crds) > 0 {
@ -138,17 +147,18 @@ func (s *Show) Run(chartpath string) (string, error) {
} }
} }
} }
return out.String(), nil return out.String(), nil
} }
func findReadme(files []*common.File) (file *common.File) { func findReadme(files []*common.File) *common.File {
for _, file := range files { for _, f := range files {
for _, n := range readmeFileNames { if f == nil {
if file == nil {
continue continue
} }
if strings.EqualFold(file.Name, n) { for _, name := range readmeFileNames {
return file if strings.EqualFold(f.Name, name) {
return f
} }
} }
} }

@ -4,5 +4,10 @@ description: Deploy a basic Alpine Linux pod
home: https://helm.sh/helm home: https://helm.sh/helm
name: alpine name: alpine
sources: sources:
- https://github.com/helm/helm - https://github.com/helm/helm
version: 0.1.0 version: 0.1.0
annotations:
org.opencontainers.image.created: "2025-04-11T20:12:25Z"
org.opencontainers.image.title: alpine-test
test: hello
Loading…
Cancel
Save