-
Notifications
You must be signed in to change notification settings - Fork 34
/
k8s_cluster.go
69 lines (63 loc) · 2.43 KB
/
k8s_cluster.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
65
66
67
68
69
package utils
import (
"fmt"
"strings"
"github.com/berops/claudie/internal/manifest"
"github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
)
// CreateK8sCluster reads the unmarshalled manifest and creates desired state for Kubernetes clusters.
// Returns slice of *pb.K8Scluster if successful, nil otherwise
func CreateK8sCluster(unmarshalledManifest *manifest.Manifest) ([]*pb.K8Scluster, error) {
var clusters []*pb.K8Scluster
// Loop through clusters mentioned in the manifest
for _, cluster := range unmarshalledManifest.Kubernetes.Clusters {
// Generate variables
newCluster := &pb.K8Scluster{
ClusterInfo: &pb.ClusterInfo{
Name: strings.ToLower(cluster.Name),
Hash: utils.CreateHash(utils.HashLength),
},
Kubernetes: cluster.Version,
Network: cluster.Network,
}
// create node-pools
controlNodePools, err := unmarshalledManifest.CreateNodepools(cluster.Pools.Control, true)
if err != nil {
return nil, fmt.Errorf("error while creating control nodepool for %s : %w", cluster.Name, err)
}
computeNodePools, err := unmarshalledManifest.CreateNodepools(cluster.Pools.Compute, false)
if err != nil {
return nil, fmt.Errorf("error while creating compute nodepool for %s : %w", cluster.Name, err)
}
newCluster.ClusterInfo.NodePools = append(controlNodePools, computeNodePools...)
clusters = append(clusters, newCluster)
}
return clusters, nil
}
// UpdateK8sClusters updates the desired state of the kubernetes clusters based on the current state
// returns error if failed, nil otherwise
func UpdateK8sClusters(newConfig *pb.Config) error {
clusterDesired:
for _, clusterDesired := range newConfig.DesiredState.Clusters {
for _, clusterCurrent := range newConfig.CurrentState.Clusters {
// Found current cluster with matching name
if clusterDesired.ClusterInfo.Name == clusterCurrent.ClusterInfo.Name {
updateClusterInfo(clusterDesired.ClusterInfo, clusterCurrent.ClusterInfo)
if clusterCurrent.Kubeconfig != "" {
clusterDesired.Kubeconfig = clusterCurrent.Kubeconfig
}
// Skip the checks bellow
continue clusterDesired
}
}
// No current cluster found with matching name, create keys
if clusterDesired.ClusterInfo.PublicKey == "" {
err := createSSHKeyPair(clusterDesired.ClusterInfo)
if err != nil {
return fmt.Errorf("error encountered while creating desired state for %s : %w", clusterDesired.ClusterInfo.Name, err)
}
}
}
return nil
}