From 1709a50d59bb8aba03713ade2f4869f86a43b8a3 Mon Sep 17 00:00:00 2001 From: Daniel Erez Date: Sun, 16 May 2021 13:38:16 +0300 Subject: [PATCH] MGMT-6456 fetch OpenshiftVersion from cache by ReleaseImage (#1719) In order to avoid unnecessary invocations of 'oc adm release info', AddOpenshiftVersion now ensures that the specified release image doesn't already exist in versions cache. Otherwise, returns the relevant version from cache. --- internal/versions/versions.go | 11 +++++++- internal/versions/versions_test.go | 41 ++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/internal/versions/versions.go b/internal/versions/versions.go index ec036c713b1..83b8fd77c5f 100644 --- a/internal/versions/versions.go +++ b/internal/versions/versions.go @@ -209,13 +209,22 @@ func (h *handler) GetOCPSemVer(openshiftVersion string) (string, error) { } func (h *handler) AddOpenshiftVersion(ocpReleaseImage, pullSecret string) (*models.OpenshiftVersion, error) { + // Check whether ocpReleaseImage already exists in cache + for _, v := range h.openshiftVersions { + if v.ReleaseImage != nil && *v.ReleaseImage == ocpReleaseImage { + // Return existing version + version := v + return &version, nil + } + } + // Get openshift version from release image metadata (oc adm release info) ocpVersion, err := h.releaseHandler.GetOpenshiftVersion(h.log, ocpReleaseImage, h.releaseImageMirror, pullSecret) if err != nil { return nil, err } - // Return if version is not speficied in OPENSHIFT_VERSIONS + // Return if version is not specified in OPENSHIFT_VERSIONS ocpVersionKey, err := h.GetKey(ocpVersion) if err != nil { return nil, err diff --git a/internal/versions/versions_test.go b/internal/versions/versions_test.go index c47e7f75761..23bdbbcc711 100644 --- a/internal/versions/versions_test.go +++ b/internal/versions/versions_test.go @@ -38,14 +38,6 @@ var defaultOpenShiftVersions = models.OpenshiftVersions{ }, } -var customOpenShiftVersions = models.OpenshiftVersions{ - "4.7": models.OpenshiftVersion{ - RhcosImage: swag.String("rhcos_4.7.0"), - RhcosVersion: swag.String("version-47.123-0"), - ReleaseVersion: nil, DisplayName: nil, ReleaseImage: nil, SupportLevel: nil, - }, -} - var supportedCustomOpenShiftVersions = models.OpenshiftVersions{ "4.8": models.OpenshiftVersion{ DisplayName: swag.String("4.8.0"), @@ -320,13 +312,25 @@ var _ = Describe("list versions", func() { Context("AddOpenshiftVersion", func() { var ( - pullSecret = "test_pull_secret" - releaseImage = "releaseImage" - ocpVersion = "4.7.0" - keyVersion = "4.7" - customOcpVersion = "4.8.0" + pullSecret = "test_pull_secret" + releaseImage = "releaseImage" + ocpVersion = "4.7.0" + keyVersion = "4.7" + customOcpVersion = "4.8.0" + customKeyVersion = "4.8" + customOpenShiftVersions models.OpenshiftVersions ) + BeforeEach(func() { + customOpenShiftVersions = models.OpenshiftVersions{ + "4.7": models.OpenshiftVersion{ + RhcosImage: swag.String("rhcos_4.7.0"), + RhcosVersion: swag.String("version-47.123-0"), + ReleaseVersion: nil, DisplayName: nil, ReleaseImage: nil, SupportLevel: nil, + }, + } + }) + It("added version successfully", func() { h := NewHandler(logger, mockRelease, versions, customOpenShiftVersions, "") mockRelease.EXPECT().GetOpenshiftVersion( @@ -398,6 +402,17 @@ var _ = Describe("list versions", func() { versionKey, _ := h.GetKey(ocpVersion) Expect(err.Error()).Should(Equal(fmt.Sprintf("OCP version is not specified in OPENSHIFT_VERSIONS: %s", versionKey))) }) + + It("release image already exists", func() { + h := NewHandler(logger, mockRelease, versions, supportedCustomOpenShiftVersions, "") + + versionFromCache, err := h.GetVersion(customKeyVersion) + Expect(err).ShouldNot(HaveOccurred()) + + version, err := h.AddOpenshiftVersion(releaseImage, pullSecret) + Expect(err).ShouldNot(HaveOccurred()) + Expect(version).Should(Equal(versionFromCache)) + }) }) })