From ba7b8fafafb7b8af49143a4565a5887c4ece2066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Zeni?= Date: Wed, 18 Nov 2026 09:56:39 -0300 Subject: [PATCH 1/2] feat(pro): set the ubuntu-advantage charm channel, revision and config from the manifest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The deploy-pro plan only set the token on the ubuntu-advantage charm, so contract_url and the other charm options kept their defaults. The only way to change them was a manual `juju config`, which Sunbeam does not own and can lose on a later apply or redeploy. Register the ubuntu-advantage charm in the pro feature's default software config, with UBUNTU_ADVANTAGE_CHANNEL (latest/edge, the existing plan default) as its channel, and map its channel, revision and config to new or existing plan variables, as other features do: - ubuntu-advantage-channel (existing) - ubuntu-advantage-revision (new, default null) - ubuntu-advantage-config (new, map(string), default {}) Any charm option can now be set in the manifest, for example: features: pro: software: charms: ubuntu-advantage: config: contract_url: https://contracts.example.com The plan merges the manifest config under the token passed on the command line, so the CLI token always wins over a token key in the manifest. Backport notes: stable/2024.1 still deploys the ubuntu-advantage charm (the switch to ubuntu-pro is only on main), so the manifest key, tfvars and default channel use the ubuntu-advantage names. The new CHARM constant holds the charm name, which differs from the ubuntu-pro application name; refresh looks manifest entries up by charm name. The ubuntu-pro plan tests from main are not included. Jira: OPEN-4937 Assisted-By: Claude Code (Claude Opus 5.5) Related-Bug: #2174604 (cherry picked from commit 03ffd06da1e45ad4a229338fc5584bd69ca2dbae) Signed-off-by: Vítor Zeni --- .../features/pro/etc/deploy-pro/main.tf | 12 +-- .../features/pro/etc/deploy-pro/variables.tf | 12 +++ .../sunbeam/features/pro/feature.py | 24 ++++- .../tests/unit/sunbeam/features/test_pro.py | 87 ++++++++++++++++++- 4 files changed, 128 insertions(+), 7 deletions(-) diff --git a/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/main.tf b/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/main.tf index d962d8005..8c0163f05 100644 --- a/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/main.tf +++ b/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/main.tf @@ -22,14 +22,16 @@ resource "juju_application" "ubuntu_pro" { model_uuid = data.juju_model.machine_model.uuid charm { - name = "ubuntu-advantage" - channel = var.ubuntu-advantage-channel - base = "ubuntu@24.04" + name = "ubuntu-advantage" + channel = var.ubuntu-advantage-channel + revision = var.ubuntu-advantage-revision + base = "ubuntu@24.04" } - config = { + # The token from the CLI always wins over a token key in the manifest config. + config = merge(var.ubuntu-advantage-config, { token = var.token - } + }) } resource "juju_integration" "juju_info" { diff --git a/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/variables.tf b/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/variables.tf index 2cfea3cb5..5e3b1fe82 100644 --- a/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/variables.tf +++ b/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/variables.tf @@ -7,6 +7,18 @@ variable "ubuntu-advantage-channel" { default = "latest/edge" } +variable "ubuntu-advantage-revision" { + description = "Revision number of ubuntu-advantage charm" + type = number + default = null +} + +variable "ubuntu-advantage-config" { + description = "Config options of the ubuntu-advantage charm, such as contract_url" + type = map(string) + default = {} +} + variable "machine_model_uuid" { description = "UUID of Juju model to deploy ubuntu-pro into" type = string diff --git a/sunbeam-python/sunbeam/features/pro/feature.py b/sunbeam-python/sunbeam/features/pro/feature.py index f76398936..183f474c5 100644 --- a/sunbeam-python/sunbeam/features/pro/feature.py +++ b/sunbeam-python/sunbeam/features/pro/feature.py @@ -18,6 +18,7 @@ from sunbeam.core.deployment import Deployment from sunbeam.core.juju import JujuHelper, JujuStepHelper from sunbeam.core.manifest import ( + CharmManifest, FeatureConfig, Manifest, SoftwareConfig, @@ -39,6 +40,12 @@ console = Console() APPLICATION = "ubuntu-pro" +# The 2024.1 plan deploys the ubuntu-advantage charm as the ubuntu-pro +# application. Manifest charm keys are charm names, as refresh looks them up +# by the deployed charm name. +CHARM = "ubuntu-advantage" +# Matches the ubuntu-advantage-channel default in deploy-pro/variables.tf. +UBUNTU_ADVANTAGE_CHANNEL = "latest/edge" APP_TIMEOUT = 300 # 5 minutes, managing the application should be fast UNIT_TIMEOUT = 1200 # 15 minutes, adding / removing units can take a long time @@ -197,13 +204,28 @@ def manifest(self) -> Manifest: def default_software_overrides(self) -> SoftwareConfig: """Feature software configuration.""" return SoftwareConfig( + charms={CHARM: CharmManifest(channel=UBUNTU_ADVANTAGE_CHANNEL)}, terraform={ self.tfplan: TerraformManifest( source=Path(__file__).parent / "etc" / self.tfplan_dir ) - } + }, ) + def manifest_attributes_tfvar_map(self) -> dict: + """Manifest attributes terraformvars map.""" + return { + self.tfplan: { + "charms": { + CHARM: { + "channel": "ubuntu-advantage-channel", + "revision": "ubuntu-advantage-revision", + "config": "ubuntu-advantage-config", + } + } + } + } + def run_enable_plans( self, deployment: Deployment, config: ProFeatureConfig, show_hints: bool ): diff --git a/sunbeam-python/tests/unit/sunbeam/features/test_pro.py b/sunbeam-python/tests/unit/sunbeam/features/test_pro.py index c3d765f61..14c8a4f2c 100644 --- a/sunbeam-python/tests/unit/sunbeam/features/test_pro.py +++ b/sunbeam-python/tests/unit/sunbeam/features/test_pro.py @@ -1,11 +1,16 @@ # SPDX-FileCopyrightText: 2023 - Canonical Ltd # SPDX-License-Identifier: Apache-2.0 +import re +from pathlib import Path import pytest +import yaml from sunbeam.core.common import ResultType -from sunbeam.core.terraform import TerraformException +from sunbeam.core.manifest import FeatureManifest, Manifest +from sunbeam.core.terraform import TerraformException, TerraformHelper +from sunbeam.features.pro import feature as pro_feature from sunbeam.features.pro.feature import ( DisableUbuntuProApplicationStep, EnableUbuntuProApplicationStep, @@ -126,3 +131,83 @@ def test_disable_tf_apply_failed(self, disable_step, basic_tfhelper, step_contex basic_tfhelper.update_tfvars_and_apply_tf.assert_called_once() assert result.result_type == ResultType.FAILED assert result.message == "apply failed..." + + +class TestManifestCharmConfig: + """Charm options such as contract_url can be set via the manifest.""" + + _MANIFEST = """ +features: + pro: + software: + charms: + ubuntu-advantage: + revision: 46 + config: + contract_url: https://contracts.example.com +""" + + def _variables(self) -> str: + plan_dir = Path(pro_feature.__file__).parent / "etc" / "deploy-pro" + return (plan_dir / "variables.tf").read_text() + + def test_default_software_registers_charm(self): + feature = pro_feature.ProFeature() + charms = feature.default_software_overrides().charms + assert charms[pro_feature.CHARM].channel == ( + pro_feature.UBUNTU_ADVANTAGE_CHANNEL + ) + + def test_channel_default_matches_plan_variable(self): + match = re.search( + r'variable\s+"ubuntu-advantage-channel"\s*\{[^}]*?default\s*=\s*"([^"]+)"', + self._variables(), + re.DOTALL, + ) + assert match, "no default found for ubuntu-advantage-channel" + assert match.group(1) == pro_feature.UBUNTU_ADVANTAGE_CHANNEL + + def test_tfvar_map_matches_plan_variables(self): + feature = pro_feature.ProFeature() + tfvar_map = feature.manifest_attributes_tfvar_map()[feature.tfplan] + variables = self._variables() + for tfvar in tfvar_map["charms"][pro_feature.CHARM].values(): + assert f'variable "{tfvar}"' in variables, tfvar + + def test_manifest_config_becomes_tfvar(self, tmp_path): + feature = pro_feature.ProFeature() + default = Manifest( + features={ + feature.name: FeatureManifest( + software=feature.default_software_overrides() + ) + } + ) + override = Manifest.model_validate(yaml.safe_load(self._MANIFEST)) + override.validate_against_default(default) + tfhelper = TerraformHelper( + path=tmp_path, + plan=feature.tfplan, + tfvar_map=feature.manifest_attributes_tfvar_map()[feature.tfplan], + ) + + tfvars = tfhelper._get_tfvars(default.merge(override)) + + # A user charm entry replaces the default one, so the channel falls + # back to the variable default in variables.tf. + assert tfvars == { + "ubuntu-advantage-revision": 46, + "ubuntu-advantage-config": { + "contract_url": "https://contracts.example.com" + }, + } + + def test_cli_token_wins_over_manifest_config(self): + text = ( + Path(pro_feature.__file__).parent / "etc" / "deploy-pro" / "main.tf" + ).read_text() + assert re.search( + r"config\s*=\s*merge\(\s*var\.ubuntu-advantage-config\s*,\s*\{\s*" + r"token\s*=\s*var\.token", + text, + ) From 8a0d6ab9712b24ccab3029f39be176c91edc4363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Zeni?= Date: Wed, 18 Nov 2026 09:56:46 -0300 Subject: [PATCH 2/2] fix(pro): store the manifest passed to `sunbeam enable --manifest pro` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pro feature never stored a manifest given with --manifest. The file only applied to that one run, and a later `sunbeam enable pro ` read the latest manifest from clusterd, fell back to an empty charm config and dropped options such as contract_url again. Start the enable plan with AddManifestStep when --manifest is given, as the OpenStack features and telemetry already do. Like for those features, the file is stored as the whole latest manifest, so it should be a full manifest (e.g. from `sunbeam manifest show latest`) with the pro section added, not a pro-only file. Without --manifest the plan is unchanged. Jira: OPEN-4937 Assisted-By: Claude Code (Claude Opus 5.5) Closes-Bug: #2174604 Signed-off-by: Vítor Zeni (cherry picked from commit 63bfa2b2badbd267f6256ad971bf51f0e1072d98) --- .../sunbeam/features/pro/feature.py | 30 +++++++----- .../tests/unit/sunbeam/features/test_pro.py | 48 ++++++++++++++++++- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/sunbeam-python/sunbeam/features/pro/feature.py b/sunbeam-python/sunbeam/features/pro/feature.py index 183f474c5..0d172047f 100644 --- a/sunbeam-python/sunbeam/features/pro/feature.py +++ b/sunbeam-python/sunbeam/features/pro/feature.py @@ -18,6 +18,7 @@ from sunbeam.core.deployment import Deployment from sunbeam.core.juju import JujuHelper, JujuStepHelper from sunbeam.core.manifest import ( + AddManifestStep, CharmManifest, FeatureConfig, Manifest, @@ -232,17 +233,24 @@ def run_enable_plans( """Run the enablement plans.""" tfhelper = deployment.get_tfhelper(self.tfplan) jhelper = JujuHelper(deployment.juju_controller) - plan = [ - TerraformInitStep(tfhelper), - EnableUbuntuProApplicationStep( - deployment.get_client(), - tfhelper, - jhelper, - self.manifest, - config.token, - deployment.openstack_machines_model, - ), - ] + plan: list[BaseStep] = [] + # Store the manifest so later applies, e.g. a re-run of enable + # without --manifest, keep the charm config it sets. + if self.user_manifest: + plan.append(AddManifestStep(deployment.get_client(), self.user_manifest)) + plan.extend( + [ + TerraformInitStep(tfhelper), + EnableUbuntuProApplicationStep( + deployment.get_client(), + tfhelper, + jhelper, + self.manifest, + config.token, + deployment.openstack_machines_model, + ), + ] + ) run_plan(plan, console, show_hints) diff --git a/sunbeam-python/tests/unit/sunbeam/features/test_pro.py b/sunbeam-python/tests/unit/sunbeam/features/test_pro.py index 14c8a4f2c..e2117b9b1 100644 --- a/sunbeam-python/tests/unit/sunbeam/features/test_pro.py +++ b/sunbeam-python/tests/unit/sunbeam/features/test_pro.py @@ -3,13 +3,18 @@ import re from pathlib import Path +from unittest.mock import Mock import pytest import yaml from sunbeam.core.common import ResultType -from sunbeam.core.manifest import FeatureManifest, Manifest -from sunbeam.core.terraform import TerraformException, TerraformHelper +from sunbeam.core.manifest import AddManifestStep, FeatureManifest, Manifest +from sunbeam.core.terraform import ( + TerraformException, + TerraformHelper, + TerraformInitStep, +) from sunbeam.features.pro import feature as pro_feature from sunbeam.features.pro.feature import ( DisableUbuntuProApplicationStep, @@ -211,3 +216,42 @@ def test_cli_token_wins_over_manifest_config(self): r"token\s*=\s*var\.token", text, ) + + +class TestRunEnablePlans: + """enable --manifest must store the manifest so later applies keep it.""" + + @pytest.fixture + def run_plan(self, mocker): + mocker.patch.object(pro_feature, "JujuHelper") + mocker.patch.object(pro_feature.click, "echo") + return mocker.patch.object(pro_feature, "run_plan") + + def _run(self, user_manifest): + feature = pro_feature.ProFeature() + feature.user_manifest = user_manifest + feature._manifest = Mock() + feature.run_enable_plans( + Mock(), pro_feature.ProFeatureConfig(token="t"), show_hints=False + ) + + def test_user_manifest_is_stored_first(self, run_plan, tmp_path): + manifest_file = tmp_path / "manifest.yaml" + self._run(manifest_file) + + plan = run_plan.call_args.args[0] + assert [type(step) for step in plan] == [ + AddManifestStep, + TerraformInitStep, + EnableUbuntuProApplicationStep, + ] + assert plan[0].manifest_file == manifest_file + + def test_no_user_manifest_stores_nothing(self, run_plan): + self._run(None) + + plan = run_plan.call_args.args[0] + assert [type(step) for step in plan] == [ + TerraformInitStep, + EnableUbuntuProApplicationStep, + ]