Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
550203b3
Commit
550203b3
authored
Jul 22, 2015
by
Vish Kannan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9162 from thockin/rand-util
Move rand string func to util for reuse
parents
fbc85e98
7874743a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
88 additions
and
13 deletions
+88
-13
generate.go
pkg/api/generate.go
+3
-13
rand.go
pkg/util/rand/rand.go
+48
-0
rand_test.go
pkg/util/rand/rand_test.go
+37
-0
No files found.
pkg/api/generate.go
View file @
550203b3
...
@@ -18,7 +18,8 @@ package api
...
@@ -18,7 +18,8 @@ package api
import
(
import
(
"fmt"
"fmt"
"math/rand"
utilrand
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/rand"
)
)
// NameGenerator generates names for objects. Some backends may have more information
// NameGenerator generates names for objects. Some backends may have more information
...
@@ -59,16 +60,5 @@ func (simpleNameGenerator) GenerateName(base string) string {
...
@@ -59,16 +60,5 @@ func (simpleNameGenerator) GenerateName(base string) string {
if
len
(
base
)
>
maxGeneratedNameLength
{
if
len
(
base
)
>
maxGeneratedNameLength
{
base
=
base
[
:
maxGeneratedNameLength
]
base
=
base
[
:
maxGeneratedNameLength
]
}
}
value
:=
randSeq
(
randomLength
)
return
fmt
.
Sprintf
(
"%s%s"
,
base
,
utilrand
.
String
(
randomLength
))
return
fmt
.
Sprintf
(
"%s%s"
,
base
,
value
)
}
var
letters
=
[]
rune
(
"abcdefghijklmnopqrstuvwxyz0123456789"
)
func
randSeq
(
n
int
)
string
{
b
:=
make
([]
rune
,
n
)
for
i
:=
range
b
{
b
[
i
]
=
letters
[
rand
.
Intn
(
len
(
letters
))]
}
return
string
(
b
)
}
}
pkg/util/rand/rand.go
0 → 100644
View file @
550203b3
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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
letters
=
[]
rune
(
"abcdefghijklmnopqrstuvwxyz0123456789"
)
var
numLetters
=
len
(
letters
)
var
rng
=
struct
{
rand
*
rand
.
Rand
lock
sync
.
Mutex
}{
rand
:
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UTC
()
.
UnixNano
())),
}
// String generates a random alphanumeric string n characters long. This will
// panic if n is less than zero.
func
String
(
n
int
)
string
{
if
n
<
0
{
panic
(
"out-of-bounds value"
)
}
b
:=
make
([]
rune
,
n
)
rng
.
lock
.
Lock
()
defer
rng
.
lock
.
Unlock
()
for
i
:=
range
b
{
b
[
i
]
=
letters
[
rng
.
rand
.
Intn
(
numLetters
)]
}
return
string
(
b
)
}
pkg/util/rand/rand_test.go
0 → 100644
View file @
550203b3
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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
import
(
"strings"
"testing"
)
func
TestString
(
t
*
testing
.
T
)
{
valid
:=
"0123456789abcdefghijklmnopqrstuvwxyz"
for
_
,
l
:=
range
[]
int
{
0
,
1
,
2
,
10
,
123
}
{
s
:=
String
(
l
)
if
len
(
s
)
!=
l
{
t
.
Errorf
(
"expected string of size %d, got %q"
,
l
,
s
)
}
for
_
,
c
:=
range
s
{
if
!
strings
.
ContainsRune
(
valid
,
c
)
{
t
.
Errorf
(
"expected valid charaters, got %v"
,
c
)
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment