-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclusterspec.go
199 lines (187 loc) · 5.88 KB
/
clusterspec.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package clusterspec
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"strconv"
)
type ClusterSpec struct {
Name string
ApiProvider string
CloudProvider string
Type string
KubernetesVersion string
NodeType string
NodeCount int
MasterNodeCount int
Region string
PodCidrBlock string
SshKeyName string
Tags string
Description string
ClusterAutoscalerEnabled bool
ClusterAutoscalerMinNodes int
ClusterAutoscalerMaxNodes int
}
const (
ApiProviderKey = "apiProvider"
CloudProviderKey = "cloudProvider"
NodeTypeKey = "nodeType"
ClusterTypeKey = "type"
KubernetesVersionKey = "kubernetesVersion"
NodeCountKey = "nodeCount"
MasterNodeCountKey = "masterNodeCount"
RegionKey = "region"
PodCidrBlockKey = "podCidrBlock"
SshKeyNameKey = "sshKeyName"
TagsKey = "tags"
DescriptionKey = "description"
ClusterAutoscalerEnabledKey = "clusterAutoscalerEnabled"
ClusterAutoscalerMinNodesKey = "clusterAutoscalerMinNodes"
ClusterAutoscalerMaxNodesKey = "clusterAutoscalerMaxNodes"
defaultMasterNodeCount = 3
defaultClusterAutoscalerEnabled = false
defaultClusterAutoscalerMinNodes = 1
defaultClusterAutoscalerMaxNodes = 9
)
var (
ValidHelmParamKeys = []string{
RegionKey,
SshKeyNameKey,
KubernetesVersionKey,
PodCidrBlockKey,
NodeCountKey,
MasterNodeCountKey,
NodeTypeKey,
ClusterAutoscalerEnabledKey,
ClusterAutoscalerMinNodesKey,
ClusterAutoscalerMaxNodesKey,
}
)
func Get(
kubeClient *kubernetes.Clientset,
arlonNs string,
specName string,
) (cs *ClusterSpec, err error) {
client := kubeClient.CoreV1()
configMapApi := client.ConfigMaps(arlonNs)
cm, err := configMapApi.Get(context.Background(), specName, metav1.GetOptions{})
if err != nil {
return nil, err
}
cs, err = FromConfigMap(cm)
if err != nil {
return nil, err
}
return
}
func FromConfigMap(cm *corev1.ConfigMap) (*ClusterSpec, error) {
cs := &ClusterSpec{
Name: cm.Name,
ApiProvider: cm.Data[ApiProviderKey],
CloudProvider: cm.Data[CloudProviderKey],
Type: cm.Data[ClusterTypeKey],
KubernetesVersion: cm.Data[KubernetesVersionKey],
NodeType: cm.Data[NodeTypeKey],
Region: cm.Data[RegionKey],
PodCidrBlock: cm.Data[PodCidrBlockKey],
SshKeyName: cm.Data[SshKeyNameKey],
Tags: cm.Data[TagsKey],
Description: cm.Data[DescriptionKey],
}
var err error
cs.NodeCount, err = strconv.Atoi(cm.Data[NodeCountKey])
if err != nil {
return nil, fmt.Errorf("could not parse clusterspec nodeCount: %s", err)
}
cs.MasterNodeCount, err = strconv.Atoi(cm.Data[MasterNodeCountKey])
if err != nil {
cs.MasterNodeCount = defaultMasterNodeCount // for backward compatibility if not present
}
cs.ClusterAutoscalerEnabled, err = strconv.ParseBool(cm.Data[ClusterAutoscalerEnabledKey])
if err != nil {
cs.ClusterAutoscalerEnabled = defaultClusterAutoscalerEnabled
}
cs.ClusterAutoscalerMinNodes, err = strconv.Atoi(cm.Data[ClusterAutoscalerMinNodesKey])
if err != nil {
cs.ClusterAutoscalerMinNodes = defaultClusterAutoscalerMinNodes
}
cs.ClusterAutoscalerMaxNodes, err = strconv.Atoi(cm.Data[ClusterAutoscalerMaxNodesKey])
if err != nil {
cs.ClusterAutoscalerMaxNodes = defaultClusterAutoscalerMaxNodes
}
return cs, nil
}
func ToConfigMap(
name string,
apiProvider string,
cloudProvider string,
clusterType string,
kubernetesVersion string,
nodeType string,
nodeCount int,
masterNodeCount int,
region string,
podCidrBlock string,
sshKeyName string,
clusterAutoscalerEnabled bool,
clusterAutoscalerMinNodes int,
clusterAutoscalerMaxNodes int,
tags string,
description string,
) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
"managed-by": "arlon",
"arlon-type": "clusterspec",
},
},
Data: map[string]string{
ApiProviderKey: apiProvider,
CloudProviderKey: cloudProvider,
ClusterTypeKey: clusterType,
KubernetesVersionKey: kubernetesVersion,
NodeTypeKey: nodeType,
NodeCountKey: strconv.Itoa(nodeCount),
MasterNodeCountKey: strconv.Itoa(masterNodeCount),
RegionKey: region,
PodCidrBlockKey: podCidrBlock,
SshKeyNameKey: sshKeyName,
ClusterAutoscalerEnabledKey: strconv.FormatBool(clusterAutoscalerEnabled),
ClusterAutoscalerMinNodesKey: strconv.Itoa(clusterAutoscalerMinNodes),
ClusterAutoscalerMaxNodesKey: strconv.Itoa(clusterAutoscalerMaxNodes),
TagsKey: tags,
DescriptionKey: description,
},
}
}
func SubchartName(cm *corev1.ConfigMap) (string, error) {
cs, err := FromConfigMap(cm)
if err != nil {
return "", err
}
return SubchartNameFromClusterSpec(cs)
}
func SubchartNameFromClusterSpec(cs *ClusterSpec) (string, error) {
if err := ValidApiProvider(cs.ApiProvider); err != nil {
return "", err
}
if err := ValidCloudProviderAndClusterType(cs.CloudProvider, cs.Type); err != nil {
return "", err
}
return fmt.Sprintf("%s-%s-%s", cs.ApiProvider,
cs.CloudProvider, cs.Type), nil
}
func ClusterAutoscalerSubchartNameFromClusterSpec(cs *ClusterSpec) (string, error) {
if err := ValidApiProvider(cs.ApiProvider); err != nil {
return "", err
}
return ClusterAutoscalerSubchartNameFromApiProvider(cs.ApiProvider), nil
}
func ClusterAutoscalerSubchartNameFromApiProvider(apiProvider string) string {
return fmt.Sprintf("%s-cluster-autoscaler", apiProvider)
}