Skip to content

kms: replace LegacyCodec with explicit json serializer#2200

Open
bertinatto wants to merge 1 commit intoopenshift:masterfrom
bertinatto:kms-k8s-codecs-key-secret-follow-up-2
Open

kms: replace LegacyCodec with explicit json serializer#2200
bertinatto wants to merge 1 commit intoopenshift:masterfrom
bertinatto:kms-k8s-codecs-key-secret-follow-up-2

Conversation

@bertinatto
Copy link
Copy Markdown
Member

@bertinatto bertinatto commented May 5, 2026

Replace LegacyCodec (deprecated) with explicit JSON serializer.

/assign @ardaguclu @benluddy @p0lyn0mial

Summary by CodeRabbit

  • Refactor
    • Improved internal encryption encoding to make initialization and error handling more robust. This change tightens reliability of encryption-related operations by surfacing encoder initialization failures earlier and reducing silent fallbacks. No public APIs or behavior visible to end users were changed.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

Walkthrough

Replaced use of codecs.LegacyCodec(...) with JSON runtime serializer selection via runtime.SerializerInfoForMediaType(..., runtime.ContentTypeJSON) and codecs.EncoderForVersion(...). jsonSerializer is initialized at package init and encoder construction now introduces an explicit error/new failure path.

Changes

JSON encoder selection and usage

Layer / File(s) Summary
Serializer init
pkg/operator/encryption/encoding/encoding.go
During package init, resolve runtime.SerializerInfoForMediaType(..., runtime.ContentTypeJSON) and store jsonSerializer; panic if JSON is unsupported.
Encoder construction
pkg/operator/encryption/encoding/encoding.go
EncodeEncryptionConfiguration and EncodeKMSConfig construct encoders with codecs.EncoderForVersion(jsonSerializer, <SchemeGroupVersion>) instead of codecs.LegacyCodec(...).
Encode calls / error paths
pkg/operator/encryption/encoding/encoding.go
runtime.Encode is invoked with the newly-constructed JSON encoders; failures during encoder construction now surface as an explicit error path (or are propagated).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 10 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Single Node Openshift (Sno) Test Compatibility ❓ Inconclusive No result was produced after verification. Marking as INCONCLUSIVE. Re-run the check or adjust instructions to produce a final result.
✅ Passed checks (10 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main change: replacing deprecated LegacyCodec with an explicit JSON serializer in the KMS encryption configuration handling.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Stable And Deterministic Test Names ✅ Passed PR modifies only production code (encoding.go). No Ginkgo tests exist in repository or are added by this PR. Check not applicable.
Test Structure And Quality ✅ Passed PR modifies only pkg/operator/encryption/encoding/encoding.go (source code). The encoding package has no test files. Custom check is not applicable to this PR.
Microshift Test Compatibility ✅ Passed No new Ginkgo e2e tests were added in this PR. The only change is a backend refactoring to replace deprecated LegacyCodec with explicit JSON serializer in encoding.go.
Topology-Aware Scheduling Compatibility ✅ Passed PR modifies only codec/serialization logic in encoding.go. No deployment manifests, controllers, or scheduling constraints are introduced or modified.
Ote Binary Stdout Contract ✅ Passed No process-level stdout writes detected. Init() and package variables only initialize runtime objects. No fmt.Print*, klog, or logging violations. Panic() writes to stderr only.
Ipv6 And Disconnected Network Test Compatibility ✅ Passed No Ginkgo e2e tests were added in this PR. The e2e test files use standard Go testing.T framework, not Ginkgo, so the IPv6 and disconnected network compatibility check does not apply.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@openshift-ci openshift-ci Bot requested review from dgrisonnet and p0lyn0mial May 5, 2026 19:27
@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci Bot commented May 5, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bertinatto

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 5, 2026
Comment on lines +117 to +124
// jsonEncoder returns an encoder for the given GroupVersion.
func jsonEncoder(gv schema.GroupVersion) (runtime.Encoder, error) {
info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), runtime.ContentTypeJSON)
if !ok {
return nil, fmt.Errorf("json is not a supported media type")
}
return codecs.EncoderForVersion(info.Serializer, gv), nil
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we resolve the encoder once at package initialization time and save a codec from "CodecForVersions" that is reused within this package? This should never fail, so an initialization-time panic sounds preferable to runtime error handling.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

@bertinatto bertinatto force-pushed the kms-k8s-codecs-key-secret-follow-up-2 branch from 513bb6d to 1851a94 Compare May 8, 2026 01:20
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/operator/encryption/encoding/encoding.go (1)

34-34: 💤 Low value

Consider pre-creating the encoder at init time.

The past review comment suggested caching the encoder (not just the serializer) at package initialization. Since EncoderForVersion doesn't return an error and the group version is known statically, this encoder could be created once in init():

var apiserverEncoder runtime.Encoder

func init() {
    // ... existing code ...
    apiserverEncoder = codecs.EncoderForVersion(jsonSerializer, apiserverconfigv1.SchemeGroupVersion)
}

This is a minor optimization since EncoderForVersion is lightweight, but it aligns with the caching pattern already established for jsonSerializer.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/operator/encryption/encoding/encoding.go` at line 34, Create and cache
the apiserver encoder at package init time instead of calling
codecs.EncoderForVersion inline; add a package-level variable apiserverEncoder
runtime.Encoder and set it inside init() using apiserverEncoder =
codecs.EncoderForVersion(jsonSerializer, apiserverconfigv1.SchemeGroupVersion),
then replace the inline call (the local use of EncoderForVersion in encoding.go)
with the cached apiserverEncoder so callers use the pre-created encoder.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@pkg/operator/encryption/encoding/encoding.go`:
- Line 34: Create and cache the apiserver encoder at package init time instead
of calling codecs.EncoderForVersion inline; add a package-level variable
apiserverEncoder runtime.Encoder and set it inside init() using apiserverEncoder
= codecs.EncoderForVersion(jsonSerializer,
apiserverconfigv1.SchemeGroupVersion), then replace the inline call (the local
use of EncoderForVersion in encoding.go) with the cached apiserverEncoder so
callers use the pre-created encoder.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: d63ac429-1dde-485d-8987-d25bbb0baae1

📥 Commits

Reviewing files that changed from the base of the PR and between 513bb6d and 1851a94.

📒 Files selected for processing (1)
  • pkg/operator/encryption/encoding/encoding.go

@bertinatto
Copy link
Copy Markdown
Member Author

/retest

@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci Bot commented May 8, 2026

@bertinatto: all tests passed!

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants