1
1
package main
2
2
3
3
import (
4
+ "bufio"
4
5
"context"
5
6
"errors"
6
7
"fmt"
8
+ "os"
9
+ "strconv"
10
+ "strings"
7
11
8
12
"github.com/google/uuid"
9
13
"github.com/openinfradev/tks-common/pkg/log"
10
14
pb "github.com/openinfradev/tks-proto/tks_pb"
11
15
)
12
16
17
+ var (
18
+ filePathAzRegion = "./az-per-region.txt"
19
+ )
20
+
21
+ const MAX_SIZE_PER_AZ = 99
22
+
13
23
func validateCreateClusterRequest (in * pb.CreateClusterRequest ) (err error ) {
14
24
if _ , err := uuid .Parse (in .GetContractId ()); err != nil {
15
25
return fmt .Errorf ("invalid contract ID %s" , in .GetContractId ())
@@ -56,6 +66,105 @@ func validateUninstallAppGroupsRequest(in *pb.UninstallAppGroupsRequest) (err er
56
66
return nil
57
67
}
58
68
69
+ func constructClusterConf (rawConf * pb.ClusterRawConf ) (clusterConf * pb.ClusterConf , err error ) {
70
+ region := "ap-northeast-2"
71
+ if rawConf != nil && rawConf .Region != "" {
72
+ region = rawConf .Region
73
+ }
74
+
75
+ numOfAz := 3
76
+ if rawConf != nil && rawConf .NumOfAz != 0 {
77
+ numOfAz = int (rawConf .NumOfAz )
78
+ }
79
+
80
+ sshKeyName := "tks-seoul"
81
+ if rawConf != nil && rawConf .SshKeyName != "" {
82
+ sshKeyName = rawConf .SshKeyName
83
+ }
84
+
85
+ machineType := "t3.large"
86
+ if rawConf != nil && rawConf .MachineType != "" {
87
+ machineType = rawConf .MachineType
88
+ }
89
+
90
+ minSizePerAz := 1
91
+ maxSizePerAz := 5
92
+
93
+ // Check if numOfAz is correct based on pre-defined mapping table
94
+ maxAzForSelectedRegion := 0
95
+
96
+ file , err := os .Open (filePathAzRegion )
97
+ if err != nil {
98
+ log .Error (err )
99
+ }
100
+ defer file .Close ()
101
+
102
+ scanner := bufio .NewScanner (file )
103
+ var found bool = false
104
+ for scanner .Scan () {
105
+ if strings .Contains (scanner .Text (), region ) {
106
+ log .Debug ("Found region line: " , scanner .Text ())
107
+ azNum := strings .Split (scanner .Text (), ":" )[1 ]
108
+ maxAzForSelectedRegion , err = strconv .Atoi (strings .TrimSpace (azNum ))
109
+ if err != nil {
110
+ log .Error ("Error while converting azNum to int var: " , err )
111
+ }
112
+ log .Debug ("Trimmed azNum var: " , maxAzForSelectedRegion )
113
+ found = true
114
+ }
115
+ }
116
+
117
+ if err := scanner .Err (); err != nil {
118
+ log .Error ("Error while processing file: " , err )
119
+ }
120
+ if ! found {
121
+ log .Error ("Couldn't find entry for region " , region )
122
+ }
123
+
124
+ if numOfAz > maxAzForSelectedRegion {
125
+ log .Error ("Invalid numOfAz: exceeded the number of Az in region " , region )
126
+ temp_err := fmt .Errorf ("Invalid numOfAz: exceeded the number of Az in region %s" , region )
127
+ return nil , temp_err
128
+ }
129
+
130
+ // Validate if machineReplicas is multiple of number of AZ
131
+ replicas := int (rawConf .MachineReplicas )
132
+ if replicas == 0 {
133
+ log .Debug ("No machineReplicas param. Using default values.." )
134
+ } else {
135
+ if remainder := replicas % numOfAz ; remainder != 0 {
136
+ log .Error ("Invalid machineReplicas: it should be multiple of numOfAz " , numOfAz )
137
+ temp_err := fmt .Errorf ("Invalid machineReplicas: it should be multiple of numOfAz %d" , numOfAz )
138
+ return nil , temp_err
139
+ } else {
140
+ log .Debug ("Valid replicas and numOfAz. Caculating minSize & maxSize.." )
141
+ minSizePerAz = int (replicas / numOfAz )
142
+ maxSizePerAz = minSizePerAz * 5
143
+
144
+ // Validate if maxSizePerAx is within allowed range
145
+ if maxSizePerAz > MAX_SIZE_PER_AZ {
146
+ fmt .Printf ("maxSizePerAz exceeded maximum value %d, so adjusted to %d" , MAX_SIZE_PER_AZ , MAX_SIZE_PER_AZ )
147
+ maxSizePerAz = MAX_SIZE_PER_AZ
148
+ }
149
+ log .Debug ("Derived minSizePerAz: " , minSizePerAz )
150
+ log .Debug ("Derived maxSizePerAz: " , maxSizePerAz )
151
+ }
152
+ }
153
+
154
+ // Construct cluster conf
155
+ tempConf := pb.ClusterConf {
156
+ SshKeyName : sshKeyName ,
157
+ Region : region ,
158
+ NumOfAz : int32 (numOfAz ),
159
+ MachineType : machineType ,
160
+ MinSizePerAz : int32 (minSizePerAz ),
161
+ MaxSizePerAz : int32 (maxSizePerAz ),
162
+ }
163
+
164
+ fmt .Printf ("Newly constructed cluster conf: %+v\n " , & tempConf )
165
+ return & tempConf , nil
166
+ }
167
+
59
168
func (s * server ) CreateCluster (ctx context.Context , in * pb.CreateClusterRequest ) (* pb.IDResponse , error ) {
60
169
log .Info ("Request 'CreateCluster' for contractId : " , in .GetContractId ())
61
170
@@ -105,32 +214,48 @@ func (s *server) CreateCluster(ctx context.Context, in *pb.CreateClusterRequest)
105
214
// check cluster
106
215
// Exactly one of those must be provided
107
216
/*
108
- res, err := clusterInfoClient.GetClusters(ctx, &pb.GetClustersRequest{
109
- ContractId : in.GetContractId(),
110
- CspId : "",
111
- })
112
- if err == nil {
113
- for _, cluster := range res.GetClusters() {
114
- if cluster.GetStatus() == pb.ClusterStatus_INSTALLING {
115
- log.Info( "Already existed installing workflow. cluster : ", cluster )
116
- return &pb.IDResponse{
117
- Code: pb.Code_ALREADY_EXISTS,
118
- Error: &pb.Error{
119
- Msg: fmt.Sprintf("Already existed installing workflow. : %s", cluster.GetName()),
120
- },
121
- }, nil
122
- }
123
- }
124
- }
217
+ res, err := clusterInfoClient.GetClusters(ctx, &pb.GetClustersRequest{
218
+ ContractId : in.GetContractId(),
219
+ CspId : "",
220
+ })
221
+ if err == nil {
222
+ for _, cluster := range res.GetClusters() {
223
+ if cluster.GetStatus() == pb.ClusterStatus_INSTALLING {
224
+ log.Info( "Already existed installing workflow. cluster : ", cluster )
225
+ return &pb.IDResponse{
226
+ Code: pb.Code_ALREADY_EXISTS,
227
+ Error: &pb.Error{
228
+ Msg: fmt.Sprintf("Already existed installing workflow. : %s", cluster.GetName()),
229
+ },
230
+ }, nil
231
+ }
232
+ }
233
+ }
125
234
*/
126
235
236
+ /***************************
237
+ * Pre-process cluster conf *
238
+ ***************************/
239
+ rawConf := in .GetConf ()
240
+ fmt .Printf ("ClusterRawConf: %+v\n " , rawConf )
241
+
242
+ clConf , err := constructClusterConf (rawConf )
243
+ if err != nil {
244
+ return & pb.IDResponse {
245
+ Code : pb .Code_INTERNAL ,
246
+ Error : & pb.Error {
247
+ Msg : fmt .Sprint (err ),
248
+ },
249
+ }, err
250
+ }
251
+
127
252
// create cluster info
128
253
clusterId := ""
129
254
resAddClusterInfo , err := clusterInfoClient .AddClusterInfo (ctx , & pb.AddClusterInfoRequest {
130
255
ContractId : in .GetContractId (),
131
256
CspId : in .GetCspId (),
132
257
Name : in .GetName (),
133
- Conf : in . GetConf () ,
258
+ Conf : clConf ,
134
259
})
135
260
if err != nil {
136
261
log .Error ("Failed to add cluster info. err : " , err )
@@ -142,7 +267,6 @@ func (s *server) CreateCluster(ctx context.Context, in *pb.CreateClusterRequest)
142
267
}, err
143
268
}
144
269
clusterId = resAddClusterInfo .Id
145
-
146
270
log .Info ("Added cluster in tks-info. clusterId : " , clusterId )
147
271
148
272
// create usercluster
@@ -160,6 +284,8 @@ func (s *server) CreateCluster(ctx context.Context, in *pb.CreateClusterRequest)
160
284
"revision=" + revision ,
161
285
}
162
286
287
+ log .Info ("Submitting workflow: " , workflow )
288
+
163
289
workflowName , err := argowfClient .SumbitWorkflowFromWftpl (ctx , workflow , nameSpace , parameters )
164
290
if err != nil {
165
291
log .Error ("failed to submit argo workflow template. err : " , err )
@@ -170,7 +296,7 @@ func (s *server) CreateCluster(ctx context.Context, in *pb.CreateClusterRequest)
170
296
},
171
297
}, err
172
298
}
173
- log .Debug ( " submited workflow name : " , workflowName )
299
+ log .Info ( "Successfully submited workflow: " , workflowName )
174
300
175
301
// update status : INSTALLING
176
302
if err := s .updateClusterStatus (ctx , clusterId , pb .ClusterStatus_INSTALLING ); err != nil {
0 commit comments