Skip to content

Commit 7a0aab2

Browse files
committed
refactor: apply the Go 1.27 modernizers
Run go fix over every first-party module: interface{} becomes any, hand-written loops become slices, maps, min/max and range-over-int, errors.As becomes errors.AsType, pointer helpers become new(expr) and are inlined at their call sites, and so on. Every rewrite keeps behaviour. Three rewrites stay out. embedlit flattens embedded-field literals such as ObjectMeta, syntax only Go 1.27 accepts, which would break backports to release branches still on Go 1.26. omitzero drops omitempty from struct fields, and openapi-gen reads omitempty as optional, so metadata, spec and status would turn required in the published schemas. And errorsastype rewrote an errors.As whose target, apierrors.APIStatus, does not implement error, which errors.AsType rejects at compile time. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent db6c20f commit 7a0aab2

93 files changed

Lines changed: 649 additions & 852 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmd/check-readiness/main.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,11 @@ func (cfg *config) kindExists(kind string) bool {
350350
}
351351
name := kind
352352
group := ""
353-
if idx := strings.IndexByte(kind, '.'); idx >= 0 {
354-
name = kind[:idx]
355-
group = kind[idx+1:]
353+
if before, after, ok := strings.Cut(kind, "."); ok {
354+
name = before
355+
group = after
356356
}
357-
for _, line := range strings.Split(cfg.apiCache, "\n") {
357+
for line := range strings.SplitSeq(cfg.apiCache, "\n") {
358358
fields := strings.Fields(line)
359359
if len(fields) < 3 {
360360
continue
@@ -447,7 +447,7 @@ func (cfg *config) processRows(e entry, rows string, b *strings.Builder) {
447447
notReady := 0
448448
total := 0
449449

450-
for _, line := range strings.Split(rows, "\n") {
450+
for line := range strings.SplitSeq(rows, "\n") {
451451
if line == "" {
452452
continue
453453
}

‎cmd/cozypkg/cmd/add.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ func readPackagesFromYAMLFile(filePath string) ([]string, error) {
181181
var packages []string
182182

183183
// Split YAML documents (in case of multiple resources)
184-
documents := strings.Split(string(data), "---")
184+
documents := strings.SplitSeq(string(data), "---")
185185

186-
for _, doc := range documents {
186+
for doc := range documents {
187187
doc = strings.TrimSpace(doc)
188188
if doc == "" {
189189
continue
@@ -220,8 +220,8 @@ func readPackagesFromYAMLFile(filePath string) ([]string, error) {
220220
items, found, err := unstructured.NestedSlice(obj.Object, "items")
221221
if err == nil && found {
222222
for _, item := range items {
223-
if itemMap, ok := item.(map[string]interface{}); ok {
224-
if metadata, ok := itemMap["metadata"].(map[string]interface{}); ok {
223+
if itemMap, ok := item.(map[string]any); ok {
224+
if metadata, ok := itemMap["metadata"].(map[string]any); ok {
225225
if name, ok := metadata["name"].(string); ok && name != "" {
226226
packages = append(packages, name)
227227
}
@@ -357,9 +357,9 @@ func createPackageFromFile(ctx context.Context, k8sClient client.Client, filePat
357357
}
358358

359359
// Split YAML documents
360-
documents := strings.Split(string(data), "---")
360+
documents := strings.SplitSeq(string(data), "---")
361361

362-
for _, doc := range documents {
362+
for doc := range documents {
363363
doc = strings.TrimSpace(doc)
364364
if doc == "" {
365365
continue

‎cmd/cozypkg/cmd/dependencies.go‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"os"
23+
"slices"
2324
"strings"
2425

2526
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
@@ -205,13 +206,7 @@ func buildGraphFromCluster(ctx context.Context, kubeconfig string, packagesOnly
205206

206207
// Filter by selected packages if specified
207208
if len(selectedPackages) > 0 {
208-
found := false
209-
for _, selected := range selectedPackages {
210-
if psName == selected {
211-
found = true
212-
break
213-
}
214-
}
209+
found := slices.Contains(selectedPackages, psName)
215210
if !found {
216211
continue
217212
}
@@ -393,7 +388,7 @@ func buildGraphFromCluster(ctx context.Context, kubeconfig string, packagesOnly
393388
graph[componentName] = append(graph[componentName], psName)
394389
existingEdges[edgeKey] = true
395390
}
396-
391+
397392
// If component is not in all variants, store variant info for component->package edge
398393
componentAllVariants := componentVariants[componentName]
399394
if len(componentAllVariants) < len(allVariantNames) {

‎cmd/cozypkg/cmd/init.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func writeScaffold(dir string, files []scaffoldFile) error {
190190
// not the apiserver-rejected "Foo-bar".
191191
func capitalize(s string) string {
192192
var b strings.Builder
193-
for _, seg := range strings.Split(s, "-") {
193+
for seg := range strings.SplitSeq(s, "-") {
194194
if seg == "" {
195195
continue
196196
}

‎cmd/cozypkg/cmd/validate.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type Report struct {
7070
Findings []Finding
7171
}
7272

73-
func (r *Report) add(sev Severity, code, location, format string, args ...interface{}) {
73+
func (r *Report) add(sev Severity, code, location, format string, args ...any) {
7474
r.Findings = append(r.Findings, Finding{
7575
Severity: sev,
7676
Code: code,

‎cmd/cozystack-operator/main.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,8 @@ func parseRefSpec(refSpec string) (map[string]string, error) {
484484
return result, nil
485485
}
486486

487-
pairs := strings.Split(refSpec, ",")
488-
for _, pair := range pairs {
487+
pairs := strings.SplitSeq(refSpec, ",")
488+
for pair := range pairs {
489489
pair = strings.TrimSpace(pair)
490490
if pair == "" {
491491
continue

‎internal/backupcontroller/altinitystrategy_controller.go‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package backupcontroller
44
import (
55
"context"
66
"fmt"
7+
"maps"
78
"strings"
89
"time"
910

@@ -107,12 +108,12 @@ func backupParameters(b *backupsv1alpha1.Backup) map[string]string {
107108
// the release shorthand, the run mode, and the resolved parameters.
108109
func renderAltinityTemplate(
109110
tmpl corev1.PodTemplateSpec,
110-
app map[string]interface{},
111+
app map[string]any,
111112
releaseName, releaseNamespace, mode string,
112113
parameters map[string]string,
113114
backup *backupsv1alpha1.Backup,
114115
) (*corev1.PodTemplateSpec, error) {
115-
ctxMap := map[string]interface{}{
116+
ctxMap := map[string]any{
116117
"Application": app,
117118
"Release": map[string]string{
118119
"Name": releaseName,
@@ -134,7 +135,7 @@ func renderAltinityTemplate(
134135
if backup.Spec.ApplicationRef.APIGroup != nil {
135136
sourceAPIGroup = *backup.Spec.ApplicationRef.APIGroup
136137
}
137-
ctxMap["Backup"] = map[string]interface{}{
138+
ctxMap["Backup"] = map[string]any{
138139
"Name": backup.Name,
139140
"Namespace": backup.Namespace,
140141
"ApplicationRef": map[string]string{
@@ -294,9 +295,7 @@ func (r *BackupJobReconciler) ensureAltinityJob(
294295
}
295296

296297
labels := map[string]string{altinityLabelMode: mode}
297-
for k, v := range ownerLabels {
298-
labels[k] = v
299-
}
298+
maps.Copy(labels, ownerLabels)
300299

301300
desired := buildAltinityBatchJob(namespace, name, labels, rendered)
302301
if err := controllerutil.SetControllerReference(owner, desired, r.Scheme); err != nil {
@@ -337,9 +336,7 @@ func (r *RestoreJobReconciler) ensureAltinityRestoreJob(
337336
}
338337

339338
labels := map[string]string{altinityLabelMode: mode}
340-
for k, v := range ownerLabels {
341-
labels[k] = v
342-
}
339+
maps.Copy(labels, ownerLabels)
343340

344341
desired := buildAltinityBatchJob(namespace, name, labels, rendered)
345342
if err := controllerutil.SetControllerReference(owner, desired, r.Scheme); err != nil {
@@ -369,9 +366,7 @@ func buildAltinityBatchJob(namespace, name string, labels map[string]string, ren
369366
if pod.Labels == nil {
370367
pod.Labels = map[string]string{}
371368
}
372-
for k, v := range labels {
373-
pod.Labels[k] = v
374-
}
369+
maps.Copy(pod.Labels, labels)
375370
backoffLimit := int32(2)
376371
return &batchv1.Job{
377372
ObjectMeta: metav1.ObjectMeta{
@@ -478,7 +473,7 @@ func (r *BackupJobReconciler) createAltinityBackupArtifact(
478473
// default ("apps.cozystack.io"); RESTMapping must apply the same default so
479474
// a BackupJob/RestoreJob with apiGroup omitted does not blow up at lookup
480475
// time with a misleading "no matches for /ClickHouse" error.
481-
func (r *BackupJobReconciler) getApplicationUnstructured(ctx context.Context, namespace string, ref corev1.TypedLocalObjectReference) (map[string]interface{}, error) {
476+
func (r *BackupJobReconciler) getApplicationUnstructured(ctx context.Context, namespace string, ref corev1.TypedLocalObjectReference) (map[string]any, error) {
482477
group := backupsv1alpha1.DefaultApplicationAPIGroup
483478
if ref.APIGroup != nil && *ref.APIGroup != "" {
484479
group = *ref.APIGroup
@@ -500,7 +495,7 @@ func (r *BackupJobReconciler) getApplicationUnstructured(ctx context.Context, na
500495

501496
// getApplicationUnstructured is the RestoreJob-side mirror. Same APIGroup
502497
// default applies (see the BackupJob variant for the rationale).
503-
func (r *RestoreJobReconciler) getApplicationUnstructured(ctx context.Context, namespace string, ref corev1.TypedLocalObjectReference) (map[string]interface{}, error) {
498+
func (r *RestoreJobReconciler) getApplicationUnstructured(ctx context.Context, namespace string, ref corev1.TypedLocalObjectReference) (map[string]any, error) {
504499
group := backupsv1alpha1.DefaultApplicationAPIGroup
505500
if ref.APIGroup != nil && *ref.APIGroup != "" {
506501
group = *ref.APIGroup
@@ -566,7 +561,7 @@ func (r *RestoreJobReconciler) reconcileAltinityRestore(ctx context.Context, res
566561
}
567562
}
568563
targetRef := corev1.TypedLocalObjectReference{
569-
APIGroup: stringPtr(targetAPIGroup),
564+
APIGroup: new(targetAPIGroup),
570565
Kind: targetAppKind,
571566
Name: targetAppName,
572567
}

‎internal/backupcontroller/altinitystrategy_controller_test.go‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ func newAltinityTestEnv(t *testing.T, app *unstructured.Unstructured, builder *c
7070
Build()
7171

7272
return &BackupJobReconciler{
73-
Client: c,
74-
Interface: dynamicClient,
75-
RESTMapper: restMapper,
76-
Scheme: testScheme,
77-
Recorder: record.NewFakeRecorder(10),
78-
}, &RestoreJobReconciler{
79-
Client: c,
80-
Interface: dynamicClient,
81-
RESTMapper: restMapper,
82-
Scheme: testScheme,
83-
Recorder: record.NewFakeRecorder(10),
84-
}, testScheme
73+
Client: c,
74+
Interface: dynamicClient,
75+
RESTMapper: restMapper,
76+
Scheme: testScheme,
77+
Recorder: record.NewFakeRecorder(10),
78+
}, &RestoreJobReconciler{
79+
Client: c,
80+
Interface: dynamicClient,
81+
RESTMapper: restMapper,
82+
Scheme: testScheme,
83+
Recorder: record.NewFakeRecorder(10),
84+
}, testScheme
8585
}
8686

8787
func newAltinityStrategy(name string) *strategyv1alpha1.Altinity {
@@ -108,7 +108,7 @@ func newAltinityStrategy(name string) *strategyv1alpha1.Altinity {
108108

109109
func newAltinityClickHouseRef(name string) corev1.TypedLocalObjectReference {
110110
return corev1.TypedLocalObjectReference{
111-
APIGroup: stringPtr(backupsv1alpha1.DefaultApplicationAPIGroup),
111+
APIGroup: new(backupsv1alpha1.DefaultApplicationAPIGroup),
112112
Kind: "ClickHouse",
113113
Name: name,
114114
}
@@ -128,7 +128,7 @@ func TestReconcileAltinity_CreatesBatchJob(t *testing.T) {
128128

129129
resolved := &ResolvedBackupConfig{
130130
StrategyRef: corev1.TypedLocalObjectReference{
131-
APIGroup: stringPtr(strategyv1alpha1.GroupVersion.Group),
131+
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
132132
Kind: strategyv1alpha1.AltinityStrategyKind,
133133
Name: "clickhouse-strategy",
134134
},
@@ -224,7 +224,7 @@ func TestReconcileAltinity_CompletesAndCreatesBackup(t *testing.T) {
224224

225225
resolved := &ResolvedBackupConfig{
226226
StrategyRef: corev1.TypedLocalObjectReference{
227-
APIGroup: stringPtr(strategyv1alpha1.GroupVersion.Group),
227+
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
228228
Kind: strategyv1alpha1.AltinityStrategyKind,
229229
Name: "clickhouse-strategy",
230230
},
@@ -304,7 +304,7 @@ func TestReconcileAltinity_FailsOnJobFailed(t *testing.T) {
304304

305305
resolved := &ResolvedBackupConfig{
306306
StrategyRef: corev1.TypedLocalObjectReference{
307-
APIGroup: stringPtr(strategyv1alpha1.GroupVersion.Group),
307+
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
308308
Kind: strategyv1alpha1.AltinityStrategyKind,
309309
Name: "clickhouse-strategy",
310310
},
@@ -357,7 +357,7 @@ func TestReconcileAltinityRestore_CreatesBatchJobInTargetNamespace(t *testing.T)
357357
Spec: backupsv1alpha1.BackupSpec{
358358
ApplicationRef: newAltinityClickHouseRef("ch-test"),
359359
StrategyRef: corev1.TypedLocalObjectReference{
360-
APIGroup: stringPtr(strategyv1alpha1.GroupVersion.Group),
360+
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
361361
Kind: strategyv1alpha1.AltinityStrategyKind,
362362
Name: "clickhouse-strategy",
363363
},
@@ -370,7 +370,7 @@ func TestReconcileAltinityRestore_CreatesBatchJobInTargetNamespace(t *testing.T)
370370
Spec: backupsv1alpha1.RestoreJobSpec{
371371
BackupRef: corev1.LocalObjectReference{Name: backup.Name},
372372
TargetApplicationRef: &corev1.TypedLocalObjectReference{
373-
APIGroup: stringPtr(backupsv1alpha1.DefaultApplicationAPIGroup),
373+
APIGroup: new(backupsv1alpha1.DefaultApplicationAPIGroup),
374374
Kind: "ClickHouse",
375375
Name: "ch-restore",
376376
},
@@ -427,7 +427,7 @@ func TestReconcileAltinity_RenderedBackupJobOwnedByBackupJob(t *testing.T) {
427427
}
428428
resolved := &ResolvedBackupConfig{
429429
StrategyRef: corev1.TypedLocalObjectReference{
430-
APIGroup: stringPtr(strategyv1alpha1.GroupVersion.Group),
430+
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
431431
Kind: strategyv1alpha1.AltinityStrategyKind,
432432
Name: "clickhouse-strategy",
433433
},
@@ -474,7 +474,7 @@ func TestReconcileAltinityRestore_RenderedRestoreJobOwnedByRestoreJob(t *testing
474474
Spec: backupsv1alpha1.BackupSpec{
475475
ApplicationRef: newAltinityClickHouseRef("ch-test"),
476476
StrategyRef: corev1.TypedLocalObjectReference{
477-
APIGroup: stringPtr(strategyv1alpha1.GroupVersion.Group),
477+
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
478478
Kind: strategyv1alpha1.AltinityStrategyKind,
479479
Name: "clickhouse-strategy",
480480
},
@@ -486,7 +486,7 @@ func TestReconcileAltinityRestore_RenderedRestoreJobOwnedByRestoreJob(t *testing
486486
Spec: backupsv1alpha1.RestoreJobSpec{
487487
BackupRef: corev1.LocalObjectReference{Name: backup.Name},
488488
TargetApplicationRef: &corev1.TypedLocalObjectReference{
489-
APIGroup: stringPtr(backupsv1alpha1.DefaultApplicationAPIGroup),
489+
APIGroup: new(backupsv1alpha1.DefaultApplicationAPIGroup),
490490
Kind: "ClickHouse",
491491
Name: "ch-restore",
492492
},
@@ -552,7 +552,7 @@ func TestReconcileAltinityRestore_TargetNamespaceIsRestoreJobNamespace(t *testin
552552
{
553553
name: "full triple targetRef -> to-copy explicit",
554554
targetRef: &corev1.TypedLocalObjectReference{
555-
APIGroup: stringPtr(backupsv1alpha1.DefaultApplicationAPIGroup),
555+
APIGroup: new(backupsv1alpha1.DefaultApplicationAPIGroup),
556556
Kind: "ClickHouse",
557557
Name: "ch-copy",
558558
},
@@ -570,7 +570,7 @@ func TestReconcileAltinityRestore_TargetNamespaceIsRestoreJobNamespace(t *testin
570570
Spec: backupsv1alpha1.BackupSpec{
571571
ApplicationRef: newAltinityClickHouseRef("ch-source"),
572572
StrategyRef: corev1.TypedLocalObjectReference{
573-
APIGroup: stringPtr(strategyv1alpha1.GroupVersion.Group),
573+
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
574574
Kind: strategyv1alpha1.AltinityStrategyKind,
575575
Name: "clickhouse-strategy",
576576
},
@@ -647,7 +647,7 @@ func TestReconcileAltinityRestore_ExposesSourceApplicationRef(t *testing.T) {
647647
Spec: backupsv1alpha1.BackupSpec{
648648
ApplicationRef: newAltinityClickHouseRef("ch-source"),
649649
StrategyRef: corev1.TypedLocalObjectReference{
650-
APIGroup: stringPtr(strategyv1alpha1.GroupVersion.Group),
650+
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
651651
Kind: strategyv1alpha1.AltinityStrategyKind,
652652
Name: "clickhouse-strategy",
653653
},
@@ -659,7 +659,7 @@ func TestReconcileAltinityRestore_ExposesSourceApplicationRef(t *testing.T) {
659659
Spec: backupsv1alpha1.RestoreJobSpec{
660660
BackupRef: corev1.LocalObjectReference{Name: backup.Name},
661661
TargetApplicationRef: &corev1.TypedLocalObjectReference{
662-
APIGroup: stringPtr(backupsv1alpha1.DefaultApplicationAPIGroup),
662+
APIGroup: new(backupsv1alpha1.DefaultApplicationAPIGroup),
663663
Kind: "ClickHouse",
664664
Name: "ch-restore",
665665
},

‎internal/backupcontroller/backup_controller_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,12 @@ func redisCleanupBackup(name, objectKey string) *backupsv1alpha1.Backup {
486486
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "tenant-test"},
487487
Spec: backupsv1alpha1.BackupSpec{
488488
ApplicationRef: corev1.TypedLocalObjectReference{
489-
APIGroup: stringPtr(backupsv1alpha1.DefaultApplicationAPIGroup),
489+
APIGroup: new(backupsv1alpha1.DefaultApplicationAPIGroup),
490490
Kind: "Redis",
491491
Name: "cache",
492492
},
493493
StrategyRef: corev1.TypedLocalObjectReference{
494-
APIGroup: stringPtr(strategyv1alpha1.GroupVersion.Group),
494+
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
495495
Kind: strategyv1alpha1.RedisStrategyKind,
496496
Name: "cozy-default-redis",
497497
},

0 commit comments

Comments
 (0)