Skip to content

Commit a11a5fc

Browse files
committed
inject ClusterInfo into RegistriesConf asset
1 parent e0cfcf4 commit a11a5fc

File tree

3 files changed

+166
-33
lines changed

3 files changed

+166
-33
lines changed

pkg/asset/agent/joiner/clusterinfo.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package joiner
22

33
import (
44
"context"
5+
"fmt"
56
"net/url"
67

78
"k8s.io/apimachinery/pkg/api/errors"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910
"k8s.io/client-go/kubernetes"
1011
"k8s.io/client-go/rest"
1112
"k8s.io/client-go/tools/clientcmd"
13+
"sigs.k8s.io/yaml"
1214

1315
configclient "github.com/openshift/client-go/config/clientset/versioned"
1416
"github.com/openshift/installer/pkg/asset"
@@ -20,15 +22,17 @@ import (
2022
// from an already existing cluster. A number of different resources
2123
// are inspected to extract the required configuration
2224
type ClusterInfo struct {
23-
ClusterID string
24-
Version string
25-
ReleaseImage string
26-
APIDNSName string
27-
PullSecret string
28-
Namespace string
29-
UserCaBundle string
30-
Proxy *types.Proxy
31-
Architecture string
25+
ClusterID string
26+
Version string
27+
ReleaseImage string
28+
APIDNSName string
29+
PullSecret string
30+
Namespace string
31+
UserCaBundle string
32+
Proxy *types.Proxy
33+
Architecture string
34+
ImageDigestSources []types.ImageDigestSource
35+
DeprecatedImageContentSources []types.ImageContentSource
3236
}
3337

3438
var _ asset.WritableAsset = (*ClusterInfo)(nil)
@@ -95,6 +99,10 @@ func (ci *ClusterInfo) Generate(dependencies asset.Parents) error {
9599
if err != nil {
96100
return err
97101
}
102+
err = ci.retrieveImageDigestSources(k8sclientset)
103+
if err != nil {
104+
return err
105+
}
98106

99107
ci.Namespace = "cluster0"
100108

@@ -183,6 +191,29 @@ func (ci *ClusterInfo) retrieveArchitecture(clientset *kubernetes.Clientset) err
183191
return nil
184192
}
185193

194+
func (ci *ClusterInfo) retrieveImageDigestSources(clientset *kubernetes.Clientset) error {
195+
clusterConfig, err := clientset.CoreV1().ConfigMaps("kube-system").Get(context.Background(), "cluster-config-v1", metav1.GetOptions{})
196+
if err != nil {
197+
if errors.IsNotFound(err) {
198+
return nil
199+
}
200+
return err
201+
}
202+
data, ok := clusterConfig.Data["install-config"]
203+
if !ok {
204+
return fmt.Errorf("cannot find install-config data")
205+
}
206+
207+
installConfig := types.InstallConfig{}
208+
if err = yaml.Unmarshal([]byte(data), &installConfig); err != nil {
209+
return err
210+
}
211+
ci.ImageDigestSources = installConfig.ImageDigestSources
212+
ci.DeprecatedImageContentSources = installConfig.DeprecatedImageContentSources
213+
214+
return nil
215+
}
216+
186217
// Files returns the files generated by the asset.
187218
func (*ClusterInfo) Files() []*asset.File {
188219
return []*asset.File{}

pkg/asset/agent/mirror/registriesconf.go

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
"github.com/openshift/installer/pkg/asset"
1515
"github.com/openshift/installer/pkg/asset/agent"
16+
"github.com/openshift/installer/pkg/asset/agent/joiner"
17+
"github.com/openshift/installer/pkg/asset/agent/workflow"
1618
"github.com/openshift/installer/pkg/asset/ignition/bootstrap"
1719
"github.com/openshift/installer/pkg/asset/releaseimage"
1820
"github.com/openshift/installer/pkg/types"
@@ -128,35 +130,84 @@ func (*RegistriesConf) Name() string {
128130
// the asset.
129131
func (*RegistriesConf) Dependencies() []asset.Asset {
130132
return []asset.Asset{
133+
&workflow.AgentWorkflow{},
134+
&joiner.ClusterInfo{},
131135
&agent.OptionalInstallConfig{},
132136
&releaseimage.Image{},
133137
}
134138
}
135139

136140
// Generate generates the registries.conf file from install-config.
137141
func (i *RegistriesConf) Generate(dependencies asset.Parents) error {
138-
142+
agentWorkflow := &workflow.AgentWorkflow{}
143+
clusterInfo := &joiner.ClusterInfo{}
139144
installConfig := &agent.OptionalInstallConfig{}
140145
releaseImage := &releaseimage.Image{}
141-
dependencies.Get(installConfig, releaseImage)
146+
dependencies.Get(installConfig, releaseImage, agentWorkflow, clusterInfo)
147+
148+
var imageDigestSources []types.ImageDigestSource
149+
var deprecatedImageContentSources []types.ImageContentSource
150+
var image string
151+
152+
switch agentWorkflow.Workflow {
153+
case workflow.AgentWorkflowTypeInstall:
154+
if installConfig.Supplied {
155+
imageDigestSources = installConfig.Config.ImageDigestSources
156+
deprecatedImageContentSources = installConfig.Config.DeprecatedImageContentSources
157+
}
158+
image = releaseImage.PullSpec
159+
160+
case workflow.AgentWorkflowTypeAddNodes:
161+
imageDigestSources = clusterInfo.ImageDigestSources
162+
deprecatedImageContentSources = clusterInfo.DeprecatedImageContentSources
163+
image = clusterInfo.ReleaseImage
142164

143-
if !installConfig.Supplied || len(installConfig.Config.DeprecatedImageContentSources) == 0 && len(installConfig.Config.ImageDigestSources) == 0 {
165+
default:
166+
return fmt.Errorf("AgentWorkflowType value not supported: %s", agentWorkflow.Workflow)
167+
}
168+
169+
if len(deprecatedImageContentSources) == 0 && len(imageDigestSources) == 0 {
144170
return i.generateDefaultRegistriesConf()
145171
}
146172

147-
if len(installConfig.Config.DeprecatedImageContentSources) != 0 && len(installConfig.Config.ImageDigestSources) != 0 {
173+
err := i.generateRegistriesConf(imageDigestSources, deprecatedImageContentSources)
174+
if err != nil {
175+
return err
176+
}
177+
178+
if !i.releaseImageIsSameInRegistriesConf(image) {
179+
logrus.Warnf(fmt.Sprintf("The imageDigestSources configuration in install-config.yaml should have at least one source field matching the releaseImage value %s", releaseImage.PullSpec))
180+
}
181+
182+
registriesData, err := toml.Marshal(i.Config)
183+
if err != nil {
184+
return err
185+
}
186+
187+
i.File = &asset.File{
188+
Filename: RegistriesConfFilename,
189+
Data: registriesData,
190+
}
191+
192+
return nil
193+
}
194+
195+
func (i *RegistriesConf) generateRegistriesConf(imageDigestSources []types.ImageDigestSource, deprecatedImageContentSources []types.ImageContentSource) error {
196+
197+
if len(deprecatedImageContentSources) != 0 && len(imageDigestSources) != 0 {
148198
return fmt.Errorf("invalid install-config.yaml, cannot set imageContentSources and imageDigestSources at the same time")
149199
}
150200

201+
digestMirrorSources := []types.ImageDigestSource{}
202+
if len(deprecatedImageContentSources) > 0 {
203+
digestMirrorSources = bootstrap.ContentSourceToDigestMirror(deprecatedImageContentSources)
204+
} else if len(imageDigestSources) > 0 {
205+
digestMirrorSources = append(digestMirrorSources, imageDigestSources...)
206+
}
207+
151208
registries := &sysregistriesv2.V2RegistriesConf{
152209
Registries: []sysregistriesv2.Registry{},
153210
}
154-
digestMirrorSources := []types.ImageDigestSource{}
155-
if len(installConfig.Config.DeprecatedImageContentSources) > 0 {
156-
digestMirrorSources = bootstrap.ContentSourceToDigestMirror(installConfig.Config.DeprecatedImageContentSources)
157-
} else if len(installConfig.Config.ImageDigestSources) > 0 {
158-
digestMirrorSources = append(digestMirrorSources, installConfig.Config.ImageDigestSources...)
159-
}
160211
for _, group := range bootstrap.MergedMirrorSets(digestMirrorSources) {
161212
if len(group.Mirrors) == 0 {
162213
continue
@@ -173,20 +224,6 @@ func (i *RegistriesConf) Generate(dependencies asset.Parents) error {
173224
i.Config = registries
174225
i.setMirrorConfig(i.Config)
175226

176-
if !i.releaseImageIsSameInRegistriesConf(releaseImage.PullSpec) {
177-
logrus.Warnf(fmt.Sprintf("The imageDigestSources configuration in install-config.yaml should have at least one source field matching the releaseImage value %s", releaseImage.PullSpec))
178-
}
179-
180-
registriesData, err := toml.Marshal(registries)
181-
if err != nil {
182-
return err
183-
}
184-
185-
i.File = &asset.File{
186-
Filename: RegistriesConfFilename,
187-
Data: registriesData,
188-
}
189-
190227
return nil
191228
}
192229

pkg/asset/agent/mirror/registriesconf_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
"github.com/openshift/installer/pkg/asset"
1313
"github.com/openshift/installer/pkg/asset/agent"
14+
"github.com/openshift/installer/pkg/asset/agent/joiner"
15+
"github.com/openshift/installer/pkg/asset/agent/workflow"
1416
"github.com/openshift/installer/pkg/asset/installconfig"
1517
"github.com/openshift/installer/pkg/asset/mock"
1618
"github.com/openshift/installer/pkg/asset/releaseimage"
@@ -27,6 +29,8 @@ func TestRegistriesConf_Generate(t *testing.T) {
2729
{
2830
name: "missing-config",
2931
dependencies: []asset.Asset{
32+
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
33+
&joiner.ClusterInfo{},
3034
&agent.OptionalInstallConfig{},
3135
&releaseimage.Image{},
3236
},
@@ -35,6 +39,8 @@ func TestRegistriesConf_Generate(t *testing.T) {
3539
{
3640
name: "default",
3741
dependencies: []asset.Asset{
42+
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
43+
&joiner.ClusterInfo{},
3844
&agent.OptionalInstallConfig{
3945
Supplied: true,
4046
AssetBase: installconfig.AssetBase{
@@ -54,6 +60,8 @@ func TestRegistriesConf_Generate(t *testing.T) {
5460
{
5561
name: "invalid-config-image-content-source-does-not-match-releaseImage",
5662
dependencies: []asset.Asset{
63+
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
64+
&joiner.ClusterInfo{},
5765
&agent.OptionalInstallConfig{
5866
Supplied: true,
5967
AssetBase: installconfig.AssetBase{
@@ -87,6 +95,8 @@ func TestRegistriesConf_Generate(t *testing.T) {
8795
{
8896
name: "valid-image-content-sources",
8997
dependencies: []asset.Asset{
98+
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
99+
&joiner.ClusterInfo{},
90100
&agent.OptionalInstallConfig{
91101
Supplied: true,
92102
AssetBase: installconfig.AssetBase{
@@ -137,6 +147,8 @@ func TestRegistriesConf_Generate(t *testing.T) {
137147
{
138148
name: "image-digest-sources",
139149
dependencies: []asset.Asset{
150+
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeInstall},
151+
&joiner.ClusterInfo{},
140152
&agent.OptionalInstallConfig{
141153
Supplied: true,
142154
AssetBase: installconfig.AssetBase{
@@ -175,6 +187,59 @@ func TestRegistriesConf_Generate(t *testing.T) {
175187
[[registry.mirror]]
176188
location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image"
177189
190+
[[registry]]
191+
location = "quay.io/openshift-release-dev/ocp-v4.0-art-dev"
192+
mirror-by-digest-only = true
193+
prefix = ""
194+
195+
[[registry.mirror]]
196+
location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image"
197+
`,
198+
},
199+
{
200+
name: "add-nodes command - missing-config",
201+
dependencies: []asset.Asset{
202+
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeAddNodes},
203+
&joiner.ClusterInfo{},
204+
&agent.OptionalInstallConfig{},
205+
&releaseimage.Image{},
206+
},
207+
expectedConfig: defaultRegistriesConf,
208+
},
209+
{
210+
name: "add-nodes command - valid image sources",
211+
dependencies: []asset.Asset{
212+
&workflow.AgentWorkflow{Workflow: workflow.AgentWorkflowTypeAddNodes},
213+
&joiner.ClusterInfo{
214+
ReleaseImage: "registry.ci.openshift.org/ocp/release:4.11.0-0.ci-2022-05-16-202609",
215+
ImageDigestSources: []types.ImageDigestSource{
216+
{
217+
Source: "registry.ci.openshift.org/ocp/release",
218+
Mirrors: []string{
219+
"virthost.ostest.test.metalkube.org:5000/localimages/local-release-image",
220+
},
221+
},
222+
{
223+
Source: "quay.io/openshift-release-dev/ocp-v4.0-art-dev",
224+
Mirrors: []string{
225+
"virthost.ostest.test.metalkube.org:5000/localimages/local-release-image",
226+
},
227+
},
228+
},
229+
},
230+
&agent.OptionalInstallConfig{},
231+
&releaseimage.Image{},
232+
},
233+
expectedConfig: `unqualified-search-registries = []
234+
235+
[[registry]]
236+
location = "registry.ci.openshift.org/ocp/release"
237+
mirror-by-digest-only = true
238+
prefix = ""
239+
240+
[[registry.mirror]]
241+
location = "virthost.ostest.test.metalkube.org:5000/localimages/local-release-image"
242+
178243
[[registry]]
179244
location = "quay.io/openshift-release-dev/ocp-v4.0-art-dev"
180245
mirror-by-digest-only = true

0 commit comments

Comments
 (0)