Skip to content

Commit

Permalink
Use get-ocp-range to translate Kube to OCP ranges
Browse files Browse the repository at this point in the history
This work had been started as part of commit
f08a985

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 <[email protected]>
  • Loading branch information
mgoerens committed Feb 8, 2024
1 parent 837a921 commit 694dfde
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 205 deletions.
1 change: 0 additions & 1 deletion internal/chartverifier/api/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func Run(options RunOptions) (*apireport.Report, error) {
}

verifyReport, err = verifier.Verify(options.ChartURI)

if err != nil {
return verifyReport, err
}
Expand Down
12 changes: 8 additions & 4 deletions internal/chartverifier/checks/charttesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions internal/chartverifier/checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
17 changes: 2 additions & 15 deletions internal/chartverifier/checks/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion internal/chartverifier/checks/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
29 changes: 0 additions & 29 deletions internal/tool/kubeOpenShiftVersionMap.yaml

This file was deleted.

58 changes: 3 additions & 55 deletions internal/tool/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -232,7 +184,3 @@ func getDaemonSetsList(k Kubectl, context context.Context, namespace string, sel
}
return list.Items, err
}

func GetLatestKubeVersion() string {
return latestKubeVersion.String()
}
89 changes: 0 additions & 89 deletions internal/tool/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit 694dfde

Please sign in to comment.