From 9e4c6ccea2748570d5028926f8743d68a5d7817f Mon Sep 17 00:00:00 2001 From: Matthias Goerens Date: Tue, 6 Feb 2024 17:26:22 +0100 Subject: [PATCH] Use get-ocp-range to translate Kube to OCP ranges This work had been started as part of commit f08a985e53d90671c6c99d88149390ef3a1c3bfc This commit removes the mapping of Kubernetes versions to OCP versions and calls get-ocp-range instead. closes #418 Signed-off-by: Matthias Goerens --- internal/chartverifier/api/verifier.go | 1 - internal/chartverifier/checks/charttesting.go | 12 ++- internal/chartverifier/checks/helm.go | 3 +- internal/tool/kubeOpenShiftVersionMap.yaml | 29 ------ internal/tool/kubectl.go | 58 +----------- internal/tool/kubectl_test.go | 89 ------------------- 6 files changed, 12 insertions(+), 180 deletions(-) delete mode 100644 internal/tool/kubeOpenShiftVersionMap.yaml diff --git a/internal/chartverifier/api/verifier.go b/internal/chartverifier/api/verifier.go index f2fe2578..4f19aa55 100644 --- a/internal/chartverifier/api/verifier.go +++ b/internal/chartverifier/api/verifier.go @@ -74,7 +74,6 @@ func Run(options RunOptions) (*apireport.Report, error) { } verifyReport, err = verifier.Verify(options.ChartURI) - if err != nil { return verifyReport, err } diff --git a/internal/chartverifier/checks/charttesting.go b/internal/chartverifier/checks/charttesting.go index b7f9e8ad..9af36388 100644 --- a/internal/chartverifier/checks/charttesting.go +++ b/internal/chartverifier/checks/charttesting.go @@ -11,6 +11,7 @@ import ( "github.com/helm/chart-testing/v3/pkg/chart" "github.com/helm/chart-testing/v3/pkg/config" "github.com/helm/chart-testing/v3/pkg/util" + "github.com/opdev/getocprange" "gopkg.in/yaml.v3" "helm.sh/helm/v3/pkg/cli" @@ -40,12 +41,15 @@ func getVersion(envSettings *cli.EnvSettings) (string, error) { // Relying on Kubernetes version can be replaced after fixing this issue: // https://bugzilla.redhat.com/show_bug.cgi?id=1850656 kubeVersion := fmt.Sprintf("%s.%s", serverVersion.Major, serverVersion.Minor) - osVersion, ok := tool.GetKubeOpenShiftVersionMap()[kubeVersion] - if !ok { - return "", fmt.Errorf("internal error: %q not found in Kubernetes-OpenShift version map", kubeVersion) + + // We can safely assume that GetOCPRange is going to return a single version rather than a range, + // given that "kubeVersion" is itself a single version and not a range. + OCPVersion, err := getocprange.GetOCPRange(kubeVersion) + if err != nil { + return "", fmt.Errorf("Error translating kubeVersion %q to an OCP version: %v", kubeVersion, err) } - return osVersion, nil + return OCPVersion, nil } type OpenShiftVersionErr string diff --git a/internal/chartverifier/checks/helm.go b/internal/chartverifier/checks/helm.go index ccbf1eb6..034b655a 100644 --- a/internal/chartverifier/checks/helm.go +++ b/internal/chartverifier/checks/helm.go @@ -38,7 +38,6 @@ import ( "helm.sh/helm/v3/pkg/storage/driver" "github.com/redhat-certification/chart-verifier/internal/helm/actions" - "github.com/redhat-certification/chart-verifier/internal/tool" ) // loadChartFromRemote attempts to retrieve a Helm chart from the given remote url. Returns an error if the given url @@ -194,7 +193,7 @@ func getImageReferences(chartURI string, vals map[string]interface{}, kubeVersio capabilities := chartutil.DefaultCapabilities if kubeVersionString == "" { - kubeVersionString = tool.GetLatestKubeVersion() + kubeVersionString = "1.27" // This is *not* a viable solution, to be discussed in PR comments } kubeVersion, err := chartutil.ParseKubeVersion(kubeVersionString) if err != nil { diff --git a/internal/tool/kubeOpenShiftVersionMap.yaml b/internal/tool/kubeOpenShiftVersionMap.yaml deleted file mode 100644 index 5ae97ed3..00000000 --- a/internal/tool/kubeOpenShiftVersionMap.yaml +++ /dev/null @@ -1,29 +0,0 @@ -versions: - - kube-version: "1.27" - ocp-version: "4.14" - - kube-version: "1.26" - ocp-version: "4.13" - - kube-version: "1.25" - ocp-version: "4.12" - - kube-version: "1.24" - ocp-version: "4.11" - - kube-version: "1.23" - ocp-version: "4.10" - - kube-version: "1.22" - ocp-version: "4.9" - - kube-version: "1.21" - ocp-version: "4.8" - - kube-version: "1.20" - ocp-version: "4.7" - - kube-version: "1.19" - ocp-version: "4.6" - - kube-version: "1.18" - ocp-version: "4.5" - - kube-version: "1.17" - ocp-version: "4.4" - - kube-version: "1.16" - ocp-version: "4.3" - - kube-version: "1.14" - ocp-version: "4.2" - - kube-version: "1.13" - ocp-version: "4.1" diff --git a/internal/tool/kubectl.go b/internal/tool/kubectl.go index b4eab169..fd24e7b1 100644 --- a/internal/tool/kubectl.go +++ b/internal/tool/kubectl.go @@ -2,13 +2,10 @@ package tool import ( "context" - "embed" "errors" "fmt" "time" - "github.com/Masterminds/semver" - "gopkg.in/yaml.v3" "helm.sh/helm/v3/pkg/cli" v1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -22,59 +19,18 @@ import ( "github.com/redhat-certification/chart-verifier/internal/chartverifier/utils" ) -//go:embed kubeOpenShiftVersionMap.yaml -var content embed.FS - -// Based on https://access.redhat.com/solutions/4870701 var ( - kubeOpenShiftVersionMap map[string]string - listDeployments = getDeploymentsList - listDaemonSets = getDaemonSetsList - listStatefulSets = getStatefulSetsList - latestKubeVersion *semver.Version + listDeployments = getDeploymentsList + listDaemonSets = getDaemonSetsList + listStatefulSets = getStatefulSetsList ) -type versionMap struct { - Versions []*versionMapping `yaml:"versions"` -} - -type versionMapping struct { - KubeVersion string `yaml:"kube-version"` - OcpVersion string `yaml:"ocp-version"` -} - type workloadNotReady struct { ResourceType string Name string Unavailable int32 } -func init() { - kubeOpenShiftVersionMap = make(map[string]string) - - yamlFile, err := content.ReadFile("kubeOpenShiftVersionMap.yaml") - if err != nil { - utils.LogError(fmt.Sprintf("Error reading content of kubeOpenShiftVersionMap.yaml: %v", err)) - return - } - - versions := versionMap{} - err = yaml.Unmarshal(yamlFile, &versions) - if err != nil { - utils.LogError(fmt.Sprintf("Error reading content of kubeOpenShiftVersionMap.yaml: %v", err)) - return - } - - latestKubeVersion, _ = semver.NewVersion("0.0") - for _, versionMap := range versions.Versions { - currentVersion, _ := semver.NewVersion(versionMap.KubeVersion) - if currentVersion.GreaterThan(latestKubeVersion) { - latestKubeVersion = currentVersion - } - kubeOpenShiftVersionMap[versionMap.KubeVersion] = versionMap.OcpVersion - } -} - type Kubectl struct { clientset kubernetes.Interface } @@ -194,10 +150,6 @@ func (k Kubectl) GetServerVersion() (*version.Info, error) { return version, err } -func GetKubeOpenShiftVersionMap() map[string]string { - return kubeOpenShiftVersionMap -} - func GetClientConfig(envSettings *cli.EnvSettings) clientcmd.ClientConfig { loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() if len(envSettings.KubeConfig) > 0 { @@ -232,7 +184,3 @@ func getDaemonSetsList(k Kubectl, context context.Context, namespace string, sel } return list.Items, err } - -func GetLatestKubeVersion() string { - return latestKubeVersion.String() -} diff --git a/internal/tool/kubectl_test.go b/internal/tool/kubectl_test.go index 36a61361..fbcdf410 100644 --- a/internal/tool/kubectl_test.go +++ b/internal/tool/kubectl_test.go @@ -11,98 +11,9 @@ import ( v1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/version" - discoveryfake "k8s.io/client-go/discovery/fake" "k8s.io/client-go/kubernetes/fake" ) -type testData struct { - getVersionOut version.Info - OCVersion string -} - -var output120 = version.Info{ - Major: "1", - Minor: "20", -} - -var output121 = version.Info{ - Major: "1", - Minor: "21", -} - -var output122 = version.Info{ - Major: "1", - Minor: "22", -} - -var output123 = version.Info{ - Major: "1", - Minor: "23", -} - -var output124 = version.Info{ - Major: "1", - Minor: "24", -} - -var output125 = version.Info{ - Major: "1", - Minor: "25", -} - -var output126 = version.Info{ - Major: "1", - Minor: "26", -} - -var output127 = version.Info{ - Major: "1", - Minor: "27", -} - -var latestVersion = output127 - -var testsData []testData - -func TestOCVersions(t *testing.T) { - testsData = append(testsData, testData{getVersionOut: output120, OCVersion: "4.7"}) - testsData = append(testsData, testData{getVersionOut: output121, OCVersion: "4.8"}) - testsData = append(testsData, testData{getVersionOut: output122, OCVersion: "4.9"}) - testsData = append(testsData, testData{getVersionOut: output123, OCVersion: "4.10"}) - testsData = append(testsData, testData{getVersionOut: output124, OCVersion: "4.11"}) - testsData = append(testsData, testData{getVersionOut: output125, OCVersion: "4.12"}) - testsData = append(testsData, testData{getVersionOut: output126, OCVersion: "4.13"}) - testsData = append(testsData, testData{getVersionOut: output127, OCVersion: "4.14"}) - - for _, testdata := range testsData { - clientset := fake.NewSimpleClientset() - clientset.Discovery().(*discoveryfake.FakeDiscovery).FakedServerVersion = &version.Info{ - Major: testdata.getVersionOut.Major, - Minor: testdata.getVersionOut.Minor, - } - kubectl := Kubectl{clientset: clientset} - serverVersion, err := kubectl.GetServerVersion() - if err != nil { - t.Error(err) - } - if serverVersion.Major != testdata.getVersionOut.Major || serverVersion.Minor != testdata.getVersionOut.Minor { - t.Errorf("server version mismatch, expected: %+v, got: %+v", testdata.getVersionOut, serverVersion) - } - kubeVersion := fmt.Sprintf("%s.%s", serverVersion.Major, serverVersion.Minor) - ocVersion := GetKubeOpenShiftVersionMap()[kubeVersion] - if ocVersion != testdata.OCVersion { - t.Errorf("version mismatch, expected: %s, got: %s", testdata.OCVersion, ocVersion) - } - } - - latestKV := GetLatestKubeVersion() - expectedLatestKV := fmt.Sprintf("%s.%s.0", latestVersion.Major, latestVersion.Minor) - if latestKV != expectedLatestKV { - t.Errorf("latest kubversion mismatch, expected: %s, got: %s", expectedLatestKV, latestKV) - } -} - var testDeployments = []v1.Deployment{ { ObjectMeta: metav1.ObjectMeta{Name: "test0"},