github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/k8s/deprecations.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package k8s provides a client for interacting with a Kubernetes cluster.
     5  package k8s
     6  
     7  import (
     8  	"github.com/Masterminds/semver/v3"
     9  	plutoversionsfile "github.com/fairwindsops/pluto/v5"
    10  	"github.com/fairwindsops/pluto/v5/pkg/api"
    11  	goyaml "github.com/goccy/go-yaml"
    12  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    13  )
    14  
    15  const k8sDeprecationComponent = "k8s"
    16  
    17  // 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)
    18  func (k *K8s) HandleDeprecations(rawData *unstructured.Unstructured, kubernetesVersion semver.Version) (*unstructured.Unstructured, bool, error) {
    19  	deprecatedVersionContent := &api.VersionFile{}
    20  	err := goyaml.Unmarshal(plutoversionsfile.Content(), deprecatedVersionContent)
    21  	if err != nil {
    22  		return rawData, false, err
    23  	}
    24  
    25  	for _, deprecation := range deprecatedVersionContent.DeprecatedVersions {
    26  		if deprecation.Component == k8sDeprecationComponent && deprecation.Kind == rawData.GetKind() && deprecation.Name == rawData.GetAPIVersion() {
    27  			removedVersion, err := semver.NewVersion(deprecation.RemovedIn)
    28  			if err != nil {
    29  				return rawData, false, err
    30  			}
    31  
    32  			if removedVersion.LessThan(&kubernetesVersion) {
    33  				if deprecation.ReplacementAPI != "" {
    34  					rawData.SetAPIVersion(deprecation.ReplacementAPI)
    35  					return rawData, true, nil
    36  				}
    37  
    38  				return nil, true, nil
    39  			}
    40  		}
    41  	}
    42  
    43  	return rawData, false, nil
    44  }