Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More random pod names in tests #1498

Merged
merged 2 commits into from
Mar 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkg/test/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ package test

import (
"fmt"
"math/rand"
"strings"
"sync"
"time"

"github.com/Pallinder/go-randomdata"
"github.com/imdario/mergo"
Expand Down Expand Up @@ -54,6 +57,13 @@ type PDBOptions struct {
MaxUnavailable *intstr.IntOrString
}

var (
sequentialPodNumber = 0
randomSource = rand.NewSource(time.Now().UnixNano())
randomizer = rand.New(randomSource) //nolint
sequentialPodNumberLock = new(sync.Mutex)
)

// Pod creates a test pod with defaults that can be overridden by PodOptions.
// Overrides are applied in order, with a last write wins semantic.
func Pod(overrides ...PodOptions) *v1.Pod {
Expand Down Expand Up @@ -81,7 +91,7 @@ func Pod(overrides ...PodOptions) *v1.Pod {
TopologySpreadConstraints: options.TopologySpreadConstraints,
Tolerations: options.Tolerations,
Containers: []v1.Container{{
Name: strings.ToLower(randomdata.SillyName()),
Name: strings.ToLower(sequentialRandomName()),
Image: options.Image,
Resources: options.ResourceRequirements,
}},
Expand All @@ -96,6 +106,13 @@ func Pod(overrides ...PodOptions) *v1.Pod {
}
}

func sequentialRandomName() string {
sequentialPodNumberLock.Lock()
defer sequentialPodNumberLock.Unlock()
tzneal marked this conversation as resolved.
Show resolved Hide resolved
sequentialPodNumber++
return fmt.Sprintf("P%04d-%s-%06d", sequentialPodNumber, randomdata.SillyName(), randomizer.Intn(99999))
}

// Pods creates homogeneous groups of pods based on the passed in options, evenly divided by the total pods requested
func Pods(total int, options ...PodOptions) []*v1.Pod {
pods := []*v1.Pod{}
Expand Down