-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These are only meant for interactive use in commands, not the API. So cmdutils is the best place to keep these.
- Loading branch information
1 parent
87fa483
commit dd0b039
Showing
4 changed files
with
59 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cmdutils | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/kubicorn/kubicorn/pkg/namer" | ||
) | ||
|
||
const ( | ||
randNodeGroupNameLength = 8 | ||
randNodeGroupNameComponents = "abcdef0123456789" | ||
) | ||
|
||
var r = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
||
// NodeGroupName generates a name string when a and b are empty strings. | ||
// If either a or b are non-empty, it returns whichever is non-empty. | ||
// If neither a nor b are empty, it returns empty name, to indicate | ||
// ambiguous usage. | ||
// It uses a different naming scheme from ClusterName, so that users can | ||
// easily distinguish a cluster name from nodegroup name. | ||
func NodeGroupName(a, b string) string { | ||
return useNameOrGenerate(a, b, func() string { | ||
name := make([]byte, randNodeGroupNameLength) | ||
for i := 0; i < randNodeGroupNameLength; i++ { | ||
name[i] = randNodeGroupNameComponents[r.Intn(len(randNodeGroupNameComponents))] | ||
} | ||
return fmt.Sprintf("ng-%s", string(name)) | ||
}) | ||
} | ||
|
||
// useNameOrGenerate picks one of the provided strings or generates a | ||
// new one using the provided generate function | ||
func useNameOrGenerate(a, b string, generate func() string) string { | ||
if a != "" && b != "" { | ||
return "" | ||
} | ||
if a != "" { | ||
return a | ||
} | ||
if b != "" { | ||
return b | ||
} | ||
return generate() | ||
} | ||
|
||
// ClusterName generates a name string when a and b are empty strings. | ||
// If either a or b are non-empty, it returns whichever is non-empty. | ||
// If neither a nor b are empty, it returns empty name, to indicate | ||
// ambiguous usage. | ||
func ClusterName(a, b string) string { | ||
return useNameOrGenerate(a, b, func() string { | ||
return fmt.Sprintf("%s-%d", namer.RandomName(), time.Now().Unix()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.