Skip to content

Commit f130b0f

Browse files
committed
resolve confict
2 parents c6eb0cd + 86f3790 commit f130b0f

File tree

6 files changed

+181
-33
lines changed

6 files changed

+181
-33
lines changed

Dockerfile

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/server ./cmd/server
1414

1515
RUN mkdir -p /dist
1616
WORKDIR /dist
17-
RUN cp /build/bin/server ./server
18-
19-
17+
RUN cp /build/bin/server /build/files/az-per-region.txt ./
2018

2119
FROM golang:alpine3.13
2220

cmd/server/handlers.go

+146-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
package main
22

33
import (
4+
"bufio"
45
"context"
56
"errors"
67
"fmt"
8+
"os"
9+
"strconv"
10+
"strings"
711

812
"github.com/google/uuid"
913
"github.com/openinfradev/tks-common/pkg/log"
1014
pb "github.com/openinfradev/tks-proto/tks_pb"
1115
)
1216

17+
var (
18+
filePathAzRegion = "./az-per-region.txt"
19+
)
20+
21+
const MAX_SIZE_PER_AZ = 99
22+
1323
func validateCreateClusterRequest(in *pb.CreateClusterRequest) (err error) {
1424
if _, err := uuid.Parse(in.GetContractId()); err != nil {
1525
return fmt.Errorf("invalid contract ID %s", in.GetContractId())
@@ -56,6 +66,105 @@ func validateUninstallAppGroupsRequest(in *pb.UninstallAppGroupsRequest) (err er
5666
return nil
5767
}
5868

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+
59168
func (s *server) CreateCluster(ctx context.Context, in *pb.CreateClusterRequest) (*pb.IDResponse, error) {
60169
log.Info("Request 'CreateCluster' for contractId : ", in.GetContractId())
61170

@@ -105,32 +214,48 @@ func (s *server) CreateCluster(ctx context.Context, in *pb.CreateClusterRequest)
105214
// check cluster
106215
// Exactly one of those must be provided
107216
/*
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+
}
125234
*/
126235

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+
127252
// create cluster info
128253
clusterId := ""
129254
resAddClusterInfo, err := clusterInfoClient.AddClusterInfo(ctx, &pb.AddClusterInfoRequest{
130255
ContractId: in.GetContractId(),
131256
CspId: in.GetCspId(),
132257
Name: in.GetName(),
133-
Conf: in.GetConf(),
258+
Conf: clConf,
134259
})
135260
if err != nil {
136261
log.Error("Failed to add cluster info. err : ", err)
@@ -142,7 +267,6 @@ func (s *server) CreateCluster(ctx context.Context, in *pb.CreateClusterRequest)
142267
}, err
143268
}
144269
clusterId = resAddClusterInfo.Id
145-
146270
log.Info("Added cluster in tks-info. clusterId : ", clusterId)
147271

148272
// create usercluster
@@ -160,6 +284,8 @@ func (s *server) CreateCluster(ctx context.Context, in *pb.CreateClusterRequest)
160284
"revision=" + revision,
161285
}
162286

287+
log.Info("Submitting workflow: ", workflow)
288+
163289
workflowName, err := argowfClient.SumbitWorkflowFromWftpl(ctx, workflow, nameSpace, parameters)
164290
if err != nil {
165291
log.Error("failed to submit argo workflow template. err : ", err)
@@ -170,7 +296,7 @@ func (s *server) CreateCluster(ctx context.Context, in *pb.CreateClusterRequest)
170296
},
171297
}, err
172298
}
173-
log.Debug("submited workflow name : ", workflowName)
299+
log.Info("Successfully submited workflow: ", workflowName)
174300

175301
// update status : INSTALLING
176302
if err := s.updateClusterStatus(ctx, clusterId, pb.ClusterStatus_INSTALLING); err != nil {

cmd/server/handlers_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ var (
3030
func init() {
3131
log.Disable()
3232

33+
// override for test
34+
filePathAzRegion = "../../files/az-per-region.txt"
35+
3336
// for CreateCluster API
3437
installAppGroupsRequest = randomInstallAppGroupsRequest()
3538
createClusterRequest = randomCreateClusterRequest()
@@ -870,15 +873,12 @@ func randomCreateClusterRequest() *pb.CreateClusterRequest {
870873
ContractId: uuid.New().String(),
871874
CspId: uuid.New().String(),
872875
Name: randomString("NAME"),
873-
Conf: &pb.ClusterConf{
874-
MasterFlavor: randomString("MASTERFLAVOR"),
875-
MasterReplicas: 3,
876-
MasterRootSize: 30,
877-
WorkerFlavor: randomString("WORKERFLAVOR"),
878-
WorkerReplicas: 3,
879-
WorkerRootSize: 30,
880-
K8SVersion: "1.21",
881-
Region: randomString("REGION"),
876+
Conf: &pb.ClusterRawConf{
877+
SshKeyName: randomString("SSHKEYNAME"),
878+
Region: "ap-northeast-2",
879+
NumOfAz: 3,
880+
MachineType: randomString("MACHINETYPE"),
881+
MachineReplicas: 3,
882882
},
883883
}
884884
}

files/az-per-region.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
ec2.af-south-1.amazonaws.com: 3
2+
ec2.eu-north-1.amazonaws.com: 3
3+
ec2.ap-south-1.amazonaws.com: 3
4+
ec2.eu-west-3.amazonaws.com: 3
5+
ec2.eu-west-2.amazonaws.com: 3
6+
ec2.eu-south-1.amazonaws.com: 3
7+
ec2.eu-west-1.amazonaws.com: 3
8+
ec2.ap-northeast-3.amazonaws.com: 3
9+
ec2.ap-northeast-2.amazonaws.com: 4
10+
ec2.me-south-1.amazonaws.com: 3
11+
ec2.ap-northeast-1.amazonaws.com: 4
12+
ec2.sa-east-1.amazonaws.com: 3
13+
ec2.ca-central-1.amazonaws.com: 3
14+
ec2.ap-east-1.amazonaws.com: 3
15+
ec2.ap-southeast-1.amazonaws.com: 3
16+
ec2.ap-southeast-2.amazonaws.com: 3
17+
ec2.eu-central-1.amazonaws.com: 3
18+
ec2.ap-southeast-3.amazonaws.com: 3
19+
ec2.us-east-1.amazonaws.com: 6
20+
ec2.us-east-2.amazonaws.com: 3
21+
ec2.us-west-1.amazonaws.com: 3
22+
ec2.us-west-2.amazonaws.com: 4

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/google/uuid v1.3.0
99
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
1010
github.com/openinfradev/tks-common v0.0.0-20220321044608-105302d33457
11-
github.com/openinfradev/tks-proto v0.0.6-0.20220318050956-8c89dcb6184c
11+
github.com/openinfradev/tks-proto v0.0.6-0.20220318062944-7fccd257bcae // indirect
1212
github.com/stretchr/testify v1.7.0
1313
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
1414
golang.org/x/net v0.0.0-20211011170408-caeb26a5c8c0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,8 @@ github.com/openinfradev/tks-common v0.0.0-20220321044608-105302d33457/go.mod h1:
782782
github.com/openinfradev/tks-proto v0.0.6-0.20211015003551-ed8f9541f40d/go.mod h1:ul6kvgOXhNQvXEUmb92Wh5BmsuzknnTCb3wqmVNd/iI=
783783
github.com/openinfradev/tks-proto v0.0.6-0.20220318050956-8c89dcb6184c h1:VM4i7oy+x32suEv4NsrhMP3P5hPkJUx5bDGhdL13Mb8=
784784
github.com/openinfradev/tks-proto v0.0.6-0.20220318050956-8c89dcb6184c/go.mod h1:3DrATRNCUJMW0oMLHT6D1teN4hX0vMGH4qOGnadakmo=
785+
github.com/openinfradev/tks-proto v0.0.6-0.20220318062944-7fccd257bcae h1:n4VlqbtNhQ36WMrqHCWkHp7PR5qUr3AVDdOBINZoa5o=
786+
github.com/openinfradev/tks-proto v0.0.6-0.20220318062944-7fccd257bcae/go.mod h1:3DrATRNCUJMW0oMLHT6D1teN4hX0vMGH4qOGnadakmo=
785787
github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis=
786788
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
787789
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=

0 commit comments

Comments
 (0)