Use rand from apimachinery to avoid seeding rng every time

This commit is contained in:
Mikhail Mazurskiy 2021-05-12 12:13:02 +10:00
parent e351b2bc43
commit 36cb2cea5e
No known key found for this signature in database
GPG Key ID: FA7917C48932DD55
4 changed files with 6 additions and 11 deletions

View File

@ -5,8 +5,8 @@ package common
import (
"fmt"
"math/rand"
"k8s.io/apimachinery/pkg/util/rand"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)
@ -44,9 +44,8 @@ const (
)
// RandomStr returns an eight-digit (with leading zeros) string of a
// random number seeded with the parameter.
func RandomStr(seed int64) string {
rand.Seed(seed)
// random number.
func RandomStr() string {
randomInt := rand.Intn(maxRandInt) // nolint:gosec
return fmt.Sprintf("%08d", randomInt)
}

View File

@ -221,7 +221,7 @@ func fileExists(path string) bool {
func (i *InitOptions) fillInValues() string {
now := time.Now()
nowStr := now.Format("2006-01-02 15:04:05 MST")
randomSuffix := common.RandomStr(now.UTC().UnixNano())
randomSuffix := common.RandomStr()
manifestStr := i.Template
klog.V(4).Infof("namespace/inventory-id: %s/%s", i.Namespace, i.InventoryID)
manifestStr = strings.ReplaceAll(manifestStr, "<DATETIME>", nowStr)

View File

@ -14,7 +14,6 @@ package inventory
import (
"fmt"
"strings"
"time"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@ -174,8 +173,7 @@ func fixLegacyInventoryName(obj *unstructured.Unstructured) error {
name := accessor.GetName()
if obj.GetName() == legacyInvName || name == legacyInvName {
klog.V(4).Infof("renaming legacy inventory name")
seed := time.Now().UTC().UnixNano()
randomSuffix := common.RandomStr(seed)
randomSuffix := common.RandomStr()
return addSuffixToName(obj, randomSuffix)
}
return nil

View File

@ -6,7 +6,6 @@ package e2e
import (
"fmt"
"strings"
"time"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
@ -22,8 +21,7 @@ import (
)
func randomString(prefix string) string {
seed := time.Now().UTC().UnixNano()
randomSuffix := common.RandomStr(seed)
randomSuffix := common.RandomStr()
return fmt.Sprintf("%s%s", prefix, randomSuffix)
}