kubeblocks icon indicating copy to clipboard operation
kubeblocks copied to clipboard

fix(configuration): reject immutable parameter updates (main port of #10204)

Open weicao opened this issue 6 months ago • 3 comments

Summary

Main-branch port of release-1.0 PR #10204. Rejects updates to parameters listed in ParametersDefinition.spec.immutableParameters, covering both update entry points:

  • Structured ParametersInFile.Parameters path: filterImmutableParameters returns an explicit error instead of silently filtering immutable keys.
  • Raw ParametersInFile.Content path: new validateImmutableContentChanges helper parses base and merged content via each file's FileFormatConfig, rejects add / remove / modify of any immutable parameter, and ignores formatting-only changes.

The two-entry-point scope matches PR #10204 on release-1.0 so the behavior stays consistent across main and release-1.0. (The earlier split into separate PRs #10237 / #10242 was unintentional and is now folded into this single PR; #10242 will be closed.)

Architectural placement (post #10254)

PR #10254 (merged 2026-05-19) restructured the validation flow so that ClassifyComponentParameters and DoMerge are now invoked inside the ComponentParameter controller (controllers/parameters/componentparameter_controller_utils.go:122-141), processing ComponentParameter.Spec.Desired. The error path is intctrlutil.NewErrorf(intctrlutil.ErrorTypeFatal, ...) → CMergeFailedPhase.

This PR lifts both immutable-parameter rejections along the same call chain:

ComponentParameter controller
  → ClassifyComponentParameters
    → ValidateComponentParameterAssignments (schema check, from #10254)
  → DoMerge
    → mergeUpdatedParams
      → validateImmutableContentChanges (this PR, Content path) → error → ErrorTypeFatal → CMergeFailedPhase
      → MergeAndValidateConfigs
        → filterImmutableParameters (this PR, Parameters path) → error → ErrorTypeFatal → CMergeFailedPhase

All three checks (schema, content-level immutable, parameter-level immutable) sit at the layer that owns ComponentParameter.Spec.Desired state. Any future writer of Spec.Desired (Parameter CR, OpsRequest reconfigure, direct edit) goes through the same gate.

Evidence

  • Observed scene: an immutable negative gate run had the OpsRequest reach Succeeded while ComponentParameter desired one value and the rendered pod configs stayed on the previous value. Reconfigure status reported "no config modified / skip reconfigure".
  • Static path matches the scene: filterImmutableParameters silently dropped immutable keys, producing a no-op patch that finished as Succeeded. The Content path had no immutable check at all, allowing the same bypass via raw file content.

What changed

  • pkg/parameters/config_util.go: filterImmutableParameters returns (map[string]any, error) and rejects any immutable key with fmt.Errorf("immutable parameter %s cannot be modified", key). The single caller in MergeAndValidateConfigs surfaces the error.
  • pkg/parameters/config_util_test.go: the rejection case now expects wantErr=true rather than a filtered map.
  • pkg/parameters/patch_merger.go: invoke validateImmutableContentChanges from mergeUpdatedParams after MergeUpdatedConfig.
  • pkg/parameters/patch_merger_test.go: 7 new cases covering modify / add / remove of immutable parameter on raw content, format-only changes (no false flag), mutable change with format noise, content on file without immutable candidate, and fail-safe when immutable declared but format missing.
  • pkg/unstructured/viper_wrap.go: support nil-safe access for viperWrap.SubConfig so the new check does not panic when a patch removes an INI sub-section entirely (Leon's review comment on the earlier #10242 split).

Helper contract for validateImmutableContentChanges

  • Only inspects files present in the current update set (no scan of unchanged files).
  • Skips files whose ParametersDefinition has no immutable-parameter contract, so benign content updates to ordinary files are not affected.
  • Parses base and merged content using each file's FileFormatConfig and compares the parsed parameter map. Format-only changes (whitespace, comment order, blank lines, key=value vs key = value spacing) are not flagged.
  • Rejects add, remove, and modify of any immutable parameter.
  • Fails safe when an immutable-parameter candidate is declared on the file but no parser is available (FileFormatConfig missing or parse error), so an unknown-format file cannot become a silent bypass.

Verification

  • go build ./pkg/parameters/... and ./pkg/unstructured/...
  • go test ./pkg/parameters -run 'Test_filterImmutableParameters|TestDoMerge' -count=1 -v (plain Go tests, 7 new content-path cases plus the existing structured-path case all PASS)
  • go test ./pkg/unstructured -count=1 PASS
  • git diff --check origin/main...HEAD clean
  • Commit attribution scan clean

Diff stats

5 files changed, +351 / -14.

Relationship to #10204 and #10242

  • PR #10204 (release-1.0, merged) is the equivalent release-1.0 PR. Same five files (under pre-#9846 paths), same two-entry-point scope.
  • PR #10242 (main, to be closed) contained the Content-path subset before this fold-up; its three commits are now part of this PR's history. Closing #10242 preserves the review trail; this PR is the authoritative landing target.

weicao avatar May 17 '26 09:05 weicao

Auto Cherry-pick Instructions

Usage:
  - /nopick: Not auto cherry-pick when PR merged.
  - /pick: release-x.x [release-x.x]: Auto cherry-pick to the specified branch when PR merged.

Example:
  - /nopick
  - /pick release-1.1

apecloud-bot avatar May 17 '26 09:05 apecloud-bot

Codecov Report

:x: Patch coverage is 70.90909% with 32 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 54.19%. Comparing base (c1eb0c6) to head (4ee70fd). :warning: Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
pkg/parameters/patch_merger.go 75.00% 8 Missing and 4 partials :warning:
pkg/parameters/config_util.go 78.43% 7 Missing and 4 partials :warning:
pkg/unstructured/viper_wrap.go 18.18% 8 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10237      +/-   ##
==========================================
+ Coverage   52.73%   54.19%   +1.46%     
==========================================
  Files         534      536       +2     
  Lines       61231    65017    +3786     
==========================================
+ Hits        32288    35238    +2950     
- Misses      25691    26291     +600     
- Partials     3252     3488     +236     
Flag Coverage Δ
unittests 54.19% <70.90%> (+1.46%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar May 17 '26 09:05 codecov[bot]

UnmanagedUpdates can still bypass the immutable-parameter guard. The new checks cover raw Content before MergeAndValidateConfigs and structured Parameters inside MergeAndValidateConfigs, but mergeUnmanagedUpdates runs afterwards and applies Set/Remove directly without checking immutableParameters. A user can still modify or remove an immutable key through ParametersInFile.UnmanagedUpdates / ComponentParameter.Spec.Desired.UnmanagedUpdates, for example setting gtid_mode in the same INI section. Please reject unmanaged updates that target immutable keys, or run the immutable diff check again after unmanaged updates are applied, and add a regression test for this path.

leon-ape avatar May 19 '26 13:05 leon-ape

Addressed Leon's unmanaged-updates comment in head f20eb22c5.

What changed:

  • UnmanagedUpdates now rejects any requested Set or Remove whose key is declared in ParametersDefinition.spec.immutableParameters for that file.
  • After unmanaged updates are applied, the same parsed immutable-parameter diff guard runs again on the touched files, so any content-level immutable delta is still rejected.
  • Added regression coverage for unmanaged Set and unmanaged Remove attempts against gtid_mode, and kept the existing allowed unmanaged update path with gtid_mode unchanged.

Validation:

go generate ./pkg/testutil/k8s/mocks/...
go test ./pkg/parameters -run 'TestDoMerge|Test_filterImmutableParameters' -count=1
go test ./pkg/unstructured -count=1
go build ./pkg/parameters/...
go build ./pkg/unstructured/...
git diff --check

weicao avatar Jun 02 '26 11:06 weicao

Addressed the new review comments in head 4ee70fda2.

Updates:

  • P1 contract/doc mismatch: changed the API wording from "ignored" to the new rejection semantics, and synchronized the checked-in CRD YAML plus generated API reference text. The documented failure surface now says immutable changes are rejected during configuration merge and surface as a merge failure.
  • P2 unmanaged section false positive: narrowed the direct unmanaged-update immutable key rejection to the managed INI section. A same-named key in another INI section is now allowed unless the post-merge parsed diff shows the managed immutable parameter changed.
  • Added a regression test for updating gtid_mode in [client] while [mysqld] declares gtid_mode immutable.

Validation:

go test ./pkg/parameters -run 'TestDoMerge|Test_filterImmutableParameters' -count=1
go test ./pkg/unstructured -count=1
go build ./pkg/parameters/...
go build ./pkg/unstructured/...
git diff --check

Generation note: make manifests was attempted, but local controller-gen v0.14.0 cannot build on this machine's Go 1.26.3 toolchain, and Go 1.25.4 produced the same golang.org/x/tools/internal/tokeninternal compile failure. I manually synchronized the exact source-comment wording into the checked-in CRD YAML and API reference files so the PR diff contains the intended generated-text change.

weicao avatar Jun 02 '26 11:06 weicao