diff --git a/src/internal/packager/helm/chart.go b/src/internal/packager/helm/chart.go index b145953173..3f0fc1ac8d 100644 --- a/src/internal/packager/helm/chart.go +++ b/src/internal/packager/helm/chart.go @@ -9,22 +9,24 @@ import ( "fmt" "time" - "github.com/defenseunicorns/pkg/helpers" - "github.com/Masterminds/semver/v3" - "github.com/defenseunicorns/zarf/src/config" - "github.com/defenseunicorns/zarf/src/types" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "sigs.k8s.io/yaml" - - "github.com/defenseunicorns/zarf/src/pkg/message" + plutoversionsfile "github.com/fairwindsops/pluto/v5" + plutoapi "github.com/fairwindsops/pluto/v5/pkg/api" + goyaml "github.com/goccy/go-yaml" "helm.sh/helm/v3/pkg/action" - "helm.sh/helm/v3/pkg/chartutil" - "helm.sh/helm/v3/pkg/releaseutil" - "helm.sh/helm/v3/pkg/chart" + "helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/release" + "helm.sh/helm/v3/pkg/releaseutil" "helm.sh/helm/v3/pkg/storage/driver" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "sigs.k8s.io/yaml" + + "github.com/defenseunicorns/pkg/helpers" + + "github.com/defenseunicorns/zarf/src/config" + "github.com/defenseunicorns/zarf/src/pkg/message" + "github.com/defenseunicorns/zarf/src/types" ) // InstallOrUpgradeChart performs a helm install of the given chart. @@ -398,7 +400,7 @@ func (h *Helm) migrateDeprecatedAPIs(latestRelease *release.Release) error { return fmt.Errorf("failed to unmarshal manifest: %#v", err) } - rawData, manifestModified, _ := h.cluster.HandleDeprecations(rawData, *kubeGitVersion) + rawData, manifestModified, _ := handleDeprecations(rawData, *kubeGitVersion) manifestContent, err := yaml.Marshal(rawData) if err != nil { return fmt.Errorf("failed to marshal raw manifest after deprecation check: %#v", err) @@ -437,3 +439,30 @@ func (h *Helm) migrateDeprecatedAPIs(latestRelease *release.Release) error { return nil } + +// handleDeprecations takes in an unstructured object and the k8s version and returns the latest version of the object and if it was modified. +func handleDeprecations(rawData *unstructured.Unstructured, kubernetesVersion semver.Version) (*unstructured.Unstructured, bool, error) { + deprecatedVersionContent := &plutoapi.VersionFile{} + err := goyaml.Unmarshal(plutoversionsfile.Content(), deprecatedVersionContent) + if err != nil { + return rawData, false, err + } + for _, deprecation := range deprecatedVersionContent.DeprecatedVersions { + if deprecation.Component == "k8s" && deprecation.Kind == rawData.GetKind() && deprecation.Name == rawData.GetAPIVersion() { + removedVersion, err := semver.NewVersion(deprecation.RemovedIn) + if err != nil { + return rawData, false, err + } + + if removedVersion.LessThan(&kubernetesVersion) { + if deprecation.ReplacementAPI != "" { + rawData.SetAPIVersion(deprecation.ReplacementAPI) + return rawData, true, nil + } + + return nil, true, nil + } + } + } + return rawData, false, nil +} diff --git a/src/pkg/k8s/deprecations.go b/src/pkg/k8s/deprecations.go deleted file mode 100644 index ea43aa3c03..0000000000 --- a/src/pkg/k8s/deprecations.go +++ /dev/null @@ -1,44 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2021-Present The Zarf Authors - -// Package k8s provides a client for interacting with a Kubernetes cluster. -package k8s - -import ( - "github.com/Masterminds/semver/v3" - plutoversionsfile "github.com/fairwindsops/pluto/v5" - "github.com/fairwindsops/pluto/v5/pkg/api" - goyaml "github.com/goccy/go-yaml" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" -) - -const k8sDeprecationComponent = "k8s" - -// HandleDeprecations takes in an unstructured object and the version of kubernetes in a cluster and returns a converted version of that object and whether it was modified (if applicable) -func (k *K8s) HandleDeprecations(rawData *unstructured.Unstructured, kubernetesVersion semver.Version) (*unstructured.Unstructured, bool, error) { - deprecatedVersionContent := &api.VersionFile{} - err := goyaml.Unmarshal(plutoversionsfile.Content(), deprecatedVersionContent) - if err != nil { - return rawData, false, err - } - - for _, deprecation := range deprecatedVersionContent.DeprecatedVersions { - if deprecation.Component == k8sDeprecationComponent && deprecation.Kind == rawData.GetKind() && deprecation.Name == rawData.GetAPIVersion() { - removedVersion, err := semver.NewVersion(deprecation.RemovedIn) - if err != nil { - return rawData, false, err - } - - if removedVersion.LessThan(&kubernetesVersion) { - if deprecation.ReplacementAPI != "" { - rawData.SetAPIVersion(deprecation.ReplacementAPI) - return rawData, true, nil - } - - return nil, true, nil - } - } - } - - return rawData, false, nil -}