Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AGENT-326: Fix releaseImage mirror handling with unit test #6275

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this could be slightly simplified to: releaseImageMirror = fmt.Sprintf("%s@sha256:%s", config.Mirror, source[3])

} else if len(source) == 3 {
releaseImageMirror = fmt.Sprintf("%s:%s", config.Mirror, source[2])
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing issue, but it's a bit surprising that we don't return or break here, so we're always choosing the last matching mirror on the list.

}

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)

})
}
}