From d5e8c65872822f1b4e908f1022ca42d654b18902 Mon Sep 17 00:00:00 2001 From: mgoerens Date: Wed, 14 Feb 2024 10:30:20 +0100 Subject: [PATCH] Use get-ocp-range to translate Kube to OCP ranges (#423) This work had been started as part of commit f08a985e53d90671c6c99d88149390ef3a1c3bfc This commit removes the mapping of Kubernetes versions to OCP versions and calls the external get-ocp-range library instead. closes #418 Signed-off-by: Matthias Goerens --- internal/chartverifier/api/verifier.go | 1 - internal/chartverifier/checks/charttesting.go | 12 ++- internal/chartverifier/checks/checks.go | 12 +-- internal/chartverifier/checks/helm.go | 17 +--- internal/chartverifier/checks/helm_test.go | 2 +- internal/tool/kubeOpenShiftVersionMap.yaml | 29 ------ internal/tool/kubectl.go | 58 +----------- internal/tool/kubectl_test.go | 89 ------------------- 8 files changed, 15 insertions(+), 205 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/checks.go b/internal/chartverifier/checks/checks.go index 7017fbb7..efef631a 100644 --- a/internal/chartverifier/checks/checks.go +++ b/internal/chartverifier/checks/checks.go @@ -463,17 +463,7 @@ func downloadFile(fileURL *url.URL, directory string) (string, error) { } func certifyImages(r Result, opts *CheckOptions, registry string) Result { - kubeVersion := "" - kubeConfig := tool.GetClientConfig(opts.HelmEnvSettings) - kubectl, kubeErr := tool.NewKubectl(kubeConfig) - if kubeErr == nil { - serverVersion, versionErr := kubectl.GetServerVersion() - if versionErr == nil { - kubeVersion = fmt.Sprintf("%s.%s", serverVersion.Major, serverVersion.Minor) - } - } - - images, err := getImageReferences(opts.URI, opts.Values, kubeVersion) + images, err := getImageReferences(opts.URI, opts.Values) if err != nil { r.SetResult(false, fmt.Sprintf("%s : Failed to get images, error running helm template : %v", ImageCertifyFailed, err)) } diff --git a/internal/chartverifier/checks/helm.go b/internal/chartverifier/checks/helm.go index ccbf1eb6..2a44a9e4 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 @@ -190,23 +189,11 @@ func IsChartNotFound(err error) bool { return ok } -func getImageReferences(chartURI string, vals map[string]interface{}, kubeVersionString string) ([]string, error) { - capabilities := chartutil.DefaultCapabilities - - if kubeVersionString == "" { - kubeVersionString = tool.GetLatestKubeVersion() - } - kubeVersion, err := chartutil.ParseKubeVersion(kubeVersionString) - if err != nil { - return nil, err - } - - capabilities.KubeVersion = *kubeVersion - +func getImageReferences(chartURI string, vals map[string]interface{}) ([]string, error) { actionConfig := &action.Configuration{ Releases: nil, KubeClient: &kubefake.PrintingKubeClient{Out: io.Discard}, - Capabilities: capabilities, + Capabilities: chartutil.DefaultCapabilities, Log: func(format string, v ...interface{}) {}, } mem := driver.NewMemory() diff --git a/internal/chartverifier/checks/helm_test.go b/internal/chartverifier/checks/helm_test.go index 47990a2b..37c22f32 100644 --- a/internal/chartverifier/checks/helm_test.go +++ b/internal/chartverifier/checks/helm_test.go @@ -167,7 +167,7 @@ func TestTemplate(t *testing.T) { for _, tc := range TestCases { t.Run(tc.description, func(t *testing.T) { - images, err := getImageReferences(tc.uri, map[string]interface{}{}, "") + images, err := getImageReferences(tc.uri, map[string]interface{}{}) require.NoError(t, err) require.Equal(t, len(images), len(tc.images)) for i := 0; i < len(tc.images); i++ { 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"},