Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ linters:
- asciicheck
- bidichk
- copyloopvar
- depguard
- dupl
- errcheck
- ginkgolinter
Expand All @@ -29,6 +30,12 @@ linters:
- wrapcheck
- whitespace
settings:
depguard:
rules:
forbid-pkg-errors:
deny:
- pkg: sort
desc: Should be replaced with slices package
ginkgolinter:
forbid-focus-container: true
forbid-spec-pollution: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"context"
"fmt"
"maps"
"sort"
"slices"
"time"

"github.com/robfig/cron"
Expand Down Expand Up @@ -373,11 +373,19 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// NB: deleting these are "best effort" -- if we fail on a particular one,
// we won't requeue just to finish the deleting.
if cronJob.Spec.FailedJobsHistoryLimit != nil {
sort.Slice(failedJobs, func(i, j int) bool {
if failedJobs[i].Status.StartTime == nil {
return failedJobs[j].Status.StartTime != nil
slices.SortStableFunc(failedJobs, func(a, b *kbatch.Job) int {
aStartTime := a.Status.StartTime
bStartTime := b.Status.StartTime
if aStartTime == nil && bStartTime != nil {
return 1
}
return failedJobs[i].Status.StartTime.Before(failedJobs[j].Status.StartTime)

if aStartTime.Before(bStartTime) {
return -1
} else if bStartTime.Before(aStartTime) {
return 1
}
return 0
})
for i, job := range failedJobs {
if int32(i) >= int32(len(failedJobs))-*cronJob.Spec.FailedJobsHistoryLimit {
Expand All @@ -392,11 +400,19 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}

if cronJob.Spec.SuccessfulJobsHistoryLimit != nil {
sort.Slice(successfulJobs, func(i, j int) bool {
if successfulJobs[i].Status.StartTime == nil {
return successfulJobs[j].Status.StartTime != nil
slices.SortStableFunc(successfulJobs, func(a, b *kbatch.Job) int {
aStartTime := a.Status.StartTime
bStartTime := b.Status.StartTime
if aStartTime == nil && bStartTime != nil {
return 1
}

if aStartTime.Before(bStartTime) {
return -1
} else if bStartTime.Before(aStartTime) {
return 1
}
return successfulJobs[i].Status.StartTime.Before(successfulJobs[j].Status.StartTime)
return 0
})
for i, job := range successfulJobs {
if int32(i) >= int32(len(successfulJobs))-*cronJob.Spec.SuccessfulJobsHistoryLimit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"context"
"fmt"
"maps"
"sort"
"slices"
"time"

"github.com/robfig/cron"
Expand Down Expand Up @@ -373,11 +373,19 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// NB: deleting these are "best effort" -- if we fail on a particular one,
// we won't requeue just to finish the deleting.
if cronJob.Spec.FailedJobsHistoryLimit != nil {
sort.Slice(failedJobs, func(i, j int) bool {
if failedJobs[i].Status.StartTime == nil {
return failedJobs[j].Status.StartTime != nil
slices.SortStableFunc(failedJobs, func(a, b *kbatch.Job) int {
aStartTime := a.Status.StartTime
bStartTime := b.Status.StartTime
if aStartTime == nil && bStartTime != nil {
return 1
}
return failedJobs[i].Status.StartTime.Before(failedJobs[j].Status.StartTime)

if aStartTime.Before(bStartTime) {
return -1
} else if bStartTime.Before(aStartTime) {
return 1
}
return 0
})
for i, job := range failedJobs {
if int32(i) >= int32(len(failedJobs))-*cronJob.Spec.FailedJobsHistoryLimit {
Expand All @@ -392,11 +400,19 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}

if cronJob.Spec.SuccessfulJobsHistoryLimit != nil {
sort.Slice(successfulJobs, func(i, j int) bool {
if successfulJobs[i].Status.StartTime == nil {
return successfulJobs[j].Status.StartTime != nil
slices.SortStableFunc(successfulJobs, func(a, b *kbatch.Job) int {
aStartTime := a.Status.StartTime
bStartTime := b.Status.StartTime
if aStartTime == nil && bStartTime != nil {
return 1
}

if aStartTime.Before(bStartTime) {
return -1
} else if bStartTime.Before(aStartTime) {
return 1
}
return successfulJobs[i].Status.StartTime.Before(successfulJobs[j].Status.StartTime)
return 0
})
for i, job := range successfulJobs {
if int32(i) >= int32(len(successfulJobs))-*cronJob.Spec.SuccessfulJobsHistoryLimit {
Expand Down
34 changes: 25 additions & 9 deletions hack/docs/internal/cronjob-tutorial/controller_implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const controllerImport = `import (
"context"
"fmt"
"maps"
"sort"
"slices"
"time"

"github.com/robfig/cron"
Expand Down Expand Up @@ -362,11 +362,19 @@ const controllerReconcileLogic = `log := logf.FromContext(ctx)
// NB: deleting these are "best effort" -- if we fail on a particular one,
// we won't requeue just to finish the deleting.
if cronJob.Spec.FailedJobsHistoryLimit != nil {
sort.Slice(failedJobs, func(i, j int) bool {
if failedJobs[i].Status.StartTime == nil {
return failedJobs[j].Status.StartTime != nil
slices.SortStableFunc(failedJobs, func(a, b *kbatch.Job) int {
aStartTime := a.Status.StartTime
bStartTime := b.Status.StartTime
if aStartTime == nil && bStartTime != nil {
return 1
}
return failedJobs[i].Status.StartTime.Before(failedJobs[j].Status.StartTime)

if aStartTime.Before(bStartTime) {
return -1
} else if bStartTime.Before(aStartTime) {
return 1
}
return 0
})
for i, job := range failedJobs {
if int32(i) >= int32(len(failedJobs))-*cronJob.Spec.FailedJobsHistoryLimit {
Expand All @@ -381,11 +389,19 @@ const controllerReconcileLogic = `log := logf.FromContext(ctx)
}

if cronJob.Spec.SuccessfulJobsHistoryLimit != nil {
sort.Slice(successfulJobs, func(i, j int) bool {
if successfulJobs[i].Status.StartTime == nil {
return successfulJobs[j].Status.StartTime != nil
slices.SortStableFunc(successfulJobs, func(a, b *kbatch.Job) int {
aStartTime := a.Status.StartTime
bStartTime := b.Status.StartTime
if aStartTime == nil && bStartTime != nil {
return 1
}

if aStartTime.Before(bStartTime) {
return -1
} else if bStartTime.Before(aStartTime) {
return 1
}
return successfulJobs[i].Status.StartTime.Before(successfulJobs[j].Status.StartTime)
return 0
})
for i, job := range successfulJobs {
if int32(i) >= int32(len(successfulJobs))-*cronJob.Spec.SuccessfulJobsHistoryLimit {
Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/alpha/internal/update/helpers/conflict.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sort"
"slices"
"strings"
)

Expand Down Expand Up @@ -83,8 +83,8 @@ func FindConflictFiles() ConflictResult {
}
}

sort.Strings(result.SourceFiles)
sort.Strings(result.GeneratedFiles)
slices.Sort(result.SourceFiles)
slices.Sort(result.GeneratedFiles)

// Build summary for existing conflict.go usage
result.Summary = ConflictSummary{
Expand Down
14 changes: 7 additions & 7 deletions pkg/cli/alpha/internal/update/helpers/open_gh_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
"os/exec"
"regexp"
"sort"
"slices"
"strings"
)

Expand Down Expand Up @@ -225,8 +225,8 @@ func listChangedFiles(base, head string) (src []string, gen []string) {
src = append(src, p)
}
}
sort.Strings(src)
sort.Strings(gen)
slices.Sort(src)
slices.Sort(gen)
return src, gen
}

Expand Down Expand Up @@ -500,12 +500,12 @@ func concatSelectedDiffs(base, head string, files []string, perFileLineCap, tota
candidates = append(candidates, p)
}
}
sort.Slice(candidates, func(i, j int) bool {
pi, pj := filePriority(candidates[i]), filePriority(candidates[j])
slices.SortStableFunc(candidates, func(a, b string) int {
pi, pj := filePriority(a), filePriority(b)
if pi != pj {
return pi < pj
return pi - pj
}
return candidates[i] < candidates[j] // stable alphabetical within same priority
return strings.Compare(a, b) // stable alphabetical within same priority
})

// Emit diffs until the global budget is hit
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package cli

import (
"fmt"
"sort"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -101,6 +101,6 @@ func (c CLI) getAvailableProjectVersions() (projectVersions []string) {
for version := range versionSet {
projectVersions = append(projectVersions, strconv.Quote(version.String()))
}
sort.Strings(projectVersions)
slices.Sort(projectVersions)
return projectVersions
}
4 changes: 2 additions & 2 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package cli

import (
"fmt"
"sort"
"slices"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -115,7 +115,7 @@ func (c CLI) getPluginTable() string {
lines = append(lines, strings.Repeat("-", maxPluginKeyLength+2)+"+"+
strings.Repeat("-", maxProjectVersionLength+2))

sort.Strings(pluginKeys)
slices.Sort(pluginKeys)
for _, pluginKey := range pluginKeys {
supportedProjectVersions := projectVersions[pluginKey]
lines = append(lines, fmt.Sprintf(" %[1]*[2]s | %[3]*[4]s",
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/v3/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package v3

import (
"sort"
"slices"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -314,7 +314,7 @@ var _ = Describe("Cfg", func() {
},
)
versions := c.ListCRDVersions()
sort.Strings(versions) // ListCRDVersions has no order guarantee so sorting for reproducibility
slices.Sort(versions) // ListCRDVersions has no order guarantee so sorting for reproducibility
Expect(versions).To(Equal([]string{"v1", "v1beta1"}))
})

Expand Down Expand Up @@ -342,7 +342,7 @@ var _ = Describe("Cfg", func() {
},
)
versions := c.ListWebhookVersions()
sort.Strings(versions) // ListWebhookVersions has no order guarantee so sorting for reproducibility
slices.Sort(versions) // ListWebhookVersions has no order guarantee so sorting for reproducibility
Expect(versions).To(Equal([]string{"v1", "v1beta1"}))
})
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package config

import (
"sort"
"slices"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -64,8 +64,8 @@ var _ = Describe("Version", func() {
})

It("sorts a valid list of versions correctly", func() {
sort.Slice(versions, func(i int, j int) bool {
return versions[i].Compare(versions[j]) == -1
slices.SortStableFunc(versions, func(a, b Version) int {
return a.Compare(b)
})
Expect(versions).To(Equal(sortedVersions))
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/model/stage/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package stage

import (
"sort"
"slices"
"testing"

. "github.com/onsi/ginkgo/v2" // An alias is required because Context is defined elsewhere in this package.
Expand Down Expand Up @@ -115,8 +115,8 @@ var _ = Describe("Stage", func() {
})

It("sorts stages correctly", func() {
sort.Slice(stages, func(i int, j int) bool {
return stages[i].Compare(stages[j]) == -1
slices.SortStableFunc(stages, func(a, b Stage) int {
return a.Compare(b)
})
Expect(stages).To(Equal(sortedStages))
})
Expand Down
Loading