From 9a8357d710d487c3d9b83967922c380601ab9da3 Mon Sep 17 00:00:00 2001 From: Luis Davim Date: Sun, 5 Jun 2022 16:51:43 +0100 Subject: [PATCH 1/2] fix: check charts in all previous helmRepos Signed-off-by: Luis Davim --- examples/composition/argo.yaml | 3 --- examples/composition/artifactory.yaml | 3 --- examples/composition/kyverno.yaml | 23 ++++++++++++++++++++ examples/composition/main.yaml | 4 ++++ examples/composition/spec.yaml | 1 + internal/app/state_files.go | 30 +++++++++++++++++++++------ internal/app/state_files_test.go | 9 ++++---- 7 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 examples/composition/kyverno.yaml diff --git a/examples/composition/argo.yaml b/examples/composition/argo.yaml index c1de1997..5bd96e07 100644 --- a/examples/composition/argo.yaml +++ b/examples/composition/argo.yaml @@ -13,9 +13,6 @@ namespaces: - name: "requests.nvidia.com/gpu" value: "2" -helmRepos: - argo: "https://argoproj.github.io/argo-helm" - apps: argo: namespace: "staging" diff --git a/examples/composition/artifactory.yaml b/examples/composition/artifactory.yaml index 610f7156..5cdde836 100644 --- a/examples/composition/artifactory.yaml +++ b/examples/composition/artifactory.yaml @@ -13,9 +13,6 @@ namespaces: max: memory: "300Mi" -helmRepos: - jfrog: "https://charts.jfrog.io" - apps: artifactory: namespace: "production" diff --git a/examples/composition/kyverno.yaml b/examples/composition/kyverno.yaml new file mode 100644 index 00000000..406ccd90 --- /dev/null +++ b/examples/composition/kyverno.yaml @@ -0,0 +1,23 @@ +helmRepos: + kyverno: https://kyverno.github.io/kyverno/ + +namespaces: + kyverno: + protected: false + +apps: + kyverno: + namespace: kyverno + enabled: true + chart: kyverno/kyverno + version: 2.4.1 + kyverno-policies: + namespace: kyverno + enabled: true + chart: kyverno/kyverno-policies + version: 2.4.0 + kyverno-reporter: + namespace: kyverno + enabled: true + chart: kyverno/kyverno-reporter + version: 2.9.0 diff --git a/examples/composition/main.yaml b/examples/composition/main.yaml index 7de34155..b0f7fb2a 100644 --- a/examples/composition/main.yaml +++ b/examples/composition/main.yaml @@ -8,3 +8,7 @@ metadata: settings: kubeContext: "minikube" globalMaxHistory: 5 + +helmRepos: + argo: "https://argoproj.github.io/argo-helm" + jfrog: "https://charts.jfrog.io" diff --git a/examples/composition/spec.yaml b/examples/composition/spec.yaml index c894bb85..ea419699 100644 --- a/examples/composition/spec.yaml +++ b/examples/composition/spec.yaml @@ -3,3 +3,4 @@ stateFiles: - path: main.yaml - path: argo.yaml - path: artifactory.yaml + - path: kyverno.yaml diff --git a/internal/app/state_files.go b/internal/app/state_files.go index d501248e..97da8117 100644 --- a/internal/app/state_files.go +++ b/internal/app/state_files.go @@ -55,7 +55,6 @@ func (s *state) fromTOML(file string) error { if _, err := toml.Decode(tomlFile, s); err != nil { return err } - s.expand(file) return nil } @@ -108,7 +107,6 @@ func (s *state) fromYAML(file string) error { if err = yaml.UnmarshalStrict([]byte(yamlFile), s); err != nil { return err } - s.expand(file) return nil } @@ -147,6 +145,20 @@ func (s *state) build(files fileOptionArray) error { } log.Infof("Parsed [[ %s ]] successfully and found [ %d ] apps", f.name, len(fileState.Apps)) + + // Add all known repos to the fileState + fileState.PreconfiguredHelmRepos = append(fileState.PreconfiguredHelmRepos, s.PreconfiguredHelmRepos...) + for n, r := range s.HelmRepos { + if fileState.HelmRepos == nil { + fileState.HelmRepos = s.HelmRepos + break + } + if _, ok := fileState.HelmRepos[n]; !ok { + fileState.HelmRepos[n] = r + } + } + fileState.expand(f.name) + // Merge Apps that already existed in the state for appName, app := range fileState.Apps { if _, ok := s.Apps[appName]; ok { @@ -190,11 +202,8 @@ func (s *state) expand(relativeToFile string) { var download bool // support env vars in path r.Chart = os.Expand(r.Chart, getEnv) - repoName := strings.Split(r.Chart, "/")[0] - _, isRepo := s.HelmRepos[repoName] - isRepo = isRepo || stringInSlice(repoName, s.PreconfiguredHelmRepos) // if there is no repo for the chart, we assume it's intended to be a local path or url - if !isRepo { + if !s.isChartFromRepo(r.Chart) { // unless explicitly requested by the user, we don't need to download if the protocol is natively supported by helm download = flags.downloadCharts || !isSupportedProtocol(r.Chart, validProtocols) } @@ -233,6 +242,15 @@ func (s *state) expand(relativeToFile string) { } } +// isChartFromRepo checks if the chart is from a known repo +func (s *state) isChartFromRepo(chart string) bool { + repoName := strings.Split(chart, "/")[0] + if _, isRepo := s.HelmRepos[repoName]; isRepo { + return true + } + return stringInSlice(repoName, s.PreconfiguredHelmRepos) +} + // cleanup deletes the k8s certificates and keys files // It also deletes any Tiller TLS certs and keys // and secret files diff --git a/internal/app/state_files_test.go b/internal/app/state_files_test.go index 99f54b02..610d9d48 100644 --- a/internal/app/state_files_test.go +++ b/internal/app/state_files_test.go @@ -363,6 +363,7 @@ func Test_build(t *testing.T) { s := new(state) files := fileOptionArray{ fileOption{name: "../../examples/composition/main.yaml"}, + fileOption{name: "../../examples/composition/kyverno.yaml"}, fileOption{name: "../../examples/composition/argo.yaml"}, fileOption{name: "../../examples/composition/artifactory.yaml"}, } @@ -370,10 +371,10 @@ func Test_build(t *testing.T) { if err != nil { t.Errorf("build() - unexpected error: %v", err) } - if len(s.Apps) != 2 { - t.Errorf("build() - unexpected number of apps, wanted 2 got %d", len(s.Apps)) + if len(s.Apps) != 5 { + t.Errorf("build() - unexpected number of apps, wanted 5 got %d", len(s.Apps)) } - if len(s.HelmRepos) != 2 { - t.Errorf("build() - unexpected number of repos, wanted 2 got %d", len(s.Apps)) + if len(s.HelmRepos) != 3 { + t.Errorf("build() - unexpected number of repos, wanted 3 got %d", len(s.Apps)) } } From 48749368c04151fca215c3ce77655c2b9b3b7449 Mon Sep 17 00:00:00 2001 From: Luis Davim Date: Sun, 5 Jun 2022 18:53:31 +0100 Subject: [PATCH 2/2] refactor: linting Signed-off-by: Luis Davim --- examples/composition/argo.yaml | 3 +++ internal/app/spec_state.go | 8 ++------ internal/app/spec_state_test.go | 13 ++++++++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/examples/composition/argo.yaml b/examples/composition/argo.yaml index 5bd96e07..c1de1997 100644 --- a/examples/composition/argo.yaml +++ b/examples/composition/argo.yaml @@ -13,6 +13,9 @@ namespaces: - name: "requests.nvidia.com/gpu" value: "2" +helmRepos: + argo: "https://argoproj.github.io/argo-helm" + apps: argo: namespace: "staging" diff --git a/internal/app/spec_state.go b/internal/app/spec_state.go index 3a7b5144..5927a7d5 100644 --- a/internal/app/spec_state.go +++ b/internal/app/spec_state.go @@ -14,7 +14,7 @@ type StateFiles struct { StateFiles []StatePath `yaml:"stateFiles"` } -// fromYAML reads a yaml file and decodes it to a state type. +// specFromYAML reads a yaml file and decodes it to a state type. // parser which throws an error if the YAML file is not valid. func (pc *StateFiles) specFromYAML(file string) error { rawYamlFile, err := ioutil.ReadFile(file) @@ -25,9 +25,5 @@ func (pc *StateFiles) specFromYAML(file string) error { yamlFile := string(rawYamlFile) - if err = yaml.UnmarshalStrict([]byte(yamlFile), pc); err != nil { - return err - } - - return nil + return yaml.UnmarshalStrict([]byte(yamlFile), pc) } diff --git a/internal/app/spec_state_test.go b/internal/app/spec_state_test.go index 80fa3686..ac514fa3 100644 --- a/internal/app/spec_state_test.go +++ b/internal/app/spec_state_test.go @@ -28,6 +28,13 @@ func Test_specFromYAML(t *testing.T) { s: new(StateFiles), }, want: false, + }, { + name: "test case 3 -- Commposition example", + args: args{ + file: "../../examples/composition/spec.yaml", + s: new(StateFiles), + }, + want: true, }, } @@ -66,9 +73,9 @@ func Test_specFileSort(t *testing.T) { args: args{ files: fileOptionArray( []fileOption{ - fileOption{"third.yaml", 0}, - fileOption{"first.yaml", -20}, - fileOption{"second.yaml", -10}, + {"third.yaml", 0}, + {"first.yaml", -20}, + {"second.yaml", -10}, }), }, want: [3]int{-20, -10, 0},