From 9fcee1b1d6afe3704e7ae91a9754e31d615ed6c6 Mon Sep 17 00:00:00 2001 From: Bob Fournier Date: Mon, 29 Aug 2022 12:57:10 -0400 Subject: [PATCH] Agent 326: Fix releaseImage mirror handling with unit test Based on https://github.com/openshift/installer/pull/6272, this fixes the mirror generation to handle release images that include the sha256 checksum, e.g: quay.io/openshift-release-dev/ocp-release@sha256:300bce8246cf880e792e106607925de0a404484637627edf5f517375517d54a4 --- pkg/asset/agent/image/ignition.go | 30 +++++++--- pkg/asset/agent/image/ignition_test.go | 81 ++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 9 deletions(-) diff --git a/pkg/asset/agent/image/ignition.go b/pkg/asset/agent/image/ignition.go index 6885f8e333c..4813069925b 100644 --- a/pkg/asset/agent/image/ignition.go +++ b/pkg/asset/agent/image/ignition.go @@ -6,6 +6,7 @@ import ( "net/url" "path" "path/filepath" + "regexp" "strings" "github.com/coreos/ignition/v2/config/util" @@ -139,15 +140,7 @@ func (a *Ignition) Generate(dependencies asset.Parents) error { registryCABundle := &mirror.CaBundle{} dependencies.Get(registriesConfig, registryCABundle) - // Get the mirror for release image - releaseImageMirror := "" - source := strings.Split(agentManifests.ClusterImageSet.Spec.ReleaseImage, ":") - for _, config := range registriesConfig.MirrorConfig { - if config.Location == source[0] { - // include the tag with the build release image - releaseImageMirror = fmt.Sprintf("%s:%s", config.Mirror, source[1]) - } - } + releaseImageMirror := getMirrorFromRelease(agentManifests.ClusterImageSet.Spec.ReleaseImage, registriesConfig) infraEnvID := uuid.New().String() logrus.Debug("Generated random infra-env id ", infraEnvID) @@ -387,3 +380,22 @@ func RetrieveRendezvousIP(agentConfig *agent.Config, nmStateConfigs []*v1beta1.N } return rendezvousIP, err } + +func getMirrorFromRelease(releaseImage string, registriesConfig *mirror.RegistriesConf) string { + + releaseImageMirror := "" + source := regexp.MustCompile(`^(.+?)(@sha256)?:(.+)`).FindStringSubmatch(releaseImage) + for _, config := range registriesConfig.MirrorConfig { + if config.Location == source[1] { + // include the tag with the build release image + if len(source) == 4 { + // Has Sha256 + releaseImageMirror = fmt.Sprintf("%s%s:%s", config.Mirror, source[2], source[3]) + } else if len(source) == 3 { + releaseImageMirror = fmt.Sprintf("%s:%s", config.Mirror, source[2]) + } + } + } + + return releaseImageMirror +} diff --git a/pkg/asset/agent/image/ignition_test.go b/pkg/asset/agent/image/ignition_test.go index 3d7c0d6f0f4..8446aef9fd9 100644 --- a/pkg/asset/agent/image/ignition_test.go +++ b/pkg/asset/agent/image/ignition_test.go @@ -414,3 +414,84 @@ func buildIgnitionAssetDefaultDependencies() []asset.Asset { &tls.AdminKubeConfigClientCertKey{}, } } + +func TestIgnition_getMirrorFromRelease(t *testing.T) { + + cases := []struct { + name string + release string + registriesConf mirror.RegistriesConf + expectedMirror string + }{ + { + name: "no-mirror", + release: "registry.ci.openshift.org/ocp/release:latest", + registriesConf: mirror.RegistriesConf{}, + expectedMirror: "", + }, + { + name: "mirror-no-match", + release: "registry.ci.openshift.org/ocp/release:4.11.0-0.nightly-foo", + registriesConf: mirror.RegistriesConf{ + File: &asset.File{ + Filename: "registries.conf", + Data: []byte(""), + }, + MirrorConfig: []mirror.RegistriesConfig{ + { + Location: "some.registry.org/release", + Mirror: "some.mirror.org", + }, + }, + }, + expectedMirror: "", + }, + { + name: "mirror-match", + release: "registry.ci.openshift.org/ocp/release:4.11.0-0.nightly-foo", + registriesConf: mirror.RegistriesConf{ + File: &asset.File{ + Filename: "registries.conf", + Data: []byte(""), + }, + MirrorConfig: []mirror.RegistriesConfig{ + { + Location: "registry.ci.openshift.org/ocp/release", + Mirror: "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image", + }, + }, + }, + expectedMirror: "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image:4.11.0-0.nightly-foo", + }, + { + name: "mirror-match-with-checksum", + release: "quay.io/openshift-release-dev/ocp-release@sha256:300bce8246cf880e792e106607925de0a404484637627edf5f517375517d54a4", + registriesConf: mirror.RegistriesConf{ + File: &asset.File{ + Filename: "registries.conf", + Data: []byte(""), + }, + MirrorConfig: []mirror.RegistriesConfig{ + { + Location: "quay.io/openshift-release-dev/ocp-v4.0-art-dev", + Mirror: "localhost:5000/openshift4/openshift/release", + }, + { + Location: "quay.io/openshift-release-dev/ocp-release", + Mirror: "localhost:5000/openshift-release-dev/ocp-release", + }, + }, + }, + expectedMirror: "localhost:5000/openshift-release-dev/ocp-release@sha256:300bce8246cf880e792e106607925de0a404484637627edf5f517375517d54a4", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + + mirror := getMirrorFromRelease(tc.release, &tc.registriesConf) + + assert.Equal(t, tc.expectedMirror, mirror) + + }) + } +}