mirror of https://github.com/fluxcd/cli-utils.git
Use rand from apimachinery to avoid seeding rng every time
This commit is contained in:
parent
e351b2bc43
commit
36cb2cea5e
|
|
@ -5,8 +5,8 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/util/rand"
|
||||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -44,9 +44,8 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// RandomStr returns an eight-digit (with leading zeros) string of a
|
// RandomStr returns an eight-digit (with leading zeros) string of a
|
||||||
// random number seeded with the parameter.
|
// random number.
|
||||||
func RandomStr(seed int64) string {
|
func RandomStr() string {
|
||||||
rand.Seed(seed)
|
|
||||||
randomInt := rand.Intn(maxRandInt) // nolint:gosec
|
randomInt := rand.Intn(maxRandInt) // nolint:gosec
|
||||||
return fmt.Sprintf("%08d", randomInt)
|
return fmt.Sprintf("%08d", randomInt)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -221,7 +221,7 @@ func fileExists(path string) bool {
|
||||||
func (i *InitOptions) fillInValues() string {
|
func (i *InitOptions) fillInValues() string {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
nowStr := now.Format("2006-01-02 15:04:05 MST")
|
nowStr := now.Format("2006-01-02 15:04:05 MST")
|
||||||
randomSuffix := common.RandomStr(now.UTC().UnixNano())
|
randomSuffix := common.RandomStr()
|
||||||
manifestStr := i.Template
|
manifestStr := i.Template
|
||||||
klog.V(4).Infof("namespace/inventory-id: %s/%s", i.Namespace, i.InventoryID)
|
klog.V(4).Infof("namespace/inventory-id: %s/%s", i.Namespace, i.InventoryID)
|
||||||
manifestStr = strings.ReplaceAll(manifestStr, "<DATETIME>", nowStr)
|
manifestStr = strings.ReplaceAll(manifestStr, "<DATETIME>", nowStr)
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ package inventory
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
|
@ -174,8 +173,7 @@ func fixLegacyInventoryName(obj *unstructured.Unstructured) error {
|
||||||
name := accessor.GetName()
|
name := accessor.GetName()
|
||||||
if obj.GetName() == legacyInvName || name == legacyInvName {
|
if obj.GetName() == legacyInvName || name == legacyInvName {
|
||||||
klog.V(4).Infof("renaming legacy inventory name")
|
klog.V(4).Infof("renaming legacy inventory name")
|
||||||
seed := time.Now().UTC().UnixNano()
|
randomSuffix := common.RandomStr()
|
||||||
randomSuffix := common.RandomStr(seed)
|
|
||||||
return addSuffixToName(obj, randomSuffix)
|
return addSuffixToName(obj, randomSuffix)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ package e2e
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
appsv1 "k8s.io/api/apps/v1"
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
|
|
@ -22,8 +21,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func randomString(prefix string) string {
|
func randomString(prefix string) string {
|
||||||
seed := time.Now().UTC().UnixNano()
|
randomSuffix := common.RandomStr()
|
||||||
randomSuffix := common.RandomStr(seed)
|
|
||||||
return fmt.Sprintf("%s%s", prefix, randomSuffix)
|
return fmt.Sprintf("%s%s", prefix, randomSuffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue