kms: replace LegacyCodec with explicit json serializer#2200
kms: replace LegacyCodec with explicit json serializer#2200bertinatto wants to merge 1 commit intoopenshift:masterfrom
Conversation
WalkthroughReplaced use of ChangesJSON encoder selection and usage
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 10 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (10 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
[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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
| // 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 | ||
| } |
There was a problem hiding this comment.
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.
513bb6d to
1851a94
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
pkg/operator/encryption/encoding/encoding.go (1)
34-34: 💤 Low valueConsider pre-creating the encoder at init time.
The past review comment suggested caching the encoder (not just the serializer) at package initialization. Since
EncoderForVersiondoesn't return an error and the group version is known statically, this encoder could be created once ininit():var apiserverEncoder runtime.Encoder func init() { // ... existing code ... apiserverEncoder = codecs.EncoderForVersion(jsonSerializer, apiserverconfigv1.SchemeGroupVersion) }This is a minor optimization since
EncoderForVersionis lightweight, but it aligns with the caching pattern already established forjsonSerializer.🤖 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
📒 Files selected for processing (1)
pkg/operator/encryption/encoding/encoding.go
|
/retest |
|
@bertinatto: all tests passed! Full PR test history. Your PR dashboard. DetailsInstructions 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. |
Replace LegacyCodec (deprecated) with explicit JSON serializer.
/assign @ardaguclu @benluddy @p0lyn0mial
Summary by CodeRabbit