Repository: openshift/assisted-service (language: python)

Issue:
MGMT-17367: Use ICSP in install-config for older OCP releases
Openshift releases earlier than 4.14 do not support the ImageDigestMirrorSet so configure ImageContentSources when building the install-config for these versions.

<!--
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

You can refer to [Kubernetes community documentation] on writing good commit messages, which provides good tips and ideas.

Some PRs address specific issues. Please, refer to the [CONTRIBUTING] documentation for more
information on how to link a PR to an existing issue.

It's recommended to take a few extra minutes to provide more information about
how this code was tested. Here are some questions that may be worth answering:

- Should this PR be tested by the reviewer?
- Is this PR relying on CI for an e2e test run?
- Should this PR be tested in a specific environment?
- Any logs, screenshots, etc that can help with the review process?

-->

## List all the issues related to this PR

- [ ] New Feature <!-- new functionality -->
- [ ] Enhancement <!-- refactor, code changes, improvement, that won't add new features -->
- [ ] Bug fix
- [ ] Tests
- [ ] Documentation
- [ ] CI/CD <!-- Notice that changes for Dockerfiles/Jenkinsfiles aren't tested in CI due to a known bug. -->

## What environments does this code impact?

- [ ] Automation (CI, tools, etc)
- [ ] Cloud
- [ ] Operator Managed Deployments
- [x] None

## How was this code tested?

<!-- Please, select one or more if needed: -->

- [ ] assisted-test-infra environment
- [ ] dev-scripts environment
- [ ] Reviewer's test appreciated
- [ ] Waiting for CI to do a full test run
- [ ] Manual (Elaborate on how it was tested)
- [x] No tests needed

## Checklist

- [ ] Title and description added to both, commit and PR.
- [ ] Relevant issues have been associated (see [CONTRIBUTING] guide)
- [ ] This change does not require a documentation update (docstring, `docs`, README, etc)
- [ ] Does this change include unit-tests (note that code changes require unit-tests)

## Reviewers Checklist

- Are the title and description (in both PR and commit) meaningful and clear?
- Is there a bug required (and linked) for this change?
- Should this PR be backported?

[Kubernetes community documentation]: https://github.com/kubernetes/community/blob/master/contributors/guide/pull-requests.md#commit-message-guidelines
[CONTRIBUTING]: https://github.com/openshift/assisted-service/blob/master/CONTRIBUTING.md



Discussion / hints:

Fix patch:
diff --git a/internal/installcfg/builder/builder.go b/internal/installcfg/builder/builder.go
index 9f7714d4480..454b4cab8b5 100644
--- a/internal/installcfg/builder/builder.go
+++ b/internal/installcfg/builder/builder.go
@@ -15,6 +15,8 @@ import (
 	"github.com/thoas/go-funk"
 )
 
+const minimalOpenShiftVersionForImageDigestSupport = "4.14.0-0.0"
+
 //go:generate mockgen -source=builder.go -package=builder -destination=mock_installcfg.go
 type InstallConfigBuilder interface {
 	GetInstallConfig(cluster *common.Cluster, clusterInfraenvs []*common.InfraEnv, rhRootCA string) ([]byte, error)
@@ -131,10 +133,22 @@ func (i *installConfigBuilder) getBasicInstallConfig(cluster *common.Cluster) (*
 	}
 
 	if i.mirrorRegistriesBuilder.IsMirrorRegistriesConfigured() {
-		err := i.setImageDigestMirrorSet(cfg)
+		isOpenShiftVersionRecentEnough, err := common.BaseVersionGreaterOrEqual(minimalOpenShiftVersionForImageDigestSupport, cluster.OpenshiftVersion)
 		if err != nil {
 			return nil, err
 		}
+		if isOpenShiftVersionRecentEnough {
+			err := i.setImageDigestMirrorSet(cfg)
+			if err != nil {
+				return nil, err
+			}
+		} else {
+			// If version does not support ImageDigestSources, set ImageContent
+			err := i.setImageContentSources(cfg)
+			if err != nil {
+				return nil, err
+			}
+		}
 	}
 
 	return cfg, nil
@@ -154,6 +168,20 @@ func (i *installConfigBuilder) setImageDigestMirrorSet(cfg *installcfg.Installer
 	return nil
 }
 
+func (i *installConfigBuilder) setImageContentSources(cfg *installcfg.InstallerConfigBaremetal) error {
+	mirrorRegistriesConfigs, err := i.mirrorRegistriesBuilder.ExtractLocationMirrorDataFromRegistries()
+	if err != nil {
+		i.log.WithError(err).Errorf("Failed to get the mirror registries conf need for ImageContentSources")
+		return err
+	}
+	imageContentSourceList := make([]installcfg.ImageContentSource, len(mirrorRegistriesConfigs))
+	for i, mirrorRegistriesConfig := range mirrorRegistriesConfigs {
+		imageContentSourceList[i] = installcfg.ImageContentSource{Source: mirrorRegistriesConfig.Location, Mirrors: mirrorRegistriesConfig.Mirror}
+	}
+	cfg.DeprecatedImageContentSources = imageContentSourceList
+	return nil
+}
+
 func (i *installConfigBuilder) applyConfigOverrides(overrides string, cfg *installcfg.InstallerConfigBaremetal) error {
 	if overrides == "" {
 		return nil


Test patch:
diff --git a/internal/installcfg/builder/builder_test.go b/internal/installcfg/builder/builder_test.go
index e28d8c2183a..809ad9413e6 100644
--- a/internal/installcfg/builder/builder_test.go
+++ b/internal/installcfg/builder/builder_test.go
@@ -149,6 +149,7 @@ aEA8gNEmV+rb7h1v0r3EwDQYJKoZIhvcNAQELBQAwYTELMAkGA1UEBhMCaXMxCzAJBgNVBAgMAmRk
 
 	It("create_configuration_with_mirror_registries", func() {
 		var result installcfg.InstallerConfigBaremetal
+		cluster.OpenshiftVersion = "4.14.0-0.0"
 		regData := []mirrorregistries.RegistriesConf{{Location: "location1", Mirror: []string{"mirror1"}}, {Location: "location2", Mirror: []string{"mirror2"}}}
 		mockMirrorRegistriesConfigBuilder.EXPECT().IsMirrorRegistriesConfigured().Return(true).Times(2)
 		mockMirrorRegistriesConfigBuilder.EXPECT().ExtractLocationMirrorDataFromRegistries().Return(regData, nil).Times(1)
@@ -158,6 +159,37 @@ aEA8gNEmV+rb7h1v0r3EwDQYJKoZIhvcNAQELBQAwYTELMAkGA1UEBhMCaXMxCzAJBgNVBAgMAmRk
 		err = json.Unmarshal(data, &result)
 		Expect(err).ShouldNot(HaveOccurred())
 		Expect(result.Networking.NetworkType).To(Equal(models.ClusterNetworkTypeOpenShiftSDN))
+		Expect(result.ImageDigestSources).To(Equal([]installcfg.ImageDigestSource{
+			installcfg.ImageDigestSource{Source: "location1",
+				Mirrors: []string{"mirror1"},
+			},
+			installcfg.ImageDigestSource{Source: "location2",
+				Mirrors: []string{"mirror2"},
+			},
+		}))
+		Expect(result.DeprecatedImageContentSources).To(BeEmpty())
+	})
+
+	It("create_configuration_with_mirror_registries_idms_not_supported", func() {
+		var result installcfg.InstallerConfigBaremetal
+		cluster.OpenshiftVersion = "4.12.0-0.0"
+		regData := []mirrorregistries.RegistriesConf{{Location: "location1", Mirror: []string{"mirror1"}}, {Location: "location2", Mirror: []string{"mirror2"}}}
+		mockMirrorRegistriesConfigBuilder.EXPECT().IsMirrorRegistriesConfigured().Return(true).Times(2)
+		mockMirrorRegistriesConfigBuilder.EXPECT().ExtractLocationMirrorDataFromRegistries().Return(regData, nil).Times(1)
+		mockMirrorRegistriesConfigBuilder.EXPECT().GetMirrorCA().Return([]byte("some sa data"), nil).Times(1)
+		data, err := installConfig.GetInstallConfig(&cluster, clusterInfraenvs, "")
+		Expect(err).ShouldNot(HaveOccurred())
+		err = json.Unmarshal(data, &result)
+		Expect(err).ShouldNot(HaveOccurred())
+		Expect(result.ImageDigestSources).To(BeEmpty())
+		Expect(result.DeprecatedImageContentSources).To(Equal([]installcfg.ImageContentSource{
+			installcfg.ImageContentSource{Source: "location1",
+				Mirrors: []string{"mirror1"},
+			},
+			installcfg.ImageContentSource{Source: "location2",
+				Mirrors: []string{"mirror2"},
+			},
+		}))
 	})
 
 	It("create_configuration_with_proxy", func() {