Skip to content

Commit

Permalink
Merge pull request #6275 from bfournie/release-image-mirror
Browse files Browse the repository at this point in the history
AGENT-326: Fix releaseImage mirror handling with unit test
  • Loading branch information
openshift-merge-robot authored Aug 31, 2022
2 parents ddf29b4 + 9fcee1b commit e4fba40
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 9 deletions.
30 changes: 21 additions & 9 deletions pkg/asset/agent/image/ignition.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"path"
"path/filepath"
"regexp"
"strings"

"github.com/coreos/ignition/v2/config/util"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
81 changes: 81 additions & 0 deletions pkg/asset/agent/image/ignition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

})
}
}

0 comments on commit e4fba40

Please sign in to comment.