GenerateRandomID returns a unique, 64-character ID consisting of a-z, 0-9. It guarantees that the ID, when truncated ([TruncateID]) does not consist of numbers only, so that the truncated ID can be used as hostname for containers.
()
| 38 | // of numbers only, so that the truncated ID can be used as hostname for |
| 39 | // containers. |
| 40 | func GenerateRandomID() string { |
| 41 | b := make([]byte, 32) |
| 42 | for { |
| 43 | if _, err := rand.Read(b); err != nil { |
| 44 | panic(err) // This shouldn't happen |
| 45 | } |
| 46 | id := hex.EncodeToString(b) |
| 47 | |
| 48 | // make sure that the truncated ID does not consist of only numeric |
| 49 | // characters, as it's used as default hostname for containers. |
| 50 | // |
| 51 | // See: |
| 52 | // - https://github.com/moby/moby/issues/3869 |
| 53 | // - https://bugzilla.redhat.com/show_bug.cgi?id=1059122 |
| 54 | if allNum(id[:shortLen]) { |
| 55 | // all numbers; try again |
| 56 | continue |
| 57 | } |
| 58 | return id |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // allNum checks whether id consists of only numbers (0-9). |
| 63 | func allNum(id string) bool { |
searching dependent graphs…