From 295d2b06ead8834d1e20d53cf1d692521e93dbf6 Mon Sep 17 00:00:00 2001 From: Tariq Ibrahim Date: Thu, 11 Apr 2024 12:18:42 -0700 Subject: [PATCH] replace math/rand with the rand pkg from k8s.io/apimachinery Signed-off-by: Tariq Ibrahim --- .golangci.yml | 10 +- pkg/mig/config/config_test.go | 2 +- .../k8s.io/apimachinery/pkg/util/rand/rand.go | 127 ++++++++++++++++++ vendor/modules.txt | 1 + 4 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 vendor/k8s.io/apimachinery/pkg/util/rand/rand.go diff --git a/.golangci.yml b/.golangci.yml index 20e4d944..1c57b246 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,5 +1,5 @@ run: - deadline: 10m + timeout: 10m linters: enable: @@ -20,11 +20,3 @@ linters: linters-settings: goimports: local-prefixes: github.com/NVIDIA/mig-parted - -issues: - exclude-rules: - # We use math/rand instead of crypto/rand for unique names in config tests. - - path: pkg/mig/config/config_test.go - linters: - - gosec - text: "G404" diff --git a/pkg/mig/config/config_test.go b/pkg/mig/config/config_test.go index 27ceb047..ad3057f7 100644 --- a/pkg/mig/config/config_test.go +++ b/pkg/mig/config/config_test.go @@ -18,10 +18,10 @@ package config import ( "fmt" - "math/rand" "testing" "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/util/rand" "github.com/NVIDIA/mig-parted/internal/nvlib" "github.com/NVIDIA/mig-parted/internal/nvml" diff --git a/vendor/k8s.io/apimachinery/pkg/util/rand/rand.go b/vendor/k8s.io/apimachinery/pkg/util/rand/rand.go new file mode 100644 index 00000000..82a473bb --- /dev/null +++ b/vendor/k8s.io/apimachinery/pkg/util/rand/rand.go @@ -0,0 +1,127 @@ +/* +Copyright 2015 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package rand provides utilities related to randomization. +package rand + +import ( + "math/rand" + "sync" + "time" +) + +var rng = struct { + sync.Mutex + rand *rand.Rand +}{ + rand: rand.New(rand.NewSource(time.Now().UnixNano())), +} + +// Int returns a non-negative pseudo-random int. +func Int() int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Int() +} + +// Intn generates an integer in range [0,max). +// By design this should panic if input is invalid, <= 0. +func Intn(max int) int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Intn(max) +} + +// IntnRange generates an integer in range [min,max). +// By design this should panic if input is invalid, <= 0. +func IntnRange(min, max int) int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Intn(max-min) + min +} + +// IntnRange generates an int64 integer in range [min,max). +// By design this should panic if input is invalid, <= 0. +func Int63nRange(min, max int64) int64 { + rng.Lock() + defer rng.Unlock() + return rng.rand.Int63n(max-min) + min +} + +// Seed seeds the rng with the provided seed. +func Seed(seed int64) { + rng.Lock() + defer rng.Unlock() + + rng.rand = rand.New(rand.NewSource(seed)) +} + +// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n) +// from the default Source. +func Perm(n int) []int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Perm(n) +} + +const ( + // We omit vowels from the set of available characters to reduce the chances + // of "bad words" being formed. + alphanums = "bcdfghjklmnpqrstvwxz2456789" + // No. of bits required to index into alphanums string. + alphanumsIdxBits = 5 + // Mask used to extract last alphanumsIdxBits of an int. + alphanumsIdxMask = 1<>= alphanumsIdxBits + remaining-- + } + return string(b) +} + +// SafeEncodeString encodes s using the same characters as rand.String. This reduces the chances of bad words and +// ensures that strings generated from hash functions appear consistent throughout the API. +func SafeEncodeString(s string) string { + r := make([]byte, len(s)) + for i, b := range []rune(s) { + r[i] = alphanums[(int(b) % len(alphanums))] + } + return string(r) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 50549683..5e3c6276 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -245,6 +245,7 @@ k8s.io/apimachinery/pkg/util/json k8s.io/apimachinery/pkg/util/managedfields k8s.io/apimachinery/pkg/util/naming k8s.io/apimachinery/pkg/util/net +k8s.io/apimachinery/pkg/util/rand k8s.io/apimachinery/pkg/util/runtime k8s.io/apimachinery/pkg/util/sets k8s.io/apimachinery/pkg/util/validation