/
hash.go
64 lines (57 loc) · 1.87 KB
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2023, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package variables
import (
"crypto/sha256"
"encoding/base32"
"fmt"
"strings"
)
/*
* Compute hashes for Control plane and node pools.
* Hashes are used to determine if there is a change in the node pool, and trigger rolling upgrades.
*/
func (v *Variables) SetHashes() {
v.SetControlPlaneHash()
v.SetNodePoolHash()
}
func (v *Variables) SetControlPlaneHash() {
b := strings.Builder{}
b.WriteString(v.KubernetesVersion)
b.WriteString(v.SSHPublicKey)
b.WriteString(v.PrivateRegistry)
b.WriteString(v.ControlPlaneShape)
b.WriteString(v.ActualImage)
b.WriteString(v.ETCDImageTag)
b.WriteString(v.CoreDNSImageTag)
b.WriteString(fmt.Sprintf("%d", v.ControlPlaneReplicas))
b.WriteString(fmt.Sprintf("%d", v.ControlPlaneOCPUs))
b.WriteString(fmt.Sprintf("%d", v.ControlPlaneMemoryGbs))
b.WriteString(fmt.Sprintf("%d", v.ControlPlaneVolumeGbs))
b.WriteString(fmt.Sprintf("%v", v.NodePVTransitEncryption))
v.ControlPlaneHash = hashSum(b.String())
}
func (v *Variables) SetNodePoolHash() {
var hashedNodePools []NodePool
for _, np := range v.NodePools {
hashedNP := np
b := strings.Builder{}
b.WriteString(v.KubernetesVersion)
b.WriteString(v.SSHPublicKey)
b.WriteString(v.ActualImage)
b.WriteString(hashedNP.Name)
b.WriteString(hashedNP.Shape)
b.WriteString(fmt.Sprintf("%d", hashedNP.Memory))
b.WriteString(fmt.Sprintf("%d", hashedNP.Ocpus))
b.WriteString(fmt.Sprintf("%v", v.NodePVTransitEncryption))
hashedNP.Hash = hashSum(b.String())
hashedNodePools = append(hashedNodePools, hashedNP)
}
v.NodePools = hashedNodePools
}
func hashSum(input string) string {
sha := sha256.New()
sha.Write([]byte(input))
encoded := base32.StdEncoding.EncodeToString(sha.Sum(nil))
return strings.ToLower(encoded[0:5])
}