Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Skip test images by default #25

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ func registerFlags(cmd *cobra.Command, images *pkg.Images) {
"it may reference a valid range (e.g. ^2.0.0). If this is not specified, the latest version is used",
)
cmd.MarkFlagsMutuallyExclusive("from-release", "chart-version")
cmd.Flags().
BoolVar(&images.IncludeTestImages, "include-test-images", false, "include images required for Helm tests")
}
1 change: 1 addition & 0 deletions docs/doc/list-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ list-images CHART|RELEASE [flags]
--from-release enable the flag to fetch the images from release instead (disabled by default)
-h, --help help for list-images
--image-regex string regex used to split helm template rendered (default "---\\n# Source:\\s.*.")
--include-test-images include images required for Helm tests
-j, --json enable the flag to display images retrieved in json format (disabled by default)
-k, --kind strings kubernetes app kind to fetch the images from (default [Deployment,StatefulSet,DaemonSet,CronJob,Job,ReplicaSet,Pod,Alertmanager,Prometheus,ThanosRuler,Grafana,Thanos,Receiver])
--kube-version string Kubernetes version used for Capabilities.KubeVersion when rendering charts (default "1.26.0")
Expand Down
29 changes: 18 additions & 11 deletions pkg/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/postrender"
"helm.sh/helm/v3/pkg/release"

imgErrors "github.com/d2iq-labs/helm-list-images/pkg/errors"
"github.com/d2iq-labs/helm-list-images/pkg/k8s"
Expand All @@ -50,6 +51,7 @@ type Images struct {
LogLevel string
FromRelease bool
UniqueImages bool
IncludeTestImages bool
KubeVersion string
ChartVersionConstraint string
PostRenderer postrender.PostRenderer
Expand All @@ -62,8 +64,8 @@ type Images struct {
writer *bufio.Writer
}

func (image *Images) SetRelease(release string) {
image.release = release
func (image *Images) SetRelease(rel string) {
image.release = rel
}

func (image *Images) SetChart(chart string) {
Expand Down Expand Up @@ -253,9 +255,7 @@ func (image *Images) getChartManifests() ([]byte, error) {

func (image *Images) getChartTemplate() ([]byte, error) {
settings := cli.New()
if strings.ToLower(image.LogLevel) == logrus.DebugLevel.String() {
settings.Debug = true
}
settings.Debug = strings.ToLower(image.LogLevel) == logrus.DebugLevel.String()

actionConfig := new(action.Configuration)

Expand All @@ -267,7 +267,6 @@ func (image *Images) getChartTemplate() ([]byte, error) {
)
if err != nil {
image.log.Error("oops initialising helm client errored with", err)

return nil, err
}

Expand Down Expand Up @@ -423,9 +422,7 @@ spec:
templateClient.KubeVersion = parsedKubeVersion
}

p := getter.All(settings)

vals, err := valueOpts.MergeValues(p)
vals, err := valueOpts.MergeValues(getter.All(settings))
if err != nil {
return nil, fmt.Errorf("failed to merge values: %w", err)
}
Expand All @@ -440,14 +437,24 @@ spec:
fmt.Fprintln(&manifests, strings.TrimSpace(templateOutput.Manifest))

for _, h := range templateOutput.Hooks {
if h != nil {
fmt.Fprintf(&manifests, "---\n# Source: %s\n%s\n", h.Path, h.Manifest)
if !image.IncludeTestImages && isTestHook(h) {
continue
}
fmt.Fprintf(&manifests, "---\n# Source: %s\n%s\n", h.Path, h.Manifest)
}

return manifests.Bytes(), nil
}

func isTestHook(h *release.Hook) bool {
for _, e := range h.Events {
if e == release.HookTest {
return true
}
}
return false
}

func (image *Images) GetTemplates(template []byte) []string {
image.log.Debugf("splitting helm manifests with regex pattern: '%s'", image.ImageRegex)
temp := regexp.MustCompile(image.ImageRegex)
Expand Down