| ... | ... |
@@ -3,6 +3,7 @@ package dockerfile |
| 3 | 3 |
import ( |
| 4 | 4 |
"fmt" |
| 5 | 5 |
"io" |
| 6 |
+ "maps" |
|
| 6 | 7 |
"sort" |
| 7 | 8 |
) |
| 8 | 9 |
|
| ... | ... |
@@ -47,12 +48,8 @@ func NewBuildArgs(argsFromOptions map[string]*string) *BuildArgs {
|
| 47 | 47 |
// Clone returns a copy of the BuildArgs type |
| 48 | 48 |
func (b *BuildArgs) Clone() *BuildArgs {
|
| 49 | 49 |
result := NewBuildArgs(b.argsFromOptions) |
| 50 |
- for k, v := range b.allowedBuildArgs {
|
|
| 51 |
- result.allowedBuildArgs[k] = v |
|
| 52 |
- } |
|
| 53 |
- for k, v := range b.allowedMetaArgs {
|
|
| 54 |
- result.allowedMetaArgs[k] = v |
|
| 55 |
- } |
|
| 50 |
+ maps.Copy(result.allowedBuildArgs, b.allowedBuildArgs) |
|
| 51 |
+ maps.Copy(result.allowedMetaArgs, b.allowedMetaArgs) |
|
| 56 | 52 |
for k := range b.referencedArgs {
|
| 57 | 53 |
result.referencedArgs[k] = struct{}{}
|
| 58 | 54 |
} |
| ... | ... |
@@ -79,12 +79,13 @@ func dispatchLabel(ctx context.Context, d dispatchRequest, c *instructions.Label |
| 79 | 79 |
if d.state.runConfig.Labels == nil {
|
| 80 | 80 |
d.state.runConfig.Labels = make(map[string]string) |
| 81 | 81 |
} |
| 82 |
- commitStr := "LABEL" |
|
| 82 |
+ var commitStr strings.Builder |
|
| 83 |
+ commitStr.WriteString("LABEL")
|
|
| 83 | 84 |
for _, v := range c.Labels {
|
| 84 | 85 |
d.state.runConfig.Labels[v.Key] = v.Value |
| 85 |
- commitStr += " " + v.String() |
|
| 86 |
+ commitStr.WriteString(" " + v.String())
|
|
| 86 | 87 |
} |
| 87 |
- return d.builder.commit(ctx, d.state, commitStr) |
|
| 88 |
+ return d.builder.commit(ctx, d.state, commitStr.String()) |
|
| 88 | 89 |
} |
| 89 | 90 |
|
| 90 | 91 |
// ADD foo /path |
| ... | ... |
@@ -628,7 +629,7 @@ func parsePortSpec(rawPort string) ([]network.PortMap, error) {
|
| 628 | 628 |
count := endPort - startPort + 1 |
| 629 | 629 |
ports := make([]network.PortMap, 0, count) |
| 630 | 630 |
|
| 631 |
- for i := uint16(0); i < count; i++ {
|
|
| 631 |
+ for i := range count {
|
|
| 632 | 632 |
hPort := "" |
| 633 | 633 |
if hostPort != "" {
|
| 634 | 634 |
hPort = strconv.Itoa(int(startHostPort + i)) |
| ... | ... |
@@ -302,7 +302,7 @@ func TestTarSumsReadSize(t *testing.T) {
|
| 302 | 302 |
// Test always on the same layer (that is big enough) |
| 303 | 303 |
layer := testLayers[0] |
| 304 | 304 |
|
| 305 |
- for i := 0; i < 5; i++ {
|
|
| 305 |
+ for i := range 5 {
|
|
| 306 | 306 |
reader, err := os.Open(layer.filename) |
| 307 | 307 |
if err != nil {
|
| 308 | 308 |
t.Fatal(err) |
| ... | ... |
@@ -580,8 +580,8 @@ func Benchmark9kTar(b *testing.B) {
|
| 580 | 580 |
reader := bytes.NewReader(buf.Bytes()) |
| 581 | 581 |
|
| 582 | 582 |
b.SetBytes(n) |
| 583 |
- b.ResetTimer() |
|
| 584 |
- for i := 0; i < b.N; i++ {
|
|
| 583 |
+ |
|
| 584 |
+ for b.Loop() {
|
|
| 585 | 585 |
reader.Seek(0, 0) |
| 586 | 586 |
ts, err := NewTarSum(reader, true, Version0) |
| 587 | 587 |
if err != nil {
|
| ... | ... |
@@ -611,8 +611,8 @@ func Benchmark9kTarGzip(b *testing.B) {
|
| 611 | 611 |
reader := bytes.NewReader(buf.Bytes()) |
| 612 | 612 |
|
| 613 | 613 |
b.SetBytes(n) |
| 614 |
- b.ResetTimer() |
|
| 615 |
- for i := 0; i < b.N; i++ {
|
|
| 614 |
+ |
|
| 615 |
+ for b.Loop() {
|
|
| 616 | 616 |
reader.Seek(0, 0) |
| 617 | 617 |
ts, err := NewTarSum(reader, false, Version0) |
| 618 | 618 |
if err != nil {
|
| ... | ... |
@@ -34,11 +34,11 @@ func WriteV1Header(h *tar.Header, w io.Writer) {
|
| 34 | 34 |
// the string or an empty string if no label separator is found. |
| 35 | 35 |
func VersionLabelForChecksum(checksum string) string {
|
| 36 | 36 |
// Checksums are in the form: {versionLabel}+{hashID}:{hex}
|
| 37 |
- sepIndex := strings.Index(checksum, "+") |
|
| 38 |
- if sepIndex < 0 {
|
|
| 37 |
+ before, _, ok := strings.Cut(checksum, "+") |
|
| 38 |
+ if !ok {
|
|
| 39 | 39 |
return "" |
| 40 | 40 |
} |
| 41 |
- return checksum[:sepIndex] |
|
| 41 |
+ return before |
|
| 42 | 42 |
} |
| 43 | 43 |
|
| 44 | 44 |
// GetVersions gets a list of all known tarsum versions. |
| ... | ... |
@@ -4,6 +4,7 @@ import ( |
| 4 | 4 |
"archive/tar" |
| 5 | 5 |
"errors" |
| 6 | 6 |
"fmt" |
| 7 |
+ "slices" |
|
| 7 | 8 |
"strings" |
| 8 | 9 |
"testing" |
| 9 | 10 |
|
| ... | ... |
@@ -96,12 +97,7 @@ func TestGetVersions(t *testing.T) {
|
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
func containsVersion(versions []Version, version Version) bool {
|
| 99 |
- for _, v := range versions {
|
|
| 100 |
- if v == version {
|
|
| 101 |
- return true |
|
| 102 |
- } |
|
| 103 |
- } |
|
| 104 |
- return false |
|
| 99 |
+ return slices.Contains(versions, version) |
|
| 105 | 100 |
} |
| 106 | 101 |
|
| 107 | 102 |
func TestSelectXattrsV1(t *testing.T) {
|
| ... | ... |
@@ -1,7 +1,6 @@ |
| 1 | 1 |
package container |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
- "context" |
|
| 5 | 4 |
"testing" |
| 6 | 5 |
"time" |
| 7 | 6 |
|
| ... | ... |
@@ -71,8 +70,7 @@ func TestWaitNodeAttachment(t *testing.T) {
|
| 71 | 71 |
} |
| 72 | 72 |
|
| 73 | 73 |
// create a context to do call the method with |
| 74 |
- ctx, cancel := context.WithCancel(context.Background()) |
|
| 75 |
- defer cancel() |
|
| 74 |
+ ctx := t.Context() |
|
| 76 | 75 |
|
| 77 | 76 |
// create a channel to allow the goroutine that we run the method call in |
| 78 | 77 |
// to signal that it's done. |
| ... | ... |
@@ -4,6 +4,7 @@ import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"encoding/json" |
| 6 | 6 |
"fmt" |
| 7 |
+ "maps" |
|
| 7 | 8 |
"math" |
| 8 | 9 |
"net" |
| 9 | 10 |
"net/netip" |
| ... | ... |
@@ -261,15 +262,11 @@ func (c *containerConfig) labels() map[string]string {
|
| 261 | 261 |
) |
| 262 | 262 |
|
| 263 | 263 |
// base labels are those defined in the spec. |
| 264 |
- for k, v := range c.spec().Labels {
|
|
| 265 |
- labels[k] = v |
|
| 266 |
- } |
|
| 264 |
+ maps.Copy(labels, c.spec().Labels) |
|
| 267 | 265 |
|
| 268 | 266 |
// we then apply the overrides from the task, which may be set via the |
| 269 | 267 |
// orchestrator. |
| 270 |
- for k, v := range c.task.Annotations.Labels {
|
|
| 271 |
- labels[k] = v |
|
| 272 |
- } |
|
| 268 |
+ maps.Copy(labels, c.task.Annotations.Labels) |
|
| 273 | 269 |
|
| 274 | 270 |
// finally, we apply the system labels, which override all labels. |
| 275 | 271 |
for k, v := range system {
|
| ... | ... |
@@ -367,9 +364,7 @@ func convertMount(m api.Mount) enginemount.Mount {
|
| 367 | 367 |
} |
| 368 | 368 |
if m.VolumeOptions.Labels != nil {
|
| 369 | 369 |
mount.VolumeOptions.Labels = make(map[string]string, len(m.VolumeOptions.Labels)) |
| 370 |
- for k, v := range m.VolumeOptions.Labels {
|
|
| 371 |
- mount.VolumeOptions.Labels[k] = v |
|
| 372 |
- } |
|
| 370 |
+ maps.Copy(mount.VolumeOptions.Labels, m.VolumeOptions.Labels) |
|
| 373 | 371 |
} |
| 374 | 372 |
if m.VolumeOptions.DriverConfig != nil {
|
| 375 | 373 |
mount.VolumeOptions.DriverConfig = &enginemount.Driver{
|
| ... | ... |
@@ -377,9 +372,7 @@ func convertMount(m api.Mount) enginemount.Mount {
|
| 377 | 377 |
} |
| 378 | 378 |
if m.VolumeOptions.DriverConfig.Options != nil {
|
| 379 | 379 |
mount.VolumeOptions.DriverConfig.Options = make(map[string]string, len(m.VolumeOptions.DriverConfig.Options)) |
| 380 |
- for k, v := range m.VolumeOptions.DriverConfig.Options {
|
|
| 381 |
- mount.VolumeOptions.DriverConfig.Options[k] = v |
|
| 382 |
- } |
|
| 380 |
+ maps.Copy(mount.VolumeOptions.DriverConfig.Options, m.VolumeOptions.DriverConfig.Options) |
|
| 383 | 381 |
} |
| 384 | 382 |
} |
| 385 | 383 |
} |
| ... | ... |
@@ -3,7 +3,6 @@ |
| 3 | 3 |
package container |
| 4 | 4 |
|
| 5 | 5 |
import ( |
| 6 |
- "context" |
|
| 7 | 6 |
"errors" |
| 8 | 7 |
"testing" |
| 9 | 8 |
"time" |
| ... | ... |
@@ -59,8 +58,7 @@ func TestHealthStates(t *testing.T) {
|
| 59 | 59 |
} |
| 60 | 60 |
|
| 61 | 61 |
errChan := make(chan error, 1) |
| 62 |
- ctx, cancel := context.WithCancel(context.Background()) |
|
| 63 |
- defer cancel() |
|
| 62 |
+ ctx := t.Context() |
|
| 64 | 63 |
|
| 65 | 64 |
// fire checkHealth |
| 66 | 65 |
go func() {
|
| ... | ... |
@@ -3,6 +3,7 @@ package daemon |
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"fmt" |
| 6 |
+ "maps" |
|
| 6 | 7 |
"runtime" |
| 7 | 8 |
"strings" |
| 8 | 9 |
"time" |
| ... | ... |
@@ -109,9 +110,7 @@ func merge(userConf, imageConf *containertypes.Config) error {
|
| 109 | 109 |
if len(userConf.Volumes) == 0 {
|
| 110 | 110 |
userConf.Volumes = imageConf.Volumes |
| 111 | 111 |
} else {
|
| 112 |
- for k, v := range imageConf.Volumes {
|
|
| 113 |
- userConf.Volumes[k] = v |
|
| 114 |
- } |
|
| 112 |
+ maps.Copy(userConf.Volumes, imageConf.Volumes) |
|
| 115 | 113 |
} |
| 116 | 114 |
|
| 117 | 115 |
if userConf.StopSignal == "" {
|
| ... | ... |
@@ -5,6 +5,7 @@ import ( |
| 5 | 5 |
"encoding/json" |
| 6 | 6 |
stderrors "errors" |
| 7 | 7 |
"fmt" |
| 8 |
+ "maps" |
|
| 8 | 9 |
"net" |
| 9 | 10 |
"net/netip" |
| 10 | 11 |
"net/url" |
| ... | ... |
@@ -587,9 +588,7 @@ func configValuesSet(config map[string]any) map[string]any {
|
| 587 | 587 |
flatten := make(map[string]any) |
| 588 | 588 |
for k, v := range config {
|
| 589 | 589 |
if m, isMap := v.(map[string]any); isMap && !flatOptions[k] {
|
| 590 |
- for km, vm := range m {
|
|
| 591 |
- flatten[km] = vm |
|
| 592 |
- } |
|
| 590 |
+ maps.Copy(flatten, m) |
|
| 593 | 591 |
continue |
| 594 | 592 |
} |
| 595 | 593 |
|
| ... | ... |
@@ -468,7 +468,7 @@ type overwriteTransformer struct {
|
| 468 | 468 |
} |
| 469 | 469 |
|
| 470 | 470 |
func (tf overwriteTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
|
| 471 |
- if typ == reflect.TypeOf(CommonConfig{}) {
|
|
| 471 |
+ if typ == reflect.TypeFor[CommonConfig]() {
|
|
| 472 | 472 |
return func(dst, src reflect.Value) error {
|
| 473 | 473 |
dst.FieldByName(tf.fieldName).Set(src.FieldByName(tf.fieldName)) |
| 474 | 474 |
return nil |
| ... | ... |
@@ -702,7 +702,7 @@ func TestConfigInvalidDNS(t *testing.T) {
|
| 702 | 702 |
} |
| 703 | 703 |
|
| 704 | 704 |
func field(field string) cmp.Option {
|
| 705 |
- tmp := reflect.TypeOf(Config{})
|
|
| 705 |
+ tmp := reflect.TypeFor[Config]() |
|
| 706 | 706 |
ignoreFields := make([]string, 0, tmp.NumField()) |
| 707 | 707 |
for i := 0; i < tmp.NumField(); i++ {
|
| 708 | 708 |
if tmp.Field(i).Name != field {
|
| ... | ... |
@@ -9,14 +9,14 @@ import ( |
| 9 | 9 |
func ReplaceOrAppendEnvValues(defaults, overrides []string) []string {
|
| 10 | 10 |
cache := make(map[string]int, len(defaults)) |
| 11 | 11 |
for i, e := range defaults {
|
| 12 |
- index := strings.Index(e, "=") |
|
| 13 |
- cache[e[:index]] = i |
|
| 12 |
+ before, _, _ := strings.Cut(e, "=") |
|
| 13 |
+ cache[before] = i |
|
| 14 | 14 |
} |
| 15 | 15 |
|
| 16 | 16 |
for _, value := range overrides {
|
| 17 | 17 |
// Values w/o = means they want this env to be removed/unset. |
| 18 |
- index := strings.Index(value, "=") |
|
| 19 |
- if index < 0 {
|
|
| 18 |
+ before, _, ok := strings.Cut(value, "=") |
|
| 19 |
+ if !ok {
|
|
| 20 | 20 |
// no "=" in value |
| 21 | 21 |
if i, exists := cache[value]; exists {
|
| 22 | 22 |
defaults[i] = "" // Used to indicate it should be removed |
| ... | ... |
@@ -24,7 +24,7 @@ func ReplaceOrAppendEnvValues(defaults, overrides []string) []string {
|
| 24 | 24 |
continue |
| 25 | 25 |
} |
| 26 | 26 |
|
| 27 |
- if i, exists := cache[value[:index]]; exists {
|
|
| 27 |
+ if i, exists := cache[before]; exists {
|
|
| 28 | 28 |
defaults[i] = value |
| 29 | 29 |
} else {
|
| 30 | 30 |
defaults = append(defaults, value) |
| ... | ... |
@@ -41,7 +41,7 @@ func benchmarkReplaceOrAppendEnvValues(b *testing.B, extraEnv int) {
|
| 41 | 41 |
|
| 42 | 42 |
if extraEnv > 0 {
|
| 43 | 43 |
buf := make([]byte, 5) |
| 44 |
- for i := 0; i < extraEnv; i++ {
|
|
| 44 |
+ for range extraEnv {
|
|
| 45 | 45 |
n, err := rand.Read(buf) |
| 46 | 46 |
assert.NilError(b, err) |
| 47 | 47 |
key := string(buf[:n]) |
| ... | ... |
@@ -2,6 +2,7 @@ package container |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 |
+ "maps" |
|
| 5 | 6 |
"runtime" |
| 6 | 7 |
"sync" |
| 7 | 8 |
|
| ... | ... |
@@ -91,9 +92,7 @@ func NewExecStore() *ExecStore {
|
| 91 | 91 |
func (e *ExecStore) Commands() map[string]*ExecConfig {
|
| 92 | 92 |
e.mu.RLock() |
| 93 | 93 |
byID := make(map[string]*ExecConfig, len(e.byID)) |
| 94 |
- for id, config := range e.byID {
|
|
| 95 |
- byID[id] = config |
|
| 96 |
- } |
|
| 94 |
+ maps.Copy(byID, e.byID) |
|
| 97 | 95 |
e.mu.RUnlock() |
| 98 | 96 |
return byID |
| 99 | 97 |
} |
| ... | ... |
@@ -347,11 +347,11 @@ func assertIndexGet(t *testing.T, snapshot *ViewDB, input, expectedResult string |
| 347 | 347 |
|
| 348 | 348 |
func BenchmarkDBAdd100(b *testing.B) {
|
| 349 | 349 |
var testSet []string |
| 350 |
- for i := 0; i < 100; i++ {
|
|
| 350 |
+ for range 100 {
|
|
| 351 | 351 |
testSet = append(testSet, stringid.GenerateRandomID()) |
| 352 | 352 |
} |
| 353 |
- b.ResetTimer() |
|
| 354 |
- for i := 0; i < b.N; i++ {
|
|
| 353 |
+ |
|
| 354 |
+ for b.Loop() {
|
|
| 355 | 355 |
db, err := NewViewDB() |
| 356 | 356 |
if err != nil {
|
| 357 | 357 |
b.Fatal(err) |
| ... | ... |
@@ -367,7 +367,7 @@ func BenchmarkDBAdd100(b *testing.B) {
|
| 367 | 367 |
func BenchmarkDBGetByPrefix100(b *testing.B) {
|
| 368 | 368 |
var testSet []string |
| 369 | 369 |
var testKeys []string |
| 370 |
- for i := 0; i < 100; i++ {
|
|
| 370 |
+ for range 100 {
|
|
| 371 | 371 |
testSet = append(testSet, stringid.GenerateRandomID()) |
| 372 | 372 |
} |
| 373 | 373 |
db, err := NewViewDB() |
| ... | ... |
@@ -381,8 +381,8 @@ func BenchmarkDBGetByPrefix100(b *testing.B) {
|
| 381 | 381 |
l := rand.Intn(12) + 12 |
| 382 | 382 |
testKeys = append(testKeys, id[:l]) |
| 383 | 383 |
} |
| 384 |
- b.ResetTimer() |
|
| 385 |
- for i := 0; i < b.N; i++ {
|
|
| 384 |
+ |
|
| 385 |
+ for b.Loop() {
|
|
| 386 | 386 |
for _, id := range testKeys {
|
| 387 | 387 |
if res, err := db.GetByPrefix(id); err != nil {
|
| 388 | 388 |
b.Fatal(res, err) |
| ... | ... |
@@ -394,7 +394,7 @@ func BenchmarkDBGetByPrefix100(b *testing.B) {
|
| 394 | 394 |
func BenchmarkDBGetByPrefix250(b *testing.B) {
|
| 395 | 395 |
var testSet []string |
| 396 | 396 |
var testKeys []string |
| 397 |
- for i := 0; i < 250; i++ {
|
|
| 397 |
+ for range 250 {
|
|
| 398 | 398 |
testSet = append(testSet, stringid.GenerateRandomID()) |
| 399 | 399 |
} |
| 400 | 400 |
db, err := NewViewDB() |
| ... | ... |
@@ -408,8 +408,8 @@ func BenchmarkDBGetByPrefix250(b *testing.B) {
|
| 408 | 408 |
l := rand.Intn(12) + 12 |
| 409 | 409 |
testKeys = append(testKeys, id[:l]) |
| 410 | 410 |
} |
| 411 |
- b.ResetTimer() |
|
| 412 |
- for i := 0; i < b.N; i++ {
|
|
| 411 |
+ |
|
| 412 |
+ for b.Loop() {
|
|
| 413 | 413 |
for _, id := range testKeys {
|
| 414 | 414 |
if res, err := db.GetByPrefix(id); err != nil {
|
| 415 | 415 |
b.Fatal(res, err) |
| ... | ... |
@@ -421,7 +421,7 @@ func BenchmarkDBGetByPrefix250(b *testing.B) {
|
| 421 | 421 |
func BenchmarkDBGetByPrefix500(b *testing.B) {
|
| 422 | 422 |
var testSet []string |
| 423 | 423 |
var testKeys []string |
| 424 |
- for i := 0; i < 500; i++ {
|
|
| 424 |
+ for range 500 {
|
|
| 425 | 425 |
testSet = append(testSet, stringid.GenerateRandomID()) |
| 426 | 426 |
} |
| 427 | 427 |
db, err := NewViewDB() |
| ... | ... |
@@ -435,8 +435,8 @@ func BenchmarkDBGetByPrefix500(b *testing.B) {
|
| 435 | 435 |
l := rand.Intn(12) + 12 |
| 436 | 436 |
testKeys = append(testKeys, id[:l]) |
| 437 | 437 |
} |
| 438 |
- b.ResetTimer() |
|
| 439 |
- for i := 0; i < b.N; i++ {
|
|
| 438 |
+ |
|
| 439 |
+ for b.Loop() {
|
|
| 440 | 440 |
for _, id := range testKeys {
|
| 441 | 441 |
if res, err := db.GetByPrefix(id); err != nil {
|
| 442 | 442 |
b.Fatal(res, err) |
| ... | ... |
@@ -4,6 +4,7 @@ import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"errors" |
| 6 | 6 |
"fmt" |
| 7 |
+ "maps" |
|
| 7 | 8 |
"net/netip" |
| 8 | 9 |
"os" |
| 9 | 10 |
"runtime" |
| ... | ... |
@@ -413,9 +414,7 @@ func (daemon *Daemon) allocateNetwork(ctx context.Context, cfg *config.Config, c |
| 413 | 413 |
|
| 414 | 414 |
// An intermediate map is necessary because "connectToNetwork" modifies "container.NetworkSettings.Networks" |
| 415 | 415 |
networks := make(map[string]*network.EndpointSettings) |
| 416 |
- for n, epConf := range ctr.NetworkSettings.Networks {
|
|
| 417 |
- networks[n] = epConf |
|
| 418 |
- } |
|
| 416 |
+ maps.Copy(networks, ctr.NetworkSettings.Networks) |
|
| 419 | 417 |
for netName, epConf := range networks {
|
| 420 | 418 |
cleanOperationalData(epConf) |
| 421 | 419 |
if err := daemon.connectToNetwork(ctx, cfg, ctr, netName, epConf); err != nil {
|
| ... | ... |
@@ -545,7 +544,7 @@ func validateEndpointSettings(nw *libnetwork.Network, nwName string, epConfig *n |
| 545 | 545 |
} |
| 546 | 546 |
|
| 547 | 547 |
if sysctls, ok := epConfig.DriverOpts[netlabel.EndpointSysctls]; ok {
|
| 548 |
- for _, sysctl := range strings.Split(sysctls, ",") {
|
|
| 548 |
+ for sysctl := range strings.SplitSeq(sysctls, ",") {
|
|
| 549 | 549 |
scname := strings.SplitN(sysctl, ".", 5) |
| 550 | 550 |
// Allow "ifname" as well as "IFNAME", because the CLI converts to lower case. |
| 551 | 551 |
if len(scname) != 5 || |
| ... | ... |
@@ -2,6 +2,7 @@ package containerd |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 |
+ "maps" |
|
| 5 | 6 |
|
| 6 | 7 |
c8dimages "github.com/containerd/containerd/v2/core/images" |
| 7 | 8 |
"github.com/moby/moby/api/types/events" |
| ... | ... |
@@ -45,7 +46,5 @@ func copyAttributes(attributes, labels map[string]string) {
|
| 45 | 45 |
if labels == nil {
|
| 46 | 46 |
return |
| 47 | 47 |
} |
| 48 |
- for k, v := range labels {
|
|
| 49 |
- attributes[k] = v |
|
| 50 |
- } |
|
| 48 |
+ maps.Copy(attributes, labels) |
|
| 51 | 49 |
} |
| ... | ... |
@@ -634,8 +634,8 @@ func setupLabelFilter(ctx context.Context, store content.Store, fltrs filters.Ar |
| 634 | 634 |
negate := strings.HasSuffix(fltrName, "!") |
| 635 | 635 |
|
| 636 | 636 |
// If filter value is key!=value then flip the above. |
| 637 |
- if strings.HasSuffix(k, "!") {
|
|
| 638 |
- k = strings.TrimSuffix(k, "!") |
|
| 637 |
+ if before, ok := strings.CutSuffix(k, "!"); ok {
|
|
| 638 |
+ k = before |
|
| 639 | 639 |
negate = !negate |
| 640 | 640 |
} |
| 641 | 641 |
|
| ... | ... |
@@ -49,7 +49,7 @@ func BenchmarkImageList(b *testing.B) {
|
| 49 | 49 |
// Use constant seed for reproducibility |
| 50 | 50 |
src := rand.NewSource(1982731263716) |
| 51 | 51 |
|
| 52 |
- for i := 0; i < count; i++ {
|
|
| 52 |
+ for i := range count {
|
|
| 53 | 53 |
platform := platforms.DefaultSpec() |
| 54 | 54 |
|
| 55 | 55 |
// 20% is other architecture than the host |
| ... | ... |
@@ -73,7 +73,7 @@ func BenchmarkImageList(b *testing.B) {
|
| 73 | 73 |
} |
| 74 | 74 |
|
| 75 | 75 |
containersCount := r2 % maxContainerCount |
| 76 |
- for j := 0; j < containersCount; j++ {
|
|
| 76 |
+ for range containersCount {
|
|
| 77 | 77 |
id := digest.FromString(desc.Name + strconv.Itoa(i)).String() |
| 78 | 78 |
|
| 79 | 79 |
target := desc.Target |
| ... | ... |
@@ -403,8 +403,8 @@ func extractDistributionSources(labels map[string]string) []distributionSource {
|
| 403 | 403 |
// Check if this blob has a distributionSource label |
| 404 | 404 |
// if yes, read it as source |
| 405 | 405 |
for k, v := range labels {
|
| 406 |
- if reg := strings.TrimPrefix(k, containerdlabels.LabelDistributionSource); reg != k {
|
|
| 407 |
- for _, repo := range strings.Split(v, ",") {
|
|
| 406 |
+ if reg, ok := strings.CutPrefix(k, containerdlabels.LabelDistributionSource); ok {
|
|
| 407 |
+ for repo := range strings.SplitSeq(v, ",") {
|
|
| 408 | 408 |
ref, err := reference.ParseNamed(reg + "/" + repo) |
| 409 | 409 |
if err != nil {
|
| 410 | 410 |
continue |
| ... | ... |
@@ -7,6 +7,7 @@ import ( |
| 7 | 7 |
"io" |
| 8 | 8 |
"os" |
| 9 | 9 |
"regexp" |
| 10 |
+ "slices" |
|
| 10 | 11 |
"strings" |
| 11 | 12 |
"sync" |
| 12 | 13 |
|
| ... | ... |
@@ -231,10 +232,8 @@ func supportsRecursivelyReadOnly(cfg *configStore, runtime string) error {
|
| 231 | 231 |
if features == nil {
|
| 232 | 232 |
return fmt.Errorf("rro is not supported by runtime %q: OCI features struct is not available", runtime)
|
| 233 | 233 |
} |
| 234 |
- for _, s := range features.MountOptions {
|
|
| 235 |
- if s == "rro" {
|
|
| 236 |
- return nil |
|
| 237 |
- } |
|
| 234 |
+ if slices.Contains(features.MountOptions, "rro") {
|
|
| 235 |
+ return nil |
|
| 238 | 236 |
} |
| 239 | 237 |
return fmt.Errorf("rro is not supported by runtime %q", runtime)
|
| 240 | 238 |
} |
| ... | ... |
@@ -2,6 +2,7 @@ package daemon |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 |
+ "maps" |
|
| 5 | 6 |
"strconv" |
| 6 | 7 |
"strings" |
| 7 | 8 |
"time" |
| ... | ... |
@@ -94,9 +95,7 @@ func copyAttributes(attributes, labels map[string]string) {
|
| 94 | 94 |
if labels == nil {
|
| 95 | 95 |
return |
| 96 | 96 |
} |
| 97 |
- for k, v := range labels {
|
|
| 98 |
- attributes[k] = v |
|
| 99 |
- } |
|
| 97 |
+ maps.Copy(attributes, labels) |
|
| 100 | 98 |
} |
| 101 | 99 |
|
| 102 | 100 |
// ProcessClusterNotifications gets changes from store and add them to event list |
| ... | ... |
@@ -74,7 +74,7 @@ func TestEventsLogTimeout(t *testing.T) {
|
| 74 | 74 |
func TestLogEvents(t *testing.T) {
|
| 75 | 75 |
e := New() |
| 76 | 76 |
|
| 77 |
- for i := 0; i < eventsLimit+16; i++ {
|
|
| 77 |
+ for i := range eventsLimit + 16 {
|
|
| 78 | 78 |
num := strconv.Itoa(i) |
| 79 | 79 |
e.Log(events.Action("action_"+num), events.ContainerEventType, events.Actor{
|
| 80 | 80 |
ID: "cont_" + num, |
| ... | ... |
@@ -83,7 +83,7 @@ func TestLogEvents(t *testing.T) {
|
| 83 | 83 |
} |
| 84 | 84 |
time.Sleep(50 * time.Millisecond) |
| 85 | 85 |
current, l, _ := e.Subscribe() |
| 86 |
- for i := 0; i < 10; i++ {
|
|
| 86 |
+ for i := range 10 {
|
|
| 87 | 87 |
num := strconv.Itoa(i + eventsLimit + 16) |
| 88 | 88 |
e.Log(events.Action("action_"+num), events.ContainerEventType, events.Actor{
|
| 89 | 89 |
ID: "cont_" + num, |
| ... | ... |
@@ -56,7 +56,7 @@ func Scan(text string) (*events.Message, error) {
|
| 56 | 56 |
} |
| 57 | 57 |
|
| 58 | 58 |
attrs := make(map[string]string) |
| 59 |
- for _, a := range strings.Split(md["attributes"], ", ") {
|
|
| 59 |
+ for a := range strings.SplitSeq(md["attributes"], ", ") {
|
|
| 60 | 60 |
k, v, _ := strings.Cut(a, "=") |
| 61 | 61 |
attrs[k] = v |
| 62 | 62 |
} |
| ... | ... |
@@ -77,7 +77,7 @@ func TestCopyDir(t *testing.T) {
|
| 77 | 77 |
} |
| 78 | 78 |
|
| 79 | 79 |
func randomMode(baseMode int) os.FileMode {
|
| 80 |
- for i := 0; i < 7; i++ {
|
|
| 80 |
+ for i := range 7 {
|
|
| 81 | 81 |
baseMode = baseMode | (1&rand.Intn(2))<<uint(i) |
| 82 | 82 |
} |
| 83 | 83 |
return os.FileMode(baseMode) |
| ... | ... |
@@ -90,7 +90,7 @@ func populateSrcDir(t *testing.T, srcDir string, remainingDepth int) {
|
| 90 | 90 |
aTime := time.Unix(rand.Int63(), 0) |
| 91 | 91 |
mTime := time.Unix(rand.Int63(), 0) |
| 92 | 92 |
|
| 93 |
- for i := 0; i < 10; i++ {
|
|
| 93 |
+ for i := range 10 {
|
|
| 94 | 94 |
dirName := filepath.Join(srcDir, fmt.Sprintf("srcdir-%d", i))
|
| 95 | 95 |
// Owner all bits set |
| 96 | 96 |
assert.NilError(t, os.Mkdir(dirName, randomMode(0o700))) |
| ... | ... |
@@ -98,7 +98,7 @@ func populateSrcDir(t *testing.T, srcDir string, remainingDepth int) {
|
| 98 | 98 |
assert.NilError(t, system.Chtimes(dirName, aTime, mTime)) |
| 99 | 99 |
} |
| 100 | 100 |
|
| 101 |
- for i := 0; i < 10; i++ {
|
|
| 101 |
+ for i := range 10 {
|
|
| 102 | 102 |
fileName := filepath.Join(srcDir, fmt.Sprintf("srcfile-%d", i))
|
| 103 | 103 |
// Owner read bit set |
| 104 | 104 |
assert.NilError(t, os.WriteFile(fileName, []byte{}, randomMode(0o400)))
|
| ... | ... |
@@ -264,7 +264,7 @@ func (d *Driver) getLowerDirs(id string) ([]string, error) {
|
| 264 | 264 |
var lowersArray []string |
| 265 | 265 |
lowers, err := os.ReadFile(path.Join(d.dir(id), lowerFile)) |
| 266 | 266 |
if err == nil {
|
| 267 |
- for _, s := range strings.Split(string(lowers), ":") {
|
|
| 267 |
+ for s := range strings.SplitSeq(string(lowers), ":") {
|
|
| 268 | 268 |
lp, err := os.Readlink(path.Join(d.home, s)) |
| 269 | 269 |
if err != nil {
|
| 270 | 270 |
return nil, err |
| ... | ... |
@@ -469,7 +469,7 @@ func (d *Driver) getLowerDirs(id string) ([]string, error) {
|
| 469 | 469 |
var lowersArray []string |
| 470 | 470 |
lowers, err := os.ReadFile(path.Join(d.dir(id), lowerFile)) |
| 471 | 471 |
if err == nil {
|
| 472 |
- for _, s := range strings.Split(string(lowers), ":") {
|
|
| 472 |
+ for s := range strings.SplitSeq(string(lowers), ":") {
|
|
| 473 | 473 |
lp, err := os.Readlink(path.Join(d.home, s)) |
| 474 | 474 |
if err != nil {
|
| 475 | 475 |
return nil, err |
| ... | ... |
@@ -2,6 +2,7 @@ package images |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 |
+ "maps" |
|
| 5 | 6 |
|
| 6 | 7 |
"github.com/moby/moby/api/types/events" |
| 7 | 8 |
"github.com/moby/moby/v2/daemon/server/imagebackend" |
| ... | ... |
@@ -32,7 +33,5 @@ func copyAttributes(attributes, labels map[string]string) {
|
| 32 | 32 |
if labels == nil {
|
| 33 | 33 |
return |
| 34 | 34 |
} |
| 35 |
- for k, v := range labels {
|
|
| 36 |
- attributes[k] = v |
|
| 37 |
- } |
|
| 35 |
+ maps.Copy(attributes, labels) |
|
| 38 | 36 |
} |
| ... | ... |
@@ -343,8 +343,8 @@ func parseInitVersion(v string) (version string, commit string, _ error) {
|
| 343 | 343 |
} |
| 344 | 344 |
} |
| 345 | 345 |
parts[0] = strings.TrimSpace(parts[0]) |
| 346 |
- if strings.HasPrefix(parts[0], "tini version ") {
|
|
| 347 |
- version = strings.TrimPrefix(parts[0], "tini version ") |
|
| 346 |
+ if after, ok := strings.CutPrefix(parts[0], "tini version "); ok {
|
|
| 347 |
+ version = after |
|
| 348 | 348 |
} |
| 349 | 349 |
if version == "" && commit == "" {
|
| 350 | 350 |
return "", "", errors.Errorf("unknown output format: %s", v)
|
| ... | ... |
@@ -361,16 +361,16 @@ func parseInitVersion(v string) (version string, commit string, _ error) {
|
| 361 | 361 |
// commit: 69663f0bd4b60df09991c08812a60108003fa340 |
| 362 | 362 |
// spec: 1.0.0 |
| 363 | 363 |
func parseRuntimeVersion(v string) (runtime, version, commit string, _ error) {
|
| 364 |
- lines := strings.Split(strings.TrimSpace(v), "\n") |
|
| 365 |
- for _, line := range lines {
|
|
| 364 |
+ lines := strings.SplitSeq(strings.TrimSpace(v), "\n") |
|
| 365 |
+ for line := range lines {
|
|
| 366 | 366 |
if strings.Contains(line, "version") {
|
| 367 | 367 |
s := strings.Split(line, "version") |
| 368 | 368 |
runtime = strings.TrimSpace(s[0]) |
| 369 | 369 |
version = strings.TrimSpace(s[len(s)-1]) |
| 370 | 370 |
continue |
| 371 | 371 |
} |
| 372 |
- if strings.HasPrefix(line, "commit:") {
|
|
| 373 |
- commit = strings.TrimSpace(strings.TrimPrefix(line, "commit:")) |
|
| 372 |
+ if after, ok := strings.CutPrefix(line, "commit:"); ok {
|
|
| 373 |
+ commit = strings.TrimSpace(after) |
|
| 374 | 374 |
continue |
| 375 | 375 |
} |
| 376 | 376 |
} |
| ... | ... |
@@ -4,6 +4,7 @@ import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"errors" |
| 6 | 6 |
"fmt" |
| 7 |
+ "maps" |
|
| 7 | 8 |
"runtime" |
| 8 | 9 |
"time" |
| 9 | 10 |
|
| ... | ... |
@@ -35,9 +36,7 @@ func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, options |
| 35 | 35 |
|
| 36 | 36 |
// TODO(thaJeztah): do we need a deep copy here? Otherwise we could use maps.Clone (see https://github.com/moby/moby/commit/7917a36cc787ada58987320e67cc6d96858f3b55) |
| 37 | 37 |
ports := make(networktypes.PortMap, len(ctr.NetworkSettings.Ports)) |
| 38 |
- for k, pm := range ctr.NetworkSettings.Ports {
|
|
| 39 |
- ports[k] = pm |
|
| 40 |
- } |
|
| 38 |
+ maps.Copy(ports, ctr.NetworkSettings.Ports) |
|
| 41 | 39 |
|
| 42 | 40 |
apiNetworks := make(map[string]*networktypes.EndpointSettings) |
| 43 | 41 |
for nwName, epConf := range ctr.NetworkSettings.Networks {
|
| ... | ... |
@@ -4,6 +4,7 @@ import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"fmt" |
| 6 | 6 |
"io" |
| 7 |
+ "maps" |
|
| 7 | 8 |
"net/netip" |
| 8 | 9 |
"strconv" |
| 9 | 10 |
"strings" |
| ... | ... |
@@ -213,9 +214,7 @@ func (b *Builder) Prune(ctx context.Context, opts buildbackend.CachePruneOptions |
| 213 | 213 |
validFilters["until"] = true |
| 214 | 214 |
validFilters["label"] = true // TODO(tiborvass): handle label |
| 215 | 215 |
validFilters["label!"] = true // TODO(tiborvass): handle label! |
| 216 |
- for k, v := range cacheFields {
|
|
| 217 |
- validFilters[k] = v |
|
| 218 |
- } |
|
| 216 |
+ maps.Copy(validFilters, cacheFields) |
|
| 219 | 217 |
if err := opts.Filters.Validate(validFilters); err != nil {
|
| 220 | 218 |
return 0, nil, err |
| 221 | 219 |
} |
| ... | ... |
@@ -60,7 +60,7 @@ func (e *imageExporter) Resolve(ctx context.Context, id int, attrs map[string]st |
| 60 | 60 |
for k, v := range attrs {
|
| 61 | 61 |
switch exptypes.ImageExporterOptKey(k) {
|
| 62 | 62 |
case exptypes.OptKeyName: |
| 63 |
- for _, v := range strings.Split(v, ",") {
|
|
| 63 |
+ for v := range strings.SplitSeq(v, ",") {
|
|
| 64 | 64 |
ref, err := reference.ParseNormalizedNamed(v) |
| 65 | 65 |
if err != nil {
|
| 66 | 66 |
return nil, err |
| ... | ... |
@@ -100,7 +100,7 @@ func (i *imageExporterInstanceWrapper) processNamedCallback(ctx context.Context, |
| 100 | 100 |
return |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 |
- for _, name := range strings.Split(imageName, ",") {
|
|
| 103 |
+ for name := range strings.SplitSeq(imageName, ",") {
|
|
| 104 | 104 |
ref, err := reference.ParseNormalizedNamed(name) |
| 105 | 105 |
if err != nil {
|
| 106 | 106 |
// Shouldn't happen, but log if it does and continue. |
| ... | ... |
@@ -86,8 +86,8 @@ func appendDistributionSourceLabel(originLabel, repo string) string {
|
| 86 | 86 |
} |
| 87 | 87 |
|
| 88 | 88 |
func hasDistributionSource(label, repo string) bool {
|
| 89 |
- sources := strings.Split(label, ",") |
|
| 90 |
- for _, s := range sources {
|
|
| 89 |
+ sources := strings.SplitSeq(label, ",") |
|
| 90 |
+ for s := range sources {
|
|
| 91 | 91 |
if s == repo {
|
| 92 | 92 |
return true |
| 93 | 93 |
} |
| ... | ... |
@@ -63,10 +63,7 @@ func TestV2MetadataService(t *testing.T) {
|
| 63 | 63 |
if err != nil {
|
| 64 | 64 |
t.Fatalf("error calling Get: %v", err)
|
| 65 | 65 |
} |
| 66 |
- expectedMetadataEntries := len(vec.metadata) |
|
| 67 |
- if expectedMetadataEntries > 50 {
|
|
| 68 |
- expectedMetadataEntries = 50 |
|
| 69 |
- } |
|
| 66 |
+ expectedMetadataEntries := min(len(vec.metadata), 50) |
|
| 70 | 67 |
if !reflect.DeepEqual(metadata, vec.metadata[len(vec.metadata)-expectedMetadataEntries:len(vec.metadata)]) {
|
| 71 | 68 |
t.Fatal("Get returned incorrect layer ID")
|
| 72 | 69 |
} |
| ... | ... |
@@ -100,7 +97,7 @@ func TestV2MetadataService(t *testing.T) {
|
| 100 | 100 |
|
| 101 | 101 |
func randomDigest() digest.Digest {
|
| 102 | 102 |
b := [32]byte{}
|
| 103 |
- for i := 0; i < len(b); i++ {
|
|
| 103 |
+ for i := range len(b) {
|
|
| 104 | 104 |
b[i] = byte(rand.Intn(256)) |
| 105 | 105 |
} |
| 106 | 106 |
d := hex.EncodeToString(b[:]) |
| ... | ... |
@@ -6,6 +6,7 @@ import ( |
| 6 | 6 |
"io" |
| 7 | 7 |
"os" |
| 8 | 8 |
"runtime" |
| 9 |
+ "slices" |
|
| 9 | 10 |
"strings" |
| 10 | 11 |
"time" |
| 11 | 12 |
|
| ... | ... |
@@ -452,10 +453,8 @@ func (p *puller) validateMediaType(mediaType string) error {
|
| 452 | 452 |
} else {
|
| 453 | 453 |
allowedMediaTypes = defaultImageTypes |
| 454 | 454 |
} |
| 455 |
- for _, t := range allowedMediaTypes {
|
|
| 456 |
- if mediaType == t {
|
|
| 457 |
- return nil |
|
| 458 |
- } |
|
| 455 |
+ if slices.Contains(allowedMediaTypes, mediaType) {
|
|
| 456 |
+ return nil |
|
| 459 | 457 |
} |
| 460 | 458 |
|
| 461 | 459 |
configClass := mediaTypeClasses[mediaType] |
| ... | ... |
@@ -6,6 +6,7 @@ package filters |
| 6 | 6 |
|
| 7 | 7 |
import ( |
| 8 | 8 |
"encoding/json" |
| 9 |
+ "maps" |
|
| 9 | 10 |
"regexp" |
| 10 | 11 |
"strings" |
| 11 | 12 |
) |
| ... | ... |
@@ -280,9 +281,7 @@ func (args Args) Clone() (newArgs Args) {
|
| 280 | 280 |
var mm map[string]bool |
| 281 | 281 |
if m != nil {
|
| 282 | 282 |
mm = make(map[string]bool, len(m)) |
| 283 |
- for kk, v := range m {
|
|
| 284 |
- mm[kk] = v |
|
| 285 |
- } |
|
| 283 |
+ maps.Copy(mm, m) |
|
| 286 | 284 |
} |
| 287 | 285 |
newArgs.fields[k] = mm |
| 288 | 286 |
} |
| ... | ... |
@@ -188,7 +188,7 @@ func TestStoreLen(t *testing.T) {
|
| 188 | 188 |
|
| 189 | 189 |
expected := 10 |
| 190 | 190 |
for i := range expected {
|
| 191 |
- _, err := imgStore.Create([]byte(fmt.Sprintf(`{"comment": "abc%d", "rootfs": {"type": "layers"}}`, i)))
|
|
| 191 |
+ _, err := imgStore.Create(fmt.Appendf(nil, `{"comment": "abc%d", "rootfs": {"type": "layers"}}`, i))
|
|
| 192 | 192 |
assert.NilError(t, err) |
| 193 | 193 |
} |
| 194 | 194 |
numImages := imgStore.Len() |
| ... | ... |
@@ -10,6 +10,7 @@ import ( |
| 10 | 10 |
"path/filepath" |
| 11 | 11 |
"reflect" |
| 12 | 12 |
"runtime" |
| 13 |
+ "strings" |
|
| 13 | 14 |
|
| 14 | 15 |
"github.com/containerd/containerd/v2/pkg/tracing" |
| 15 | 16 |
"github.com/containerd/log" |
| ... | ... |
@@ -78,7 +79,7 @@ func (l *tarexporter) Load(ctx context.Context, inTar io.ReadCloser, outStream i |
| 78 | 78 |
} |
| 79 | 79 |
|
| 80 | 80 |
var parentLinks []parentLink |
| 81 |
- var imageIDsStr string |
|
| 81 |
+ var imageIDsStr strings.Builder |
|
| 82 | 82 |
var imageRefCount int |
| 83 | 83 |
|
| 84 | 84 |
for _, m := range manifest {
|
| ... | ... |
@@ -142,7 +143,7 @@ func (l *tarexporter) Load(ctx context.Context, inTar io.ReadCloser, outStream i |
| 142 | 142 |
if err != nil {
|
| 143 | 143 |
return err |
| 144 | 144 |
} |
| 145 |
- imageIDsStr += fmt.Sprintf("Loaded image ID: %s\n", imgID)
|
|
| 145 |
+ imageIDsStr.WriteString(fmt.Sprintf("Loaded image ID: %s\n", imgID))
|
|
| 146 | 146 |
|
| 147 | 147 |
imageRefCount = 0 |
| 148 | 148 |
for _, repoTag := range m.RepoTags {
|
| ... | ... |
@@ -172,7 +173,7 @@ func (l *tarexporter) Load(ctx context.Context, inTar io.ReadCloser, outStream i |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
if imageRefCount == 0 {
|
| 175 |
- outStream.Write([]byte(imageIDsStr)) |
|
| 175 |
+ outStream.Write([]byte(imageIDsStr.String())) |
|
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
return nil |
| ... | ... |
@@ -81,10 +81,7 @@ func PrefixCompare(a, b netip.Prefix) int {
|
| 81 | 81 |
|
| 82 | 82 |
// PrefixAfter returns the prefix of size 'sz' right after 'prev'. |
| 83 | 83 |
func PrefixAfter(prev netip.Prefix, sz int) netip.Prefix {
|
| 84 |
- s := sz |
|
| 85 |
- if prev.Bits() < sz {
|
|
| 86 |
- s = prev.Bits() |
|
| 87 |
- } |
|
| 84 |
+ s := min(prev.Bits(), sz) |
|
| 88 | 85 |
addr := ipbits.Add(prev.Addr(), 1, uint(prev.Addr().BitLen()-s)) |
| 89 | 86 |
if addr.IsUnspecified() {
|
| 90 | 87 |
return netip.Prefix{}
|
| ... | ... |
@@ -287,8 +287,8 @@ func BenchmarkWrite(b *testing.B) {
|
| 287 | 287 |
data := []byte("Test line for testing stdwriter performance\n")
|
| 288 | 288 |
data = bytes.Repeat(data, 100) |
| 289 | 289 |
b.SetBytes(int64(len(data))) |
| 290 |
- b.ResetTimer() |
|
| 291 |
- for i := 0; i < b.N; i++ {
|
|
| 290 |
+ |
|
| 291 |
+ for b.Loop() {
|
|
| 292 | 292 |
if _, err := w.Write(data); err != nil {
|
| 293 | 293 |
b.Fatal(err) |
| 294 | 294 |
} |
| ... | ... |
@@ -96,10 +96,7 @@ loop0: |
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
// add new byte slice to the buffers slice and continue writing |
| 99 |
- nextCap := b.Cap() * 2 |
|
| 100 |
- if nextCap > maxCap {
|
|
| 101 |
- nextCap = maxCap |
|
| 102 |
- } |
|
| 99 |
+ nextCap := min(b.Cap()*2, maxCap) |
|
| 103 | 100 |
bp.buf = append(bp.buf, getBuffer(nextCap)) |
| 104 | 101 |
} |
| 105 | 102 |
bp.wait.Broadcast() |
| ... | ... |
@@ -184,7 +184,7 @@ func TestBytesPipeWriteRandomChunks(t *testing.T) {
|
| 184 | 184 |
func BenchmarkBytesPipeWrite(b *testing.B) {
|
| 185 | 185 |
b.ReportAllocs() |
| 186 | 186 |
testData := []byte("pretty short line, because why not?")
|
| 187 |
- for i := 0; i < b.N; i++ {
|
|
| 187 |
+ for b.Loop() {
|
|
| 188 | 188 |
readBuf := make([]byte, 1024) |
| 189 | 189 |
buf := New() |
| 190 | 190 |
go func() {
|
| ... | ... |
@@ -193,7 +193,7 @@ func BenchmarkBytesPipeWrite(b *testing.B) {
|
| 193 | 193 |
_, err = buf.Read(readBuf) |
| 194 | 194 |
} |
| 195 | 195 |
}() |
| 196 |
- for j := 0; j < 1000; j++ {
|
|
| 196 |
+ for range 1000 {
|
|
| 197 | 197 |
_, _ = buf.Write(testData) |
| 198 | 198 |
} |
| 199 | 199 |
_ = buf.Close() |
| ... | ... |
@@ -203,14 +203,14 @@ func BenchmarkBytesPipeWrite(b *testing.B) {
|
| 203 | 203 |
func BenchmarkBytesPipeRead(b *testing.B) {
|
| 204 | 204 |
b.ReportAllocs() |
| 205 | 205 |
rd := make([]byte, 512) |
| 206 |
- for i := 0; i < b.N; i++ {
|
|
| 206 |
+ for b.Loop() {
|
|
| 207 | 207 |
b.StopTimer() |
| 208 | 208 |
buf := New() |
| 209 |
- for j := 0; j < 500; j++ {
|
|
| 209 |
+ for range 500 {
|
|
| 210 | 210 |
_, _ = buf.Write(make([]byte, 1024)) |
| 211 | 211 |
} |
| 212 | 212 |
b.StartTimer() |
| 213 |
- for j := 0; j < 1000; j++ {
|
|
| 213 |
+ for range 1000 {
|
|
| 214 | 214 |
if n, _ := buf.Read(rd); n != 512 {
|
| 215 | 215 |
b.Fatalf("Wrong number of bytes: %d", n)
|
| 216 | 216 |
} |
| ... | ... |
@@ -130,7 +130,7 @@ func TestRaceUnbuffered(t *testing.T) {
|
| 130 | 130 |
func BenchmarkUnbuffered(b *testing.B) {
|
| 131 | 131 |
writer := new(unbuffered) |
| 132 | 132 |
setUpWriter := func() {
|
| 133 |
- for i := 0; i < 100; i++ {
|
|
| 133 |
+ for range 100 {
|
|
| 134 | 134 |
writer.Add(devNullCloser(0)) |
| 135 | 135 |
writer.Add(devNullCloser(0)) |
| 136 | 136 |
writer.Add(devNullCloser(0)) |
| ... | ... |
@@ -138,20 +138,20 @@ func BenchmarkUnbuffered(b *testing.B) {
|
| 138 | 138 |
} |
| 139 | 139 |
testLine := "Line that thinks that it is log line from docker" |
| 140 | 140 |
var buf bytes.Buffer |
| 141 |
- for i := 0; i < 100; i++ {
|
|
| 141 |
+ for range 100 {
|
|
| 142 | 142 |
buf.WriteString(testLine + "\n") |
| 143 | 143 |
} |
| 144 | 144 |
// line without eol |
| 145 | 145 |
buf.WriteString(testLine) |
| 146 | 146 |
testText := buf.Bytes() |
| 147 | 147 |
b.SetBytes(int64(5 * len(testText))) |
| 148 |
- b.ResetTimer() |
|
| 149 |
- for i := 0; i < b.N; i++ {
|
|
| 148 |
+ |
|
| 149 |
+ for b.Loop() {
|
|
| 150 | 150 |
b.StopTimer() |
| 151 | 151 |
setUpWriter() |
| 152 | 152 |
b.StartTimer() |
| 153 | 153 |
|
| 154 |
- for j := 0; j < 5; j++ {
|
|
| 154 |
+ for range 5 {
|
|
| 155 | 155 |
if _, err := writer.Write(testText); err != nil {
|
| 156 | 156 |
b.Fatal(err) |
| 157 | 157 |
} |
| ... | ... |
@@ -16,11 +16,8 @@ func compare(v1, v2 string) int {
|
| 16 | 16 |
otherTab = strings.Split(v2, ".") |
| 17 | 17 |
) |
| 18 | 18 |
|
| 19 |
- maxVer := len(currTab) |
|
| 20 |
- if len(otherTab) > maxVer {
|
|
| 21 |
- maxVer = len(otherTab) |
|
| 22 |
- } |
|
| 23 |
- for i := 0; i < maxVer; i++ {
|
|
| 19 |
+ maxVer := max(len(otherTab), len(currTab)) |
|
| 20 |
+ for i := range maxVer {
|
|
| 24 | 21 |
var currInt, otherInt int |
| 25 | 22 |
|
| 26 | 23 |
if len(currTab) > i {
|
| ... | ... |
@@ -923,7 +923,7 @@ func TestMethods(t *testing.T) {
|
| 923 | 923 |
t.Fatalf("Unexpected sequence string: %s", hnd.head.toString())
|
| 924 | 924 |
} |
| 925 | 925 |
|
| 926 |
- for i := 0; i < 192; i++ {
|
|
| 926 |
+ for range 192 {
|
|
| 927 | 927 |
_, err := hnd.SetAny(false) |
| 928 | 928 |
if err != nil {
|
| 929 | 929 |
t.Fatal(err) |
| ... | ... |
@@ -1185,7 +1185,7 @@ func TestMarshalJSON(t *testing.T) {
|
| 1185 | 1185 |
hnd := New(uint64(len(expected) * 8)) |
| 1186 | 1186 |
|
| 1187 | 1187 |
for i, c := range expected {
|
| 1188 |
- for j := 0; j < 8; j++ {
|
|
| 1188 |
+ for j := range 8 {
|
|
| 1189 | 1189 |
if c&(1<<j) == 0 {
|
| 1190 | 1190 |
continue |
| 1191 | 1191 |
} |
| ... | ... |
@@ -283,7 +283,7 @@ func checkTable(ctx context.Context, ips []string, port, networkName, tableName |
| 283 | 283 |
func waitWriters(parallelWriters int, mustWrite bool, doneCh chan resultTuple) map[string]int {
|
| 284 | 284 |
var totalKeys int |
| 285 | 285 |
resultTable := make(map[string]int) |
| 286 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 286 |
+ for i := range parallelWriters {
|
|
| 287 | 287 |
log.G(context.TODO()).Infof("Waiting for %d workers", parallelWriters-i)
|
| 288 | 288 |
workerReturn := <-doneCh |
| 289 | 289 |
totalKeys += workerReturn.result |
| ... | ... |
@@ -337,7 +337,7 @@ func doClusterPeers(ips []string, args []string) {
|
| 337 | 337 |
doneCh := make(chan resultTuple, len(ips)) |
| 338 | 338 |
expectedPeers, _ := strconv.Atoi(args[0]) |
| 339 | 339 |
maxRetry, _ := strconv.Atoi(args[1]) |
| 340 |
- for retry := 0; retry < maxRetry; retry++ {
|
|
| 340 |
+ for retry := range maxRetry {
|
|
| 341 | 341 |
// check all the nodes |
| 342 | 342 |
for _, ip := range ips {
|
| 343 | 343 |
go clusterPeersNumber(ip, servicePort, doneCh) |
| ... | ... |
@@ -398,7 +398,7 @@ func doNetworkPeers(ips []string, args []string) {
|
| 398 | 398 |
networkName := args[0] |
| 399 | 399 |
expectedPeers, _ := strconv.Atoi(args[1]) |
| 400 | 400 |
maxRetry, _ := strconv.Atoi(args[2]) |
| 401 |
- for retry := 0; retry < maxRetry; retry++ {
|
|
| 401 |
+ for retry := range maxRetry {
|
|
| 402 | 402 |
// check all the nodes |
| 403 | 403 |
for _, ip := range ips {
|
| 404 | 404 |
go networkPeersNumber(ip, servicePort, networkName, doneCh) |
| ... | ... |
@@ -469,14 +469,14 @@ func doWriteKeys(ips []string, args []string) {
|
| 469 | 469 |
|
| 470 | 470 |
doneCh := make(chan resultTuple, parallelWriters) |
| 471 | 471 |
// Enable watch of tables from clients |
| 472 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 472 |
+ for i := range parallelWriters {
|
|
| 473 | 473 |
go clientWatchTable(ips[i], servicePort, networkName, tableName, doneCh) |
| 474 | 474 |
} |
| 475 | 475 |
waitWriters(parallelWriters, false, doneCh) |
| 476 | 476 |
|
| 477 | 477 |
// Start parallel writers that will create and delete unique keys |
| 478 | 478 |
defer close(doneCh) |
| 479 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 479 |
+ for i := range parallelWriters {
|
|
| 480 | 480 |
key := "key-" + strconv.Itoa(i) + "-" |
| 481 | 481 |
log.G(context.TODO()).Infof("Spawn worker: %d on IP:%s", i, ips[i])
|
| 482 | 482 |
go writeKeysNumber(ips[i], servicePort, networkName, tableName, key, numberOfKeys, doneCh) |
| ... | ... |
@@ -502,14 +502,14 @@ func doDeleteKeys(ips []string, args []string) {
|
| 502 | 502 |
|
| 503 | 503 |
doneCh := make(chan resultTuple, parallelWriters) |
| 504 | 504 |
// Enable watch of tables from clients |
| 505 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 505 |
+ for i := range parallelWriters {
|
|
| 506 | 506 |
go clientWatchTable(ips[i], servicePort, networkName, tableName, doneCh) |
| 507 | 507 |
} |
| 508 | 508 |
waitWriters(parallelWriters, false, doneCh) |
| 509 | 509 |
|
| 510 | 510 |
// Start parallel writers that will create and delete unique keys |
| 511 | 511 |
defer close(doneCh) |
| 512 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 512 |
+ for i := range parallelWriters {
|
|
| 513 | 513 |
key := "key-" + strconv.Itoa(i) + "-" |
| 514 | 514 |
log.G(context.TODO()).Infof("Spawn worker: %d on IP:%s", i, ips[i])
|
| 515 | 515 |
go deleteKeysNumber(ips[i], servicePort, networkName, tableName, key, numberOfKeys, doneCh) |
| ... | ... |
@@ -535,14 +535,14 @@ func doWriteDeleteUniqueKeys(ips []string, args []string) {
|
| 535 | 535 |
|
| 536 | 536 |
doneCh := make(chan resultTuple, parallelWriters) |
| 537 | 537 |
// Enable watch of tables from clients |
| 538 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 538 |
+ for i := range parallelWriters {
|
|
| 539 | 539 |
go clientWatchTable(ips[i], servicePort, networkName, tableName, doneCh) |
| 540 | 540 |
} |
| 541 | 541 |
waitWriters(parallelWriters, false, doneCh) |
| 542 | 542 |
|
| 543 | 543 |
// Start parallel writers that will create and delete unique keys |
| 544 | 544 |
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(writeTimeSec)*time.Second) |
| 545 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 545 |
+ for i := range parallelWriters {
|
|
| 546 | 546 |
key := "key-" + strconv.Itoa(i) + "-" |
| 547 | 547 |
log.G(ctx).Infof("Spawn worker: %d on IP:%s", i, ips[i])
|
| 548 | 548 |
go writeDeleteUniqueKeys(ctx, ips[i], servicePort, networkName, tableName, key, doneCh) |
| ... | ... |
@@ -572,7 +572,7 @@ func doWriteUniqueKeys(ips []string, args []string) {
|
| 572 | 572 |
|
| 573 | 573 |
doneCh := make(chan resultTuple, parallelWriters) |
| 574 | 574 |
// Enable watch of tables from clients |
| 575 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 575 |
+ for i := range parallelWriters {
|
|
| 576 | 576 |
go clientWatchTable(ips[i], servicePort, networkName, tableName, doneCh) |
| 577 | 577 |
} |
| 578 | 578 |
waitWriters(parallelWriters, false, doneCh) |
| ... | ... |
@@ -580,7 +580,7 @@ func doWriteUniqueKeys(ips []string, args []string) {
|
| 580 | 580 |
// Start parallel writers that will create and delete unique keys |
| 581 | 581 |
defer close(doneCh) |
| 582 | 582 |
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(writeTimeSec)*time.Second) |
| 583 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 583 |
+ for i := range parallelWriters {
|
|
| 584 | 584 |
key := "key-" + strconv.Itoa(i) + "-" |
| 585 | 585 |
log.G(ctx).Infof("Spawn worker: %d on IP:%s", i, ips[i])
|
| 586 | 586 |
go writeUniqueKeys(ctx, ips[i], servicePort, networkName, tableName, key, doneCh) |
| ... | ... |
@@ -609,7 +609,7 @@ func doWriteDeleteLeaveJoin(ips []string, args []string) {
|
| 609 | 609 |
doneCh := make(chan resultTuple, parallelWriters) |
| 610 | 610 |
defer close(doneCh) |
| 611 | 611 |
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(writeTimeSec)*time.Second) |
| 612 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 612 |
+ for i := range parallelWriters {
|
|
| 613 | 613 |
key := "key-" + strconv.Itoa(i) + "-" |
| 614 | 614 |
log.G(ctx).Infof("Spawn worker: %d on IP:%s", i, ips[i])
|
| 615 | 615 |
go writeDeleteLeaveJoin(ctx, ips[i], servicePort, networkName, tableName, key, doneCh) |
| ... | ... |
@@ -638,7 +638,7 @@ func doWriteDeleteWaitLeaveJoin(ips []string, args []string) {
|
| 638 | 638 |
doneCh := make(chan resultTuple, parallelWriters) |
| 639 | 639 |
defer close(doneCh) |
| 640 | 640 |
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(writeTimeSec)*time.Second) |
| 641 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 641 |
+ for i := range parallelWriters {
|
|
| 642 | 642 |
key := "key-" + strconv.Itoa(i) + "-" |
| 643 | 643 |
log.G(ctx).Infof("Spawn worker: %d on IP:%s", i, ips[i])
|
| 644 | 644 |
go writeDeleteUniqueKeys(ctx, ips[i], servicePort, networkName, tableName, key, doneCh) |
| ... | ... |
@@ -650,7 +650,7 @@ func doWriteDeleteWaitLeaveJoin(ips []string, args []string) {
|
| 650 | 650 |
log.G(ctx).Infof("Written a total of %d keys on the cluster", keyMap[totalWrittenKeys])
|
| 651 | 651 |
|
| 652 | 652 |
// The writers will leave the network |
| 653 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 653 |
+ for i := range parallelWriters {
|
|
| 654 | 654 |
log.G(ctx).Infof("worker leaveNetwork: %d on IP:%s", i, ips[i])
|
| 655 | 655 |
go leaveNetwork(ips[i], servicePort, networkName, doneCh) |
| 656 | 656 |
} |
| ... | ... |
@@ -660,7 +660,7 @@ func doWriteDeleteWaitLeaveJoin(ips []string, args []string) {
|
| 660 | 660 |
time.Sleep(100 * time.Millisecond) |
| 661 | 661 |
|
| 662 | 662 |
// The writers will join the network |
| 663 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 663 |
+ for i := range parallelWriters {
|
|
| 664 | 664 |
log.G(ctx).Infof("worker joinNetwork: %d on IP:%s", i, ips[i])
|
| 665 | 665 |
go joinNetwork(ips[i], servicePort, networkName, doneCh) |
| 666 | 666 |
} |
| ... | ... |
@@ -684,7 +684,7 @@ func doWriteWaitLeave(ips []string, args []string) {
|
| 684 | 684 |
doneCh := make(chan resultTuple, parallelWriters) |
| 685 | 685 |
defer close(doneCh) |
| 686 | 686 |
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(writeTimeSec)*time.Second) |
| 687 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 687 |
+ for i := range parallelWriters {
|
|
| 688 | 688 |
key := "key-" + strconv.Itoa(i) + "-" |
| 689 | 689 |
log.G(ctx).Infof("Spawn worker: %d on IP:%s", i, ips[i])
|
| 690 | 690 |
go writeUniqueKeys(ctx, ips[i], servicePort, networkName, tableName, key, doneCh) |
| ... | ... |
@@ -696,7 +696,7 @@ func doWriteWaitLeave(ips []string, args []string) {
|
| 696 | 696 |
log.G(ctx).Infof("Written a total of %d keys on the cluster", keyMap[totalWrittenKeys])
|
| 697 | 697 |
|
| 698 | 698 |
// The writers will leave the network |
| 699 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 699 |
+ for i := range parallelWriters {
|
|
| 700 | 700 |
log.G(ctx).Infof("worker leaveNetwork: %d on IP:%s", i, ips[i])
|
| 701 | 701 |
go leaveNetwork(ips[i], servicePort, networkName, doneCh) |
| 702 | 702 |
} |
| ... | ... |
@@ -721,7 +721,7 @@ func doWriteWaitLeaveJoin(ips []string, args []string) {
|
| 721 | 721 |
doneCh := make(chan resultTuple, parallelWriters) |
| 722 | 722 |
defer close(doneCh) |
| 723 | 723 |
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(writeTimeSec)*time.Second) |
| 724 |
- for i := 0; i < parallelWriters; i++ {
|
|
| 724 |
+ for i := range parallelWriters {
|
|
| 725 | 725 |
key := "key-" + strconv.Itoa(i) + "-" |
| 726 | 726 |
log.G(ctx).Infof("Spawn worker: %d on IP:%s", i, ips[i])
|
| 727 | 727 |
go writeUniqueKeys(ctx, ips[i], servicePort, networkName, tableName, key, doneCh) |
| ... | ... |
@@ -734,7 +734,7 @@ func doWriteWaitLeaveJoin(ips []string, args []string) {
|
| 734 | 734 |
|
| 735 | 735 |
keysExpected := keyMap[totalWrittenKeys] |
| 736 | 736 |
// The Leavers will leave the network |
| 737 |
- for i := 0; i < parallelLeaver; i++ {
|
|
| 737 |
+ for i := range parallelLeaver {
|
|
| 738 | 738 |
log.G(ctx).Infof("worker leaveNetwork: %d on IP:%s", i, ips[i])
|
| 739 | 739 |
go leaveNetwork(ips[i], servicePort, networkName, doneCh) |
| 740 | 740 |
// Once a node leave all the keys written previously will be deleted, so the expected keys will consider that as removed |
| ... | ... |
@@ -746,7 +746,7 @@ func doWriteWaitLeaveJoin(ips []string, args []string) {
|
| 746 | 746 |
time.Sleep(100 * time.Millisecond) |
| 747 | 747 |
|
| 748 | 748 |
// The writers will join the network |
| 749 |
- for i := 0; i < parallelLeaver; i++ {
|
|
| 749 |
+ for i := range parallelLeaver {
|
|
| 750 | 750 |
log.G(ctx).Infof("worker joinNetwork: %d on IP:%s", i, ips[i])
|
| 751 | 751 |
go joinNetwork(ips[i], servicePort, networkName, doneCh) |
| 752 | 752 |
} |
| ... | ... |
@@ -3,6 +3,7 @@ package cnmallocator |
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"fmt" |
| 6 |
+ "maps" |
|
| 6 | 7 |
"net" |
| 7 | 8 |
"net/netip" |
| 8 | 9 |
"slices" |
| ... | ... |
@@ -725,14 +726,10 @@ func (na *cnmNetworkAllocator) allocateDriverState(d *networkDriver, n *api.Netw |
| 725 | 725 |
// reconcile the driver specific options from the network spec |
| 726 | 726 |
// and from the operational state retrieved from the store |
| 727 | 727 |
if n.Spec.DriverConfig != nil {
|
| 728 |
- for k, v := range n.Spec.DriverConfig.Options {
|
|
| 729 |
- options[k] = v |
|
| 730 |
- } |
|
| 728 |
+ maps.Copy(options, n.Spec.DriverConfig.Options) |
|
| 731 | 729 |
} |
| 732 | 730 |
if n.DriverState != nil {
|
| 733 |
- for k, v := range n.DriverState.Options {
|
|
| 734 |
- options[k] = v |
|
| 735 |
- } |
|
| 731 |
+ maps.Copy(options, n.DriverState.Options) |
|
| 736 | 732 |
} |
| 737 | 733 |
|
| 738 | 734 |
// Construct IPAM data for driver consumption. |
| ... | ... |
@@ -49,6 +49,7 @@ import ( |
| 49 | 49 |
"net" |
| 50 | 50 |
"path/filepath" |
| 51 | 51 |
"runtime" |
| 52 |
+ "slices" |
|
| 52 | 53 |
"strings" |
| 53 | 54 |
"sync" |
| 54 | 55 |
"time" |
| ... | ... |
@@ -831,10 +832,8 @@ func (c *Controller) Networks(ctx context.Context) []*Network {
|
| 831 | 831 |
|
| 832 | 832 |
// WalkNetworks uses the provided function to walk the Network(s) managed by this controller. |
| 833 | 833 |
func (c *Controller) WalkNetworks(walker NetworkWalker) {
|
| 834 |
- for _, n := range c.Networks(context.TODO()) {
|
|
| 835 |
- if walker(n) {
|
|
| 836 |
- return |
|
| 837 |
- } |
|
| 834 |
+ if slices.ContainsFunc(c.Networks(context.TODO()), walker) {
|
|
| 835 |
+ return |
|
| 838 | 836 |
} |
| 839 | 837 |
} |
| 840 | 838 |
|
| ... | ... |
@@ -7,6 +7,7 @@ import ( |
| 7 | 7 |
"net" |
| 8 | 8 |
"net/http" |
| 9 | 9 |
"strconv" |
| 10 |
+ "strings" |
|
| 10 | 11 |
"sync" |
| 11 | 12 |
"time" |
| 12 | 13 |
|
| ... | ... |
@@ -154,13 +155,13 @@ func (s *Server) help(w http.ResponseWriter, r *http.Request) {
|
| 154 | 154 |
"url": r.URL.String(), |
| 155 | 155 |
}).Info("help done")
|
| 156 | 156 |
|
| 157 |
- var result string |
|
| 157 |
+ var result strings.Builder |
|
| 158 | 158 |
s.mu.Lock() |
| 159 | 159 |
for path := range s.handlers {
|
| 160 |
- result += fmt.Sprintf("%s\n", path)
|
|
| 160 |
+ result.WriteString(fmt.Sprintf("%s\n", path))
|
|
| 161 | 161 |
} |
| 162 | 162 |
s.mu.Unlock() |
| 163 |
- _, _ = HTTPReply(w, CommandSucceed(&StringCmd{Info: result}), jsonOutput)
|
|
| 163 |
+ _, _ = HTTPReply(w, CommandSucceed(&StringCmd{Info: result.String()}), jsonOutput)
|
|
| 164 | 164 |
} |
| 165 | 165 |
|
| 166 | 166 |
func ready(w http.ResponseWriter, r *http.Request) {
|
| ... | ... |
@@ -3,6 +3,7 @@ package diagnostic |
| 3 | 3 |
import ( |
| 4 | 4 |
"fmt" |
| 5 | 5 |
"net/netip" |
| 6 |
+ "strings" |
|
| 6 | 7 |
) |
| 7 | 8 |
|
| 8 | 9 |
// StringInterface interface that has to be implemented by messages |
| ... | ... |
@@ -82,11 +83,12 @@ type TableObj struct {
|
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 | 84 |
func (t *TableObj) String() string {
|
| 85 |
- output := fmt.Sprintf("total entries: %d\n", t.Length)
|
|
| 85 |
+ var output strings.Builder |
|
| 86 |
+ output.WriteString(fmt.Sprintf("total entries: %d\n", t.Length))
|
|
| 86 | 87 |
for _, e := range t.Elements {
|
| 87 |
- output += e.String() |
|
| 88 |
+ output.WriteString(e.String()) |
|
| 88 | 89 |
} |
| 89 |
- return output |
|
| 90 |
+ return output.String() |
|
| 90 | 91 |
} |
| 91 | 92 |
|
| 92 | 93 |
// PeerEntryObj entry in the networkdb peer table |
| ... | ... |
@@ -211,7 +211,7 @@ func compareBindings(a, b []portmapperapi.PortBinding) bool {
|
| 211 | 211 |
if len(a) != len(b) {
|
| 212 | 212 |
return false |
| 213 | 213 |
} |
| 214 |
- for i := 0; i < len(a); i++ {
|
|
| 214 |
+ for i := range a {
|
|
| 215 | 215 |
if !comparePortBinding(&a[i].PortBinding, &b[i].PortBinding) {
|
| 216 | 216 |
return false |
| 217 | 217 |
} |
| ... | ... |
@@ -1250,7 +1250,7 @@ func TestCreateParallel(t *testing.T) {
|
| 1250 | 1250 |
ipV4Data := getIPv4Data(t) |
| 1251 | 1251 |
|
| 1252 | 1252 |
ch := make(chan error, 100) |
| 1253 |
- for i := 0; i < 100; i++ {
|
|
| 1253 |
+ for i := range 100 {
|
|
| 1254 | 1254 |
name := "net" + strconv.Itoa(i) |
| 1255 | 1255 |
c.Go(t, func() {
|
| 1256 | 1256 |
config := &networkConfiguration{BridgeName: name, EnableIPv4: true}
|
| ... | ... |
@@ -1269,7 +1269,7 @@ func TestCreateParallel(t *testing.T) {
|
| 1269 | 1269 |
} |
| 1270 | 1270 |
// wait for the go routines |
| 1271 | 1271 |
var success int |
| 1272 |
- for i := 0; i < 100; i++ {
|
|
| 1272 |
+ for range 100 {
|
|
| 1273 | 1273 |
val := <-ch |
| 1274 | 1274 |
if val == nil {
|
| 1275 | 1275 |
success++ |
| ... | ... |
@@ -163,16 +163,16 @@ func testIptabler(t *testing.T, tn string, config firewaller.Config, netConfig f |
| 163 | 163 |
// - iptables-nft and iptables-legacy pick a different order when dumping all tables |
| 164 | 164 |
// - if the raw table isn't used it's not included in the all-tables dump but, once it's been used, it's always |
| 165 | 165 |
// included ... so, "cleaned" results would differ only in the empty raw table. |
| 166 |
- var dump string |
|
| 166 |
+ var dump strings.Builder |
|
| 167 | 167 |
for _, table := range []string{"raw", "filter", "nat"} {
|
| 168 | 168 |
res := icmd.RunCommand(cmd+"-save", "-t", table) |
| 169 | 169 |
assert.Assert(t, res.Error) |
| 170 | 170 |
if !en {
|
| 171 | 171 |
name = tn + "/no" |
| 172 | 172 |
} |
| 173 |
- dump += res.Combined() |
|
| 173 |
+ dump.WriteString(res.Combined()) |
|
| 174 | 174 |
} |
| 175 |
- assert.Check(t, golden.String(stripComments(dump), name+"__"+cmd+".golden")) |
|
| 175 |
+ assert.Check(t, golden.String(stripComments(dump.String()), name+"__"+cmd+".golden")) |
|
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
makePB := func(hip string, cip netip.Addr) types.PortBinding {
|
| ... | ... |
@@ -1036,9 +1036,7 @@ func (ep *Endpoint) getEtcHostsAddrs() []netip.Addr {
|
| 1036 | 1036 |
// in a Dictionary of Key-Value pair |
| 1037 | 1037 |
func EndpointOptionGeneric(generic map[string]any) EndpointOption {
|
| 1038 | 1038 |
return func(ep *Endpoint) {
|
| 1039 |
- for k, v := range generic {
|
|
| 1040 |
- ep.generic[k] = v |
|
| 1041 |
- } |
|
| 1039 |
+ maps.Copy(ep.generic, generic) |
|
| 1042 | 1040 |
} |
| 1043 | 1041 |
} |
| 1044 | 1042 |
|
| ... | ... |
@@ -371,7 +371,7 @@ func TestConcurrentWrites(t *testing.T) {
|
| 371 | 371 |
} |
| 372 | 372 |
|
| 373 | 373 |
group := new(errgroup.Group) |
| 374 |
- for i := byte(0); i < 10; i++ {
|
|
| 374 |
+ for i := range byte(10) {
|
|
| 375 | 375 |
group.Go(func() error {
|
| 376 | 376 |
addr, ok := netip.AddrFromSlice([]byte{i, i, i, i})
|
| 377 | 377 |
assert.Assert(t, ok) |
| ... | ... |
@@ -382,7 +382,7 @@ func TestConcurrentWrites(t *testing.T) {
|
| 382 | 382 |
}, |
| 383 | 383 |
} |
| 384 | 384 |
|
| 385 |
- for j := 0; j < 25; j++ {
|
|
| 385 |
+ for range 25 {
|
|
| 386 | 386 |
if err := Add(file.Name(), rec); err != nil {
|
| 387 | 387 |
return err |
| 388 | 388 |
} |
| ... | ... |
@@ -429,7 +429,7 @@ func benchDelete(b *testing.B) {
|
| 429 | 429 |
|
| 430 | 430 |
var records []Record |
| 431 | 431 |
var toDelete []Record |
| 432 |
- for i := byte(0); i < 255; i++ {
|
|
| 432 |
+ for i := range byte(255) {
|
|
| 433 | 433 |
addr, ok := netip.AddrFromSlice([]byte{i, i, i, i})
|
| 434 | 434 |
assert.Assert(b, ok) |
| 435 | 435 |
record := Record{
|
| ... | ... |
@@ -453,7 +453,7 @@ func benchDelete(b *testing.B) {
|
| 453 | 453 |
} |
| 454 | 454 |
|
| 455 | 455 |
func BenchmarkDelete(b *testing.B) {
|
| 456 |
- for i := 0; i < b.N; i++ {
|
|
| 456 |
+ for b.Loop() {
|
|
| 457 | 457 |
benchDelete(b) |
| 458 | 458 |
} |
| 459 | 459 |
} |
| ... | ... |
@@ -203,10 +203,7 @@ func (as *AddrSet) AddrsInPrefix(prefix netip.Prefix) (hi, lo uint64) {
|
| 203 | 203 |
} |
| 204 | 204 |
|
| 205 | 205 |
func (as *AddrSet) getBitmap(addr netip.Addr) (*bitmap.Bitmap, netip.Prefix, error) {
|
| 206 |
- bits := as.pool.Addr().BitLen() - as.pool.Bits() |
|
| 207 |
- if bits > maxBitsPerBitmap {
|
|
| 208 |
- bits = maxBitsPerBitmap |
|
| 209 |
- } |
|
| 206 |
+ bits := min(as.pool.Addr().BitLen()-as.pool.Bits(), maxBitsPerBitmap) |
|
| 210 | 207 |
bmKey, err := addr.Prefix(as.pool.Addr().BitLen() - bits) |
| 211 | 208 |
if err != nil {
|
| 212 | 209 |
return nil, netip.Prefix{}, err
|
| ... | ... |
@@ -220,10 +217,7 @@ func (as *AddrSet) getBitmap(addr netip.Addr) (*bitmap.Bitmap, netip.Prefix, err |
| 220 | 220 |
} |
| 221 | 221 |
|
| 222 | 222 |
func (as *AddrSet) addrsPerBitmap() uint64 {
|
| 223 |
- bits := as.pool.Addr().BitLen() - as.pool.Bits() |
|
| 224 |
- if bits > maxBitsPerBitmap {
|
|
| 225 |
- bits = maxBitsPerBitmap |
|
| 226 |
- } |
|
| 223 |
+ bits := min(as.pool.Addr().BitLen()-as.pool.Bits(), maxBitsPerBitmap) |
|
| 227 | 224 |
return uint64(1) << bits |
| 228 | 225 |
} |
| 229 | 226 |
|
| ... | ... |
@@ -199,19 +199,19 @@ func TestRCModify(t *testing.T) {
|
| 199 | 199 |
for _, tc := range testcases {
|
| 200 | 200 |
t.Run(tc.name, func(t *testing.T) {
|
| 201 | 201 |
tc := tc |
| 202 |
- var input string |
|
| 202 |
+ var input strings.Builder |
|
| 203 | 203 |
if len(tc.inputNS) != 0 {
|
| 204 | 204 |
for _, ns := range tc.inputNS {
|
| 205 |
- input += "nameserver " + ns + "\n" |
|
| 205 |
+ input.WriteString("nameserver " + ns + "\n")
|
|
| 206 | 206 |
} |
| 207 | 207 |
} |
| 208 | 208 |
if len(tc.inputSearch) != 0 {
|
| 209 |
- input += "search " + strings.Join(tc.inputSearch, " ") + "\n" |
|
| 209 |
+ input.WriteString("search " + strings.Join(tc.inputSearch, " ") + "\n")
|
|
| 210 | 210 |
} |
| 211 | 211 |
if len(tc.inputOptions) != 0 {
|
| 212 |
- input += "options " + strings.Join(tc.inputOptions, " ") + "\n" |
|
| 212 |
+ input.WriteString("options " + strings.Join(tc.inputOptions, " ") + "\n")
|
|
| 213 | 213 |
} |
| 214 |
- rc, err := Parse(bytes.NewBufferString(input), "") |
|
| 214 |
+ rc, err := Parse(bytes.NewBufferString(input.String()), "") |
|
| 215 | 215 |
assert.NilError(t, err) |
| 216 | 216 |
assert.Check(t, is.DeepEqual(a2s(rc.NameServers()), tc.inputNS, cmpopts.EquateEmpty())) |
| 217 | 217 |
assert.Check(t, is.DeepEqual(rc.Search(), tc.inputSearch)) |
| ... | ... |
@@ -580,7 +580,7 @@ func BenchmarkGenerate(b *testing.B) {
|
| 580 | 580 |
} |
| 581 | 581 |
|
| 582 | 582 |
b.ReportAllocs() |
| 583 |
- for i := 0; i < b.N; i++ {
|
|
| 583 |
+ for b.Loop() {
|
|
| 584 | 584 |
_, err := rc.Generate(true) |
| 585 | 585 |
if err != nil {
|
| 586 | 586 |
b.Fatal(err) |
| ... | ... |
@@ -1099,7 +1099,7 @@ func testAllocateRandomDeallocate(t *testing.T, pool, subPool string, num int, s |
| 1099 | 1099 |
// Allocate num ip addresses |
| 1100 | 1100 |
indices := make(map[int]*net.IPNet, num) |
| 1101 | 1101 |
allocated := make(map[string]bool, num) |
| 1102 |
- for i := 0; i < num; i++ {
|
|
| 1102 |
+ for i := range num {
|
|
| 1103 | 1103 |
ip, _, err := a.RequestAddress(alloc.PoolID, nil, nil) |
| 1104 | 1104 |
if err != nil {
|
| 1105 | 1105 |
t.Fatal(err) |
| ... | ... |
@@ -1202,7 +1202,7 @@ func runParallelTests(t *testing.T, instance int) {
|
| 1202 | 1202 |
if instance == first {
|
| 1203 | 1203 |
done.Wait() |
| 1204 | 1204 |
// Now check each instance got a different pool |
| 1205 |
- for i := 0; i < numInstances; i++ {
|
|
| 1205 |
+ for i := range numInstances {
|
|
| 1206 | 1206 |
for j := i + 1; j < numInstances; j++ {
|
| 1207 | 1207 |
if types.CompareIPNet(pools[i], pools[j]) {
|
| 1208 | 1208 |
t.Errorf("Instance %d and %d were given the same predefined pool: %v", i, j, pools)
|
| ... | ... |
@@ -1240,7 +1240,7 @@ func TestRequestReleaseAddressDuplicate(t *testing.T) {
|
| 1240 | 1240 |
|
| 1241 | 1241 |
group, ctx := errgroup.WithContext(context.Background()) |
| 1242 | 1242 |
outer: |
| 1243 |
- for n := 0; n < 10000; n++ {
|
|
| 1243 |
+ for range 10000 {
|
|
| 1244 | 1244 |
var c *net.IPNet |
| 1245 | 1245 |
for {
|
| 1246 | 1246 |
select {
|
| ... | ... |
@@ -1331,7 +1331,7 @@ func BenchmarkPoolIDToString(b *testing.B) {
|
| 1331 | 1331 |
} |
| 1332 | 1332 |
|
| 1333 | 1333 |
b.ReportAllocs() |
| 1334 |
- for i := 0; i < b.N; i++ {
|
|
| 1334 |
+ for b.Loop() {
|
|
| 1335 | 1335 |
_ = k.String() |
| 1336 | 1336 |
} |
| 1337 | 1337 |
} |
| ... | ... |
@@ -1340,7 +1340,7 @@ func BenchmarkPoolIDFromString(b *testing.B) {
|
| 1340 | 1340 |
const poolIDString = "default/172.27.0.0/16/172.27.3.0/24" |
| 1341 | 1341 |
|
| 1342 | 1342 |
b.ReportAllocs() |
| 1343 |
- for i := 0; i < b.N; i++ {
|
|
| 1343 |
+ for b.Loop() {
|
|
| 1344 | 1344 |
_, err := PoolIDFromString(poolIDString) |
| 1345 | 1345 |
if err != nil {
|
| 1346 | 1346 |
b.Fatal(err) |
| ... | ... |
@@ -75,7 +75,7 @@ func TestRequestPoolParallel(t *testing.T) {
|
| 75 | 75 |
imax := 1 << (a.local4.predefined[0].Size - a.local4.predefined[0].Base.Bits()) |
| 76 | 76 |
allocCh := make(chan string, imax) |
| 77 | 77 |
|
| 78 |
- for i := 0; i < imax; i++ {
|
|
| 78 |
+ for i := range imax {
|
|
| 79 | 79 |
expected = append(expected, fmt.Sprintf("10.%d.%d.0/24", uint(i/256), i%256))
|
| 80 | 80 |
|
| 81 | 81 |
eg.Go(func() error {
|
| ... | ... |
@@ -177,7 +177,7 @@ func allocate(t *testing.T, tctx *testContext, parallel int64) {
|
| 177 | 177 |
wg.Wait() |
| 178 | 178 |
|
| 179 | 179 |
// process results |
| 180 |
- for i := 0; i < routineNum; i++ {
|
|
| 180 |
+ for range routineNum {
|
|
| 181 | 181 |
ip := <-ch |
| 182 | 182 |
if ip == nil {
|
| 183 | 183 |
continue |
| ... | ... |
@@ -6,6 +6,7 @@ import ( |
| 6 | 6 |
"context" |
| 7 | 7 |
"fmt" |
| 8 | 8 |
"os" |
| 9 |
+ "slices" |
|
| 9 | 10 |
"strings" |
| 10 | 11 |
"sync" |
| 11 | 12 |
"sync/atomic" |
| ... | ... |
@@ -177,10 +178,8 @@ func reloaded() {
|
| 177 | 177 |
|
| 178 | 178 |
// OnReloaded add callback |
| 179 | 179 |
func OnReloaded(callback func()) {
|
| 180 |
- for _, pf := range onReloaded {
|
|
| 181 |
- if pf == &callback {
|
|
| 182 |
- return |
|
| 183 |
- } |
|
| 180 |
+ if slices.Contains(onReloaded, &callback) {
|
|
| 181 |
+ return |
|
| 184 | 182 |
} |
| 185 | 183 |
onReloaded = append(onReloaded, &callback) |
| 186 | 184 |
} |
| ... | ... |
@@ -366,10 +365,5 @@ type interfaceNotFound struct{ error }
|
| 366 | 366 |
func (interfaceNotFound) NotFound() {}
|
| 367 | 367 |
|
| 368 | 368 |
func contains(list []string, val string) bool {
|
| 369 |
- for _, v := range list {
|
|
| 370 |
- if v == val {
|
|
| 371 |
- return true |
|
| 372 |
- } |
|
| 373 |
- } |
|
| 374 |
- return false |
|
| 369 |
+ return slices.Contains(list, val) |
|
| 375 | 370 |
} |
| ... | ... |
@@ -137,7 +137,7 @@ func TestConcurrencyWithWait(t *testing.T) {
|
| 137 | 137 |
proto := "tcp" |
| 138 | 138 |
|
| 139 | 139 |
group := new(errgroup.Group) |
| 140 |
- for i := 0; i < 10; i++ {
|
|
| 140 |
+ for range 10 {
|
|
| 141 | 141 |
group.Go(func() error {
|
| 142 | 142 |
return addSomeRules(natChain, ip, port, proto, dstAddr, dstPort) |
| 143 | 143 |
}) |
| ... | ... |
@@ -249,7 +249,7 @@ func compareIpamConfList(listA, listB []*IpamConf) bool {
|
| 249 | 249 |
if len(listA) != len(listB) {
|
| 250 | 250 |
return false |
| 251 | 251 |
} |
| 252 |
- for i := 0; i < len(listA); i++ {
|
|
| 252 |
+ for i := range listA {
|
|
| 253 | 253 |
a = listA[i] |
| 254 | 254 |
b = listB[i] |
| 255 | 255 |
if a.PreferredPool != b.PreferredPool || |
| ... | ... |
@@ -266,7 +266,7 @@ func compareIpamInfoList(listA, listB []*IpamInfo) bool {
|
| 266 | 266 |
if len(listA) != len(listB) {
|
| 267 | 267 |
return false |
| 268 | 268 |
} |
| 269 |
- for i := 0; i < len(listA); i++ {
|
|
| 269 |
+ for i := range listA {
|
|
| 270 | 270 |
a = listA[i] |
| 271 | 271 |
b = listB[i] |
| 272 | 272 |
if a.PoolID != b.PoolID || !compareStringMaps(a.Meta, b.Meta) || |
| ... | ... |
@@ -117,7 +117,7 @@ func queryOnLinkRoutes() []netip.Prefix {
|
| 117 | 117 |
// prefix and the length of random bytes. The api ensures that the |
| 118 | 118 |
// there is no interface which exists with that name. |
| 119 | 119 |
func GenerateIfaceName(nlh nlwrap.Handle, prefix string, length int) (string, error) {
|
| 120 |
- for i := 0; i < 3; i++ {
|
|
| 120 |
+ for range 3 {
|
|
| 121 | 121 |
name, err := GenerateRandomName(prefix, length) |
| 122 | 122 |
if err != nil {
|
| 123 | 123 |
return "", err |
| ... | ... |
@@ -476,9 +476,7 @@ func (n *Network) applyConfigurationTo(to *Network) error {
|
| 476 | 476 |
} |
| 477 | 477 |
if len(n.generic) > 0 {
|
| 478 | 478 |
to.generic = options.Generic{}
|
| 479 |
- for k, v := range n.generic {
|
|
| 480 |
- to.generic[k] = v |
|
| 481 |
- } |
|
| 479 |
+ maps.Copy(to.generic, n.generic) |
|
| 482 | 480 |
} |
| 483 | 481 |
|
| 484 | 482 |
// Network drivers only see generic flags. So, make sure they match. |
| ... | ... |
@@ -525,15 +523,11 @@ func (n *Network) CopyTo(o datastore.KVObject) error {
|
| 525 | 525 |
if dstN.labels == nil {
|
| 526 | 526 |
dstN.labels = make(map[string]string, len(n.labels)) |
| 527 | 527 |
} |
| 528 |
- for k, v := range n.labels {
|
|
| 529 |
- dstN.labels[k] = v |
|
| 530 |
- } |
|
| 528 |
+ maps.Copy(dstN.labels, n.labels) |
|
| 531 | 529 |
|
| 532 | 530 |
if n.ipamOptions != nil {
|
| 533 | 531 |
dstN.ipamOptions = make(map[string]string, len(n.ipamOptions)) |
| 534 |
- for k, v := range n.ipamOptions {
|
|
| 535 |
- dstN.ipamOptions[k] = v |
|
| 536 |
- } |
|
| 532 |
+ maps.Copy(dstN.ipamOptions, n.ipamOptions) |
|
| 537 | 533 |
} |
| 538 | 534 |
|
| 539 | 535 |
for _, c := range n.ipamV4Config {
|
| ... | ... |
@@ -553,9 +547,7 @@ func (n *Network) CopyTo(o datastore.KVObject) error {
|
| 553 | 553 |
} |
| 554 | 554 |
|
| 555 | 555 |
dstN.generic = options.Generic{}
|
| 556 |
- for k, v := range n.generic {
|
|
| 557 |
- dstN.generic[k] = v |
|
| 558 |
- } |
|
| 556 |
+ maps.Copy(dstN.generic, n.generic) |
|
| 559 | 557 |
|
| 560 | 558 |
return nil |
| 561 | 559 |
} |
| ... | ... |
@@ -793,9 +785,7 @@ func NetworkOptionGeneric(generic map[string]any) NetworkOption {
|
| 793 | 793 |
if val, ok := generic[netlabel.Internal]; ok {
|
| 794 | 794 |
n.internal = val.(bool) |
| 795 | 795 |
} |
| 796 |
- for k, v := range generic {
|
|
| 797 |
- n.generic[k] = v |
|
| 798 |
- } |
|
| 796 |
+ maps.Copy(n.generic, generic) |
|
| 799 | 797 |
} |
| 800 | 798 |
} |
| 801 | 799 |
|
| ... | ... |
@@ -1285,10 +1275,8 @@ func (n *Network) HasContainerAttachments() bool {
|
| 1285 | 1285 |
|
| 1286 | 1286 |
// WalkEndpoints uses the provided function to walk the Endpoints. |
| 1287 | 1287 |
func (n *Network) WalkEndpoints(walker EndpointWalker) {
|
| 1288 |
- for _, e := range n.Endpoints() {
|
|
| 1289 |
- if walker(e) {
|
|
| 1290 |
- return |
|
| 1291 |
- } |
|
| 1288 |
+ if slices.ContainsFunc(n.Endpoints(), walker) {
|
|
| 1289 |
+ return |
|
| 1292 | 1290 |
} |
| 1293 | 1291 |
} |
| 1294 | 1292 |
|
| ... | ... |
@@ -1875,9 +1863,7 @@ func (n *Network) Labels() map[string]string {
|
| 1875 | 1875 |
defer n.mu.Unlock() |
| 1876 | 1876 |
|
| 1877 | 1877 |
lbls := make(map[string]string, len(n.labels)) |
| 1878 |
- for k, v := range n.labels {
|
|
| 1879 |
- lbls[k] = v |
|
| 1880 |
- } |
|
| 1878 |
+ maps.Copy(lbls, n.labels) |
|
| 1881 | 1879 |
|
| 1882 | 1880 |
return lbls |
| 1883 | 1881 |
} |
| ... | ... |
@@ -9,6 +9,7 @@ import ( |
| 9 | 9 |
rnd "math/rand" |
| 10 | 10 |
"net" |
| 11 | 11 |
"net/netip" |
| 12 |
+ "slices" |
|
| 12 | 13 |
"strings" |
| 13 | 14 |
"time" |
| 14 | 15 |
|
| ... | ... |
@@ -542,15 +543,7 @@ func (nDB *NetworkDB) bulkSyncTables() {
|
| 542 | 542 |
// successfully completed bulk sync in this iteration. |
| 543 | 543 |
updatedNetworks := make([]string, 0, len(networks)) |
| 544 | 544 |
for _, nid := range networks {
|
| 545 |
- var found bool |
|
| 546 |
- for _, completedNid := range completed {
|
|
| 547 |
- if nid == completedNid {
|
|
| 548 |
- found = true |
|
| 549 |
- break |
|
| 550 |
- } |
|
| 551 |
- } |
|
| 552 |
- |
|
| 553 |
- if !found {
|
|
| 545 |
+ if !slices.Contains(completed, nid) {
|
|
| 554 | 546 |
updatedNetworks = append(updatedNetworks, nid) |
| 555 | 547 |
} |
| 556 | 548 |
} |
| ... | ... |
@@ -3,6 +3,7 @@ package networkdb |
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"net" |
| 6 |
+ "slices" |
|
| 6 | 7 |
"time" |
| 7 | 8 |
|
| 8 | 9 |
"github.com/containerd/log" |
| ... | ... |
@@ -153,13 +154,7 @@ func (nDB *NetworkDB) handleTableEvent(tEvent *TableEvent, isBulkSync bool) bool |
| 153 | 153 |
network, ok := nDB.thisNodeNetworks[tEvent.NetworkID] |
| 154 | 154 |
// Check if the owner of the event is still part of the network |
| 155 | 155 |
nodes := nDB.networkNodes[tEvent.NetworkID] |
| 156 |
- var nodePresent bool |
|
| 157 |
- for _, node := range nodes {
|
|
| 158 |
- if node == tEvent.NodeName {
|
|
| 159 |
- nodePresent = true |
|
| 160 |
- break |
|
| 161 |
- } |
|
| 162 |
- } |
|
| 156 |
+ nodePresent := slices.Contains(nodes, tEvent.NodeName) |
|
| 163 | 157 |
|
| 164 | 158 |
if !ok || network.leaving || !nodePresent {
|
| 165 | 159 |
// I'm out of the network OR the event owner is not anymore part of the network so do not propagate |
| ... | ... |
@@ -9,6 +9,7 @@ import ( |
| 9 | 9 |
"math/rand/v2" |
| 10 | 10 |
"net/netip" |
| 11 | 11 |
"os" |
| 12 |
+ "slices" |
|
| 12 | 13 |
"strings" |
| 13 | 14 |
"sync" |
| 14 | 15 |
"sync/atomic" |
| ... | ... |
@@ -393,7 +394,7 @@ func (nDB *NetworkDB) GetEntry(tname, nid, key string) ([]byte, error) {
|
| 393 | 393 |
} |
| 394 | 394 |
|
| 395 | 395 |
func (nDB *NetworkDB) getEntry(tname, nid, key string) (*entry, error) {
|
| 396 |
- e, ok := nDB.indexes[byTable].Get([]byte(fmt.Sprintf("/%s/%s/%s", tname, nid, key)))
|
|
| 396 |
+ e, ok := nDB.indexes[byTable].Get(fmt.Appendf(nil, "/%s/%s/%s", tname, nid, key)) |
|
| 397 | 397 |
if !ok {
|
| 398 | 398 |
return nil, types.NotFoundErrorf("could not get entry in table %s with network id %s and key %s", tname, nid, key)
|
| 399 | 399 |
} |
| ... | ... |
@@ -470,7 +471,7 @@ func (nDB *NetworkDB) GetTableByNetwork(tname, nid string) map[string]*TableElem |
| 470 | 470 |
root := nDB.indexes[byTable].Root() |
| 471 | 471 |
nDB.RUnlock() |
| 472 | 472 |
entries := make(map[string]*TableElem) |
| 473 |
- root.WalkPrefix([]byte(fmt.Sprintf("/%s/%s", tname, nid)), func(k []byte, v *entry) bool {
|
|
| 473 |
+ root.WalkPrefix(fmt.Appendf(nil, "/%s/%s", tname, nid), func(k []byte, v *entry) bool {
|
|
| 474 | 474 |
if v.deleting {
|
| 475 | 475 |
return false |
| 476 | 476 |
} |
| ... | ... |
@@ -729,11 +730,8 @@ func (nDB *NetworkDB) LeaveNetwork(nid string) error {
|
| 729 | 729 |
// in the passed network only if it is not already present. Caller |
| 730 | 730 |
// should hold the NetworkDB lock while calling this |
| 731 | 731 |
func (nDB *NetworkDB) addNetworkNode(nid string, nodeName string) {
|
| 732 |
- nodes := nDB.networkNodes[nid] |
|
| 733 |
- for _, node := range nodes {
|
|
| 734 |
- if node == nodeName {
|
|
| 735 |
- return |
|
| 736 |
- } |
|
| 732 |
+ if slices.Contains(nDB.networkNodes[nid], nodeName) {
|
|
| 733 |
+ return |
|
| 737 | 734 |
} |
| 738 | 735 |
|
| 739 | 736 |
nDB.networkNodes[nid] = append(nDB.networkNodes[nid], nodeName) |
| ... | ... |
@@ -794,8 +792,8 @@ func (nDB *NetworkDB) updateLocalNetworkTime() {
|
| 794 | 794 |
// createOrUpdateEntry this function handles the creation or update of entries into the local |
| 795 | 795 |
// tree store. It is also used to keep in sync the entries number of the network (all tables are aggregated) |
| 796 | 796 |
func (nDB *NetworkDB) createOrUpdateEntry(nid, tname, key string, v *entry) (okTable bool, okNetwork bool) {
|
| 797 |
- nDB.indexes[byTable], _, okTable = nDB.indexes[byTable].Insert([]byte(fmt.Sprintf("/%s/%s/%s", tname, nid, key)), v)
|
|
| 798 |
- nDB.indexes[byNetwork], _, okNetwork = nDB.indexes[byNetwork].Insert([]byte(fmt.Sprintf("/%s/%s/%s", nid, tname, key)), v)
|
|
| 797 |
+ nDB.indexes[byTable], _, okTable = nDB.indexes[byTable].Insert(fmt.Appendf(nil, "/%s/%s/%s", tname, nid, key), v) |
|
| 798 |
+ nDB.indexes[byNetwork], _, okNetwork = nDB.indexes[byNetwork].Insert(fmt.Appendf(nil, "/%s/%s/%s", nid, tname, key), v) |
|
| 799 | 799 |
if !okNetwork {
|
| 800 | 800 |
// Add only if it is an insert not an update |
| 801 | 801 |
n, ok := nDB.thisNodeNetworks[nid] |
| ... | ... |
@@ -809,8 +807,8 @@ func (nDB *NetworkDB) createOrUpdateEntry(nid, tname, key string, v *entry) (okT |
| 809 | 809 |
// deleteEntry this function handles the deletion of entries into the local tree store. |
| 810 | 810 |
// It is also used to keep in sync the entries number of the network (all tables are aggregated) |
| 811 | 811 |
func (nDB *NetworkDB) deleteEntry(nid, tname, key string) (okTable bool, okNetwork bool) {
|
| 812 |
- nDB.indexes[byTable], _, okTable = nDB.indexes[byTable].Delete([]byte(fmt.Sprintf("/%s/%s/%s", tname, nid, key)))
|
|
| 813 |
- nDB.indexes[byNetwork], _, okNetwork = nDB.indexes[byNetwork].Delete([]byte(fmt.Sprintf("/%s/%s/%s", nid, tname, key)))
|
|
| 812 |
+ nDB.indexes[byTable], _, okTable = nDB.indexes[byTable].Delete(fmt.Appendf(nil, "/%s/%s/%s", tname, nid, key)) |
|
| 813 |
+ nDB.indexes[byNetwork], _, okNetwork = nDB.indexes[byNetwork].Delete(fmt.Appendf(nil, "/%s/%s/%s", nid, tname, key)) |
|
| 814 | 814 |
if okNetwork {
|
| 815 | 815 |
// Remove only if the delete is successful |
| 816 | 816 |
n, ok := nDB.thisNodeNetworks[nid] |
| ... | ... |
@@ -51,7 +51,7 @@ func launchNode(t TestingT, conf Config) *NetworkDB {
|
| 51 | 51 |
func createNetworkDBInstances(t TestingT, num int, namePrefix string, conf *Config) []*NetworkDB {
|
| 52 | 52 |
t.Helper() |
| 53 | 53 |
var dbs []*NetworkDB |
| 54 |
- for i := 0; i < num; i++ {
|
|
| 54 |
+ for i := range num {
|
|
| 55 | 55 |
localConfig := *conf |
| 56 | 56 |
localConfig.Hostname = fmt.Sprintf("%s%d", namePrefix, i+1)
|
| 57 | 57 |
localConfig.NodeID = stringid.TruncateID(stringid.GenerateRandomID()) |
| ... | ... |
@@ -69,7 +69,7 @@ func createNetworkDBInstances(t TestingT, num int, namePrefix string, conf *Conf |
| 69 | 69 |
// Wait till the cluster creation is successful |
| 70 | 70 |
check := func(t poll.LogT) poll.Result {
|
| 71 | 71 |
// Check that the cluster is properly created |
| 72 |
- for i := 0; i < num; i++ {
|
|
| 72 |
+ for i := range num {
|
|
| 73 | 73 |
if num != len(dbs[i].ClusterPeers()) {
|
| 74 | 74 |
return poll.Continue("%s:Waiting for cluster peers to be established", dbs[i].config.Hostname)
|
| 75 | 75 |
} |
| ... | ... |
@@ -91,7 +91,7 @@ func closeNetworkDBInstances(t TestingT, dbs []*NetworkDB) {
|
| 91 | 91 |
|
| 92 | 92 |
func (nDB *NetworkDB) verifyNodeExistence(t *testing.T, node string, present bool) {
|
| 93 | 93 |
t.Helper() |
| 94 |
- for i := 0; i < 80; i++ {
|
|
| 94 |
+ for range 80 {
|
|
| 95 | 95 |
nDB.RLock() |
| 96 | 96 |
_, ok := nDB.nodes[node] |
| 97 | 97 |
nDB.RUnlock() |
| ... | ... |
@@ -154,7 +154,7 @@ func (nDB *NetworkDB) verifyEntryExistence(t *testing.T, tname, nid, key, value |
| 154 | 154 |
t.Helper() |
| 155 | 155 |
n := 80 |
| 156 | 156 |
var v []byte |
| 157 |
- for i := 0; i < n; i++ {
|
|
| 157 |
+ for range n {
|
|
| 158 | 158 |
var err error |
| 159 | 159 |
v, err = nDB.GetEntry(tname, nid, key) |
| 160 | 160 |
if present && err == nil && string(v) == value {
|
| ... | ... |
@@ -324,14 +324,14 @@ func TestNetworkDBCRUDTableEntries(t *testing.T) {
|
| 324 | 324 |
for i := 1; i <= n; i++ {
|
| 325 | 325 |
err = dbs[0].CreateEntry("test_table", "network1",
|
| 326 | 326 |
fmt.Sprintf("test_key0%d", i),
|
| 327 |
- []byte(fmt.Sprintf("test_value0%d", i)))
|
|
| 327 |
+ fmt.Appendf(nil, "test_value0%d", i)) |
|
| 328 | 328 |
assert.NilError(t, err) |
| 329 | 329 |
} |
| 330 | 330 |
|
| 331 | 331 |
for i := 1; i <= n; i++ {
|
| 332 | 332 |
err = dbs[1].CreateEntry("test_table", "network1",
|
| 333 | 333 |
fmt.Sprintf("test_key1%d", i),
|
| 334 |
- []byte(fmt.Sprintf("test_value1%d", i)))
|
|
| 334 |
+ fmt.Appendf(nil, "test_value1%d", i)) |
|
| 335 | 335 |
assert.NilError(t, err) |
| 336 | 336 |
} |
| 337 | 337 |
|
| ... | ... |
@@ -445,7 +445,7 @@ func TestNetworkDBBulkSync(t *testing.T) {
|
| 445 | 445 |
for i := 1; i <= n; i++ {
|
| 446 | 446 |
err = dbs[0].CreateEntry("test_table", "network1",
|
| 447 | 447 |
fmt.Sprintf("test_key0%d", i),
|
| 448 |
- []byte(fmt.Sprintf("test_value0%d", i)))
|
|
| 448 |
+ fmt.Appendf(nil, "test_value0%d", i)) |
|
| 449 | 449 |
assert.NilError(t, err) |
| 450 | 450 |
} |
| 451 | 451 |
|
| ... | ... |
@@ -485,8 +485,8 @@ func TestNetworkDBCRUDMediumCluster(t *testing.T) {
|
| 485 | 485 |
}(db) |
| 486 | 486 |
} |
| 487 | 487 |
|
| 488 |
- for i := 0; i < n; i++ {
|
|
| 489 |
- for j := 0; j < n; j++ {
|
|
| 488 |
+ for i := range n {
|
|
| 489 |
+ for j := range n {
|
|
| 490 | 490 |
if i == j {
|
| 491 | 491 |
continue |
| 492 | 492 |
} |
| ... | ... |
@@ -495,13 +495,13 @@ func TestNetworkDBCRUDMediumCluster(t *testing.T) {
|
| 495 | 495 |
} |
| 496 | 496 |
} |
| 497 | 497 |
|
| 498 |
- for i := 0; i < n; i++ {
|
|
| 498 |
+ for i := range n {
|
|
| 499 | 499 |
err := dbs[i].JoinNetwork("network1")
|
| 500 | 500 |
assert.NilError(t, err) |
| 501 | 501 |
} |
| 502 | 502 |
|
| 503 |
- for i := 0; i < n; i++ {
|
|
| 504 |
- for j := 0; j < n; j++ {
|
|
| 503 |
+ for i := range n {
|
|
| 504 |
+ for j := range n {
|
|
| 505 | 505 |
dbs[i].verifyNetworkExistence(t, dbs[j].config.NodeID, "network1", true) |
| 506 | 506 |
} |
| 507 | 507 |
} |
| ... | ... |
@@ -627,16 +627,16 @@ func TestNetworkDBGarbageCollection(t *testing.T) {
|
| 627 | 627 |
err = dbs[1].JoinNetwork("network1")
|
| 628 | 628 |
assert.NilError(t, err) |
| 629 | 629 |
|
| 630 |
- for i := 0; i < keysWriteDelete; i++ {
|
|
| 630 |
+ for i := range keysWriteDelete {
|
|
| 631 | 631 |
err = dbs[i%2].CreateEntry("testTable", "network1", "key-"+strconv.Itoa(i), []byte("value"))
|
| 632 | 632 |
assert.NilError(t, err) |
| 633 | 633 |
} |
| 634 | 634 |
time.Sleep(time.Second) |
| 635 |
- for i := 0; i < keysWriteDelete; i++ {
|
|
| 635 |
+ for i := range keysWriteDelete {
|
|
| 636 | 636 |
err = dbs[i%2].DeleteEntry("testTable", "network1", "key-"+strconv.Itoa(i))
|
| 637 | 637 |
assert.NilError(t, err) |
| 638 | 638 |
} |
| 639 |
- for i := 0; i < 2; i++ {
|
|
| 639 |
+ for i := range 2 {
|
|
| 640 | 640 |
dbs[i].Lock() |
| 641 | 641 |
assert.Check(t, is.Equal(int64(keysWriteDelete), dbs[i].thisNodeNetworks["network1"].entriesNumber.Load()), "entries number should match") |
| 642 | 642 |
dbs[i].Unlock() |
| ... | ... |
@@ -647,14 +647,14 @@ func TestNetworkDBGarbageCollection(t *testing.T) {
|
| 647 | 647 |
|
| 648 | 648 |
err = dbs[2].JoinNetwork("network1")
|
| 649 | 649 |
assert.NilError(t, err) |
| 650 |
- for i := 0; i < 3; i++ {
|
|
| 650 |
+ for i := range 3 {
|
|
| 651 | 651 |
dbs[i].Lock() |
| 652 | 652 |
assert.Check(t, is.Equal(int64(keysWriteDelete), dbs[i].thisNodeNetworks["network1"].entriesNumber.Load()), "entries number should match") |
| 653 | 653 |
dbs[i].Unlock() |
| 654 | 654 |
} |
| 655 | 655 |
// at this point the entries should had been all deleted |
| 656 | 656 |
time.Sleep(30 * time.Second) |
| 657 |
- for i := 0; i < 3; i++ {
|
|
| 657 |
+ for i := range 3 {
|
|
| 658 | 658 |
dbs[i].Lock() |
| 659 | 659 |
assert.Check(t, is.Equal(int64(0), dbs[i].thisNodeNetworks["network1"].entriesNumber.Load()), "entries should had been garbage collected") |
| 660 | 660 |
dbs[i].Unlock() |
| ... | ... |
@@ -662,7 +662,7 @@ func TestNetworkDBGarbageCollection(t *testing.T) {
|
| 662 | 662 |
|
| 663 | 663 |
// make sure that entries are not coming back |
| 664 | 664 |
time.Sleep(15 * time.Second) |
| 665 |
- for i := 0; i < 3; i++ {
|
|
| 665 |
+ for i := range 3 {
|
|
| 666 | 666 |
dbs[i].Lock() |
| 667 | 667 |
assert.Check(t, is.Equal(int64(0), dbs[i].thisNodeNetworks["network1"].entriesNumber.Load()), "entries should had been garbage collected") |
| 668 | 668 |
dbs[i].Unlock() |
| ... | ... |
@@ -832,7 +832,7 @@ func TestParallelCreate(t *testing.T) {
|
| 832 | 832 |
startCh := make(chan int) |
| 833 | 833 |
doneCh := make(chan error) |
| 834 | 834 |
var success atomic.Uint32 |
| 835 |
- for i := 0; i < 20; i++ {
|
|
| 835 |
+ for range 20 {
|
|
| 836 | 836 |
go func() {
|
| 837 | 837 |
<-startCh |
| 838 | 838 |
err := dbs[0].CreateEntry("testTable", "testNetwork", "key", []byte("value"))
|
| ... | ... |
@@ -845,7 +845,7 @@ func TestParallelCreate(t *testing.T) {
|
| 845 | 845 |
|
| 846 | 846 |
close(startCh) |
| 847 | 847 |
|
| 848 |
- for i := 0; i < 20; i++ {
|
|
| 848 |
+ for range 20 {
|
|
| 849 | 849 |
<-doneCh |
| 850 | 850 |
} |
| 851 | 851 |
close(doneCh) |
| ... | ... |
@@ -864,7 +864,7 @@ func TestParallelDelete(t *testing.T) {
|
| 864 | 864 |
startCh := make(chan int) |
| 865 | 865 |
doneCh := make(chan error) |
| 866 | 866 |
var success atomic.Uint32 |
| 867 |
- for i := 0; i < 20; i++ {
|
|
| 867 |
+ for range 20 {
|
|
| 868 | 868 |
go func() {
|
| 869 | 869 |
<-startCh |
| 870 | 870 |
err := dbs[0].DeleteEntry("testTable", "testNetwork", "key")
|
| ... | ... |
@@ -877,7 +877,7 @@ func TestParallelDelete(t *testing.T) {
|
| 877 | 877 |
|
| 878 | 878 |
close(startCh) |
| 879 | 879 |
|
| 880 |
- for i := 0; i < 20; i++ {
|
|
| 880 |
+ for range 20 {
|
|
| 881 | 881 |
<-doneCh |
| 882 | 882 |
} |
| 883 | 883 |
close(doneCh) |
| ... | ... |
@@ -923,7 +923,7 @@ func TestNetworkDBIslands(t *testing.T) {
|
| 923 | 923 |
} |
| 924 | 924 |
|
| 925 | 925 |
// Now the 3 bootstrap nodes will cleanly leave, and will be properly removed from the other 2 nodes |
| 926 |
- for i := 0; i < 3; i++ {
|
|
| 926 |
+ for i := range 3 {
|
|
| 927 | 927 |
log.G(t.Context()).Infof("node %d leaving", i)
|
| 928 | 928 |
dbs[i].Close() |
| 929 | 929 |
} |
| ... | ... |
@@ -958,7 +958,7 @@ func TestNetworkDBIslands(t *testing.T) {
|
| 958 | 958 |
poll.WaitOn(t, check, poll.WithDelay(time.Second), poll.WithTimeout(pollTimeout())) |
| 959 | 959 |
|
| 960 | 960 |
// Spawn again the first 3 nodes with different names but same IP:port |
| 961 |
- for i := 0; i < 3; i++ {
|
|
| 961 |
+ for i := range 3 {
|
|
| 962 | 962 |
log.G(t.Context()).Infof("node %d coming back", i)
|
| 963 | 963 |
conf := *dbs[i].config |
| 964 | 964 |
conf.NodeID = stringid.TruncateID(stringid.GenerateRandomID()) |
| ... | ... |
@@ -968,7 +968,7 @@ func TestNetworkDBIslands(t *testing.T) {
|
| 968 | 968 |
// Give some time for the reconnect routine to run, it runs every 6s. |
| 969 | 969 |
check = func(t poll.LogT) poll.Result {
|
| 970 | 970 |
// Verify that the cluster is again all connected. Note that the 3 previous node did not do any join |
| 971 |
- for i := 0; i < 5; i++ {
|
|
| 971 |
+ for i := range 5 {
|
|
| 972 | 972 |
db := dbs[i] |
| 973 | 973 |
db.RLock() |
| 974 | 974 |
if len(db.nodes) != 5 {
|
| ... | ... |
@@ -76,8 +76,8 @@ func GenerateKey(containerID string) string {
|
| 76 | 76 |
|
| 77 | 77 |
for _, v := range dir {
|
| 78 | 78 |
id := v.Name() |
| 79 |
- if strings.HasSuffix(id, containerID[:maxLen-1]) {
|
|
| 80 |
- indexStr = strings.TrimSuffix(id, containerID[:maxLen-1]) |
|
| 79 |
+ if before, ok := strings.CutSuffix(id, containerID[:maxLen-1]); ok {
|
|
| 80 |
+ indexStr = before |
|
| 81 | 81 |
tmpindex, err := strconv.Atoi(indexStr) |
| 82 | 82 |
if err != nil {
|
| 83 | 83 |
return "" |
| ... | ... |
@@ -147,7 +147,7 @@ func TestAllocateAllPorts(t *testing.T) {
|
| 147 | 147 |
func BenchmarkAllocatePorts(b *testing.B) {
|
| 148 | 148 |
p := newInstance() |
| 149 | 149 |
|
| 150 |
- for n := 0; n < b.N; n++ {
|
|
| 150 |
+ for b.Loop() {
|
|
| 151 | 151 |
for i := 0; i <= p.end-p.begin; i++ {
|
| 152 | 152 |
port, err := p.RequestPort(net.IPv4zero, "tcp", 0) |
| 153 | 153 |
if err != nil {
|
| ... | ... |
@@ -4,6 +4,7 @@ import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"encoding/json" |
| 6 | 6 |
"fmt" |
| 7 |
+ "maps" |
|
| 7 | 8 |
"net" |
| 8 | 9 |
"net/netip" |
| 9 | 10 |
"slices" |
| ... | ... |
@@ -126,9 +127,7 @@ func (sb *Sandbox) Labels() map[string]any {
|
| 126 | 126 |
sb.mu.Lock() |
| 127 | 127 |
defer sb.mu.Unlock() |
| 128 | 128 |
opts := make(map[string]any, len(sb.config.generic)) |
| 129 |
- for k, v := range sb.config.generic {
|
|
| 130 |
- opts[k] = v |
|
| 131 |
- } |
|
| 129 |
+ maps.Copy(opts, sb.config.generic) |
|
| 132 | 130 |
return opts |
| 133 | 131 |
} |
| 134 | 132 |
|
| ... | ... |
@@ -280,9 +279,7 @@ func (sb *Sandbox) UpdateLabels(labels map[string]any) {
|
| 280 | 280 |
if sb.config.generic == nil {
|
| 281 | 281 |
sb.config.generic = make(map[string]any, len(labels)) |
| 282 | 282 |
} |
| 283 |
- for k, v := range labels {
|
|
| 284 |
- sb.config.generic[k] = v |
|
| 285 |
- } |
|
| 283 |
+ maps.Copy(sb.config.generic, labels) |
|
| 286 | 284 |
} |
| 287 | 285 |
|
| 288 | 286 |
func (sb *Sandbox) MarshalJSON() ([]byte, error) {
|
| ... | ... |
@@ -143,8 +143,8 @@ func TestLinkMultipleEnv(t *testing.T) {
|
| 143 | 143 |
|
| 144 | 144 |
func BenchmarkLinkMultipleEnv(b *testing.B) {
|
| 145 | 145 |
b.ReportAllocs() |
| 146 |
- b.ResetTimer() |
|
| 147 |
- for i := 0; i < b.N; i++ {
|
|
| 146 |
+ |
|
| 147 |
+ for b.Loop() {
|
|
| 148 | 148 |
_ = EnvVars("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, network.PortSet{
|
| 149 | 149 |
network.MustParsePort("6300/udp"): struct{}{},
|
| 150 | 150 |
network.MustParsePort("6379/tcp"): struct{}{},
|
| ... | ... |
@@ -134,10 +134,7 @@ func (daemon *Daemon) Containers(ctx context.Context, config *backend.ContainerL |
| 134 | 134 |
// dispatch a set number of worker goroutines to do the jobs. We choose |
| 135 | 135 |
// log2(numContainers) workers to avoid creating too many goroutines |
| 136 | 136 |
// for large number of containers. |
| 137 |
- numWorkers := int(math.Log2(float64(numContainers))) |
|
| 138 |
- if numWorkers < 1 {
|
|
| 139 |
- numWorkers = 1 |
|
| 140 |
- } |
|
| 137 |
+ numWorkers := max(int(math.Log2(float64(numContainers))), 1) |
|
| 141 | 138 |
|
| 142 | 139 |
resultsMut := sync.Mutex{}
|
| 143 | 140 |
results := make([]containertypes.Summary, numContainers) |
| ... | ... |
@@ -5,6 +5,7 @@ import ( |
| 5 | 5 |
"fmt" |
| 6 | 6 |
"os" |
| 7 | 7 |
"path/filepath" |
| 8 |
+ "slices" |
|
| 8 | 9 |
"testing" |
| 9 | 10 |
"time" |
| 10 | 11 |
|
| ... | ... |
@@ -72,10 +73,8 @@ func setupContainerWithName(t *testing.T, name string, daemon *Daemon) *containe |
| 72 | 72 |
|
| 73 | 73 |
func containerListContainsName(containers []containertypes.Summary, name string) bool {
|
| 74 | 74 |
for _, ctr := range containers {
|
| 75 |
- for _, containerName := range ctr.Names {
|
|
| 76 |
- if containerName == name {
|
|
| 77 |
- return true |
|
| 78 |
- } |
|
| 75 |
+ if slices.Contains(ctr.Names, name) {
|
|
| 76 |
+ return true |
|
| 79 | 77 |
} |
| 80 | 78 |
} |
| 81 | 79 |
|
| ... | ... |
@@ -37,12 +37,12 @@ const ( |
| 37 | 37 |
|
| 38 | 38 |
// Generates i multi-line events each with j lines |
| 39 | 39 |
func (l *logStream) logGenerator(lineCount int, multilineCount int) {
|
| 40 |
- for i := 0; i < multilineCount; i++ {
|
|
| 40 |
+ for range multilineCount {
|
|
| 41 | 41 |
l.Log(&logger.Message{
|
| 42 | 42 |
Line: []byte(multilineLogline), |
| 43 | 43 |
Timestamp: time.Time{},
|
| 44 | 44 |
}) |
| 45 |
- for j := 0; j < lineCount; j++ {
|
|
| 45 |
+ for range lineCount {
|
|
| 46 | 46 |
l.Log(&logger.Message{
|
| 47 | 47 |
Line: []byte(logline), |
| 48 | 48 |
Timestamp: time.Time{},
|
| ... | ... |
@@ -728,7 +728,7 @@ func TestCollectBatchMultilinePattern(t *testing.T) {
|
| 728 | 728 |
} |
| 729 | 729 |
|
| 730 | 730 |
func BenchmarkCollectBatch(b *testing.B) {
|
| 731 |
- for i := 0; i < b.N; i++ {
|
|
| 731 |
+ for b.Loop() {
|
|
| 732 | 732 |
mockClient := &mockClient{}
|
| 733 | 733 |
stream := &logStream{
|
| 734 | 734 |
client: mockClient, |
| ... | ... |
@@ -759,7 +759,7 @@ func BenchmarkCollectBatch(b *testing.B) {
|
| 759 | 759 |
} |
| 760 | 760 |
|
| 761 | 761 |
func BenchmarkCollectBatchMultilinePattern(b *testing.B) {
|
| 762 |
- for i := 0; i < b.N; i++ {
|
|
| 762 |
+ for b.Loop() {
|
|
| 763 | 763 |
mockClient := &mockClient{}
|
| 764 | 764 |
multilinePattern := regexp.MustCompile(`\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1,2][0-9]|3[0,1]) (?:[0,1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]`)
|
| 765 | 765 |
stream := &logStream{
|
| ... | ... |
@@ -1288,7 +1288,7 @@ func TestCollectBatchMaxTotalBytes(t *testing.T) {
|
| 1288 | 1288 |
// no ticks, guarantee batch by size (and chan close) |
| 1289 | 1289 |
stream.Close() |
| 1290 | 1290 |
|
| 1291 |
- for i := 0; i < expectedPuts; i++ {
|
|
| 1291 |
+ for range expectedPuts {
|
|
| 1292 | 1292 |
<-called |
| 1293 | 1293 |
} |
| 1294 | 1294 |
assert.Assert(t, len(calls) == expectedPuts) |
| ... | ... |
@@ -1364,7 +1364,7 @@ func TestCollectBatchMaxTotalBytesWithBinary(t *testing.T) {
|
| 1364 | 1364 |
// no ticks, guarantee batch by size (and chan close) |
| 1365 | 1365 |
stream.Close() |
| 1366 | 1366 |
|
| 1367 |
- for i := 0; i < expectedPuts; i++ {
|
|
| 1367 |
+ for range expectedPuts {
|
|
| 1368 | 1368 |
<-called |
| 1369 | 1369 |
} |
| 1370 | 1370 |
assert.Assert(t, len(calls) == expectedPuts) |
| ... | ... |
@@ -1422,7 +1422,7 @@ func TestCollectBatchWithDuplicateTimestamps(t *testing.T) {
|
| 1422 | 1422 |
var expectedEvents []types.InputLogEvent |
| 1423 | 1423 |
times := maximumLogEventsPerPut |
| 1424 | 1424 |
timestamp := time.Now() |
| 1425 |
- for i := 0; i < times; i++ {
|
|
| 1425 |
+ for i := range times {
|
|
| 1426 | 1426 |
line := strconv.Itoa(i) |
| 1427 | 1427 |
if i%2 == 0 {
|
| 1428 | 1428 |
timestamp = timestamp.Add(1 * time.Nanosecond) |
| ... | ... |
@@ -1446,7 +1446,7 @@ func TestCollectBatchWithDuplicateTimestamps(t *testing.T) {
|
| 1446 | 1446 |
close(called) |
| 1447 | 1447 |
assert.Assert(t, argument != nil) |
| 1448 | 1448 |
assert.Assert(t, len(argument.LogEvents) == times) |
| 1449 |
- for i := 0; i < times; i++ {
|
|
| 1449 |
+ for i := range times {
|
|
| 1450 | 1450 |
if !reflect.DeepEqual(argument.LogEvents[i], expectedEvents[i]) {
|
| 1451 | 1451 |
t.Errorf("Expected event to be %v but was %v", expectedEvents[i], argument.LogEvents[i])
|
| 1452 | 1452 |
} |
| ... | ... |
@@ -1659,15 +1659,14 @@ func TestCreateTagSuccess(t *testing.T) {
|
| 1659 | 1659 |
|
| 1660 | 1660 |
func BenchmarkUnwrapEvents(b *testing.B) {
|
| 1661 | 1661 |
events := make([]wrappedEvent, maximumLogEventsPerPut) |
| 1662 |
- for i := 0; i < maximumLogEventsPerPut; i++ {
|
|
| 1662 |
+ for i := range maximumLogEventsPerPut {
|
|
| 1663 | 1663 |
mes := strings.Repeat("0", maximumBytesPerEvent)
|
| 1664 | 1664 |
events[i].inputLogEvent = types.InputLogEvent{
|
| 1665 | 1665 |
Message: &mes, |
| 1666 | 1666 |
} |
| 1667 | 1667 |
} |
| 1668 | 1668 |
|
| 1669 |
- b.ResetTimer() |
|
| 1670 |
- for i := 0; i < b.N; i++ {
|
|
| 1669 |
+ for b.Loop() {
|
|
| 1671 | 1670 |
res := unwrapEvents(events) |
| 1672 | 1671 |
assert.Check(b, is.Len(res, maximumLogEventsPerPut)) |
| 1673 | 1672 |
} |
| ... | ... |
@@ -78,10 +78,7 @@ func (c *Copier) copySrc(name string, src io.Reader) {
|
| 78 | 78 |
return |
| 79 | 79 |
default: |
| 80 | 80 |
// Work out how much more data we are okay with reading this time. |
| 81 |
- upto := n + readSize |
|
| 82 |
- if upto > cap(buf) {
|
|
| 83 |
- upto = cap(buf) |
|
| 84 |
- } |
|
| 81 |
+ upto := min(n+readSize, cap(buf)) |
|
| 85 | 82 |
// Try to read that data. |
| 86 | 83 |
if upto > n {
|
| 87 | 84 |
read, err := src.Read(buf[n:upto]) |
| ... | ... |
@@ -57,7 +57,7 @@ func TestCopier(t *testing.T) {
|
| 57 | 57 |
|
| 58 | 58 |
var stdout bytes.Buffer |
| 59 | 59 |
var stderr bytes.Buffer |
| 60 |
- for i := 0; i < 30; i++ {
|
|
| 60 |
+ for range 30 {
|
|
| 61 | 61 |
if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
|
| 62 | 62 |
t.Fatal(err) |
| 63 | 63 |
} |
| ... | ... |
@@ -131,7 +131,7 @@ func TestCopierLongLines(t *testing.T) {
|
| 131 | 131 |
var stdout bytes.Buffer |
| 132 | 132 |
var stderr bytes.Buffer |
| 133 | 133 |
|
| 134 |
- for i := 0; i < 3; i++ {
|
|
| 134 |
+ for range 3 {
|
|
| 135 | 135 |
if _, err := stdout.WriteString(stdoutLongLine); err != nil {
|
| 136 | 136 |
t.Fatal(err) |
| 137 | 137 |
} |
| ... | ... |
@@ -196,7 +196,7 @@ func TestCopierLongLines(t *testing.T) {
|
| 196 | 196 |
func TestCopierSlow(t *testing.T) {
|
| 197 | 197 |
stdoutLine := "Line that thinks that it is log line from docker stdout" |
| 198 | 198 |
var stdout bytes.Buffer |
| 199 |
- for i := 0; i < 30; i++ {
|
|
| 199 |
+ for range 30 {
|
|
| 200 | 200 |
if _, err := stdout.WriteString(stdoutLine + "\n"); err != nil {
|
| 201 | 201 |
t.Fatal(err) |
| 202 | 202 |
} |
| ... | ... |
@@ -300,7 +300,7 @@ func TestCopierWithPartial(t *testing.T) {
|
| 300 | 300 |
var stderr bytes.Buffer |
| 301 | 301 |
var normalMsg bytes.Buffer |
| 302 | 302 |
|
| 303 |
- for i := 0; i < 3; i++ {
|
|
| 303 |
+ for range 3 {
|
|
| 304 | 304 |
if _, err := stdout.WriteString(stdoutLongLine); err != nil {
|
| 305 | 305 |
t.Fatal(err) |
| 306 | 306 |
} |
| ... | ... |
@@ -479,7 +479,7 @@ func piped(b *testing.B, iterations int, delay time.Duration, buf []byte) io.Rea |
| 479 | 479 |
return nil |
| 480 | 480 |
} |
| 481 | 481 |
go func() {
|
| 482 |
- for i := 0; i < iterations; i++ {
|
|
| 482 |
+ for range iterations {
|
|
| 483 | 483 |
time.Sleep(delay) |
| 484 | 484 |
if n, err := w.Write(buf); err != nil || n != len(buf) {
|
| 485 | 485 |
if err != nil {
|
| ... | ... |
@@ -4,6 +4,7 @@ package fluentd |
| 4 | 4 |
|
| 5 | 5 |
import ( |
| 6 | 6 |
"context" |
| 7 |
+ "maps" |
|
| 7 | 8 |
"math" |
| 8 | 9 |
"net/url" |
| 9 | 10 |
"strconv" |
| ... | ... |
@@ -120,9 +121,7 @@ func (f *fluentd) Log(msg *logger.Message) error {
|
| 120 | 120 |
"source": msg.Source, |
| 121 | 121 |
"log": string(msg.Line), |
| 122 | 122 |
} |
| 123 |
- for k, v := range f.extra {
|
|
| 124 |
- data[k] = v |
|
| 125 |
- } |
|
| 123 |
+ maps.Copy(data, f.extra) |
|
| 126 | 124 |
if msg.PLogMetaData != nil {
|
| 127 | 125 |
data["partial_message"] = "true" |
| 128 | 126 |
data["partial_id"] = msg.PLogMetaData.ID |
| ... | ... |
@@ -3,6 +3,7 @@ package fluentd |
| 3 | 3 |
import ( |
| 4 | 4 |
"bufio" |
| 5 | 5 |
"context" |
| 6 |
+ "maps" |
|
| 6 | 7 |
"net" |
| 7 | 8 |
"path/filepath" |
| 8 | 9 |
"runtime" |
| ... | ... |
@@ -350,9 +351,7 @@ func TestReadWriteTimeoutsAreEffective(t *testing.T) {
|
| 350 | 350 |
"fluentd-buffer-limit": "1", |
| 351 | 351 |
} |
| 352 | 352 |
// Update the config with test specific configs. |
| 353 |
- for k, v := range tc.cfg {
|
|
| 354 |
- cfg[k] = v |
|
| 355 |
- } |
|
| 353 |
+ maps.Copy(cfg, tc.cfg) |
|
| 356 | 354 |
|
| 357 | 355 |
f, err := New(logger.Info{
|
| 358 | 356 |
ContainerName: "/test-container", |
| ... | ... |
@@ -5,6 +5,7 @@ package journald |
| 5 | 5 |
import ( |
| 6 | 6 |
"errors" |
| 7 | 7 |
"fmt" |
| 8 |
+ "maps" |
|
| 8 | 9 |
"strconv" |
| 9 | 10 |
"sync/atomic" |
| 10 | 11 |
"time" |
| ... | ... |
@@ -129,9 +130,7 @@ func newJournald(info logger.Info) (*journald, error) {
|
| 129 | 129 |
if err != nil {
|
| 130 | 130 |
return nil, err |
| 131 | 131 |
} |
| 132 |
- for k, v := range extraAttrs {
|
|
| 133 |
- vars[k] = v |
|
| 134 |
- } |
|
| 132 |
+ maps.Copy(vars, extraAttrs) |
|
| 135 | 133 |
return &journald{
|
| 136 | 134 |
epoch: epoch, |
| 137 | 135 |
vars: vars, |
| ... | ... |
@@ -159,9 +158,7 @@ func validateLogOpt(cfg map[string]string) error {
|
| 159 | 159 |
|
| 160 | 160 |
func (s *journald) Log(msg *logger.Message) error {
|
| 161 | 161 |
vars := map[string]string{}
|
| 162 |
- for k, v := range s.vars {
|
|
| 163 |
- vars[k] = v |
|
| 164 |
- } |
|
| 162 |
+ maps.Copy(vars, s.vars) |
|
| 165 | 163 |
if !msg.Timestamp.IsZero() {
|
| 166 | 164 |
vars[fieldSyslogTimestamp] = msg.Timestamp.Format(time.RFC3339Nano) |
| 167 | 165 |
} |
| ... | ... |
@@ -168,7 +168,7 @@ func TestJSONFileLoggerWithOpts(t *testing.T) {
|
| 168 | 168 |
t.Fatal(err) |
| 169 | 169 |
} |
| 170 | 170 |
defer l.Close() |
| 171 |
- for i := 0; i < 36; i++ {
|
|
| 171 |
+ for i := range 36 {
|
|
| 172 | 172 |
if err := l.Log(&logger.Message{Line: []byte("line" + strconv.Itoa(i)), Source: "src1"}); err != nil {
|
| 173 | 173 |
t.Fatal(err) |
| 174 | 174 |
} |
| ... | ... |
@@ -98,7 +98,7 @@ type decoder struct {
|
| 98 | 98 |
|
| 99 | 99 |
func (d *decoder) readRecord(size int) error {
|
| 100 | 100 |
var err error |
| 101 |
- for i := 0; i < maxDecodeRetry; i++ {
|
|
| 101 |
+ for range maxDecodeRetry {
|
|
| 102 | 102 |
var n int |
| 103 | 103 |
n, err = io.ReadFull(d.rdr, d.buf[d.offset:size]) |
| 104 | 104 |
d.offset += n |
| ... | ... |
@@ -1,5 +1,7 @@ |
| 1 | 1 |
package logger |
| 2 | 2 |
|
| 3 |
+import "maps" |
|
| 4 |
+ |
|
| 3 | 5 |
var externalValidators []LogOptValidator |
| 4 | 6 |
|
| 5 | 7 |
// RegisterExternalValidator adds the validator to the list of external validators. |
| ... | ... |
@@ -14,9 +16,7 @@ func RegisterExternalValidator(v LogOptValidator) {
|
| 14 | 14 |
// not be exposed as a usable log driver to the API. |
| 15 | 15 |
// This should only be called on package initialization. |
| 16 | 16 |
func AddBuiltinLogOpts(opts map[string]bool) {
|
| 17 |
- for k, v := range opts {
|
|
| 18 |
- builtInLogOpts[k] = v |
|
| 19 |
- } |
|
| 17 |
+ maps.Copy(builtInLogOpts, opts) |
|
| 20 | 18 |
} |
| 21 | 19 |
|
| 22 | 20 |
func validateExternal(cfg map[string]string) error {
|
| ... | ... |
@@ -43,7 +43,7 @@ func TestLog(t *testing.T) {
|
| 43 | 43 |
defer l.Close() |
| 44 | 44 |
|
| 45 | 45 |
var messages []logger.Message |
| 46 |
- for i := 0; i < 100; i++ {
|
|
| 46 |
+ for range 100 {
|
|
| 47 | 47 |
messages = append(messages, logger.Message{
|
| 48 | 48 |
Timestamp: time.Now(), |
| 49 | 49 |
Line: append(bytes.Repeat([]byte("a"), 100), '\n'),
|
| ... | ... |
@@ -29,7 +29,7 @@ func TestSharedTempFileConverter(t *testing.T) {
|
| 29 | 29 |
uut := newSharedTempFileConverter(copyTransform(strings.ToUpper)) |
| 30 | 30 |
uut.TempDir = dir |
| 31 | 31 |
|
| 32 |
- for i := 0; i < 3; i++ {
|
|
| 32 |
+ for i := range 3 {
|
|
| 33 | 33 |
t.Logf("Iteration %v", i)
|
| 34 | 34 |
|
| 35 | 35 |
rdr := convertPath(t, uut, name) |
| ... | ... |
@@ -113,7 +113,7 @@ func TestSharedTempFileConverter(t *testing.T) {
|
| 113 | 113 |
closers := make(chan io.Closer, 4) |
| 114 | 114 |
var wg sync.WaitGroup |
| 115 | 115 |
wg.Add(3) |
| 116 |
- for i := 0; i < 3; i++ {
|
|
| 116 |
+ for i := range 3 {
|
|
| 117 | 117 |
go func() {
|
| 118 | 118 |
defer wg.Done() |
| 119 | 119 |
t.Logf("goroutine %v: enter", i)
|
| ... | ... |
@@ -174,7 +174,7 @@ func TestSharedTempFileConverter(t *testing.T) {
|
| 174 | 174 |
|
| 175 | 175 |
var done sync.WaitGroup |
| 176 | 176 |
done.Add(3) |
| 177 |
- for i := 0; i < 3; i++ {
|
|
| 177 |
+ for i := range 3 {
|
|
| 178 | 178 |
go func() {
|
| 179 | 179 |
defer done.Done() |
| 180 | 180 |
t.Logf("goroutine %v: enter", i)
|
| ... | ... |
@@ -31,7 +31,7 @@ func (info *Info) ExtraAttributes(keyMod func(string) string) (map[string]string |
| 31 | 31 |
extra := make(map[string]string) |
| 32 | 32 |
|
| 33 | 33 |
if labels, ok := info.Config["labels"]; ok && labels != "" {
|
| 34 |
- for _, l := range strings.Split(labels, ",") {
|
|
| 34 |
+ for l := range strings.SplitSeq(labels, ",") {
|
|
| 35 | 35 |
if v, ok := info.ContainerLabels[l]; ok {
|
| 36 | 36 |
if keyMod != nil {
|
| 37 | 37 |
l = keyMod(l) |
| ... | ... |
@@ -69,7 +69,7 @@ func (info *Info) ExtraAttributes(keyMod func(string) string) (map[string]string |
| 69 | 69 |
} |
| 70 | 70 |
|
| 71 | 71 |
if env, ok := info.Config["env"]; ok && env != "" {
|
| 72 |
- for _, l := range strings.Split(env, ",") {
|
|
| 72 |
+ for l := range strings.SplitSeq(env, ",") {
|
|
| 73 | 73 |
if v, ok := envMapping[l]; ok {
|
| 74 | 74 |
if keyMod != nil {
|
| 75 | 75 |
l = keyMod(l) |
| ... | ... |
@@ -51,7 +51,7 @@ func TestRingLogger(t *testing.T) {
|
| 51 | 51 |
|
| 52 | 52 |
func TestRingCap(t *testing.T) {
|
| 53 | 53 |
r := newRing(5) |
| 54 |
- for i := 0; i < 10; i++ {
|
|
| 54 |
+ for i := range 10 {
|
|
| 55 | 55 |
// queue messages with "0" to "10" |
| 56 | 56 |
// the "5" to "10" messages should be dropped since we only allow 5 bytes in the buffer |
| 57 | 57 |
if err := r.Enqueue(&Message{Line: []byte(strconv.Itoa(i))}); err != nil {
|
| ... | ... |
@@ -60,7 +60,7 @@ func TestRingCap(t *testing.T) {
|
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 | 62 |
// should have messages in the queue for "0" to "4" |
| 63 |
- for i := 0; i < 5; i++ {
|
|
| 63 |
+ for i := range 5 {
|
|
| 64 | 64 |
m, err := r.Dequeue() |
| 65 | 65 |
if err != nil {
|
| 66 | 66 |
t.Fatal(err) |
| ... | ... |
@@ -119,7 +119,7 @@ func TestRingClose(t *testing.T) {
|
| 119 | 119 |
|
| 120 | 120 |
func TestRingDrain(t *testing.T) {
|
| 121 | 121 |
r := newRing(5) |
| 122 |
- for i := 0; i < 5; i++ {
|
|
| 122 |
+ for i := range 5 {
|
|
| 123 | 123 |
if err := r.Enqueue(&Message{Line: []byte(strconv.Itoa(i))}); err != nil {
|
| 124 | 124 |
t.Fatal(err) |
| 125 | 125 |
} |
| ... | ... |
@@ -130,7 +130,7 @@ func TestRingDrain(t *testing.T) {
|
| 130 | 130 |
t.Fatal("got unexpected length after drain")
|
| 131 | 131 |
} |
| 132 | 132 |
|
| 133 |
- for i := 0; i < 5; i++ {
|
|
| 133 |
+ for i := range 5 {
|
|
| 134 | 134 |
if string(ls[i].Line) != strconv.Itoa(i) {
|
| 135 | 135 |
t.Fatalf("got unexpected message at position %d: %s", i, string(ls[i].Line))
|
| 136 | 136 |
} |
| ... | ... |
@@ -158,7 +158,7 @@ func BenchmarkRingLoggerThroughputNoReceiver(b *testing.B) {
|
| 158 | 158 |
msg := &Message{Line: []byte("hello humans and everyone else!")}
|
| 159 | 159 |
b.SetBytes(int64(len(msg.Line))) |
| 160 | 160 |
|
| 161 |
- for i := 0; i < b.N; i++ {
|
|
| 161 |
+ for b.Loop() {
|
|
| 162 | 162 |
if err := l.Log(msg); err != nil {
|
| 163 | 163 |
b.Fatal(err) |
| 164 | 164 |
} |
| ... | ... |
@@ -170,7 +170,7 @@ func BenchmarkRingLoggerThroughputWithReceiverDelay0(b *testing.B) {
|
| 170 | 170 |
msg := &Message{Line: []byte("hello humans and everyone else!")}
|
| 171 | 171 |
b.SetBytes(int64(len(msg.Line))) |
| 172 | 172 |
|
| 173 |
- for i := 0; i < b.N; i++ {
|
|
| 173 |
+ for b.Loop() {
|
|
| 174 | 174 |
if err := l.Log(msg); err != nil {
|
| 175 | 175 |
b.Fatal(err) |
| 176 | 176 |
} |
| ... | ... |
@@ -206,7 +206,7 @@ func BenchmarkRingLoggerThroughputConsumeDelay1(b *testing.B) {
|
| 206 | 206 |
cancel := consumeWithDelay(1*time.Millisecond, mockLog.c) |
| 207 | 207 |
defer cancel() |
| 208 | 208 |
|
| 209 |
- for i := 0; i < b.N; i++ {
|
|
| 209 |
+ for b.Loop() {
|
|
| 210 | 210 |
if err := l.Log(msg); err != nil {
|
| 211 | 211 |
b.Fatal(err) |
| 212 | 212 |
} |
| ... | ... |
@@ -223,7 +223,7 @@ func BenchmarkRingLoggerThroughputConsumeDelay10(b *testing.B) {
|
| 223 | 223 |
cancel := consumeWithDelay(10*time.Millisecond, mockLog.c) |
| 224 | 224 |
defer cancel() |
| 225 | 225 |
|
| 226 |
- for i := 0; i < b.N; i++ {
|
|
| 226 |
+ for b.Loop() {
|
|
| 227 | 227 |
if err := l.Log(msg); err != nil {
|
| 228 | 228 |
b.Fatal(err) |
| 229 | 229 |
} |
| ... | ... |
@@ -240,7 +240,7 @@ func BenchmarkRingLoggerThroughputConsumeDelay50(b *testing.B) {
|
| 240 | 240 |
cancel := consumeWithDelay(50*time.Millisecond, mockLog.c) |
| 241 | 241 |
defer cancel() |
| 242 | 242 |
|
| 243 |
- for i := 0; i < b.N; i++ {
|
|
| 243 |
+ for b.Loop() {
|
|
| 244 | 244 |
if err := l.Log(msg); err != nil {
|
| 245 | 245 |
b.Fatal(err) |
| 246 | 246 |
} |
| ... | ... |
@@ -257,7 +257,7 @@ func BenchmarkRingLoggerThroughputConsumeDelay100(b *testing.B) {
|
| 257 | 257 |
cancel := consumeWithDelay(100*time.Millisecond, mockLog.c) |
| 258 | 258 |
defer cancel() |
| 259 | 259 |
|
| 260 |
- for i := 0; i < b.N; i++ {
|
|
| 260 |
+ for b.Loop() {
|
|
| 261 | 261 |
if err := l.Log(msg); err != nil {
|
| 262 | 262 |
b.Fatal(err) |
| 263 | 263 |
} |
| ... | ... |
@@ -274,7 +274,7 @@ func BenchmarkRingLoggerThroughputConsumeDelay300(b *testing.B) {
|
| 274 | 274 |
cancel := consumeWithDelay(300*time.Millisecond, mockLog.c) |
| 275 | 275 |
defer cancel() |
| 276 | 276 |
|
| 277 |
- for i := 0; i < b.N; i++ {
|
|
| 277 |
+ for b.Loop() {
|
|
| 278 | 278 |
if err := l.Log(msg); err != nil {
|
| 279 | 279 |
b.Fatal(err) |
| 280 | 280 |
} |
| ... | ... |
@@ -291,7 +291,7 @@ func BenchmarkRingLoggerThroughputConsumeDelay500(b *testing.B) {
|
| 291 | 291 |
cancel := consumeWithDelay(500*time.Millisecond, mockLog.c) |
| 292 | 292 |
defer cancel() |
| 293 | 293 |
|
| 294 |
- for i := 0; i < b.N; i++ {
|
|
| 294 |
+ for b.Loop() {
|
|
| 295 | 295 |
if err := l.Log(msg); err != nil {
|
| 296 | 296 |
b.Fatal(err) |
| 297 | 297 |
} |
| ... | ... |
@@ -421,10 +421,7 @@ func (l *splunkLogger) postMessages(messages []*splunkMessage, lastChance bool) |
| 421 | 421 |
|
| 422 | 422 |
messagesLen := len(messages) |
| 423 | 423 |
for i := 0; i < messagesLen; i += l.postMessagesBatchSize {
|
| 424 |
- upperBound := i + l.postMessagesBatchSize |
|
| 425 |
- if upperBound > messagesLen {
|
|
| 426 |
- upperBound = messagesLen |
|
| 427 |
- } |
|
| 424 |
+ upperBound := min(i+l.postMessagesBatchSize, messagesLen) |
|
| 428 | 425 |
|
| 429 | 426 |
if err := l.tryPostMessages(ctx, messages[i:upperBound]); err != nil {
|
| 430 | 427 |
log.G(ctx).WithError(err).WithField("module", "logger/splunk").Warn("Error while sending logs")
|
| ... | ... |
@@ -827,7 +827,7 @@ func TestBatching(t *testing.T) {
|
| 827 | 827 |
t.Fatal(err) |
| 828 | 828 |
} |
| 829 | 829 |
|
| 830 |
- for i := 0; i < defaultStreamChannelSize*4; i++ {
|
|
| 830 |
+ for i := range defaultStreamChannelSize * 4 {
|
|
| 831 | 831 |
if err := loggerDriver.Log(&logger.Message{Line: []byte(strconv.Itoa(i)), Source: "stdout", Timestamp: time.Now()}); err != nil {
|
| 832 | 832 |
t.Fatal(err) |
| 833 | 833 |
} |
| ... | ... |
@@ -887,7 +887,7 @@ func TestFrequency(t *testing.T) {
|
| 887 | 887 |
t.Fatal(err) |
| 888 | 888 |
} |
| 889 | 889 |
|
| 890 |
- for i := 0; i < 10; i++ {
|
|
| 890 |
+ for i := range 10 {
|
|
| 891 | 891 |
if err := loggerDriver.Log(&logger.Message{Line: []byte(strconv.Itoa(i)), Source: "stdout", Timestamp: time.Now()}); err != nil {
|
| 892 | 892 |
t.Fatal(err) |
| 893 | 893 |
} |
| ... | ... |
@@ -958,7 +958,7 @@ func TestOneMessagePerRequest(t *testing.T) {
|
| 958 | 958 |
t.Fatal(err) |
| 959 | 959 |
} |
| 960 | 960 |
|
| 961 |
- for i := 0; i < 10; i++ {
|
|
| 961 |
+ for i := range 10 {
|
|
| 962 | 962 |
if err := loggerDriver.Log(&logger.Message{Line: []byte(strconv.Itoa(i)), Source: "stdout", Timestamp: time.Now()}); err != nil {
|
| 963 | 963 |
t.Fatal(err) |
| 964 | 964 |
} |
| ... | ... |
@@ -1050,7 +1050,7 @@ func TestSkipVerify(t *testing.T) {
|
| 1050 | 1050 |
t.Fatal("Connection should not be verified")
|
| 1051 | 1051 |
} |
| 1052 | 1052 |
|
| 1053 |
- for i := 0; i < defaultStreamChannelSize*2; i++ {
|
|
| 1053 |
+ for i := range defaultStreamChannelSize * 2 {
|
|
| 1054 | 1054 |
if err := loggerDriver.Log(&logger.Message{Line: []byte(strconv.Itoa(i)), Source: "stdout", Timestamp: time.Now()}); err != nil {
|
| 1055 | 1055 |
t.Fatal(err) |
| 1056 | 1056 |
} |
| ... | ... |
@@ -1124,7 +1124,7 @@ func TestBufferMaximum(t *testing.T) {
|
| 1124 | 1124 |
t.Fatal("Connection should not be verified")
|
| 1125 | 1125 |
} |
| 1126 | 1126 |
|
| 1127 |
- for i := 0; i < 11; i++ {
|
|
| 1127 |
+ for i := range 11 {
|
|
| 1128 | 1128 |
if err := loggerDriver.Log(&logger.Message{Line: []byte(strconv.Itoa(i)), Source: "stdout", Timestamp: time.Now()}); err != nil {
|
| 1129 | 1129 |
t.Fatal(err) |
| 1130 | 1130 |
} |
| ... | ... |
@@ -1193,7 +1193,7 @@ func TestServerAlwaysDown(t *testing.T) {
|
| 1193 | 1193 |
t.Fatal("Connection should not be verified")
|
| 1194 | 1194 |
} |
| 1195 | 1195 |
|
| 1196 |
- for i := 0; i < 5; i++ {
|
|
| 1196 |
+ for i := range 5 {
|
|
| 1197 | 1197 |
if err := loggerDriver.Log(&logger.Message{Line: []byte(strconv.Itoa(i)), Source: "stdout", Timestamp: time.Now()}); err != nil {
|
| 1198 | 1198 |
t.Fatal(err) |
| 1199 | 1199 |
} |
| ... | ... |
@@ -87,7 +87,7 @@ func (daemon *Daemon) releaseName(name string) {
|
| 87 | 87 |
|
| 88 | 88 |
func (daemon *Daemon) generateAndReserveName(id string) (string, error) {
|
| 89 | 89 |
var name string |
| 90 |
- for i := 0; i < 6; i++ {
|
|
| 90 |
+ for i := range 6 {
|
|
| 91 | 91 |
name = namesgenerator.GetRandomName(i) |
| 92 | 92 |
if name[0] != '/' {
|
| 93 | 93 |
name = "/" + name |
| ... | ... |
@@ -7,6 +7,7 @@ import ( |
| 7 | 7 |
"maps" |
| 8 | 8 |
"net" |
| 9 | 9 |
"net/netip" |
| 10 |
+ "slices" |
|
| 10 | 11 |
"sort" |
| 11 | 12 |
"strconv" |
| 12 | 13 |
"strings" |
| ... | ... |
@@ -304,9 +305,7 @@ func (daemon *Daemon) createNetwork(ctx context.Context, cfg *config.Config, cre |
| 304 | 304 |
} |
| 305 | 305 |
|
| 306 | 306 |
networkOptions := make(map[string]string) |
| 307 |
- for k, v := range create.Options {
|
|
| 308 |
- networkOptions[k] = v |
|
| 309 |
- } |
|
| 307 |
+ maps.Copy(networkOptions, create.Options) |
|
| 310 | 308 |
if defaultOpts, ok := cfg.DefaultNetworkOpts[driver]; create.ConfigFrom == nil && ok {
|
| 311 | 309 |
for k, v := range defaultOpts {
|
| 312 | 310 |
if _, ok := networkOptions[k]; !ok {
|
| ... | ... |
@@ -435,10 +434,8 @@ func (daemon *Daemon) pluginRefCount(driver, capability string, mode int) {
|
| 435 | 435 |
// other capabilities can be ignored for now |
| 436 | 436 |
} |
| 437 | 437 |
|
| 438 |
- for _, d := range builtinDrivers {
|
|
| 439 |
- if d == driver {
|
|
| 440 |
- return |
|
| 441 |
- } |
|
| 438 |
+ if slices.Contains(builtinDrivers, driver) {
|
|
| 439 |
+ return |
|
| 442 | 440 |
} |
| 443 | 441 |
|
| 444 | 442 |
if daemon.PluginStore != nil {
|
| ... | ... |
@@ -3,8 +3,10 @@ package daemon |
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"fmt" |
| 6 |
+ "maps" |
|
| 6 | 7 |
"os" |
| 7 | 8 |
"path/filepath" |
| 9 |
+ "slices" |
|
| 8 | 10 |
"strconv" |
| 9 | 11 |
"strings" |
| 10 | 12 |
|
| ... | ... |
@@ -408,7 +410,7 @@ const ( |
| 408 | 408 |
// hasMountInfoOption checks if any of the passed any of the given option values |
| 409 | 409 |
// are set in the passed in option string. |
| 410 | 410 |
func hasMountInfoOption(opts string, vals ...string) bool {
|
| 411 |
- for _, opt := range strings.Split(opts, " ") {
|
|
| 411 |
+ for opt := range strings.SplitSeq(opts, " ") {
|
|
| 412 | 412 |
for _, val := range vals {
|
| 413 | 413 |
if strings.HasPrefix(opt, val) {
|
| 414 | 414 |
return true |
| ... | ... |
@@ -464,17 +466,6 @@ var ( |
| 464 | 464 |
} |
| 465 | 465 |
) |
| 466 | 466 |
|
| 467 |
-// inSlice tests whether a string is contained in a slice of strings or not. |
|
| 468 |
-// Comparison is case sensitive |
|
| 469 |
-func inSlice(slice []string, s string) bool {
|
|
| 470 |
- for _, ss := range slice {
|
|
| 471 |
- if s == ss {
|
|
| 472 |
- return true |
|
| 473 |
- } |
|
| 474 |
- } |
|
| 475 |
- return false |
|
| 476 |
-} |
|
| 477 |
- |
|
| 478 | 467 |
// withMounts sets the container's mounts |
| 479 | 468 |
func withMounts(daemon *Daemon, daemonCfg *configStore, c *container.Container, mounts []container.Mount) coci.SpecOpts {
|
| 480 | 469 |
return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
|
| ... | ... |
@@ -644,7 +635,7 @@ func withMounts(daemon *Daemon, daemonCfg *configStore, c *container.Container, |
| 644 | 644 |
continue |
| 645 | 645 |
} |
| 646 | 646 |
if _, ok := userMounts[m.Destination]; !ok {
|
| 647 |
- if !inSlice(m.Options, "ro") {
|
|
| 647 |
+ if !slices.Contains(m.Options, "ro") {
|
|
| 648 | 648 |
s.Mounts[i].Options = append(s.Mounts[i].Options, "ro") |
| 649 | 649 |
} |
| 650 | 650 |
} |
| ... | ... |
@@ -985,9 +976,7 @@ func WithSysctls(c *container.Container) coci.SpecOpts {
|
| 985 | 985 |
} |
| 986 | 986 |
// We merge the sysctls injected above with the HostConfig (latter takes |
| 987 | 987 |
// precedence for backwards-compatibility reasons). |
| 988 |
- for k, v := range c.HostConfig.Sysctls {
|
|
| 989 |
- s.Linux.Sysctl[k] = v |
|
| 990 |
- } |
|
| 988 |
+ maps.Copy(s.Linux.Sysctl, c.HostConfig.Sysctls) |
|
| 991 | 989 |
return nil |
| 992 | 990 |
} |
| 993 | 991 |
} |
| ... | ... |
@@ -4,6 +4,7 @@ import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"os" |
| 6 | 6 |
"path/filepath" |
| 7 |
+ "slices" |
|
| 7 | 8 |
"testing" |
| 8 | 9 |
|
| 9 | 10 |
"github.com/google/go-cmp/cmp/cmpopts" |
| ... | ... |
@@ -119,7 +120,7 @@ func TestIpcPrivateVsReadonly(t *testing.T) {
|
| 119 | 119 |
if m.Destination != "/dev/shm" {
|
| 120 | 120 |
continue |
| 121 | 121 |
} |
| 122 |
- assert.Check(t, is.Equal(false, inSlice(m.Options, "ro"))) |
|
| 122 |
+ assert.Check(t, is.Equal(false, slices.Contains(m.Options, "ro"))) |
|
| 123 | 123 |
} |
| 124 | 124 |
} |
| 125 | 125 |
|
| ... | ... |
@@ -2,6 +2,7 @@ package caps |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"fmt" |
| 5 |
+ "slices" |
|
| 5 | 6 |
"strings" |
| 6 | 7 |
|
| 7 | 8 |
"github.com/moby/moby/v2/errdefs" |
| ... | ... |
@@ -37,16 +38,6 @@ func knownCapabilities() map[string]*struct{} {
|
| 37 | 37 |
return knownCaps |
| 38 | 38 |
} |
| 39 | 39 |
|
| 40 |
-// inSlice tests whether a string is contained in a slice of strings or not. |
|
| 41 |
-func inSlice(slice []string, s string) bool {
|
|
| 42 |
- for _, ss := range slice {
|
|
| 43 |
- if s == ss {
|
|
| 44 |
- return true |
|
| 45 |
- } |
|
| 46 |
- } |
|
| 47 |
- return false |
|
| 48 |
-} |
|
| 49 |
- |
|
| 50 | 40 |
const allCapabilities = "ALL" |
| 51 | 41 |
|
| 52 | 42 |
// NormalizeLegacyCapabilities normalizes, and validates CapAdd/CapDrop capabilities |
| ... | ... |
@@ -103,20 +94,20 @@ func TweakCapabilities(basics, adds, drops []string, privileged bool) ([]string, |
| 103 | 103 |
var caps []string |
| 104 | 104 |
|
| 105 | 105 |
switch {
|
| 106 |
- case inSlice(capAdd, allCapabilities): |
|
| 106 |
+ case slices.Contains(capAdd, allCapabilities): |
|
| 107 | 107 |
// Add all capabilities except ones on capDrop |
| 108 | 108 |
for _, c := range GetAllCapabilities() {
|
| 109 |
- if !inSlice(capDrop, c) {
|
|
| 109 |
+ if !slices.Contains(capDrop, c) {
|
|
| 110 | 110 |
caps = append(caps, c) |
| 111 | 111 |
} |
| 112 | 112 |
} |
| 113 |
- case inSlice(capDrop, allCapabilities): |
|
| 113 |
+ case slices.Contains(capDrop, allCapabilities): |
|
| 114 | 114 |
// "Drop" all capabilities; use what's in capAdd instead |
| 115 | 115 |
caps = capAdd |
| 116 | 116 |
default: |
| 117 | 117 |
// First drop some capabilities |
| 118 | 118 |
for _, c := range basics {
|
| 119 |
- if !inSlice(capDrop, c) {
|
|
| 119 |
+ if !slices.Contains(capDrop, c) {
|
|
| 120 | 120 |
caps = append(caps, c) |
| 121 | 121 |
} |
| 122 | 122 |
} |
| ... | ... |
@@ -2,6 +2,7 @@ package caps |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 |
+ "slices" |
|
| 5 | 6 |
"sync" |
| 6 | 7 |
|
| 7 | 8 |
ccaps "github.com/containerd/containerd/v2/pkg/cap" |
| ... | ... |
@@ -27,7 +28,7 @@ func initCaps() {
|
| 27 | 27 |
// old (pre-detection) behavior, and prevents creating containers with |
| 28 | 28 |
// no capabilities. The OCI runtime or kernel may still refuse capa- |
| 29 | 29 |
// bilities that are not available, and produce an error in that case. |
| 30 |
- if len(curCaps) > 0 && !inSlice(curCaps, capName) {
|
|
| 30 |
+ if len(curCaps) > 0 && !slices.Contains(curCaps, capName) {
|
|
| 31 | 31 |
knownCaps[capName] = nil |
| 32 | 32 |
continue |
| 33 | 33 |
} |
| ... | ... |
@@ -5,6 +5,7 @@ import ( |
| 5 | 5 |
"fmt" |
| 6 | 6 |
"net" |
| 7 | 7 |
"path" |
| 8 |
+ "slices" |
|
| 8 | 9 |
"strings" |
| 9 | 10 |
|
| 10 | 11 |
"github.com/docker/go-units" |
| ... | ... |
@@ -94,12 +95,7 @@ func (opts *ListOpts) GetAllOrEmpty() []string {
|
| 94 | 94 |
|
| 95 | 95 |
// Get checks the existence of the specified key. |
| 96 | 96 |
func (opts *ListOpts) Get(key string) bool {
|
| 97 |
- for _, k := range *opts.values {
|
|
| 98 |
- if k == key {
|
|
| 99 |
- return true |
|
| 100 |
- } |
|
| 101 |
- } |
|
| 102 |
- return false |
|
| 97 |
+ return slices.Contains(*opts.values, key) |
|
| 103 | 98 |
} |
| 104 | 99 |
|
| 105 | 100 |
// Len returns the amount of element in the slice. |
| ... | ... |
@@ -3,6 +3,7 @@ package v2 |
| 3 | 3 |
import ( |
| 4 | 4 |
"errors" |
| 5 | 5 |
"fmt" |
| 6 |
+ "slices" |
|
| 6 | 7 |
"strings" |
| 7 | 8 |
) |
| 8 | 9 |
|
| ... | ... |
@@ -71,19 +72,11 @@ func (set *settable) isSettable(allowedSettableFields []string, settable []strin |
| 71 | 71 |
} |
| 72 | 72 |
} |
| 73 | 73 |
|
| 74 |
- isAllowed := false |
|
| 75 |
- for _, allowedSettableField := range allowedSettableFields {
|
|
| 76 |
- if set.field == allowedSettableField {
|
|
| 77 |
- isAllowed = true |
|
| 78 |
- break |
|
| 79 |
- } |
|
| 80 |
- } |
|
| 74 |
+ isAllowed := slices.Contains(allowedSettableFields, set.field) |
|
| 81 | 75 |
|
| 82 | 76 |
if isAllowed {
|
| 83 |
- for _, settableField := range settable {
|
|
| 84 |
- if set.field == settableField {
|
|
| 85 |
- return true, nil |
|
| 86 |
- } |
|
| 77 |
+ if slices.Contains(settable, set.field) {
|
|
| 78 |
+ return true, nil |
|
| 87 | 79 |
} |
| 88 | 80 |
} |
| 89 | 81 |
|
| ... | ... |
@@ -102,9 +102,7 @@ func newServiceConfig(options ServiceOptions) (*serviceConfig, error) {
|
| 102 | 102 |
// copy constructs a new ServiceConfig with a copy of the configuration in config. |
| 103 | 103 |
func (config *serviceConfig) copy() *registry.ServiceConfig {
|
| 104 | 104 |
ic := make(map[string]*registry.IndexInfo) |
| 105 |
- for key, value := range config.IndexConfigs {
|
|
| 106 |
- ic[key] = value |
|
| 107 |
- } |
|
| 105 |
+ maps.Copy(ic, config.IndexConfigs) |
|
| 108 | 106 |
return ®istry.ServiceConfig{
|
| 109 | 107 |
InsecureRegistryCIDRs: slices.Clone(config.InsecureRegistryCIDRs), |
| 110 | 108 |
IndexConfigs: ic, |
| ... | ... |
@@ -318,7 +318,7 @@ func TestDaemonReloadNetworkDiagnosticPort(t *testing.T) {
|
| 318 | 318 |
daemon.netController = controller |
| 319 | 319 |
|
| 320 | 320 |
// Enable/Disable the server for some iterations |
| 321 |
- for i := 0; i < 10; i++ {
|
|
| 321 |
+ for range 10 {
|
|
| 322 | 322 |
enableConfig.CommonConfig.NetworkDiagnosticPort++ |
| 323 | 323 |
if err := daemon.Reload(enableConfig); err != nil {
|
| 324 | 324 |
t.Fatal(err) |
| ... | ... |
@@ -1,6 +1,7 @@ |
| 1 | 1 |
package container |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
+ "maps" |
|
| 4 | 5 |
"strings" |
| 5 | 6 |
"testing" |
| 6 | 7 |
|
| ... | ... |
@@ -315,9 +316,7 @@ func TestHandleSysctlBC(t *testing.T) {
|
| 315 | 315 |
NetworkMode: container.NetworkMode(tc.networkMode), |
| 316 | 316 |
Sysctls: map[string]string{},
|
| 317 | 317 |
} |
| 318 |
- for k, v := range tc.sysctls {
|
|
| 319 |
- hostCfg.Sysctls[k] = v |
|
| 320 |
- } |
|
| 318 |
+ maps.Copy(hostCfg.Sysctls, tc.sysctls) |
|
| 321 | 319 |
netCfg := &network.NetworkingConfig{
|
| 322 | 320 |
EndpointsConfig: tc.epConfig, |
| 323 | 321 |
} |
| ... | ... |
@@ -3,6 +3,7 @@ package drivers |
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"errors" |
| 6 |
+ "maps" |
|
| 6 | 7 |
"strings" |
| 7 | 8 |
"time" |
| 8 | 9 |
|
| ... | ... |
@@ -169,8 +170,6 @@ func (a *volumeAdapter) CreatedAt() (time.Time, error) {
|
| 169 | 169 |
|
| 170 | 170 |
func (a *volumeAdapter) Status() map[string]any {
|
| 171 | 171 |
out := make(map[string]any, len(a.status)) |
| 172 |
- for k, v := range a.status {
|
|
| 173 |
- out[k] = v |
|
| 174 |
- } |
|
| 172 |
+ maps.Copy(out, a.status) |
|
| 175 | 173 |
return out |
| 176 | 174 |
} |
| ... | ... |
@@ -419,9 +419,9 @@ func (v *localVolume) LiveRestoreVolume(ctx context.Context, _ string) error {
|
| 419 | 419 |
|
| 420 | 420 |
// getAddress finds out address/hostname from options |
| 421 | 421 |
func getAddress(opts string) string {
|
| 422 |
- for _, opt := range strings.Split(opts, ",") {
|
|
| 423 |
- if strings.HasPrefix(opt, "addr=") {
|
|
| 424 |
- return strings.TrimPrefix(opt, "addr=") |
|
| 422 |
+ for opt := range strings.SplitSeq(opts, ",") {
|
|
| 423 |
+ if after, ok := strings.CutPrefix(opt, "addr="); ok {
|
|
| 424 |
+ return after |
|
| 425 | 425 |
} |
| 426 | 426 |
} |
| 427 | 427 |
return "" |
| ... | ... |
@@ -429,9 +429,9 @@ func getAddress(opts string) string {
|
| 429 | 429 |
|
| 430 | 430 |
// getPassword finds out a password from options |
| 431 | 431 |
func getPassword(opts string) string {
|
| 432 |
- for _, opt := range strings.Split(opts, ",") {
|
|
| 433 |
- if strings.HasPrefix(opt, "password=") {
|
|
| 434 |
- return strings.TrimPrefix(opt, "password=") |
|
| 432 |
+ for opt := range strings.SplitSeq(opts, ",") {
|
|
| 433 |
+ if after, ok := strings.CutPrefix(opt, "password="); ok {
|
|
| 434 |
+ return after |
|
| 435 | 435 |
} |
| 436 | 436 |
} |
| 437 | 437 |
return "" |
| ... | ... |
@@ -174,7 +174,7 @@ var linuxPropagationModes = map[mount.Propagation]bool{
|
| 174 | 174 |
const linuxDefaultPropagationMode = mount.PropagationRPrivate |
| 175 | 175 |
|
| 176 | 176 |
func linuxGetPropagation(mode string) mount.Propagation {
|
| 177 |
- for _, o := range strings.Split(mode, ",") {
|
|
| 177 |
+ for o := range strings.SplitSeq(mode, ",") {
|
|
| 178 | 178 |
prop := mount.Propagation(o) |
| 179 | 179 |
if linuxPropagationModes[prop] {
|
| 180 | 180 |
return prop |
| ... | ... |
@@ -184,7 +184,7 @@ func linuxGetPropagation(mode string) mount.Propagation {
|
| 184 | 184 |
} |
| 185 | 185 |
|
| 186 | 186 |
func linuxHasPropagation(mode string) bool {
|
| 187 |
- for _, o := range strings.Split(mode, ",") {
|
|
| 187 |
+ for o := range strings.SplitSeq(mode, ",") {
|
|
| 188 | 188 |
if linuxPropagationModes[mount.Propagation(o)] {
|
| 189 | 189 |
return true |
| 190 | 190 |
} |
| ... | ... |
@@ -203,7 +203,7 @@ func linuxValidMountMode(mode string) bool {
|
| 203 | 203 |
copyModeCount := 0 |
| 204 | 204 |
consistencyModeCount := 0 |
| 205 | 205 |
|
| 206 |
- for _, o := range strings.Split(mode, ",") {
|
|
| 206 |
+ for o := range strings.SplitSeq(mode, ",") {
|
|
| 207 | 207 |
switch {
|
| 208 | 208 |
case rwModes[o]: |
| 209 | 209 |
rwModeCount++ |
| ... | ... |
@@ -256,7 +256,7 @@ func (p *linuxParser) ReadWrite(mode string) bool {
|
| 256 | 256 |
return false |
| 257 | 257 |
} |
| 258 | 258 |
|
| 259 |
- for _, o := range strings.Split(mode, ",") {
|
|
| 259 |
+ for o := range strings.SplitSeq(mode, ",") {
|
|
| 260 | 260 |
if o == "ro" {
|
| 261 | 261 |
return false |
| 262 | 262 |
} |
| ... | ... |
@@ -14,7 +14,7 @@ func copyModeExists(mode string) bool {
|
| 14 | 14 |
|
| 15 | 15 |
// GetCopyMode gets the copy mode from the mode string for mounts |
| 16 | 16 |
func getCopyMode(mode string, def bool) (bool, bool) {
|
| 17 |
- for _, o := range strings.Split(mode, ",") {
|
|
| 17 |
+ for o := range strings.SplitSeq(mode, ",") {
|
|
| 18 | 18 |
if isEnabled, exists := copyModes[o]; exists {
|
| 19 | 19 |
return isEnabled, true |
| 20 | 20 |
} |
| ... | ... |
@@ -3,9 +3,11 @@ package service |
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"fmt" |
| 6 |
+ "maps" |
|
| 6 | 7 |
"net" |
| 7 | 8 |
"os" |
| 8 | 9 |
"path/filepath" |
| 10 |
+ "slices" |
|
| 9 | 11 |
"sync" |
| 10 | 12 |
"time" |
| 11 | 13 |
|
| ... | ... |
@@ -39,9 +41,7 @@ func (v volumeWrapper) Options() map[string]string {
|
| 39 | 39 |
return nil |
| 40 | 40 |
} |
| 41 | 41 |
options := make(map[string]string, len(v.options)) |
| 42 |
- for key, value := range v.options {
|
|
| 43 |
- options[key] = value |
|
| 44 |
- } |
|
| 42 |
+ maps.Copy(options, v.options) |
|
| 45 | 43 |
return options |
| 46 | 44 |
} |
| 47 | 45 |
|
| ... | ... |
@@ -51,9 +51,7 @@ func (v volumeWrapper) Labels() map[string]string {
|
| 51 | 51 |
} |
| 52 | 52 |
|
| 53 | 53 |
labels := make(map[string]string, len(v.labels)) |
| 54 |
- for key, value := range v.labels {
|
|
| 55 |
- labels[key] = value |
|
| 56 |
- } |
|
| 54 |
+ maps.Copy(labels, v.labels) |
|
| 57 | 55 |
return labels |
| 58 | 56 |
} |
| 59 | 57 |
|
| ... | ... |
@@ -232,12 +230,7 @@ type VolumeStore struct {
|
| 232 | 232 |
|
| 233 | 233 |
func filterByDriver(names []string) filterFunc {
|
| 234 | 234 |
return func(v volume.Volume) bool {
|
| 235 |
- for _, name := range names {
|
|
| 236 |
- if name == v.DriverName() {
|
|
| 237 |
- return true |
|
| 238 |
- } |
|
| 239 |
- } |
|
| 240 |
- return false |
|
| 235 |
+ return slices.Contains(names, v.DriverName()) |
|
| 241 | 236 |
} |
| 242 | 237 |
} |
| 243 | 238 |
|
| ... | ... |
@@ -3,6 +3,7 @@ package daemon |
| 3 | 3 |
import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"encoding/hex" |
| 6 |
+ "maps" |
|
| 6 | 7 |
"os" |
| 7 | 8 |
"path/filepath" |
| 8 | 9 |
"sort" |
| ... | ... |
@@ -97,9 +98,7 @@ func (daemon *Daemon) registerMountPoints(ctr *container.Container, defaultReadO |
| 97 | 97 |
} |
| 98 | 98 |
|
| 99 | 99 |
// 1. Read already configured mount points. |
| 100 |
- for destination, point := range ctr.MountPoints {
|
|
| 101 |
- mountPoints[destination] = point |
|
| 102 |
- } |
|
| 100 |
+ maps.Copy(mountPoints, ctr.MountPoints) |
|
| 103 | 101 |
|
| 104 | 102 |
// 2. Read volumes from other containers. |
| 105 | 103 |
for _, v := range ctr.HostConfig.VolumesFrom {
|
| ... | ... |
@@ -4,6 +4,7 @@ import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"fmt" |
| 6 | 6 |
"runtime" |
| 7 |
+ "strings" |
|
| 7 | 8 |
"sync" |
| 8 | 9 |
|
| 9 | 10 |
"github.com/moby/moby/v2/pkg/parsers/kernel" |
| ... | ... |
@@ -80,19 +81,19 @@ const charsToEscape = `();\` |
| 80 | 80 |
|
| 81 | 81 |
// escapeStr returns s with every rune in charsToEscape escaped by a backslash |
| 82 | 82 |
func escapeStr(s string) string {
|
| 83 |
- var ret string |
|
| 83 |
+ var ret strings.Builder |
|
| 84 | 84 |
for _, currRune := range s {
|
| 85 | 85 |
appended := false |
| 86 | 86 |
for _, escapableRune := range charsToEscape {
|
| 87 | 87 |
if currRune == escapableRune {
|
| 88 |
- ret += `\` + string(currRune) |
|
| 88 |
+ ret.WriteString(`\` + string(currRune)) |
|
| 89 | 89 |
appended = true |
| 90 | 90 |
break |
| 91 | 91 |
} |
| 92 | 92 |
} |
| 93 | 93 |
if !appended {
|
| 94 |
- ret += string(currRune) |
|
| 94 |
+ ret.WriteRune(currRune) |
|
| 95 | 95 |
} |
| 96 | 96 |
} |
| 97 |
- return ret |
|
| 97 |
+ return ret.String() |
|
| 98 | 98 |
} |
| ... | ... |
@@ -33,7 +33,7 @@ func (s *DockerBenchmarkSuite) BenchmarkConcurrentContainerActions(c *testing.B) |
| 33 | 33 |
outerGroup.Add(maxConcurrency) |
| 34 | 34 |
chErr := make(chan error, numIterations*2*maxConcurrency) |
| 35 | 35 |
|
| 36 |
- for i := 0; i < maxConcurrency; i++ {
|
|
| 36 |
+ for range maxConcurrency {
|
|
| 37 | 37 |
go func() {
|
| 38 | 38 |
defer outerGroup.Done() |
| 39 | 39 |
innerGroup := &sync.WaitGroup{}
|
| ... | ... |
@@ -41,7 +41,7 @@ func (s *DockerBenchmarkSuite) BenchmarkConcurrentContainerActions(c *testing.B) |
| 41 | 41 |
|
| 42 | 42 |
go func() {
|
| 43 | 43 |
defer innerGroup.Done() |
| 44 |
- for i := 0; i < numIterations; i++ {
|
|
| 44 |
+ for range numIterations {
|
|
| 45 | 45 |
args := []string{"run", "-d", "busybox"}
|
| 46 | 46 |
args = append(args, sleepCommandForDaemonPlatform()...) |
| 47 | 47 |
out, _, err := dockerCmdWithError(args...) |
| ... | ... |
@@ -88,7 +88,7 @@ func (s *DockerBenchmarkSuite) BenchmarkConcurrentContainerActions(c *testing.B) |
| 88 | 88 |
|
| 89 | 89 |
go func() {
|
| 90 | 90 |
defer innerGroup.Done() |
| 91 |
- for i := 0; i < numIterations; i++ {
|
|
| 91 |
+ for range numIterations {
|
|
| 92 | 92 |
out, _, err := dockerCmdWithError("ps")
|
| 93 | 93 |
if err != nil {
|
| 94 | 94 |
chErr <- errors.New(out) |
| ... | ... |
@@ -200,7 +200,7 @@ func (d *Daemon) CmdRetryOutOfSequence(args ...string) (string, error) {
|
| 200 | 200 |
err error |
| 201 | 201 |
) |
| 202 | 202 |
|
| 203 |
- for i := 0; i < 10; i++ {
|
|
| 203 |
+ for range 10 {
|
|
| 204 | 204 |
output, err = d.Cmd(args...) |
| 205 | 205 |
// error, no error, whatever. if we don't have "update out of |
| 206 | 206 |
// sequence", we don't retry, we just return. |
| ... | ... |
@@ -569,7 +569,7 @@ type buildLine struct {
|
| 569 | 569 |
|
| 570 | 570 |
func getImageIDsFromBuild(t *testing.T, output []byte) []string {
|
| 571 | 571 |
var ids []string |
| 572 |
- for _, line := range bytes.Split(output, []byte("\n")) {
|
|
| 572 |
+ for line := range bytes.SplitSeq(output, []byte("\n")) {
|
|
| 573 | 573 |
if len(line) == 0 {
|
| 574 | 574 |
continue |
| 575 | 575 |
} |
| ... | ... |
@@ -125,7 +125,7 @@ func (s *DockerAPISuite) TestLogsAPIUntilFutureFollow(c *testing.T) {
|
| 125 | 125 |
go func() {
|
| 126 | 126 |
bufReader := bufio.NewReader(reader) |
| 127 | 127 |
defer reader.Close() |
| 128 |
- for i := 0; i < untilSecs; i++ {
|
|
| 128 |
+ for range untilSecs {
|
|
| 129 | 129 |
out, _, err := bufReader.ReadLine() |
| 130 | 130 |
if err != nil {
|
| 131 | 131 |
if err == io.EOF {
|
| ... | ... |
@@ -148,7 +148,7 @@ func (s *DockerAPISuite) TestLogsAPIUntilFutureFollow(c *testing.T) {
|
| 148 | 148 |
} |
| 149 | 149 |
}() |
| 150 | 150 |
|
| 151 |
- for i := 0; i < untilSecs; i++ {
|
|
| 151 |
+ for range untilSecs {
|
|
| 152 | 152 |
select {
|
| 153 | 153 |
case l := <-chLog: |
| 154 | 154 |
assert.NilError(c, l.err) |
| ... | ... |
@@ -149,7 +149,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesUpdate(c *testing.T) {
|
| 149 | 149 |
ctx := testutil.GetContext(c) |
| 150 | 150 |
const nodeCount = 3 |
| 151 | 151 |
var daemons [nodeCount]*daemon.Daemon |
| 152 |
- for i := 0; i < nodeCount; i++ {
|
|
| 152 |
+ for i := range nodeCount {
|
|
| 153 | 153 |
daemons[i] = s.AddDaemon(ctx, c, true, i == 0) |
| 154 | 154 |
} |
| 155 | 155 |
// wait for nodes ready |
| ... | ... |
@@ -305,7 +305,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServicesFailedUpdate(c *testing.T) {
|
| 305 | 305 |
ctx := testutil.GetContext(c) |
| 306 | 306 |
const nodeCount = 3 |
| 307 | 307 |
var daemons [nodeCount]*daemon.Daemon |
| 308 |
- for i := 0; i < nodeCount; i++ {
|
|
| 308 |
+ for i := range nodeCount {
|
|
| 309 | 309 |
daemons[i] = s.AddDaemon(ctx, c, true, i == 0) |
| 310 | 310 |
} |
| 311 | 311 |
// wait for nodes ready |
| ... | ... |
@@ -344,7 +344,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServiceConstraintRole(c *testing.T) {
|
| 344 | 344 |
ctx := testutil.GetContext(c) |
| 345 | 345 |
const nodeCount = 3 |
| 346 | 346 |
var daemons [nodeCount]*daemon.Daemon |
| 347 |
- for i := 0; i < nodeCount; i++ {
|
|
| 347 |
+ for i := range nodeCount {
|
|
| 348 | 348 |
daemons[i] = s.AddDaemon(ctx, c, true, i == 0) |
| 349 | 349 |
} |
| 350 | 350 |
// wait for nodes ready |
| ... | ... |
@@ -397,7 +397,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServiceConstraintLabel(c *testing.T) {
|
| 397 | 397 |
ctx := testutil.GetContext(c) |
| 398 | 398 |
const nodeCount = 3 |
| 399 | 399 |
var daemons [nodeCount]*daemon.Daemon |
| 400 |
- for i := 0; i < nodeCount; i++ {
|
|
| 400 |
+ for i := range nodeCount {
|
|
| 401 | 401 |
daemons[i] = s.AddDaemon(ctx, c, true, i == 0) |
| 402 | 402 |
} |
| 403 | 403 |
// wait for nodes ready |
| ... | ... |
@@ -494,7 +494,7 @@ func (s *DockerSwarmSuite) TestAPISwarmServicePlacementPrefs(c *testing.T) {
|
| 494 | 494 |
|
| 495 | 495 |
const nodeCount = 3 |
| 496 | 496 |
var daemons [nodeCount]*daemon.Daemon |
| 497 |
- for i := 0; i < nodeCount; i++ {
|
|
| 497 |
+ for i := range nodeCount {
|
|
| 498 | 498 |
daemons[i] = s.AddDaemon(ctx, c, true, i == 0) |
| 499 | 499 |
} |
| 500 | 500 |
// wait for nodes ready |
| ... | ... |
@@ -815,7 +815,7 @@ func (s *DockerSwarmSuite) TestAPISwarmRestartCluster(c *testing.T) {
|
| 815 | 815 |
mCount, wCount := 5, 1 |
| 816 | 816 |
|
| 817 | 817 |
var nodes []*daemon.Daemon |
| 818 |
- for i := 0; i < mCount; i++ {
|
|
| 818 |
+ for range mCount {
|
|
| 819 | 819 |
manager := s.AddDaemon(ctx, c, true, true) |
| 820 | 820 |
info := manager.SwarmInfo(ctx, c) |
| 821 | 821 |
assert.Equal(c, info.ControlAvailable, true) |
| ... | ... |
@@ -823,7 +823,7 @@ func (s *DockerSwarmSuite) TestAPISwarmRestartCluster(c *testing.T) {
|
| 823 | 823 |
nodes = append(nodes, manager) |
| 824 | 824 |
} |
| 825 | 825 |
|
| 826 |
- for i := 0; i < wCount; i++ {
|
|
| 826 |
+ for range wCount {
|
|
| 827 | 827 |
worker := s.AddDaemon(ctx, c, true, false) |
| 828 | 828 |
info := worker.SwarmInfo(ctx, c) |
| 829 | 829 |
assert.Equal(c, info.ControlAvailable, false) |
| ... | ... |
@@ -959,7 +959,7 @@ func (s *DockerSwarmSuite) TestSwarmRepeatedRootRotation(c *testing.T) {
|
| 959 | 959 |
currentTrustRoot := info.Cluster.TLSInfo.TrustRoot |
| 960 | 960 |
|
| 961 | 961 |
// rotate multiple times |
| 962 |
- for i := 0; i < 4; i++ {
|
|
| 962 |
+ for i := range 4 {
|
|
| 963 | 963 |
var err error |
| 964 | 964 |
var cert, key []byte |
| 965 | 965 |
if i%2 != 0 {
|
| ... | ... |
@@ -979,7 +979,7 @@ func (s *DockerSwarmSuite) TestSwarmRepeatedRootRotation(c *testing.T) {
|
| 979 | 979 |
|
| 980 | 980 |
// poll to make sure update succeeds |
| 981 | 981 |
var clusterTLSInfo swarm.TLSInfo |
| 982 |
- for j := 0; j < 18; j++ {
|
|
| 982 |
+ for range 18 {
|
|
| 983 | 983 |
info := m.SwarmInfo(ctx, c) |
| 984 | 984 |
|
| 985 | 985 |
// the desired CA cert and key is always redacted |
| ... | ... |
@@ -1001,7 +1001,7 @@ func (s *DockerSwarmSuite) TestSwarmRepeatedRootRotation(c *testing.T) {
|
| 1001 | 1001 |
} |
| 1002 | 1002 |
// could take another second or two for the nodes to trust the new roots after they've all gotten |
| 1003 | 1003 |
// new TLS certificates |
| 1004 |
- for j := 0; j < 18; j++ {
|
|
| 1004 |
+ for range 18 {
|
|
| 1005 | 1005 |
mInfo := m.GetNode(ctx, c, m.NodeID()).Description.TLSInfo |
| 1006 | 1006 |
wInfo := m.GetNode(ctx, c, w.NodeID()).Description.TLSInfo |
| 1007 | 1007 |
|
| ... | ... |
@@ -12,6 +12,7 @@ import ( |
| 12 | 12 |
"reflect" |
| 13 | 13 |
"regexp" |
| 14 | 14 |
"runtime" |
| 15 |
+ "slices" |
|
| 15 | 16 |
"strconv" |
| 16 | 17 |
"strings" |
| 17 | 18 |
"testing" |
| ... | ... |
@@ -1590,8 +1591,8 @@ func (s *DockerCLIBuildSuite) TestBuildExposeMorePorts(c *testing.T) {
|
| 1590 | 1590 |
portList := make([]string, 50) |
| 1591 | 1591 |
line := make([]string, 100) |
| 1592 | 1592 |
expectedPorts := make([]int, len(portList)*len(line)) |
| 1593 |
- for i := 0; i < len(portList); i++ {
|
|
| 1594 |
- for j := 0; j < len(line); j++ {
|
|
| 1593 |
+ for i := range portList {
|
|
| 1594 |
+ for j := range line {
|
|
| 1595 | 1595 |
p := i*len(line) + j + 1 |
| 1596 | 1596 |
line[j] = strconv.Itoa(p) |
| 1597 | 1597 |
expectedPorts[p-1] = p |
| ... | ... |
@@ -4357,13 +4358,7 @@ func (s *DockerCLIBuildSuite) TestBuildBuildTimeArgExpansion(c *testing.T) {
|
| 4357 | 4357 |
var resArr []string |
| 4358 | 4358 |
inspectFieldAndUnmarshall(c, imgName, "Config.Env", &resArr) |
| 4359 | 4359 |
|
| 4360 |
- found := false |
|
| 4361 |
- for _, v := range resArr {
|
|
| 4362 |
- if fmt.Sprintf("%s=%s", envVar, envVal) == v {
|
|
| 4363 |
- found = true |
|
| 4364 |
- break |
|
| 4365 |
- } |
|
| 4366 |
- } |
|
| 4360 |
+ found := slices.Contains(resArr, fmt.Sprintf("%s=%s", envVar, envVal))
|
|
| 4367 | 4361 |
if !found {
|
| 4368 | 4362 |
c.Fatalf("Config.Env value mismatch. Expected <key=value> to exist: %s=%s, got: %v",
|
| 4369 | 4363 |
envVar, envVal, resArr) |
| ... | ... |
@@ -4707,11 +4702,8 @@ func (s *DockerCLIBuildSuite) TestBuildTagEvent(c *testing.T) {
|
| 4707 | 4707 |
events := strings.Split(strings.TrimSpace(out), "\n") |
| 4708 | 4708 |
actions := eventActionsByIDAndType(c, events, imgName+":latest", "image") |
| 4709 | 4709 |
var foundTag bool |
| 4710 |
- for _, a := range actions {
|
|
| 4711 |
- if a == "tag" {
|
|
| 4712 |
- foundTag = true |
|
| 4713 |
- break |
|
| 4714 |
- } |
|
| 4710 |
+ if slices.Contains(actions, "tag") {
|
|
| 4711 |
+ foundTag = true |
|
| 4715 | 4712 |
} |
| 4716 | 4713 |
|
| 4717 | 4714 |
assert.Assert(c, foundTag, "No tag event found:\n%s", out) |
| ... | ... |
@@ -5645,7 +5637,7 @@ func (s *DockerCLIBuildSuite) TestBuildMultiStageCopyFromSyntax(c *testing.T) {
|
| 5645 | 5645 |
assert.Equal(c, strings.Count(result.Combined(), "Using cache"), 7) |
| 5646 | 5646 |
assert.Equal(c, getIDByName(c, "build1"), getIDByName(c, "build2")) |
| 5647 | 5647 |
|
| 5648 |
- err := os.WriteFile(filepath.Join(ctx.Dir, "Dockerfile"), []byte(fmt.Sprintf(dockerfile, "COPY baz/aa foo")), 0o644) |
|
| 5648 |
+ err := os.WriteFile(filepath.Join(ctx.Dir, "Dockerfile"), fmt.Appendf(nil, dockerfile, "COPY baz/aa foo"), 0o644) |
|
| 5649 | 5649 |
assert.NilError(c, err) |
| 5650 | 5650 |
|
| 5651 | 5651 |
// changing file in parent block should not affect last block |
| ... | ... |
@@ -1127,7 +1127,7 @@ func (s *DockerDaemonSuite) TestDaemonRestartContainerLinksRestart(c *testing.T) |
| 1127 | 1127 |
maxChildren := 10 |
| 1128 | 1128 |
chErr := make(chan error, maxChildren) |
| 1129 | 1129 |
|
| 1130 |
- for i := 0; i < maxChildren; i++ {
|
|
| 1130 |
+ for i := range maxChildren {
|
|
| 1131 | 1131 |
wg.Add(1) |
| 1132 | 1132 |
name := fmt.Sprintf("test%d", i)
|
| 1133 | 1133 |
|
| ... | ... |
@@ -2125,7 +2125,7 @@ func (s *DockerDaemonSuite) TestShmSizeReload(c *testing.T) {
|
| 2125 | 2125 |
configFile := filepath.Join(configPath, "config.json") |
| 2126 | 2126 |
|
| 2127 | 2127 |
size := 67108864 * 2 |
| 2128 |
- configData := []byte(fmt.Sprintf(`{"default-shm-size": "%dM"}`, size/1024/1024))
|
|
| 2128 |
+ configData := fmt.Appendf(nil, `{"default-shm-size": "%dM"}`, size/1024/1024)
|
|
| 2129 | 2129 |
assert.Assert(c, os.WriteFile(configFile, configData, 0o666) == nil, "could not write temp file for config reload") |
| 2130 | 2130 |
pattern := regexp.MustCompile(fmt.Sprintf("shm on /dev/shm type tmpfs(.*)size=%dk", size/1024))
|
| 2131 | 2131 |
|
| ... | ... |
@@ -2140,7 +2140,7 @@ func (s *DockerDaemonSuite) TestShmSizeReload(c *testing.T) {
|
| 2140 | 2140 |
assert.Equal(c, strings.TrimSpace(out), fmt.Sprintf("%v", size))
|
| 2141 | 2141 |
|
| 2142 | 2142 |
size = 67108864 * 3 |
| 2143 |
- configData = []byte(fmt.Sprintf(`{"default-shm-size": "%dM"}`, size/1024/1024))
|
|
| 2143 |
+ configData = fmt.Appendf(nil, `{"default-shm-size": "%dM"}`, size/1024/1024)
|
|
| 2144 | 2144 |
assert.Assert(c, os.WriteFile(configFile, configData, 0o666) == nil, "could not write temp file for config reload") |
| 2145 | 2145 |
pattern = regexp.MustCompile(fmt.Sprintf("shm on /dev/shm type tmpfs(.*)size=%dk", size/1024))
|
| 2146 | 2146 |
|
| ... | ... |
@@ -283,7 +283,7 @@ func (s *DockerCLIExecSuite) TestExecCgroup(c *testing.T) {
|
| 283 | 283 |
var execCgroups []sort.StringSlice |
| 284 | 284 |
errChan := make(chan error, 5) |
| 285 | 285 |
// exec a few times concurrently to get consistent failure |
| 286 |
- for i := 0; i < 5; i++ {
|
|
| 286 |
+ for range 5 {
|
|
| 287 | 287 |
wg.Add(1) |
| 288 | 288 |
go func() {
|
| 289 | 289 |
defer wg.Done() |
| ... | ... |
@@ -337,7 +337,7 @@ func (s *DockerCLIExecSuite) TestExecInspectID(c *testing.T) {
|
| 337 | 337 |
|
| 338 | 338 |
// Give the exec 10 chances/seconds to start then give up and stop the test |
| 339 | 339 |
tries := 10 |
| 340 |
- for i := 0; i < tries; i++ {
|
|
| 340 |
+ for i := range tries {
|
|
| 341 | 341 |
// Since its still running we should see exec as part of the container |
| 342 | 342 |
out = strings.TrimSpace(inspectField(c, id, "ExecIDs")) |
| 343 | 343 |
|
| ... | ... |
@@ -360,7 +360,7 @@ func (s *DockerCLIExecSuite) TestExecInspectID(c *testing.T) {
|
| 360 | 360 |
cmd.Wait() |
| 361 | 361 |
|
| 362 | 362 |
// Give the exec 10 chances/seconds to stop then give up and stop the test |
| 363 |
- for i := 0; i < tries; i++ {
|
|
| 363 |
+ for i := range tries {
|
|
| 364 | 364 |
// Since its still running we should see exec as part of the container |
| 365 | 365 |
out = strings.TrimSpace(inspectField(c, id, "ExecIDs")) |
| 366 | 366 |
|
| ... | ... |
@@ -547,7 +547,7 @@ func (s *DockerExternalVolumeSuite) TestExternalVolumeDriverCapabilities(c *test |
| 547 | 547 |
s.d.Start(c) |
| 548 | 548 |
assert.Equal(c, s.ec.caps, 0) |
| 549 | 549 |
|
| 550 |
- for i := 0; i < 3; i++ {
|
|
| 550 |
+ for i := range 3 {
|
|
| 551 | 551 |
out, err := s.d.Cmd("volume", "create", "-d", volumePluginName, fmt.Sprintf("test%d", i))
|
| 552 | 552 |
assert.NilError(c, err, out) |
| 553 | 553 |
assert.Equal(c, s.ec.caps, 1) |
| ... | ... |
@@ -62,7 +62,7 @@ LABEL label.Z="Z"`)) |
| 62 | 62 |
actualValues := strings.Split(out, "\n")[1:27] |
| 63 | 63 |
expectedValues := [26]string{"Z", "Y", "X", "W", "V", "U", "T", "S", "R", "Q", "P", "O", "N", "M", "L", "K", "J", "I", "H", "G", "F", "E", "D", "C", "B", "A"}
|
| 64 | 64 |
|
| 65 |
- for i := 0; i < 26; i++ {
|
|
| 65 |
+ for i := range 26 {
|
|
| 66 | 66 |
echoValue := fmt.Sprintf("LABEL label.%s=%s", expectedValues[i], expectedValues[i])
|
| 67 | 67 |
actualValue := actualValues[i] |
| 68 | 68 |
assert.Assert(c, is.Contains(actualValue, echoValue)) |
| ... | ... |
@@ -199,8 +199,8 @@ func (s *DockerCLIImagesSuite) TestImagesFilterSinceAndBefore(c *testing.T) {
|
| 199 | 199 |
|
| 200 | 200 |
func getImageIDs(out string) []string {
|
| 201 | 201 |
var actual []string |
| 202 |
- imgs := strings.Split(out, "\n") |
|
| 203 |
- for _, l := range imgs {
|
|
| 202 |
+ imgs := strings.SplitSeq(out, "\n") |
|
| 203 |
+ for l := range imgs {
|
|
| 204 | 204 |
imgTag, imgDigest, _ := strings.Cut(l, "\t") |
| 205 | 205 |
if strings.HasPrefix(imgTag, "test_") {
|
| 206 | 206 |
actual = append(actual, imgDigest) |
| ... | ... |
@@ -77,7 +77,7 @@ func (s *DockerRegistryAuthHtpasswdSuite) TestLogoutWithWrongHostnamesStored(c * |
| 77 | 77 |
c.Setenv("PATH", testPath)
|
| 78 | 78 |
|
| 79 | 79 |
cmd := exec.Command("docker-credential-shell-test", "store")
|
| 80 |
- stdin := bytes.NewReader([]byte(fmt.Sprintf(`{"ServerURL": "https://%s", "Username": %q, "Secret": %q}`, privateRegistryURL, s.reg.Username(), s.reg.Password())))
|
|
| 80 |
+ stdin := bytes.NewReader(fmt.Appendf(nil, `{"ServerURL": "https://%s", "Username": %q, "Secret": %q}`, privateRegistryURL, s.reg.Username(), s.reg.Password()))
|
|
| 81 | 81 |
cmd.Stdin = stdin |
| 82 | 82 |
assert.NilError(c, cmd.Run()) |
| 83 | 83 |
|
| ... | ... |
@@ -217,8 +217,8 @@ func (s *DockerCLILogsSuite) TestLogsSinceFutureFollow(c *testing.T) {
|
| 217 | 217 |
since := t.Unix() + 2 |
| 218 | 218 |
out := cli.DockerCmd(c, "logs", "-t", "-f", fmt.Sprintf("--since=%v", since), name).Combined()
|
| 219 | 219 |
assert.Assert(c, out != "", "cannot read from empty log") |
| 220 |
- lines := strings.Split(strings.TrimSpace(out), "\n") |
|
| 221 |
- for _, v := range lines {
|
|
| 220 |
+ lines := strings.SplitSeq(strings.TrimSpace(out), "\n") |
|
| 221 |
+ for v := range lines {
|
|
| 222 | 222 |
ts, err := time.Parse(time.RFC3339Nano, strings.Split(v, " ")[0]) |
| 223 | 223 |
assert.NilError(c, err, "cannot parse timestamp output from log: '%v'", v) |
| 224 | 224 |
assert.Assert(c, ts.Unix() >= since, "earlier log found. since=%v logdate=%v", since, ts) |
| ... | ... |
@@ -1123,7 +1123,7 @@ func (s *DockerNetworkSuite) TestDockerNetworkHostModeUngracefulDaemonRestart(c |
| 1123 | 1123 |
s.d.StartWithBusybox(ctx, c) |
| 1124 | 1124 |
|
| 1125 | 1125 |
// Run a few containers on host network |
| 1126 |
- for i := 0; i < 10; i++ {
|
|
| 1126 |
+ for i := range 10 {
|
|
| 1127 | 1127 |
cName := fmt.Sprintf("hostc-%d", i)
|
| 1128 | 1128 |
out, err := s.d.Cmd("run", "-d", "--name", cName, "--net=host", "--restart=always", "busybox", "top")
|
| 1129 | 1129 |
assert.NilError(c, err, out) |
| ... | ... |
@@ -1138,7 +1138,7 @@ func (s *DockerNetworkSuite) TestDockerNetworkHostModeUngracefulDaemonRestart(c |
| 1138 | 1138 |
s.d.Start(c) |
| 1139 | 1139 |
|
| 1140 | 1140 |
// make sure all the containers are up and running |
| 1141 |
- for i := 0; i < 10; i++ {
|
|
| 1141 |
+ for i := range 10 {
|
|
| 1142 | 1142 |
err := s.d.WaitRun(fmt.Sprintf("hostc-%d", i))
|
| 1143 | 1143 |
assert.NilError(c, err) |
| 1144 | 1144 |
} |
| ... | ... |
@@ -104,7 +104,7 @@ func (s *DockerCLIPortSuite) TestPortList(c *testing.T) {
|
| 104 | 104 |
testRange := func() {
|
| 105 | 105 |
// host port ranges used |
| 106 | 106 |
IDs := make([]string, 3) |
| 107 |
- for i := 0; i < 3; i++ {
|
|
| 107 |
+ for i := range 3 {
|
|
| 108 | 108 |
out = cli.DockerCmd(c, "run", "-d", "-p", "9090-9092:80", "busybox", "top").Stdout() |
| 109 | 109 |
IDs[i] = strings.TrimSpace(out) |
| 110 | 110 |
|
| ... | ... |
@@ -121,7 +121,7 @@ func (s *DockerCLIPortSuite) TestPortList(c *testing.T) {
|
| 121 | 121 |
// Exhausted port range did not return an error |
| 122 | 122 |
assert.Assert(c, err != nil, "out: %s", out) |
| 123 | 123 |
|
| 124 |
- for i := 0; i < 3; i++ {
|
|
| 124 |
+ for i := range 3 {
|
|
| 125 | 125 |
cli.DockerCmd(c, "rm", "-f", IDs[i]) |
| 126 | 126 |
} |
| 127 | 127 |
} |
| ... | ... |
@@ -182,7 +182,7 @@ func assertPortList(t *testing.T, out string, expected []string) {
|
| 182 | 182 |
return old |
| 183 | 183 |
} |
| 184 | 184 |
|
| 185 |
- for i := 0; i < len(expected); i++ {
|
|
| 185 |
+ for i := range expected {
|
|
| 186 | 186 |
if lines[i] == expected[i] {
|
| 187 | 187 |
continue |
| 188 | 188 |
} |
| ... | ... |
@@ -136,7 +136,7 @@ func assertContainerList(out string, expected []string) bool {
|
| 136 | 136 |
} |
| 137 | 137 |
|
| 138 | 138 |
containerIDIndex := strings.Index(lines[0], "CONTAINER ID") |
| 139 |
- for i := 0; i < len(expected); i++ {
|
|
| 139 |
+ for i := range expected {
|
|
| 140 | 140 |
foundID := lines[i+1][containerIDIndex : containerIDIndex+12] |
| 141 | 141 |
if foundID != expected[i][:12] {
|
| 142 | 142 |
return false |
| ... | ... |
@@ -531,7 +531,7 @@ func (s *DockerCLIPsSuite) TestPsListContainersFilterCreated(c *testing.T) {
|
| 531 | 531 |
out = cli.DockerCmd(c, "ps", "-a").Stdout() |
| 532 | 532 |
|
| 533 | 533 |
hits := 0 |
| 534 |
- for _, line := range strings.Split(out, "\n") {
|
|
| 534 |
+ for line := range strings.SplitSeq(out, "\n") {
|
|
| 535 | 535 |
if !strings.Contains(line, shortCID) {
|
| 536 | 536 |
continue |
| 537 | 537 |
} |
| ... | ... |
@@ -157,7 +157,7 @@ func (s *DockerHubPullSuite) TestPullAllTagsFromCentralRegistry(c *testing.T) {
|
| 157 | 157 |
|
| 158 | 158 |
// Verify that the line for 'dockercore/engine-pull-all-test-fixture:latest' is left unchanged. |
| 159 | 159 |
var latestLine string |
| 160 |
- for _, line := range strings.Split(outImageAllTagCmd, "\n") {
|
|
| 160 |
+ for line := range strings.SplitSeq(outImageAllTagCmd, "\n") {
|
|
| 161 | 161 |
if strings.HasPrefix(line, "dockercore/engine-pull-all-test-fixture") && strings.Contains(line, "latest") {
|
| 162 | 162 |
latestLine = line |
| 163 | 163 |
break |
| ... | ... |
@@ -79,7 +79,7 @@ func (s *DockerRegistrySuite) TestPushMultipleTags(c *testing.T) {
|
| 79 | 79 |
// Ensure layer list is equivalent for repoTag1 and repoTag2 |
| 80 | 80 |
out1 := cli.DockerCmd(c, "push", repoTag1).Combined() |
| 81 | 81 |
var out1Lines []string |
| 82 |
- for _, outputLine := range strings.Split(out1, "\n") {
|
|
| 82 |
+ for outputLine := range strings.SplitSeq(out1, "\n") {
|
|
| 83 | 83 |
if strings.Contains(outputLine, imageAlreadyExists) {
|
| 84 | 84 |
out1Lines = append(out1Lines, outputLine) |
| 85 | 85 |
} |
| ... | ... |
@@ -87,7 +87,7 @@ func (s *DockerRegistrySuite) TestPushMultipleTags(c *testing.T) {
|
| 87 | 87 |
|
| 88 | 88 |
out2 := cli.DockerCmd(c, "push", repoTag2).Combined() |
| 89 | 89 |
var out2Lines []string |
| 90 |
- for _, outputLine := range strings.Split(out2, "\n") {
|
|
| 90 |
+ for outputLine := range strings.SplitSeq(out2, "\n") {
|
|
| 91 | 91 |
if strings.Contains(outputLine, imageAlreadyExists) {
|
| 92 | 92 |
out2Lines = append(out2Lines, outputLine) |
| 93 | 93 |
} |
| ... | ... |
@@ -806,7 +806,7 @@ func (s *DockerCLIRunSuite) TestRunTwoConcurrentContainers(c *testing.T) {
|
| 806 | 806 |
group.Add(2) |
| 807 | 807 |
|
| 808 | 808 |
errChan := make(chan error, 2) |
| 809 |
- for i := 0; i < 2; i++ {
|
|
| 809 |
+ for range 2 {
|
|
| 810 | 810 |
go func() {
|
| 811 | 811 |
defer group.Done() |
| 812 | 812 |
_, _, err := dockerCmdWithError("run", "busybox", "sleep", sleepTime)
|
| ... | ... |
@@ -2857,7 +2857,7 @@ func (s *DockerCLIRunSuite) TestRunUnshareProc(c *testing.T) {
|
| 2857 | 2857 |
}() |
| 2858 | 2858 |
|
| 2859 | 2859 |
var retErr error |
| 2860 |
- for i := 0; i < 3; i++ {
|
|
| 2860 |
+ for range 3 {
|
|
| 2861 | 2861 |
err := <-errChan |
| 2862 | 2862 |
if retErr == nil && err != nil {
|
| 2863 | 2863 |
retErr = err |
| ... | ... |
@@ -4111,7 +4111,7 @@ func (s *DockerCLIRunSuite) TestSlowStdinClosing(c *testing.T) {
|
| 4111 | 4111 |
skip.If(c, testEnv.GitHubActions()) |
| 4112 | 4112 |
} |
| 4113 | 4113 |
const repeat = 3 // regression happened 50% of the time |
| 4114 |
- for i := 0; i < repeat; i++ {
|
|
| 4114 |
+ for i := range repeat {
|
|
| 4115 | 4115 |
c.Run(strconv.Itoa(i), func(c *testing.T) {
|
| 4116 | 4116 |
cmd := icmd.Cmd{
|
| 4117 | 4117 |
Command: []string{dockerBinary, "run", "--rm", "-i", "busybox", "cat"},
|
| ... | ... |
@@ -1486,7 +1486,7 @@ func (s *DockerSwarmSuite) TestSwarmAlternateLockUnlock(c *testing.T) {
|
| 1486 | 1486 |
ctx := testutil.GetContext(c) |
| 1487 | 1487 |
d := s.AddDaemon(ctx, c, true, true) |
| 1488 | 1488 |
|
| 1489 |
- for i := 0; i < 2; i++ {
|
|
| 1489 |
+ for range 2 {
|
|
| 1490 | 1490 |
// set to lock |
| 1491 | 1491 |
outs, err := d.Cmd("swarm", "update", "--autolock")
|
| 1492 | 1492 |
assert.Assert(c, err == nil, "out: %v", outs) |
| ... | ... |
@@ -1790,7 +1790,7 @@ func (s *DockerSwarmSuite) TestSwarmJoinLeave(c *testing.T) {
|
| 1790 | 1790 |
|
| 1791 | 1791 |
// Verify that back to back join/leave does not cause panics |
| 1792 | 1792 |
d1 := s.AddDaemon(ctx, c, false, false) |
| 1793 |
- for i := 0; i < 10; i++ {
|
|
| 1793 |
+ for range 10 {
|
|
| 1794 | 1794 |
out, err = d1.Cmd("swarm", "join", "--token", token, d.SwarmListenAddr())
|
| 1795 | 1795 |
assert.NilError(c, err) |
| 1796 | 1796 |
assert.Assert(c, strings.TrimSpace(out) != "") |
| ... | ... |
@@ -1808,7 +1808,7 @@ func waitForEvent(t *testing.T, d *daemon.Daemon, since string, filter string, e |
| 1808 | 1808 |
return "" |
| 1809 | 1809 |
} |
| 1810 | 1810 |
var out string |
| 1811 |
- for i := 0; i < retry; i++ {
|
|
| 1811 |
+ for i := range retry {
|
|
| 1812 | 1812 |
until := daemonUnixTime(t) |
| 1813 | 1813 |
var err error |
| 1814 | 1814 |
if filter != "" {
|
| ... | ... |
@@ -55,8 +55,8 @@ func getContainerCount(t *testing.T) int {
|
| 55 | 55 |
result := icmd.RunCommand(dockerBinary, "info") |
| 56 | 56 |
result.Assert(t, icmd.Success) |
| 57 | 57 |
|
| 58 |
- lines := strings.Split(result.Combined(), "\n") |
|
| 59 |
- for _, line := range lines {
|
|
| 58 |
+ lines := strings.SplitSeq(result.Combined(), "\n") |
|
| 59 |
+ for line := range lines {
|
|
| 60 | 60 |
if strings.Contains(line, containers) {
|
| 61 | 61 |
output := strings.TrimSpace(line) |
| 62 | 62 |
output = strings.TrimPrefix(output, containers) |
| ... | ... |
@@ -435,7 +435,7 @@ func loadSpecialImage(t *testing.T, imageFunc specialimage.SpecialImageFunc) str |
| 435 | 435 |
|
| 436 | 436 |
out := cli.DockerCmd(t, "load", "-i", imgTar).Stdout() |
| 437 | 437 |
|
| 438 |
- for _, line := range strings.Split(out, "\n") {
|
|
| 438 |
+ for line := range strings.SplitSeq(out, "\n") {
|
|
| 439 | 439 |
line = strings.TrimSpace(line) |
| 440 | 440 |
|
| 441 | 441 |
if _, imageID, hasID := strings.Cut(line, "Loaded image ID: "); hasID {
|
| ... | ... |
@@ -102,8 +102,8 @@ func (e *eventObserver) CheckEventError(t *testing.T, id, event string, match ev |
| 102 | 102 |
if e.disconnectionError != nil {
|
| 103 | 103 |
until := daemonUnixTime(t) |
| 104 | 104 |
out := cli.DockerCmd(t, "events", "--since", e.startTime, "--until", until).Stdout() |
| 105 |
- events := strings.Split(strings.TrimSpace(out), "\n") |
|
| 106 |
- for _, e := range events {
|
|
| 105 |
+ events := strings.SplitSeq(strings.TrimSpace(out), "\n") |
|
| 106 |
+ for e := range events {
|
|
| 107 | 107 |
if _, ok := match(e); ok {
|
| 108 | 108 |
foundEvent = true |
| 109 | 109 |
break |
| ... | ... |
@@ -176,7 +176,7 @@ func matchEventID(matches map[string]string, id string) bool {
|
| 176 | 176 |
if !matchID && matches["attributes"] != "" {
|
| 177 | 177 |
// try matching a name in the attributes |
| 178 | 178 |
attributes := map[string]string{}
|
| 179 |
- for _, a := range strings.Split(matches["attributes"], ", ") {
|
|
| 179 |
+ for a := range strings.SplitSeq(matches["attributes"], ", ") {
|
|
| 180 | 180 |
kv := strings.Split(a, "=") |
| 181 | 181 |
attributes[kv[0]] = kv[1] |
| 182 | 182 |
} |
| ... | ... |
@@ -186,8 +186,8 @@ func matchEventID(matches map[string]string, id string) bool {
|
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
func parseEvents(t *testing.T, out, match string) {
|
| 189 |
- events := strings.Split(strings.TrimSpace(out), "\n") |
|
| 190 |
- for _, event := range events {
|
|
| 189 |
+ events := strings.SplitSeq(strings.TrimSpace(out), "\n") |
|
| 190 |
+ for event := range events {
|
|
| 191 | 191 |
matches := eventstestutils.ScanMap(event) |
| 192 | 192 |
matched, err := regexp.MatchString(match, matches["action"]) |
| 193 | 193 |
assert.NilError(t, err) |
| ... | ... |
@@ -24,7 +24,7 @@ func getPrefixAndSlashFromDaemonPlatform() (prefix, slash string) {
|
| 24 | 24 |
// a map which cgroup name as key and path as value. |
| 25 | 25 |
func ParseCgroupPaths(procCgroupData string) map[string]string {
|
| 26 | 26 |
cgroupPaths := map[string]string{}
|
| 27 |
- for _, line := range strings.Split(procCgroupData, "\n") {
|
|
| 27 |
+ for line := range strings.SplitSeq(procCgroupData, "\n") {
|
|
| 28 | 28 |
parts := strings.Split(line, ":") |
| 29 | 29 |
if len(parts) != 3 {
|
| 30 | 30 |
continue |
| ... | ... |
@@ -120,7 +120,7 @@ func existingElements(t *testing.T, opts elementListOptions) []string {
|
| 120 | 120 |
} |
| 121 | 121 |
out := cli.DockerCmd(t, args...).Combined() |
| 122 | 122 |
var lines []string |
| 123 |
- for _, l := range strings.Split(out, "\n") {
|
|
| 123 |
+ for l := range strings.SplitSeq(out, "\n") {
|
|
| 124 | 124 |
if l != "" {
|
| 125 | 125 |
lines = append(lines, l) |
| 126 | 126 |
} |
| ... | ... |
@@ -861,7 +861,7 @@ type buildLine struct {
|
| 861 | 861 |
|
| 862 | 862 |
func getImageIDsFromBuild(output []byte) ([]string, error) {
|
| 863 | 863 |
var ids []string |
| 864 |
- for _, line := range bytes.Split(output, []byte("\n")) {
|
|
| 864 |
+ for line := range bytes.SplitSeq(output, []byte("\n")) {
|
|
| 865 | 865 |
if len(line) == 0 {
|
| 866 | 866 |
continue |
| 867 | 867 |
} |
| ... | ... |
@@ -893,7 +893,7 @@ func TestFirewallBackendSwitch(t *testing.T) {
|
| 893 | 893 |
}) |
| 894 | 894 |
|
| 895 | 895 |
// TODO: (When Go 1.24 is min version) Replace with `strings.Lines(dump)`. |
| 896 |
- for _, line := range strings.Split(dump, "\n") {
|
|
| 896 |
+ for line := range strings.SplitSeq(dump, "\n") {
|
|
| 897 | 897 |
line = strings.TrimSpace(line) |
| 898 | 898 |
if line == "" {
|
| 899 | 899 |
continue |
| ... | ... |
@@ -1593,7 +1593,7 @@ func checkProxies(ctx context.Context, t *testing.T, c *client.Client, daemonPid |
| 1593 | 1593 |
t.Error(res) |
| 1594 | 1594 |
return |
| 1595 | 1595 |
} |
| 1596 |
- for _, line := range strings.Split(res.Stdout(), "\n") {
|
|
| 1596 |
+ for line := range strings.SplitSeq(res.Stdout(), "\n") {
|
|
| 1597 | 1597 |
_, args, ok := strings.Cut(line, "docker-proxy") |
| 1598 | 1598 |
if !ok {
|
| 1599 | 1599 |
continue |
| ... | ... |
@@ -1787,7 +1787,7 @@ func TestAdvertiseAddresses(t *testing.T) {
|
| 1787 | 1787 |
// the associated MAC address. |
| 1788 | 1788 |
findNeighMAC := func(neighOut, ip string) string {
|
| 1789 | 1789 |
t.Helper() |
| 1790 |
- for _, line := range strings.Split(neighOut, "\n") {
|
|
| 1790 |
+ for line := range strings.SplitSeq(neighOut, "\n") {
|
|
| 1791 | 1791 |
// Lines look like ... |
| 1792 | 1792 |
// 172.22.22.22 dev eth0 lladdr 36:bc:ce:67:f3:e4 ref 1 used 0/7/0 probes 1 DELAY |
| 1793 | 1793 |
fields := strings.Fields(line) |
| ... | ... |
@@ -2017,7 +2017,7 @@ func TestLegacyLinksEnvVars(t *testing.T) {
|
| 2017 | 2017 |
|
| 2018 | 2018 |
// Check the list of environment variables set in the linking container. |
| 2019 | 2019 |
var found bool |
| 2020 |
- for _, l := range strings.Split(exportRes.Stdout.String(), "\n") {
|
|
| 2020 |
+ for l := range strings.SplitSeq(exportRes.Stdout.String(), "\n") {
|
|
| 2021 | 2021 |
if strings.HasPrefix(l, "export CTR1_") {
|
| 2022 | 2022 |
// Legacy links env var found, but not expected. |
| 2023 | 2023 |
if !tc.expectEnvVars {
|
| ... | ... |
@@ -281,8 +281,8 @@ func enableIPv6OnAll(t *testing.T) func() {
|
| 281 | 281 |
ifaces := map[string]string{}
|
| 282 | 282 |
var allVal string |
| 283 | 283 |
|
| 284 |
- sysctls := strings.Split(string(out), "\n") |
|
| 285 |
- for _, sysctl := range sysctls {
|
|
| 284 |
+ sysctls := strings.SplitSeq(string(out), "\n") |
|
| 285 |
+ for sysctl := range sysctls {
|
|
| 286 | 286 |
if sysctl == "" {
|
| 287 | 287 |
continue |
| 288 | 288 |
} |
| ... | ... |
@@ -1470,7 +1470,7 @@ func TestAccessPortPublishedOnLoopbackAddress(t *testing.T) {
|
| 1470 | 1470 |
func sendPayloadFromHost(t *testing.T, host networking.Host, daddr, dport, payload string, check func() bool) bool {
|
| 1471 | 1471 |
var res bool |
| 1472 | 1472 |
host.Do(t, func() {
|
| 1473 |
- for i := 0; i < 10; i++ {
|
|
| 1473 |
+ for i := range 10 {
|
|
| 1474 | 1474 |
t.Logf("Sending probe #%d to %s:%s from host %s", i, daddr, dport, host.Name)
|
| 1475 | 1475 |
icmd.RunCommand("/bin/sh", "-c", fmt.Sprintf("echo '%s' | nc -w1 -u %s %s", payload, daddr, dport)).Assert(t, icmd.Success)
|
| 1476 | 1476 |
|
| ... | ... |
@@ -121,7 +121,7 @@ func TestCreateServiceMultipleTimes(t *testing.T) {
|
| 121 | 121 |
// a few times, to give tasks time to be deallocated |
| 122 | 122 |
poll.WaitOn(t, swarm.NoTasksForService(ctx, apiClient, serviceID2), swarm.ServicePoll) |
| 123 | 123 |
|
| 124 |
- for retry := 0; retry < 5; retry++ {
|
|
| 124 |
+ for range 5 {
|
|
| 125 | 125 |
_, err = apiClient.NetworkRemove(ctx, overlayID, client.NetworkRemoveOptions{})
|
| 126 | 126 |
// TODO(dperny): using strings.Contains for error checking is awful, |
| 127 | 127 |
// but so is the fact that swarm functions don't return errdefs errors. |
| ... | ... |
@@ -37,7 +37,7 @@ func TestServiceListWithStatuses(t *testing.T) {
|
| 37 | 37 |
|
| 38 | 38 |
serviceCount := 3 |
| 39 | 39 |
// create some services. |
| 40 |
- for i := 0; i < serviceCount; i++ {
|
|
| 40 |
+ for i := range serviceCount {
|
|
| 41 | 41 |
spec := fullSwarmServiceSpec(fmt.Sprintf("test-list-%d", i), uint64(i+1))
|
| 42 | 42 |
// for whatever reason, the args "-u root", when included, cause these |
| 43 | 43 |
// tasks to fail and exit. instead, we'll just pass no args, which |
| ... | ... |
@@ -16,7 +16,7 @@ import ( |
| 16 | 16 |
// TimeoutFlag is the flag to set a per-test timeout when running tests. Defaults to `-timeout`. |
| 17 | 17 |
var TimeoutFlag = flag.Duration("timeout", 0, "DO NOT USE")
|
| 18 | 18 |
|
| 19 |
-var typTestingT = reflect.TypeOf(new(testing.T)) |
|
| 19 |
+var typTestingT = reflect.TypeFor[*testing.T]() |
|
| 20 | 20 |
|
| 21 | 21 |
// Run takes a testing suite and runs all of the tests attached to it. |
| 22 | 22 |
func Run(ctx context.Context, t *testing.T, suite any) {
|
| ... | ... |
@@ -1,6 +1,7 @@ |
| 1 | 1 |
package labelstore |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
+ "maps" |
|
| 4 | 5 |
"sync" |
| 5 | 6 |
|
| 6 | 7 |
"github.com/opencontainers/go-digest" |
| ... | ... |
@@ -40,9 +41,7 @@ func (s *InMemory) Update(dgst digest.Digest, update map[string]string) (map[str |
| 40 | 40 |
if !ok {
|
| 41 | 41 |
labels = map[string]string{}
|
| 42 | 42 |
} |
| 43 |
- for k, v := range update {
|
|
| 44 |
- labels[k] = v |
|
| 45 |
- } |
|
| 43 |
+ maps.Copy(labels, update) |
|
| 46 | 44 |
if s.labels == nil {
|
| 47 | 45 |
s.labels = map[digest.Digest]map[string]string{}
|
| 48 | 46 |
} |
| ... | ... |
@@ -20,7 +20,7 @@ func RandomSinglePlatform(dir string, platform ocispec.Platform, source rand.Sou |
| 20 | 20 |
layerCount := r.Intn(8) |
| 21 | 21 |
|
| 22 | 22 |
var layers []ocispec.Descriptor |
| 23 |
- for i := 0; i < layerCount; i++ {
|
|
| 23 |
+ for i := range layerCount {
|
|
| 24 | 24 |
layerDesc, err := writeLayerWithOneFile(dir, "layer-"+strconv.Itoa(i), []byte(strconv.Itoa(i))) |
| 25 | 25 |
if err != nil {
|
| 26 | 26 |
return nil, err |
| ... | ... |
@@ -56,8 +56,8 @@ func getValueFromOsRelease(key string) (string, error) {
|
| 56 | 56 |
scanner := bufio.NewScanner(osReleaseFile) |
| 57 | 57 |
for scanner.Scan() {
|
| 58 | 58 |
line := scanner.Text() |
| 59 |
- if strings.HasPrefix(line, key+"=") {
|
|
| 60 |
- value = strings.TrimPrefix(line, key+"=") |
|
| 59 |
+ if after, ok := strings.CutPrefix(line, key+"="); ok {
|
|
| 60 |
+ value = after |
|
| 61 | 61 |
value = strings.Trim(value, `"' `) // remove leading/trailing quotes and whitespace |
| 62 | 62 |
} |
| 63 | 63 |
} |
| ... | ... |
@@ -71,7 +71,7 @@ func IsContainerized() (bool, error) {
|
| 71 | 71 |
if err != nil {
|
| 72 | 72 |
return false, err |
| 73 | 73 |
} |
| 74 |
- for _, line := range bytes.Split(b, []byte{'\n'}) {
|
|
| 74 |
+ for line := range bytes.SplitSeq(b, []byte{'\n'}) {
|
|
| 75 | 75 |
if len(line) > 0 && !bytes.HasSuffix(line, []byte(":/")) && !bytes.HasSuffix(line, []byte(":/init.scope")) {
|
| 76 | 76 |
return true, nil |
| 77 | 77 |
} |
| ... | ... |
@@ -16,19 +16,20 @@ func printArgs(args []fnArg) string {
|
| 16 | 16 |
} |
| 17 | 17 |
|
| 18 | 18 |
func buildImports(specs []importSpec) string {
|
| 19 |
- imports := ` |
|
| 19 |
+ var imports strings.Builder |
|
| 20 |
+ imports.WriteString(` |
|
| 20 | 21 |
import( |
| 21 | 22 |
"errors" |
| 22 | 23 |
"time" |
| 23 | 24 |
|
| 24 | 25 |
"github.com/moby/moby/v2/pkg/plugins" |
| 25 |
-` |
|
| 26 |
+`) |
|
| 26 | 27 |
for _, i := range specs {
|
| 27 |
- imports += "\t" + i.String() + "\n" |
|
| 28 |
+ imports.WriteString("\t" + i.String() + "\n")
|
|
| 28 | 29 |
} |
| 29 |
- imports += `) |
|
| 30 |
-` |
|
| 31 |
- return imports |
|
| 30 |
+ imports.WriteString(`) |
|
| 31 |
+`) |
|
| 32 |
+ return imports.String() |
|
| 32 | 33 |
} |
| 33 | 34 |
|
| 34 | 35 |
func marshalType(t string) string {
|
| ... | ... |
@@ -26,6 +26,7 @@ import ( |
| 26 | 26 |
"context" |
| 27 | 27 |
"errors" |
| 28 | 28 |
"fmt" |
| 29 |
+ "slices" |
|
| 29 | 30 |
"sync" |
| 30 | 31 |
"time" |
| 31 | 32 |
|
| ... | ... |
@@ -193,12 +194,7 @@ func (p *Plugin) implements(kind string) bool {
|
| 193 | 193 |
if p.Manifest == nil {
|
| 194 | 194 |
return false |
| 195 | 195 |
} |
| 196 |
- for _, driver := range p.Manifest.Implements {
|
|
| 197 |
- if driver == kind {
|
|
| 198 |
- return true |
|
| 199 |
- } |
|
| 200 |
- } |
|
| 201 |
- return false |
|
| 196 |
+ return slices.Contains(p.Manifest.Implements, kind) |
|
| 202 | 197 |
} |
| 203 | 198 |
|
| 204 | 199 |
func loadWithRetry(name string, retry bool) (*Plugin, error) {
|
| ... | ... |
@@ -55,7 +55,7 @@ func findCgroupV1Mountpoints() (map[string]string, error) {
|
| 55 | 55 |
|
| 56 | 56 |
mps := make(map[string]string) |
| 57 | 57 |
for _, mi := range mounts {
|
| 58 |
- for _, opt := range strings.Split(mi.VFSOptions, ",") {
|
|
| 58 |
+ for opt := range strings.SplitSeq(mi.VFSOptions, ",") {
|
|
| 59 | 59 |
seen, known := allMap[opt] |
| 60 | 60 |
if known && !seen {
|
| 61 | 61 |
allMap[opt] = true |
| ... | ... |
@@ -375,7 +375,7 @@ func parseUintList(val string, maximum int) (map[int]struct{}, error) {
|
| 375 | 375 |
availableInts := make(map[int]struct{})
|
| 376 | 376 |
errInvalidFormat := fmt.Errorf("invalid format: %s", val)
|
| 377 | 377 |
|
| 378 |
- for _, r := range strings.Split(val, ",") {
|
|
| 378 |
+ for r := range strings.SplitSeq(val, ",") {
|
|
| 379 | 379 |
if !strings.Contains(r, "-") {
|
| 380 | 380 |
v, err := strconv.Atoi(r) |
| 381 | 381 |
if err != nil {
|
| ... | ... |
@@ -103,10 +103,7 @@ func NewTailReaderWithDelimiter(ctx context.Context, r SizeReaderAt, reqLines in |
| 103 | 103 |
|
| 104 | 104 |
func newScanner(r SizeReaderAt, delim []byte) *scanner {
|
| 105 | 105 |
size := r.Size() |
| 106 |
- readSize := blockSize |
|
| 107 |
- if readSize > int(size) {
|
|
| 108 |
- readSize = int(size) |
|
| 109 |
- } |
|
| 106 |
+ readSize := min(blockSize, int(size)) |
|
| 110 | 107 |
// silly case... |
| 111 | 108 |
if len(delim) >= readSize/2 {
|
| 112 | 109 |
readSize = len(delim)*2 + 2 |
| ... | ... |
@@ -178,10 +175,7 @@ func (s *scanner) Scan(ctx context.Context) bool {
|
| 178 | 178 |
|
| 179 | 179 |
idx := s.idx - len(s.delim) |
| 180 | 180 |
if idx < 0 {
|
| 181 |
- readSize := int(s.pos) |
|
| 182 |
- if readSize > len(s.buf) {
|
|
| 183 |
- readSize = len(s.buf) |
|
| 184 |
- } |
|
| 181 |
+ readSize := min(int(s.pos), len(s.buf)) |
|
| 185 | 182 |
|
| 186 | 183 |
if readSize < len(s.delim) {
|
| 187 | 184 |
return false |
| ... | ... |
@@ -146,13 +146,13 @@ func BenchmarkTail(b *testing.B) {
|
| 146 | 146 |
} |
| 147 | 147 |
defer f.Close() |
| 148 | 148 |
defer os.RemoveAll(f.Name()) |
| 149 |
- for i := 0; i < 10000; i++ {
|
|
| 149 |
+ for range 10000 {
|
|
| 150 | 150 |
if _, err := f.WriteString("tailfile pretty interesting line\n"); err != nil {
|
| 151 | 151 |
b.Fatal(err) |
| 152 | 152 |
} |
| 153 | 153 |
} |
| 154 |
- b.ResetTimer() |
|
| 155 |
- for i := 0; i < b.N; i++ {
|
|
| 154 |
+ |
|
| 155 |
+ for b.Loop() {
|
|
| 156 | 156 |
if _, err := TailFile(f, 1000); err != nil {
|
| 157 | 157 |
b.Fatal(err) |
| 158 | 158 |
} |
| ... | ... |
@@ -185,7 +185,7 @@ func TestNewTailReader(t *testing.T) {
|
| 185 | 185 |
|
| 186 | 186 |
s8 := `{"log":"Don't panic!\n","stream":"stdout","time":"2018-04-04T20:28:44.7207062Z"}`
|
| 187 | 187 |
jsonTest := make([]string, 0, 20) |
| 188 |
- for i := 0; i < 20; i++ {
|
|
| 188 |
+ for range 20 {
|
|
| 189 | 189 |
jsonTest = append(jsonTest, s8) |
| 190 | 190 |
} |
| 191 | 191 |
|
| ... | ... |
@@ -210,10 +210,7 @@ func TestNewTailReader(t *testing.T) {
|
| 210 | 210 |
test := test |
| 211 | 211 |
t.Parallel() |
| 212 | 212 |
|
| 213 |
- maxLen := len(test.data) |
|
| 214 |
- if maxLen > 10 {
|
|
| 215 |
- maxLen = 10 |
|
| 216 |
- } |
|
| 213 |
+ maxLen := min(len(test.data), 10) |
|
| 217 | 214 |
|
| 218 | 215 |
s := strings.Join(test.data, string(delim)) |
| 219 | 216 |
if len(test.data) > 0 {
|