Skip to content

Commit

Permalink
MGMT-6456 fetch OpenshiftVersion from cache by ReleaseImage (#1719)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danielerez authored May 16, 2021
1 parent f7facf4 commit 1709a50
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
11 changes: 10 additions & 1 deletion internal/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 28 additions & 13 deletions internal/versions/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
})
})
})

Expand Down

0 comments on commit 1709a50

Please sign in to comment.