Repository navigation
Apply the modernize embedlit fixes - #9441
cert-manager-prow[bot] merged 2 commits into
Conversation
Go 1.27 allows omitting embedded field types from struct literals. Re-enable golangci-lint's modernize/embedlit check, which was disabled in cert-manager#9440, and apply its fixes with make fix-golangci-lint. Remove the imports that became unused as a result. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Felix Phipps <[email protected]>
|
/kind cleanup |
wallrj
left a comment
There was a problem hiding this comment.
Thank you. I checked the rewrite for behaviour changes and found none. The fixer only promotes a field when the outer type resolves it to the same object (isEmbeddedFieldLit in x/tools v0.50.0), every removed comment is re-added verbatim, and no generated file is touched. CI is green.
Two things I would like changed before merge, and some formatting debris to clean up.
Please keep the option groups in cmd/controller/app/controller.go. Flattening ContextOptions removed the six struct names that told readers which options struct each field belongs to. That nesting carried information. A //nolint:modernize on that one literal, or a comment per group, would keep it.
Please reflow the two-pair lines. The fixer deletes text without reflowing, and gofmt accepts the result, so roughly 120 literals now read Namespace: "x", Name: "y", on one line while the neighbouring literal is one pair per line. In the apply.go and controller files it also breaks gofmt alignment for the rest of the literal. A single pass now is cheaper than fixing it piecemeal in later PRs. I have marked one example of each shape inline; the rest are the same pattern.
Other sites with the same shapes
Glued closing braces, no trailing comma:
pkg/controller/cainjector/indexers.golines 87, 122, 159pkg/controller/certificate-shim/ingresses/controller_test.golines 61, 75pkg/controller/certificate-shim/listenerset/controller_test.gointernal/controller/certificates/policies/checks_test.go(Labels: nil},)
Two pairs on one line, alignment broken:
internal/controller/{certificaterequests,certificates,issuers,orders}/apply.gopkg/controller/certificates/issuing/issuing_controller.go,keymanager_controller.go,readiness_controller.go,trigger_controller.go,pkg/controller/acmeorders/sync.gosecret_manager_test.go,secret_test.go,approver_test.go,keymanager_controller_test.go,readiness_controller_test.go,test/integration/*,test/e2e/*
[Claude Fable 5.1]
|
|
||
| ClusterIssuerAmbientCredentials: opts.ClusterIssuerAmbientCredentials, | ||
| IssuerAmbientCredentials: opts.IssuerAmbientCredentials, | ||
| ClusterResourceNamespace: opts.ClusterResourceNamespace, |
There was a problem hiding this comment.
This literal used to name the six options structs (ACMEOptions, SchedulerOptions, IssuerOptions, IngressShimOptions, CertificateOptions, ConfigOptions). Now blank lines are the only separator, and the compiler accepts a field in any block, so the grouping will drift. Someone adding an option has to open pkg/controller/context.go to find which struct it belongs to.
The spec allows omitting the embedded type but does not require it. Please keep the nested form here with //nolint:modernize // embedlit: the group names document which options struct each field belongs to, or add a comment per group.
There was a problem hiding this comment.
Done in 0f88562. The ContextOptions literal is back to the nested form from master, with //nolint:modernize // embedlit: the group names document which options struct each field belongs to above it. I checked that embedlit flags the literal without the comment and passes with it.
| Name: metaInfo.GetName(), | ||
| Namespace: metaInfo.GetNamespace(), | ||
| }}) | ||
| Namespace: metaInfo.GetNamespace()}) |
There was a problem hiding this comment.
The fixer left the closing braces glued to the last field with no trailing comma. Same at lines 122 and 159. One field per line with a trailing comma, please, so the next field added does not have to re-break the line.
There was a problem hiding this comment.
Fixed in 0f88562, at lines 87, 122 and 159. Each now has one field per line, a trailing comma, and }) on its own line.
| func serializeApply(req *cmapi.CertificateRequest) ([]byte, error) { | ||
| req = &cmapi.CertificateRequest{ | ||
| TypeMeta: metav1.TypeMeta{Kind: cmapi.CertificateRequestKind, APIVersion: cmapi.SchemeGroupVersion.Identifier()}, | ||
| Kind: cmapi.CertificateRequestKind, APIVersion: cmapi.SchemeGroupVersion.Identifier(), |
There was a problem hiding this comment.
Two problems with this literal.
Kind and APIVersion are promoted but ObjectMeta four lines down is still written out, so the reader has to know that TypeMeta is also embedded to see what is being serialised. serializeApplyStatus below promotes both, so the two functions no longer look alike. Either promote nothing here or make both read the same way.
The two pairs on one line also break gofmt alignment for the fields below. One key/value per line fixes both. Same in internal/controller/certificates/apply.go.
There was a problem hiding this comment.
Fixed in 0f88562. Both functions now put Kind and APIVersion on their own lines, with one aligned pair per line. The same change is in internal/controller/certificates/apply.go. serializeApply still writes out ObjectMeta: *req.ObjectMeta.DeepCopy(), because that's a copied value rather than a literal, so there's nothing to promote.
| ObjectMeta: metav1.ObjectMeta{Namespace: req.Namespace, Name: req.Name}, | ||
| Spec: cmapi.CertificateRequestSpec{}, | ||
| Status: *req.Status.DeepCopy(), | ||
| Kind: cmapi.CertificateRequestKind, APIVersion: cmapi.SchemeGroupVersion.Identifier(), |
There was a problem hiding this comment.
Same here: Kind: .., APIVersion: .., and Namespace: .., Name: .., on one line each, with Spec and Status aligned to each other but not to the lines above. One pair per line, please. Same shape in the certificates, issuers and orders apply.go files.
There was a problem hiding this comment.
Fixed in 0f88562, here and in the certificates, issuers and orders apply.go files.
|
|
||
| return internalcertificates.ApplyStatus(ctx, c.client, c.fieldManager, &cmapi.Certificate{ | ||
| ObjectMeta: metav1.ObjectMeta{Namespace: crt.Namespace, Name: crt.Name}, | ||
| Namespace: crt.Namespace, Name: crt.Name, |
There was a problem hiding this comment.
Namespace: crt.Namespace, Name: crt.Name, on one line next to a one-pair Status: line. The same shape is in keymanager_controller.go, readiness_controller.go, trigger_controller.go and acmeorders/sync.go. One pair per line, please.
There was a problem hiding this comment.
Fixed in 0f88562, here and in keymanager_controller.go, readiness_controller.go, trigger_controller.go and acmeorders/sync.go.
| Namespace: "namespace-1", Name: "ingress-1", | ||
| }}}, | ||
| existingKObjects: []runtime.Object{&networkingv1.Ingress{ | ||
| Namespace: "namespace-1", Name: "ingress-1"}}, |
There was a problem hiding this comment.
Two pairs plus glued }}. Adding a label or annotation to this fixture means re-breaking the line by hand. Same at line 75 and in listenerset/controller_test.go.
There was a problem hiding this comment.
Fixed in 0f88562, here, at line 75 and in listenerset/controller_test.go. The same goes for gateways/controller_test.go.
| "if Certificate and Secret exists, but the Secret contains no certificate or private key data, do nothing": { | ||
| cert: &cmapi.Certificate{ | ||
| ObjectMeta: metav1.ObjectMeta{Namespace: "test-namespace", Name: "test-name"}, | ||
| Namespace: "test-namespace", Name: "test-name", |
There was a problem hiding this comment.
This file now has about forty of these two-pair lines because the original ObjectMeta: metav1.ObjectMeta{Namespace: .., Name: ..}, was a one-liner. It is now the dominant style here and in secret_test.go, approver_test.go, the keymanager and readiness tests, and the integration and e2e suites, while hand-written neighbours are one pair per line. gofmt will not reflow these later, so a single pass now is the cheap moment.
There was a problem hiding this comment.
Done in 0f88562, in one pass across the PR. I used a small AST script rather than editing by hand.
- It reflowed 324 literals the fixer had reshaped across 40 files, including the integration and e2e suites. Each now has one field per line, a trailing comma and the closing brace on its own line.
- Lines that already had several fields on master and were only re-indented keep their layout, unless their literal was reflowed anyway. Examples are
CertificateName: "test", IssuerName: "ca-issuer", ...and the{Manager: ..., FieldsV1: ...}entries. - I checked each file's tokens are unchanged apart from commas.
| # literals. Remove this once the codebase has been migrated in a | ||
| # separate PR (golangci-lint run --enable-only modernize --fix). | ||
| - embedlit | ||
| staticcheck: |
There was a problem hiding this comment.
Removing the exclusion makes nesting a lint failure rather than a style choice. A contributor who writes IssuerSpec{IssuerConfig: cmapi.IssuerConfig{CA: ...}} because it mirrors the API docs will have to flatten or add //nolint:modernize. That is fine, but the PR description should say that flattening is now mandatory, so the policy is recorded somewhere searchable.
There was a problem hiding this comment.
I've added this to the PR description. One detail: embedlit only checks literals that aren't themselves a field's value (runEmbedLit in x/tools v0.50.0 skips edge.KeyValueExpr_Value). So Spec: cmapi.IssuerSpec{IssuerConfig: cmapi.IssuerConfig{...}} still passes; there are a few of those in the tree. The description uses &corev1.Secret{ObjectMeta: metav1.ObjectMeta{...}} as the example instead.
Address review comments: - Keep the nested options groups in the controller.ContextOptions literal, with a nolint:modernize comment, because the group names show which options struct each field belongs to. - Reflow the struct literals the embedlit fixer left with several fields on one line, or with the closing brace on the last field's line, so they have one field per line and a trailing comma. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Felix Phipps <[email protected]>
|
This PR was discussed in our weekly dev meeting today, and no one objected to merging it after a review. /approve |
wallrj
left a comment
There was a problem hiding this comment.
Thank you, all my comments are addressed in 0f88562.
I compared every one of the 41 reflowed Go files against the fixer output by AST with positions stripped. 40 are identical, so the reflow changed layout only. The one difference is cmd/controller/app/controller.go, which is now master plus the single //nolint:modernize line. The PR description now records that flattening is mandatory and what the check does not catch.
/lgtm
[Claude Fable 5.1]
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: erikgb, wallrj 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 |
The embedlit modernize check added by cert-manager#9441 rejects the explicit metav1.ObjectMeta literal. Match the promoted-field form used throughout the file. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: Richard Wall <[email protected]>
The embedlit modernize check added by cert-manager#9441 rejects the explicit metav1.ObjectMeta literal. Match the promoted-field form used throughout the file. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: Richard Wall <[email protected]>
The embedlit modernize check added by cert-manager#9441 rejects the explicit metav1.ObjectMeta literal. Match the promoted-field form used throughout the file. Co-Authored-By: Claude Fable 5 <[email protected]> Signed-off-by: Richard Wall <[email protected]>
Follow-up to #9440, as requested by @erikgb in #9440 (review).
Go 1.27 allows omitting embedded field types from struct literals, so
ObjectMeta: metav1.ObjectMeta{Name: "x"}can be written asName: "x". #9440 disabled golangci-lint'smodernize/embedlitcheck to keep that PR small. This PR re-enables it and applies its fixes.With the check enabled, flattening is required: a struct literal that names an embedded field's type, like
&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "x"}}, now fails lint. Write it without the embedded type, or add//nolint:modernizewith a reason where the nesting is worth keeping. The check only looks at literals that aren't themselves a field's value, soSpec: cmapi.IssuerSpec{IssuerConfig: cmapi.IssuerConfig{...}}still passes.How it was generated:
embedlitexclusion from.golangci.yamlmake VENDOR_GO=1 fix-golangci-lintmetav1), using thego veterrorsOne exception: the
controller.ContextOptionsliteral incmd/controller/app/controller.gokeeps its nestedACMEOptions,SchedulerOptions,IssuerOptions,IngressShimOptions,CertificateOptionsandConfigOptionsgroups, with a//nolint:modernizecomment, because the group names show which options struct each field belongs to.No behaviour change: the old and new literals are equivalent. Comments inside the rewritten literals are preserved. No generated files are touched.