Skip to content

distro: add DNFSetReleaseVerVar to ImageConfig - #1465

Merged
achilleas-k merged 1 commit into
osbuild:mainfrom
mvo5:dnf-vars-declarative,2
Apr 29, 2025
Merged

achilleas-k merged 1 commit into
osbuild:mainfrom
mvo5:dnf-vars-declarative,2

Conversation

@mvo5

@mvo5 mvo5 commented Apr 25, 2025 •

Copy link
Copy Markdown
Contributor

This commit adds a new DNFSetReleaseVerVar to
ImageConfig and a new `DNFConfigOptions() helper.

The rational is that DNFConfigStageOptions current gets the osVersion passed in directly. When we move to a YAML based ImageConfig this will be problematic as we do not have access to variables like this. By making this new declarative option we sidestep the problem.

This option is used in the rhel sap images.

Its slightly unfortunate that we need to change two places (images.go in rhel/fedora) to use the new
helper (pr#1387 would fix this) but with more YAML work eventually this should converge.

Thanks to thozza for suggesting this simplified approach.

JIRA: HMS-5284

@mvo5
mvo5 requested a review from thozza April 25, 2025 07:29
@mvo5
mvo5 requested a review from a team as a code owner April 25, 2025 07:29
@mvo5
mvo5 requested review from achilleas-k and ondrejbudai April 25, 2025 07:29
@achilleas-k

Copy link
Copy Markdown
Member

When we move to a YAML based ImageConfig this will be problematic as we do not have access to variables like this.

Well, now you got me thinking about having pre-defined variables in our image definition yaml files. But no, let's not go down that path just yet please :)

achilleas-k
achilleas-k previously approved these changes Apr 28, 2025

@achilleas-k achilleas-k left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks!

@thozza thozza left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for this new version. I like it a lot more. I've added two comments. The question about not exporting the DNF config-related options in ImageConfig is probably something for the future, but the other comment is something that I'd like to resolve before we merge this PR.

Comment on lines +60 to +62
// Do not use DNFConfig directly, call "DNFConfigOptions()"
DNFConfig []*osbuild.DNFConfigStageOptions
DNFSetReleaseVerVar *bool

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps we could make these non-exported and export only DNFConfigOptions(). This, however, would require a custom Unmarshaler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, I'm inclined to do this in a followup.

Comment on lines +151 to +178
func (c *ImageConfig) DNFConfigOptions(osVersion string) []*osbuild.DNFConfigStageOptions {
if c.DNFSetReleaseVerVar == nil || !*c.DNFSetReleaseVerVar {
return c.DNFConfig
}

return append(c.DNFConfig, osbuild.NewDNFConfigStageOptions(
[]osbuild.DNFVariable{
{
Name: "releasever",
Value: osVersion,
},
},
nil,
))
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have two comments:

  1. The result of this change is that two DNF config stages may be added to the manifest, instead of a single one. Probably not a big deal, but it is a behavior change.
  2. I'm not sure how pedantic we should be, because if two DNF config stages are added to the manifest, the one not created from the newly introduced bool option can also define the releasever variable. Then we will end up with one overriding the other. Would it be worth it to handle such a situation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, the old behavior is to just override any existing DNFConfig and because we do not do a deep merge inheriting also resulting silent overriding.

If we want to be as close as possible to the original behavior we could do:

diff --git a/pkg/distro/image_config.go b/pkg/distro/image_config.go
index 39023874c..6fc0c46c7 100644
--- a/pkg/distro/image_config.go
+++ b/pkg/distro/image_config.go
@@ -153,15 +153,22 @@ func (c *ImageConfig) DNFConfigOptions(osVersi
on string) []*osbuild.DNFConfigSta
 		return c.DNFConfig
 	}
 
-	return append(c.DNFConfig, osbuild.NewDNFConfigStageOptions(
-		[]osbuild.DNFVariable{
-			{
-				Name:  "releasever",
-				Value: osVersion,
+	// YAGNI: support both DNFConfig and DNFSetReleaseVerVar onc
e we
+	// have a use-case
+	if c.DNFConfig != nil {
+		panic("internal error: DNFConfig and DNFSetReleaseVe
rVar cannot be used together")
+	}
+	return []*osbuild.DNFConfigStageOptions{
+		osbuild.NewDNFConfigStageOptions(
+			[]osbuild.DNFVariable{
+				{
+					Name:  "releasever",
+					Value: osVersion,
+				},
 			},
-		},
-		nil,
-	))
+			nil,
+		),
+	}
 }
 
 func (c *ImageConfig) WSLConfStageOptions() *osbuild.WSLConfStageOp
tions {
diff --git a/pkg/distro/image_config_test.go b/pkg/distro/image_conf
ig_test.go
index ad63570c6..a439df96f 100644
--- a/pkg/distro/image_config_test.go
+++ b/pkg/distro/image_config_test.go
@@ -190,14 +190,8 @@ func TestImageConfigDNFConfigOptionsPreExisting
(t *testing.T) {
 	}
 	assert.Equal(t, cnf.DNFConfig, cnf.DNFConfigOptions("9-strea
m"))
 
-	expected := append(cnf.DNFConfig, &osbuild.DNFConfigStageOpt
ions{
-		Variables: []osbuild.DNFVariable{
-			{
-				Name:  "releasever",
-				Value: "9-stream",
-			},
-		},
-	})
 	cnf.DNFSetReleaseVerVar = common.ToPtr(true)
-	assert.Equal(t, expected, cnf.DNFConfigOptions("9-stream"))
+	assert.Panics(t, func() {
+		cnf.DNFConfigOptions("9-stream")
+	})
 }

(the upside is that it becomes very clear when we need to handle this case which is not a bad property).

As for (2) - that is a good question. My strawman would be that we just leave it to the user for now, if they want to manually create a releasever that conflicts with the one we automatically set they can keep the pieces. If mean, if we expose all of the DNFConfig options they have the freedom to break things, if we don't want this, we probably need an approach close to the original PR that restricts the settings. Maybe we could add a comment to the code saying something like // users could create DNFConfig stages with conflicting releasever values maually, for now we ignore this failure scenario as there are more things a user can do that might break the DNFConfig stages (or something along these lines).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right, the old behavior is to just override any existing DNFConfig and because we do not do a deep merge inheriting also resulting silent overriding.

I'm not sure what do you mean by silent overriding. ImageConfig can only inherit unset values from another ImageConfig instance. The behavior is very much intentional, because doing a deep merge would become a nightmare to get right for any reasonably complex stage options (especially if we want to support "unseting" values).

Nevertheless, you brought up an important case that I didn't consider before - ImageConfig inheritance.

The desire is for ImageConfig on lower level (i.e. distro-level vs. image-type-level) not to inherit values that are set on the lower level. However, by splitting the DNF config into two ImageConfig properties, one needs to set both values in the lower-level ImageConfig. I'm wondering if it wouldn't be better to nest all "DNF config options" (meaning the stage options + the new bool option) under a common struct. In this particular case, the use case for it is probably not that prominent, but we should keep this in mind to keep the ImageConfig inheritance model sane at least for the time being.

If we want to be as close as possible to the original behavior we could do:

diff --git a/pkg/distro/image_config.go b/pkg/distro/image_config.go
index 39023874c..6fc0c46c7 100644
--- a/pkg/distro/image_config.go
+++ b/pkg/distro/image_config.go
@@ -153,15 +153,22 @@ func (c *ImageConfig) DNFConfigOptions(osVersi
on string) []*osbuild.DNFConfigSta
 		return c.DNFConfig
 	}
 
-	return append(c.DNFConfig, osbuild.NewDNFConfigStageOptions(
-		[]osbuild.DNFVariable{
-			{
-				Name:  "releasever",
-				Value: osVersion,
+	// YAGNI: support both DNFConfig and DNFSetReleaseVerVar onc
e we
+	// have a use-case
+	if c.DNFConfig != nil {
+		panic("internal error: DNFConfig and DNFSetReleaseVe
rVar cannot be used together")
+	}
+	return []*osbuild.DNFConfigStageOptions{
+		osbuild.NewDNFConfigStageOptions(
+			[]osbuild.DNFVariable{
+				{
+					Name:  "releasever",
+					Value: osVersion,
+				},
 			},
-		},
-		nil,
-	))
+			nil,
+		),
+	}
 }
 
 func (c *ImageConfig) WSLConfStageOptions() *osbuild.WSLConfStageOp
tions {
diff --git a/pkg/distro/image_config_test.go b/pkg/distro/image_conf
ig_test.go
index ad63570c6..a439df96f 100644
--- a/pkg/distro/image_config_test.go
+++ b/pkg/distro/image_config_test.go
@@ -190,14 +190,8 @@ func TestImageConfigDNFConfigOptionsPreExisting
(t *testing.T) {
 	}
 	assert.Equal(t, cnf.DNFConfig, cnf.DNFConfigOptions("9-strea
m"))
 
-	expected := append(cnf.DNFConfig, &osbuild.DNFConfigStageOpt
ions{
-		Variables: []osbuild.DNFVariable{
-			{
-				Name:  "releasever",
-				Value: "9-stream",
-			},
-		},
-	})
 	cnf.DNFSetReleaseVerVar = common.ToPtr(true)
-	assert.Equal(t, expected, cnf.DNFConfigOptions("9-stream"))
+	assert.Panics(t, func() {
+		cnf.DNFConfigOptions("9-stream")
+	})
 }

(the upside is that it becomes very clear when we need to handle this case which is not a bad property).

As for (2) - that is a good question. My strawman would be that we just leave it to the user for now, if they want to manually create a releasever that conflicts with the one we automatically set they can keep the pieces. If mean, if we expose all of the DNFConfig options they have the freedom to break things, if we don't want this, we probably need an approach close to the original PR that restricts the settings. Maybe we could add a comment to the code saying something like // users could create DNFConfig stages with conflicting releasever values maually, for now we ignore this failure scenario as there are more things a user can do that might break the DNFConfig stages (or something along these lines).

For now, the user in this context is someone defining an image type or a distribution. If we ever expose the defining DNF configuration as a BP customization, it should override any settings set in the ImageConfig.

That said, I'm mostly worried about our image definitions silently overriding configuration values in the resulting image. So from that point of view, we should ideally catch such issues at compile time or at startup (when reading the definition from the YAML). Your suggestion is OK from my PoV. An alternative would be to check for the existence of the releasever variable in the config when DNFSetReleaseVerVar is set. I'm fine with deferring this to the time when we will need to set both, the DNFSetReleaseVerVar value as well as some other DNF config values.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Grouping them into a struct makes a lot of sense. 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, the old behavior is to just override any existing DNFConfig and because we do not do a deep merge inheriting also resulting silent overriding.

I'm not sure what do you mean by silent overriding. ImageConfig can only inherit unset values from another ImageConfig instance. The behavior is very much intentional, because doing a deep merge would become a nightmare to get right for any reasonably complex stage options (especially if we want to support "unseting" values).
[..]

Sorry, I agree with the exiting merging behavior of imageConfig. I just wanted to mention that it can be surprising in this particular context: if we would create a gce-sap image today just lke we have azure-sap and ec2-sap we would end up with something like:
gce-sap.diff.txt, the relevant part:

+func mkGCESapImageType(osVersion string) *rhel.ImageType {
...
+       it.DefaultImageConfig = sapImageConfig(osVersion).InheritFrom(baseGCEImageConfig())
...

but that would not include the baseGCEImageConfig.DNFConfig that sets IPResolve: 4 as the sap image config overrides this (silently) with the DNFConfig to set the releasever. (just did this diff to illustrate what I meant with the inheriting/silent override comment).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@thozza Fwiw, I updated the code so that it errors when both a manual DNFConfig is set and DNFSetReleaseVerVar is requested.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks.

Can you please group the two DNF config options in a single struct in a follow-up?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, just to double check that I got the suggestion right, you mean group them like this into a single struct?

diff --git a/pkg/distro/image_config.go b/pkg/distro/image_config.go
index f09e3e734..5cdcd6a4a 100644
--- a/pkg/distro/image_config.go
+++ b/pkg/distro/image_config.go
@@ -58,8 +58,7 @@ type ImageConfig struct {
        PamLimitsConf []*osbuild.PamLimitsConfStageOptions
        Sysctld       []*osbuild.SysctldStageOptions
        // Do not use DNFConfig directly, call "DNFConfigOptions()"
-       DNFConfig           []*osbuild.DNFConfigStageOptions
-       DNFSetReleaseVerVar *bool
+       DNFConfig           *DNFConfig
        SshdConfig          *osbuild.SshdConfigStageOptions
        Authconfig          *osbuild.AuthconfigStageOptions
        PwQuality           *osbuild.PwqualityConfStageOptions
@@ -119,6 +118,11 @@ type ImageConfig struct {
        MountUnits *bool
 }
 
+type DNFConfig struct {
+       Options             []*osbuild.DNFConfigStageOptions
+       DNFSetReleaseVerVar *bool
+}
+
 type WSLConfig struct {
        BootSystemd bool
 }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, exactly like this. Thank you.

This commit adds a new `DNFSetReleaseVerVar` to
`ImageConfig` and a new `DNFConfigOptions() helper.

The rational is that `DNFConfigStageOptions` current gets
the `osVersion` passed in directly. When we move to a
YAML based ImageConfig this will be problematic as we
do not have access to variables like this. By making this
new declarative option we sidestep the problem.

This option is used in the rhel sap images.

Its slightly unfortunate that we need to change two
places (images.go in rhel/fedora) to use the new
helper (pr#1387 would fix this) but with more YAML
work eventually this should converge.

Thanks to thozza for suggesting this simplified approach.

@thozza thozza left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you.

@achilleas-k
achilleas-k added this pull request to the merge queue Apr 29, 2025
Merged via the queue into osbuild:main with commit 71a1889 Apr 29, 2025
achilleas-k added a commit to achilleas-k/osbuild-composer that referenced this pull request Apr 29, 2025
tag v0.140.0
Tagger: imagebuilder-bot <[email protected]>

Changes with 0.140.0

----------------
  * Loader part override followups (osbuild/image-builder#1466)
    * Author: Michael Vogt, Reviewers: Tomáš Hozza
  * RHEL-10/gce: install cloud-utils-growpart by default (osbuild/image-builder#1463)
    * Author: Tomáš Hozza, Reviewers: Michael Vogt, Simon de Vlieger
  * base-host-check: expand to cover more (COMPOSER-2498) (osbuild/image-builder#1432)
    * Author: Simon de Vlieger, Reviewers: Tomáš Hozza
  * rhel8,9: move partitioning into YAML (osbuild/image-builder#1390)
    * Author: Michael Vogt, Reviewers: Tomáš Hozza

— Somewhere on the Internet, 2025-04-28

---

tag v0.141.0
Tagger: imagebuilder-bot <[email protected]>

Changes with 0.141.0

----------------
  * Shrink the current installer images a bit (osbuild/image-builder#1423)
    * Author: Brian C. Lane, Reviewers: Achilleas Koutsou, Simon de Vlieger, Tomáš Hozza
  * Update EL10 Azure images with changes made in RHEL 9.6 (osbuild/image-builder#1472)
    * Author: Achilleas Koutsou, Reviewers: Michael Vogt, Tomáš Hozza
  * distro: add DNFSetReleaseVerVar to ImageConfig (osbuild/image-builder#1465)
    * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Tomáš Hozza
  * test/base-host-check.sh: fix disabled firewall services check (osbuild/image-builder#1471)
    * Author: Sanne Raymaekers, Reviewers: Achilleas Koutsou, Tomáš Hozza

— Somewhere on the Internet, 2025-04-29

---
thozza pushed a commit to osbuild/osbuild-composer that referenced this pull request Apr 29, 2025
tag v0.140.0
Tagger: imagebuilder-bot <[email protected]>

Changes with 0.140.0

----------------
  * Loader part override followups (osbuild/image-builder#1466)
    * Author: Michael Vogt, Reviewers: Tomáš Hozza
  * RHEL-10/gce: install cloud-utils-growpart by default (osbuild/image-builder#1463)
    * Author: Tomáš Hozza, Reviewers: Michael Vogt, Simon de Vlieger
  * base-host-check: expand to cover more (COMPOSER-2498) (osbuild/image-builder#1432)
    * Author: Simon de Vlieger, Reviewers: Tomáš Hozza
  * rhel8,9: move partitioning into YAML (osbuild/image-builder#1390)
    * Author: Michael Vogt, Reviewers: Tomáš Hozza

— Somewhere on the Internet, 2025-04-28

---

tag v0.141.0
Tagger: imagebuilder-bot <[email protected]>

Changes with 0.141.0

----------------
  * Shrink the current installer images a bit (osbuild/image-builder#1423)
    * Author: Brian C. Lane, Reviewers: Achilleas Koutsou, Simon de Vlieger, Tomáš Hozza
  * Update EL10 Azure images with changes made in RHEL 9.6 (osbuild/image-builder#1472)
    * Author: Achilleas Koutsou, Reviewers: Michael Vogt, Tomáš Hozza
  * distro: add DNFSetReleaseVerVar to ImageConfig (osbuild/image-builder#1465)
    * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Tomáš Hozza
  * test/base-host-check.sh: fix disabled firewall services check (osbuild/image-builder#1471)
    * Author: Sanne Raymaekers, Reviewers: Achilleas Koutsou, Tomáš Hozza

— Somewhere on the Internet, 2025-04-29

---
mvo5 added a commit to mvo5/images that referenced this pull request Jun 5, 2025
This commit creates a new 'ImageConfig.DNFConfig` struct that
contains the `Options` and the `SetReleaseVerVar` options
in a single struct.

This is a followup for
osbuild#1465 (comment)

Thanks to Tomáš for the suggestion.
github-merge-queue Bot pushed a commit that referenced this pull request Jun 5, 2025
This commit creates a new 'ImageConfig.DNFConfig` struct that
contains the `Options` and the `SetReleaseVerVar` options
in a single struct.

This is a followup for
#1465 (comment)

Thanks to Tomáš for the suggestion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants