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 Helm chart validation bug #360

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions pkg/image/validation/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ func validateHelm(k8s *image.Kubernetes, imageConfigDir string) []FailedValidati
return failures
}

var helmRepositoryNames []string
for _, repo := range k8s.Helm.Repositories {
helmRepositoryNames = append(helmRepositoryNames, repo.Name)
}

if failure := validateHelmChartDuplicates(k8s.Helm.Charts); failure != "" {
failures = append(failures, FailedValidation{
UserMessage: failure,
Expand All @@ -170,7 +175,7 @@ func validateHelm(k8s *image.Kubernetes, imageConfigDir string) []FailedValidati
seenHelmRepos := make(map[string]bool)
for _, chart := range k8s.Helm.Charts {
c := chart
failures = append(failures, validateChart(&c, imageConfigDir)...)
failures = append(failures, validateChart(&c, helmRepositoryNames, imageConfigDir)...)

seenHelmRepos[chart.RepositoryName] = true
}
Expand All @@ -183,7 +188,7 @@ func validateHelm(k8s *image.Kubernetes, imageConfigDir string) []FailedValidati
return failures
}

func validateChart(chart *image.HelmChart, imageConfigDir string) []FailedValidation {
func validateChart(chart *image.HelmChart, repositoryNames []string, imageConfigDir string) []FailedValidation {
var failures []FailedValidation

if chart.Name == "" {
Expand All @@ -196,6 +201,10 @@ func validateChart(chart *image.HelmChart, imageConfigDir string) []FailedValida
failures = append(failures, FailedValidation{
UserMessage: fmt.Sprintf("Helm chart 'repositoryName' field for %q must be defined.", chart.Name),
})
} else if !slices.Contains(repositoryNames, chart.RepositoryName) {
failures = append(failures, FailedValidation{
UserMessage: fmt.Sprintf("Helm chart 'repositoryName' %q for Helm chart %q does not match the name of any defined repository.", chart.RepositoryName, chart.Name),
})
}

if chart.Version == "" {
Expand Down
30 changes: 29 additions & 1 deletion pkg/image/validation/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func TestValidateKubernetes(t *testing.T) {
"Entries in 'urls' must begin with either 'http://' or 'https://'.",
"Helm chart 'name' field must be defined.",
"Helm repository 'name' field for \"apache-repo\" must match the 'repositoryName' field in at least one defined Helm chart.",
"Helm chart 'repositoryName' \"another-apache-repo\" for Helm chart \"\" does not match the name of any defined repository.",
},
},
}
Expand Down Expand Up @@ -481,7 +482,7 @@ func TestValidateHelmCharts(t *testing.T) {
"Helm chart 'name' field must be defined.",
},
},
`helm chart no repository name`: {
`helm chart undefined repository name`: {
K8s: image.Kubernetes{
Helm: image.Helm{
Charts: []image.HelmChart{
Expand All @@ -508,6 +509,33 @@ func TestValidateHelmCharts(t *testing.T) {
"Helm chart 'repositoryName' field for \"metallb\" must be defined.",
},
},
`helm chart no matching repository name`: {
K8s: image.Kubernetes{
Helm: image.Helm{
Charts: []image.HelmChart{
{
Name: "kubevirt",
RepositoryName: "suse-edge",
Version: "0.2.2",
},
{
Name: "metallb",
RepositoryName: "this-is-not-suse-edge",
Version: "0.14.3",
},
},
Repositories: []image.HelmRepository{
{
Name: "suse-edge",
URL: "https://suse-edge.github.io/charts",
},
},
},
},
ExpectedFailedMessages: []string{
"Helm chart 'repositoryName' \"this-is-not-suse-edge\" for Helm chart \"metallb\" does not match the name of any defined repository.",
},
},
`helm chart no version`: {
K8s: image.Kubernetes{
Helm: image.Helm{
Expand Down
Loading