| 1 |
/* |
| 2 |
Copyright 2019 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package discovery |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto/sha256" |
| 21 |
"encoding/base64" |
| 22 |
) |
| 23 |
|
| 24 |
// StorageVersionHash calculates the storage version hash for a |
| 25 |
// <group/version/kind> tuple. |
| 26 |
// WARNING: this function is subject to change. Clients shouldn't depend on |
| 27 |
// this function. |
| 28 |
func StorageVersionHash(group, version, kind string) string { |
| 29 |
gvk := group + "/" + version + "/" + kind |
| 30 |
bytes := sha256.Sum256([]byte(gvk)) |
| 31 |
// Assuming there are N kinds in the cluster, and the hash is X-byte long, |
| 32 |
// the chance of colliding hash P(N,X) approximates to 1-e^(-(N^2)/2^(8X+1)). |
| 33 |
// P(10,000, 8) ~= 2.7*10^(-12), which is low enough. |
| 34 |
// See https://en.wikipedia.org/wiki/Birthday_problem#Approximations. |
| 35 |
return base64.StdEncoding.EncodeToString(bytes[:8]) |
| 36 |
} |