Skip to content

Commit 29833fd

Browse files
committed
Add feature for parsing raw kubernetes manifest rather helm chart or relase
Below command would fetch images from raw kubernetes manifests: helm template example/chart/sample | helm images get --raw - -o yaml fixes #44
1 parent ff665fe commit 29833fd

File tree

9 files changed

+53
-9
lines changed

9 files changed

+53
-9
lines changed

cmd/commands.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"io"
89
"os"
910
"strings"
1011

@@ -38,19 +39,33 @@ func getImagesCommand() *cobra.Command {
3839
helm images get prometheus-standalone --from-release --registry quay.io --unique
3940
helm images get prometheus-standalone --from-release --registry quay.io -o yaml
4041
helm images get oci://registry-1.docker.io/bitnamicharts/airflow -o yaml
41-
helm images get kong-2.35.0.tgz -o json`,
42+
helm images get kong-2.35.0.tgz -o json
43+
helm template example/chart/sample | helm images get --raw -
44+
helm template example/chart/sample | helm images get --raw - -o yaml`,
4245
Args: validateAndSetArgs,
4346
PreRunE: setCLIClient,
4447
RunE: func(cmd *cobra.Command, _ []string) error {
4548
cmd.SilenceUsage = true
4649

50+
if images.Raw {
51+
stdIn := cmd.InOrStdin()
52+
imagesRaw, err := io.ReadAll(stdIn)
53+
if err != nil {
54+
return err
55+
}
56+
57+
images.SetRaw(imagesRaw)
58+
}
59+
4760
return images.GetImages()
4861
},
4962
}
5063

5164
registerCommonFlags(imageCommand)
5265
registerGetFlags(imageCommand)
5366

67+
imageCommand.MarkFlagsMutuallyExclusive("raw", "from-release")
68+
5469
return imageCommand
5570
}
5671

@@ -149,6 +164,10 @@ func validateAndSetArgs(cmd *cobra.Command, args []string) error {
149164
defaultReleaseName := "sample"
150165
cmd.SilenceUsage = true
151166

167+
if images.Raw {
168+
return nil
169+
}
170+
152171
if images.Revision != 0 && !images.FromRelease {
153172
cliLogger.Fatalf("the '--revision' flag can only be used when retrieving images from a release, i.e., when the '--from-release' flag is set")
154173
}
@@ -174,6 +193,10 @@ func validateAndSetArgs(cmd *cobra.Command, args []string) error {
174193
cliLogger.Fatal(oneOfThemError)
175194
}
176195

196+
if len(args) == 0 {
197+
cliLogger.Fatal("[RELEASE] name missing")
198+
}
199+
177200
images.SetRelease(args[0])
178201

179202
return nil

cmd/flags.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func registerCommonFlags(cmd *cobra.Command) {
5858
func registerGetFlags(cmd *cobra.Command) {
5959
cmd.PersistentFlags().BoolVarP(&images.FromRelease, "from-release", "", false,
6060
"enable the flag to fetch the images from release instead (disabled by default)")
61+
cmd.PersistentFlags().BoolVarP(&images.Raw, "raw", "", false,
62+
"when enabled, expects raw kubernetes manifests rather helm release or chart")
6163
}
6264

6365
func registerGetAllFlags(cmd *cobra.Command) {

docs/doc/images.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ images [command] [flags]
3232
* [images get](images_get.md) - Fetches all images those are part of specified chart/release
3333
* [images version](images_version.md) - Command to fetch the version of helm-images installed
3434

35-
###### Auto generated by spf13/cobra on 6-Aug-2024
35+
###### Auto generated by spf13/cobra on 12-Oct-2024

docs/doc/images_all.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Fetches all images from all release
77
Lists all images part of all release present in the cluster with matching pattern or part of specified registry.
88

99
```
10-
images all [RELEASE] [CHART] [flags]
10+
images all [flags]
1111
```
1212

1313
### Examples
@@ -55,4 +55,4 @@ images all [RELEASE] [CHART] [flags]
5555

5656
* [images](images.md) - Utility that helps in fetching images which are part of deployment
5757

58-
###### Auto generated by spf13/cobra on 6-Aug-2024
58+
###### Auto generated by spf13/cobra on 12-Oct-2024

docs/doc/images_get.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ images get [RELEASE] [CHART] [flags]
1919
helm images get prometheus-standalone --from-release --registry quay.io -o yaml
2020
helm images get oci://registry-1.docker.io/bitnamicharts/airflow -o yaml
2121
helm images get kong-2.35.0.tgz -o json
22+
helm template example/chart/sample | helm images get --raw -
23+
helm template example/chart/sample | helm images get --raw - -o yaml
2224
```
2325

2426
### Options
@@ -32,6 +34,7 @@ images get [RELEASE] [CHART] [flags]
3234
-l, --log-level string log level for the plugin helm images (defaults to info) (default "info")
3335
--no-color when enabled does not color encode the output
3436
-o, --output string the format to which the output should be rendered to, it should be one of yaml|json|table|csv, if nothing specified it sets to default
37+
--raw when enabled, expects raw kubernetes manifests rather helm release or chart
3538
-r, --registry strings registry name (docker images belonging to this registry)
3639
--skip strings list of resources to skip from identifying images, ex: ConfigMap=sample-configmap | configmap=sample-configmap
3740
-u, --unique enable the flag if duplicates to be removed from the retrieved list (disabled by default also overrides --kind)
@@ -56,4 +59,4 @@ images get [RELEASE] [CHART] [flags]
5659

5760
* [images](images.md) - Utility that helps in fetching images which are part of deployment
5861

59-
###### Auto generated by spf13/cobra on 6-Aug-2024
62+
###### Auto generated by spf13/cobra on 12-Oct-2024

docs/doc/images_version.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ images version [flags]
3535

3636
* [images](images.md) - Utility that helps in fetching images which are part of deployment
3737

38-
###### Auto generated by spf13/cobra on 6-Aug-2024
38+
###### Auto generated by spf13/cobra on 12-Oct-2024

pkg/images.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Images struct {
4040
LogLevel string `json:"log_level,omitempty" yaml:"log_level,omitempty"`
4141
OutputFormat string `json:"output_format,omitempty" yaml:"output_format,omitempty"`
4242
Revision int `json:"revision,omitempty" yaml:"revision,omitempty"`
43+
Raw bool `json:"raw,omitempty" yaml:"raw,omitempty"`
4344
SkipTests bool `json:"skip_tests,omitempty" yaml:"skip_tests,omitempty"`
4445
SkipCRDS bool `json:"skip_crds,omitempty" yaml:"skip_crds,omitempty"`
4546
FromRelease bool `json:"from_release,omitempty" yaml:"from_release,omitempty"`
@@ -53,6 +54,7 @@ type Images struct {
5354
table bool
5455
csv bool
5556
all bool
57+
raw []byte
5658
release string
5759
chart string
5860
namespace string
@@ -85,6 +87,11 @@ func (image *Images) SetChart(chart string) {
8587
image.chart = chart
8688
}
8789

90+
// SetRaw sets raw.
91+
func (image *Images) SetRaw(raw []byte) {
92+
image.raw = raw
93+
}
94+
8895
// SetRenderer sets renderer to Images.
8996
func (image *Images) SetRenderer() {
9097
render := renderer.GetRenderer(os.Stdout, image.log, image.NoColor, image.yaml, image.json, image.csv, image.table)
@@ -183,6 +190,12 @@ func (image *Images) GetImages() error {
183190
}
184191

185192
func (image *Images) getChartManifests() ([]byte, error) {
193+
if image.Raw {
194+
image.log.Debug("reading the manifest from stdin")
195+
196+
return image.raw, nil
197+
}
198+
186199
if image.FromRelease {
187200
image.log.Debugf("from-release is selected, hence fetching manifests for '%s' from helm release", image.release)
188201

pkg/k8s/k8s.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func (kin *Kind) Get(dataMap string, log *logrus.Logger) (string, error) {
123123
kind, kindExists := kindYaml[kubeKind].(string)
124124
if !kindExists {
125125
log.Warn("failed to get 'kind' from the manifest")
126+
126127
return "", nil
127128
}
128129

pkg/k8s/k8s_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55
"testing"
66

77
"github.com/ghodss/yaml"
8+
"github.com/nikhilsbhat/helm-images/pkg"
89
"github.com/nikhilsbhat/helm-images/pkg/k8s"
10+
"github.com/sirupsen/logrus"
911
"github.com/stretchr/testify/assert"
1012
"github.com/stretchr/testify/require"
1113
)
1214

1315
func TestGetVal(t *testing.T) {
16+
log := logrus.New()
1417
yamlData := `image: 'ghcr.io/example/sample:v2.2.0'
1518
enemies: aliens
1619
lives: '3'
@@ -35,7 +38,7 @@ config:
3538
err := yaml.Unmarshal([]byte(yamlData), &valueMap)
3639
require.NoError(t, err)
3740

38-
valueFound, _ := k8s.GetImage(valueMap, "image", "", nil)
41+
valueFound, _ := k8s.GetImage(valueMap, "image", pkg.ConfigMapImageRegex, log)
3942
assert.ElementsMatch(t, []string{
4043
"ghcr.io/example/config:v2.3.0",
4144
"ghcr.io/example/testConfig:v2.3.0",
@@ -49,9 +52,8 @@ config:
4952
err := json.Unmarshal([]byte(jsonData), &valueMap)
5053
require.NoError(t, err)
5154

52-
valueFound, _ := k8s.GetImage(valueMap, "image", "", nil)
55+
valueFound, _ := k8s.GetImage(valueMap, "image", pkg.ConfigMapImageRegex, log)
5356
assert.ElementsMatch(t, []string{
54-
"ghcr.io/prometheus/prom:v2.0.0",
5557
"ghcr.io/example/sample:v2.2.0",
5658
"ghcr.io/example/config:v2.3.0",
5759
}, valueFound)

0 commit comments

Comments
 (0)