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