Skip to content

Commit 4c02195

Browse files
authored
Merge pull request #5560 from pablintino/ocpbugs-71238
OCPBUGS-71238: Consider image mirrors for OSImageStream fetching
2 parents d6a4d03 + 010dc0a commit 4c02195

11 files changed

Lines changed: 617 additions & 132 deletions

File tree

cmd/machine-config-operator/start.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ func runStartCmd(_ *cobra.Command, _ []string) {
9292
ctrlctx.KubeInformerFactory.Core().V1().Nodes(),
9393
ctrlctx.KubeMAOSharedInformer.Core().V1().Secrets(),
9494
ctrlctx.ConfigInformerFactory.Config().V1().Images(),
95+
ctrlctx.ConfigInformerFactory.Config().V1().ImageDigestMirrorSets(),
96+
ctrlctx.ConfigInformerFactory.Config().V1().ImageTagMirrorSets(),
97+
ctrlctx.OperatorInformerFactory.Operator().V1alpha1().ImageContentSourcePolicies(),
9598
ctrlctx.KubeNamespacedInformerFactory.Core().V1().ServiceAccounts(),
9699
ctrlctx.KubeNamespacedInformerFactory.Core().V1().Secrets(),
97100
ctrlctx.OpenShiftConfigKubeNamespacedInformerFactory.Core().V1().ConfigMaps(),

pkg/controller/bootstrap/bootstrap.go

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"time"
1212

13+
"github.com/openshift/machine-config-operator/pkg/imageutils"
1314
"github.com/openshift/machine-config-operator/pkg/osimagestream"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516

@@ -220,33 +221,19 @@ func (b *Bootstrap) Run(destDir string) error {
220221
var osImageStream *mcfgv1alpha1.OSImageStream
221222
// Enable OSImageStreams if the FeatureGate is active and the deployment is not OKD
222223
if osimagestream.IsFeatureEnabled(fgHandler) {
223-
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
224-
defer cancel()
225-
226-
osImageStream, err = osimagestream.BuildOsImageStreamBootstrap(ctx,
227-
pullSecret,
228-
cconfig,
229-
imageStream,
230-
&osimagestream.OSImageTuple{
231-
OSImage: cconfig.Spec.BaseOSContainerImage,
232-
OSExtensionsImage: cconfig.Spec.BaseOSExtensionsContainerImage,
233-
},
234-
osimagestream.NewDefaultStreamSourceFactory(nil, &osimagestream.DefaultImagesInspectorFactory{}),
235-
)
224+
osImageStream, err = b.fetchOSImageStream(imageStream, cconfig, icspRules, idmsRules, itmsRules, imgCfg, pullSecret)
236225
if err != nil {
237-
return fmt.Errorf("error inspecting available OSImageStreams: %w", err)
226+
return err
238227
}
239228

240-
// If no error happened override the ControllerConfig URLs with the default stream ones
241-
if err == nil {
242-
defaultStreamSet, err := osimagestream.GetOSImageStreamSetByName(osImageStream, "")
243-
if err != nil {
244-
// Should never happen
245-
return fmt.Errorf("error getting default OSImageStreamSet: %w", err)
246-
}
247-
cconfig.Spec.BaseOSContainerImage = string(defaultStreamSet.OSImage)
248-
cconfig.Spec.BaseOSExtensionsContainerImage = string(defaultStreamSet.OSExtensionsImage)
229+
// Override the ControllerConfig URLs with the default stream ones
230+
defaultStreamSet, err := osimagestream.GetOSImageStreamSetByName(osImageStream, "")
231+
if err != nil {
232+
// Should never happen
233+
return fmt.Errorf("error getting default OSImageStreamSet: %w", err)
249234
}
235+
cconfig.Spec.BaseOSContainerImage = string(defaultStreamSet.OSImage)
236+
cconfig.Spec.BaseOSExtensionsContainerImage = string(defaultStreamSet.OSExtensionsImage)
250237
}
251238

252239
pullSecretBytes := pullSecret.Data[corev1.DockerConfigJsonKey]
@@ -416,6 +403,46 @@ func (b *Bootstrap) Run(destDir string) error {
416403

417404
}
418405

406+
func (b *Bootstrap) fetchOSImageStream(
407+
imageStream *imagev1.ImageStream,
408+
cconfig *mcfgv1.ControllerConfig,
409+
icspRules []*apioperatorsv1alpha1.ImageContentSourcePolicy,
410+
idmsRules []*apicfgv1.ImageDigestMirrorSet,
411+
itmsRules []*apicfgv1.ImageTagMirrorSet,
412+
imgCfg *apicfgv1.Image,
413+
pullSecret *corev1.Secret,
414+
) (*mcfgv1alpha1.OSImageStream, error) {
415+
416+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
417+
defer cancel()
418+
419+
sysCtxBuilder := imageutils.NewSysContextBuilder().
420+
WithControllerConfig(cconfig).
421+
WithSecret(pullSecret)
422+
423+
registriesConfig, err := imageutils.GenerateRegistriesConfig(imgCfg, icspRules, idmsRules, itmsRules)
424+
if err != nil {
425+
return nil, fmt.Errorf("failed to generate registries config for OSImageStreams fetching: %w", err)
426+
}
427+
if registriesConfig != nil {
428+
sysCtxBuilder.WithRegistriesConfig(registriesConfig)
429+
}
430+
431+
osImageStream, err := osimagestream.BuildOsImageStreamBootstrap(ctx,
432+
sysCtxBuilder,
433+
imageStream,
434+
&osimagestream.OSImageTuple{
435+
OSImage: cconfig.Spec.BaseOSContainerImage,
436+
OSExtensionsImage: cconfig.Spec.BaseOSExtensionsContainerImage,
437+
},
438+
osimagestream.NewDefaultStreamSourceFactory(nil, &osimagestream.DefaultImagesInspectorFactory{}),
439+
)
440+
if err != nil {
441+
return nil, fmt.Errorf("error inspecting available OSImageStreams: %w", err)
442+
}
443+
return osImageStream, nil
444+
}
445+
419446
func getValidPullSecretFromBytes(sData []byte) (*corev1.Secret, error) {
420447
obji, err := runtime.Decode(kscheme.Codecs.UniversalDecoder(corev1.SchemeGroupVersion), sData)
421448
if err != nil {

pkg/controller/build/imagepruner/imagepruner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewImagePruner() ImagePruner {
3838
// InspectImage inspects the given image using the provided secret. It also accepts a
3939
// ControllerConfig so that certificates may be placed on the filesystem for authentication.
4040
func (i *imagePrunerImpl) InspectImage(ctx context.Context, pullspec string, secret *corev1.Secret, cc *mcfgv1.ControllerConfig) (*types.ImageInspectInfo, *digest.Digest, error) {
41-
sysCtx, err := imageutils.NewSysContextFromControllerConfig(secret, cc)
41+
sysCtx, err := imageutils.NewSysContextBuilder().WithSecret(secret).WithControllerConfig(cc).Build()
4242
if err != nil {
4343
return nil, nil, fmt.Errorf("could not prepare for image inspection: %w", err)
4444
}
@@ -60,7 +60,7 @@ func (i *imagePrunerImpl) InspectImage(ctx context.Context, pullspec string, sec
6060
// DeleteImage deletes the given image using the provided secret. It also accepts a
6161
// ControllerConfig so that certificates may be placed on the filesystem for authentication.
6262
func (i *imagePrunerImpl) DeleteImage(ctx context.Context, pullspec string, secret *corev1.Secret, cc *mcfgv1.ControllerConfig) error {
63-
sysCtx, err := imageutils.NewSysContextFromControllerConfig(secret, cc)
63+
sysCtx, err := imageutils.NewSysContextBuilder().WithSecret(secret).WithControllerConfig(cc).Build()
6464
if err != nil {
6565
return fmt.Errorf("could not prepare for image deletion: %w", err)
6666
}

pkg/imageutils/registries.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package imageutils
2+
3+
import (
4+
"github.com/containers/image/v5/pkg/sysregistriesv2"
5+
apicfgv1 "github.com/openshift/api/config/v1"
6+
configv1 "github.com/openshift/api/config/v1"
7+
apioperatorsv1alpha1 "github.com/openshift/api/operator/v1alpha1"
8+
"github.com/openshift/runtime-utils/pkg/registries"
9+
)
10+
11+
// GenerateRegistriesConfig builds a container runtime registries configuration by consolidating
12+
// cluster image policies, registry mirrors (ICSP/IDMS/ITMS), and registry access controls.
13+
func GenerateRegistriesConfig(
14+
image *configv1.Image,
15+
icspRules []*apioperatorsv1alpha1.ImageContentSourcePolicy,
16+
idmsRules []*apicfgv1.ImageDigestMirrorSet,
17+
itmsRules []*apicfgv1.ImageTagMirrorSet) (*sysregistriesv2.V2RegistriesConf, error) {
18+
19+
// TODO: Consume this values from templates
20+
// Tracked by https://issues.redhat.com/browse/MCO-2060
21+
tomlConf := &sysregistriesv2.V2RegistriesConf{
22+
UnqualifiedSearchRegistries: []string{"registry.access.redhat.com", "docker.io"},
23+
}
24+
25+
var insecureScopes []string
26+
var blockedScopes []string
27+
if image != nil {
28+
insecureScopes = image.Spec.RegistrySources.InsecureRegistries
29+
blockedScopes = image.Spec.RegistrySources.BlockedRegistries
30+
}
31+
if err := registries.EditRegistriesConfig(tomlConf, insecureScopes, blockedScopes, icspRules, idmsRules, itmsRules); err != nil {
32+
return nil, err
33+
}
34+
return tomlConf, nil
35+
}

0 commit comments

Comments
 (0)