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

fix: sub chart loading #55

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 15 additions & 12 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"path/filepath"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -31,25 +30,21 @@ $ helm datarobot generate chart.tgz
Args: cobra.MinimumNArgs(1), // Requires at least one argument (file path)
RunE: func(cmd *cobra.Command, args []string) error {
chartPath := args[0]
manifest, err := render_helper.NewRenderItems(chartPath)
manifest, err := render_helper.RenderChart(chartPath, g.ValueFiles, g.Values)
if err != nil {
return fmt.Errorf("Error loading chart %s: %v", chartPath, err)
}

uniqueEntries := make(map[string]string)
for fileName, template := range manifest {
// // We only apply the following lint rules to yaml files
if filepath.Ext(fileName) != ".yaml" || filepath.Ext(fileName) == ".yml" {
continue
}
for _, template := range strings.Split(manifest, "\n---\n") {

if generateDebug {
fmt.Printf("---\n# Source: %s\n%s\n", fileName, template)
if g.Debug {
fmt.Printf("---\n%s\n", template)
}

manifestImages, err := ExtractImagesFromManifest(template)
if err != nil {
return fmt.Errorf("Error ExtractImagesFromManifest chart %s: %v", fileName, err)
return fmt.Errorf("Error ExtractImagesFromManifest chart: %v", err)
}

re := regexp.MustCompile("[^a-zA-Z0-9]+")
Expand Down Expand Up @@ -105,10 +100,18 @@ $ helm datarobot generate chart.tgz
},
}

var generateDebug bool
type generateInput struct {
Values []string
ValueFiles []string
Debug bool
}

var g generateInput

func init() {
rootCmd.AddCommand(generateCmd)
generateCmd.Flags().StringVarP(&annotation, "annotation", "a", "datarobot.com/images", "annotation to lookup")
generateCmd.Flags().BoolVarP(&generateDebug, "debug", "d", false, "debug")
generateCmd.Flags().BoolVarP(&g.Debug, "debug", "d", false, "debug")
generateCmd.Flags().StringSliceVarP(&g.ValueFiles, "values", "f", []string{}, "specify values in a YAML file or a URL (can specify multiple)")
generateCmd.Flags().StringArrayVar(&g.Values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
}
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func SetVersionInfo(version, commit string) {
// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"sigs.k8s.io/yaml"
)

// isImageAllowed checks if the image is in the allowed list
func isImageAllowed(image string, imageDoc []dr_chartutil.DatarobotImageDeclaration) bool {
// isImageDeclared checks if the image is in the declared imagedoc list
func isImageDeclared(image string, imageDoc []dr_chartutil.DatarobotImageDeclaration) bool {
for _, im := range imageDoc {
if strings.TrimSpace(image) == strings.TrimSpace(im.Image) {
return true
Expand Down
41 changes: 24 additions & 17 deletions cmd/validate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"encoding/json"
"fmt"
"path/filepath"
"sort"
"strings"

Expand All @@ -27,7 +27,7 @@ $ helm datarobot validate chart.tgz
Args: cobra.MinimumNArgs(1), // Requires at least one argument (file path)
RunE: func(cmd *cobra.Command, args []string) error {
chartPath := args[0]
manifest, err := render_helper.NewRenderItems(chartPath)
manifest, err := render_helper.RenderChart(chartPath, v.ValueFiles, v.Values)
if err != nil {
return fmt.Errorf("Error loading chart %s: %v", chartPath, err)
}
Expand All @@ -36,32 +36,30 @@ $ helm datarobot validate chart.tgz
if err != nil {
return fmt.Errorf("Error ExtractImagesFromCharts: %v", err)
}
if validateDebug {
if v.Debug {
fmt.Printf("---\n# annotation: %s\n", annotation)
fmt.Printf("---\n# imageDoc: %s\n", imageDoc)
b, err := json.MarshalIndent(imageDoc, "", " ")
if err == nil {
fmt.Println(string(b))
}
}

if len(imageDoc) == 0 {
return fmt.Errorf("imageDoc is empty")
}
var errorImageAllowed []string
for fileName, template := range manifest {
// We only apply the following lint rules to yaml files
if filepath.Ext(fileName) != ".yaml" || filepath.Ext(fileName) == ".yml" {
continue
}

if validateDebug {
fmt.Printf("---\n# Source: %s\n%s\n", fileName, template)
for _, template := range strings.Split(manifest, "\n---\n") {
if v.Debug {
fmt.Printf("---\n%s\n", template)
}

manifestImages, err := ExtractImagesFromManifest(template)
if err != nil {
return fmt.Errorf("Error ExtractImagesFromManifest chart %s: %v", fileName, err)
return fmt.Errorf("Error ExtractImagesFromManifest chart: %v", err)
}
// Validate manifestImages against the imageDoc
for _, image := range manifestImages {
if !isImageAllowed(image, imageDoc) {
if !isImageDeclared(image, imageDoc) {
if !SliceHas(errorImageAllowed, image) {
errorImageAllowed = append(errorImageAllowed, image)
}
Expand All @@ -72,7 +70,7 @@ $ helm datarobot validate chart.tgz

if len(errorImageAllowed) > 0 {
sort.Strings(errorImageAllowed)
return fmt.Errorf("Images not declared as ImageDoc: %v", errorImageAllowed)
return fmt.Errorf("Images not declared as ImageDoc:\n%s", strings.Join(errorImageAllowed, "\n"))
} else {
cmd.Print("Image Doc Valid")
}
Expand All @@ -81,10 +79,19 @@ $ helm datarobot validate chart.tgz
},
}

var validateDebug bool
type validateInput struct {
Values []string
ValueFiles []string
Debug bool
}

var v validateInput

func init() {
rootCmd.AddCommand(validateCmd)
validateCmd.Flags().StringVarP(&annotation, "annotation", "a", "datarobot.com/images", "annotation to lookup")
validateCmd.Flags().BoolVarP(&validateDebug, "debug", "d", false, "debug")
validateCmd.Flags().BoolVarP(&v.Debug, "debug", "d", false, "debug")
validateCmd.Flags().StringSliceVarP(&v.ValueFiles, "values", "f", []string{}, "specify values in a YAML file or a URL (can specify multiple)")
validateCmd.Flags().StringArrayVar(&v.Values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")

}
8 changes: 7 additions & 1 deletion cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestCommandValidate(t *testing.T) {
t.Run("test-chart5/error", func(t *testing.T) {
output, err := executeCommand(rootCmd, "validate ../testdata/test-chart5 -a \"custom/images-wrong\"")
assert.Error(t, err)
expectedOutput := `Error: Images not declared as ImageDoc: [busybox:1.36.1 docker.io/alpine/curl:8.9.1]`
expectedOutput := "Error: Images not declared as ImageDoc:\nbusybox:1.36.1\ndocker.io/alpine/curl:8.9.1"
assert.Equal(t, expectedOutput, output)
})
t.Run("test-chart5/empty", func(t *testing.T) {
Expand All @@ -31,5 +31,11 @@ func TestCommandValidate(t *testing.T) {
expectedOutput := `Error: imageDoc is empty`
assert.Equal(t, expectedOutput, output)
})
t.Run("test-chart3/override", func(t *testing.T) {
output, err := executeCommand(rootCmd, "validate ../testdata/test-chart3 -a custom.com/images-override --set image.repository=docker.io/testoverride/test-image3")
assert.NoError(t, err)
expectedOutput := `Image Doc Valid`
assert.Equal(t, expectedOutput, output)
})

}
2 changes: 2 additions & 0 deletions docs/helm-datarobot_generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ helm-datarobot generate [flags]
-a, --annotation string annotation to lookup (default "datarobot.com/images")
-d, --debug debug
-h, --help help for generate
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
-f, --values strings specify values in a YAML file or a URL (can specify multiple)
```

### SEE ALSO
Expand Down
2 changes: 2 additions & 0 deletions docs/helm-datarobot_validate.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ helm-datarobot validate [flags]
-a, --annotation string annotation to lookup (default "datarobot.com/images")
-d, --debug debug
-h, --help help for validate
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
-f, --values strings specify values in a YAML file or a URL (can specify multiple)
```

### SEE ALSO
Expand Down
72 changes: 72 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.0

require (
github.com/google/go-containerregistry v0.20.2
github.com/imdario/mergo v0.3.16
github.com/mattn/go-shellwords v1.0.12
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.10.0
Expand All @@ -15,46 +16,100 @@ require (

require (
dario.cat/mergo v1.0.1 // indirect
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/semver/v3 v3.3.0 // indirect
github.com/Masterminds/sprig/v3 v3.3.0 // indirect
github.com/Masterminds/squirrel v1.5.4 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/containerd/containerd v1.7.23 // indirect
github.com/containerd/errdefs v0.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/cyphar/filepath-securejoin v0.3.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/cli v27.1.1+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v25.0.6+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gosuri/uitable v0.0.4 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmoiron/sqlx v1.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/spdystream v0.4.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rubenv/sql-migrate v1.7.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
Expand All @@ -65,6 +120,12 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
Expand All @@ -73,15 +134,26 @@ require (
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.31.3 // indirect
k8s.io/apimachinery v0.31.4 // indirect
k8s.io/apiserver v0.31.3 // indirect
k8s.io/cli-runtime v0.31.3 // indirect
k8s.io/client-go v0.31.3 // indirect
k8s.io/component-base v0.31.3 // indirect
k8s.io/helm v2.17.0+incompatible // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/kubectl v0.31.3 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
oras.land/oras-go v1.2.5 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.17.2 // indirect
sigs.k8s.io/kustomize/kyaml v0.17.1 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
Loading
Loading