Skip to content

Commit

Permalink
fix: check charts in all previous helmRepos #673
Browse files Browse the repository at this point in the history
  • Loading branch information
luisdavim authored Jun 6, 2022
2 parents 14c4154 + 4874936 commit 63bdf11
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 22 deletions.
3 changes: 0 additions & 3 deletions examples/composition/artifactory.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ namespaces:
max:
memory: "300Mi"

helmRepos:
jfrog: "https://charts.jfrog.io"

apps:
artifactory:
namespace: "production"
Expand Down
23 changes: 23 additions & 0 deletions examples/composition/kyverno.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions examples/composition/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ metadata:
settings:
kubeContext: "minikube"
globalMaxHistory: 5

helmRepos:
argo: "https://argoproj.github.io/argo-helm"
jfrog: "https://charts.jfrog.io"
1 change: 1 addition & 0 deletions examples/composition/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ stateFiles:
- path: main.yaml
- path: argo.yaml
- path: artifactory.yaml
- path: kyverno.yaml
8 changes: 2 additions & 6 deletions internal/app/spec_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
13 changes: 10 additions & 3 deletions internal/app/spec_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -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},
Expand Down
30 changes: 24 additions & 6 deletions internal/app/state_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions internal/app/state_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,18 @@ 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"},
}
err = s.build(files)
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))
}
}

0 comments on commit 63bdf11

Please sign in to comment.