Skip to content

Commit

Permalink
fix local helm releases deletion issue (#3243)
Browse files Browse the repository at this point in the history
Read local chart name from Chart.yaml

update the chart name check

lint
  • Loading branch information
mohamed-rafraf authored Jun 19, 2024
1 parent d0deec6 commit 190dd5a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
8 changes: 8 additions & 0 deletions pkg/apis/kubeone/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"k8c.io/kubeone/pkg/addons"
kubeoneapi "k8c.io/kubeone/pkg/apis/kubeone"
helm "k8c.io/kubeone/pkg/localhelm"
"k8c.io/kubeone/pkg/semverutil"

"k8s.io/apimachinery/pkg/util/validation"
Expand Down Expand Up @@ -697,6 +698,13 @@ func ValidateHelmReleases(helmReleases []kubeoneapi.HelmRelease, fldPath *field.
allErrs = append(allErrs, field.Required(fldPath.Child("namespace"), hr.Namespace))
}

if hr.RepoURL == "" {
_, err := helm.GetChartNameFromChartYAML(hr.Chart)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("chart"), hr.Chart, fmt.Sprintf("invalid local chart: %v", err)))
}
}

for idx, helmValues := range hr.Values {
fldIdentity := fldPath.Child("values").Index(idx)

Expand Down
32 changes: 29 additions & 3 deletions pkg/localhelm/helm3.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"os"
"path"
"sort"

"github.com/google/go-cmp/cmp"
Expand All @@ -37,6 +38,7 @@ import (
"helm.sh/helm/v3/pkg/registry"
helmrelease "helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/storage/driver"
"sigs.k8s.io/yaml"

kubeoneapi "k8c.io/kubeone/pkg/apis/kubeone"
"k8c.io/kubeone/pkg/fail"
Expand Down Expand Up @@ -169,11 +171,16 @@ func Deploy(st *state.State) error {
func releasesFilterFn(helmReleases []kubeoneapi.HelmRelease, logger logrus.FieldLogger) func(rel *helmrelease.Release) bool {
return func(rel *helmrelease.Release) bool {
for _, hr := range helmReleases {
if rel.Name == hr.ReleaseName && rel.Namespace == hr.Namespace && rel.Chart.Name() == hr.Chart {
return false
if rel.Name == hr.ReleaseName && rel.Namespace == hr.Namespace {
chartName := hr.Chart
if hr.RepoURL == "" {
chartName, _ = GetChartNameFromChartYAML(chartName)
}
if chartName == rel.Chart.Name() {
return false
}
}
}

_, found := rel.Labels[releasedByKubeone]
if found {
logger.Infof("queue %s/%s v%d helm release to uninstall", rel.Namespace, rel.Name, rel.Version)
Expand All @@ -183,6 +190,25 @@ func releasesFilterFn(helmReleases []kubeoneapi.HelmRelease, logger logrus.Field
}
}

// Function to extract chart name from Chart.yaml in case of local charts
func GetChartNameFromChartYAML(chartPath string) (string, error) {
chartYAMLPath := path.Join(chartPath, "Chart.yaml")
yamlFile, err := os.ReadFile(chartYAMLPath)
if err != nil {
return "", fmt.Errorf("failed to read Chart.yaml: %w", err)
}

var chartMetadata struct {
Name string `yaml:"name"`
}
err = yaml.Unmarshal(yamlFile, &chartMetadata)
if err != nil {
return "", fmt.Errorf("failed to unmarshal Chart.yaml: %w", err)
}

return chartMetadata.Name, nil
}

func newHelmSettings(verbose bool) *helmcli.EnvSettings {
helmSettings := helmcli.New()
helmSettings.Debug = verbose
Expand Down

0 comments on commit 190dd5a

Please sign in to comment.