Skip to content

Commit

Permalink
fix lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Sep 20, 2023
1 parent 459e788 commit 3e989b5
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 167 deletions.
162 changes: 0 additions & 162 deletions internal/usecase/cloud-account.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,168 +439,6 @@ func (u *CloudAccountUsecase) GetResourceQuota(ctx context.Context, cloudAccount
return true, out, nil
}

func (u *CloudAccountUsecase) checkAwsResourceQuota(ctx context.Context, cloudAccount domain.CloudAccount) (err error) {
awsAccessKeyId, awsSecretAccessKey, _ := kubernetes.GetAwsSecret()
if err != nil || awsAccessKeyId == "" || awsSecretAccessKey == "" {
log.ErrorWithContext(ctx, err)
return httpErrors.NewInternalServerError(fmt.Errorf("Invalid aws secret."), "", "")
}

cfg, err := config.LoadDefaultConfig(ctx,
config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: awsAccessKeyId, SecretAccessKey: awsSecretAccessKey,
},
}))
if err != nil {
log.ErrorWithContext(ctx, err)
}

stsSvc := sts.NewFromConfig(cfg)

if !strings.Contains(cloudAccount.Name, domain.CLOUD_ACCOUNT_INCLUSTER) {
log.InfoWithContext(ctx, "Use assume role. awsAccountId : ", cloudAccount.AwsAccountId)
creds := stscreds.NewAssumeRoleProvider(stsSvc, "arn:aws:iam::"+cloudAccount.AwsAccountId+":role/controllers.cluster-api-provider-aws.sigs.k8s.io")
cfg.Credentials = aws.NewCredentialsCache(creds)
}
client := servicequotas.NewFromConfig(cfg)

quotaMap := map[string]string{
"L-69A177A2": "elasticloadbalancing", // NLB
"L-E9E9831D": "elasticloadbalancing", // Classic
"L-A4707A72": "vpc", // IGW
"L-1194D53C": "eks", // Cluster
"L-0263D0A3": "ec2", // Elastic IP
}

// current usage
type CurrentUsage struct {
NLB int
CLB int
IGW int
Cluster int
EIP int
}

// get current usage
currentUsage := CurrentUsage{}
{
c := elasticloadbalancingv2.NewFromConfig(cfg)
pageSize := int32(100)
res, err := c.DescribeLoadBalancers(ctx, &elasticloadbalancingv2.DescribeLoadBalancersInput{
PageSize: &pageSize,
}, func(o *elasticloadbalancingv2.Options) {
o.Region = "ap-northeast-2"
})
if err != nil {
return err
}

for _, elb := range res.LoadBalancers {
switch elb.Type {
case "network":
currentUsage.NLB += 1
}
}
}

{
c := elasticloadbalancing.NewFromConfig(cfg)
pageSize := int32(100)
res, err := c.DescribeLoadBalancers(ctx, &elasticloadbalancing.DescribeLoadBalancersInput{
PageSize: &pageSize,
}, func(o *elasticloadbalancing.Options) {
o.Region = "ap-northeast-2"
})
if err != nil {
return err
}
currentUsage.CLB = len(res.LoadBalancerDescriptions)
}

{
c := ec2.NewFromConfig(cfg)
res, err := c.DescribeInternetGateways(ctx, &ec2.DescribeInternetGatewaysInput{}, func(o *ec2.Options) {
o.Region = "ap-northeast-2"
})
if err != nil {
return err
}
currentUsage.IGW = len(res.InternetGateways)
}

{
c := eks.NewFromConfig(cfg)
res, err := c.ListClusters(ctx, &eks.ListClustersInput{}, func(o *eks.Options) {
o.Region = "ap-northeast-2"
})
if err != nil {
return err
}
currentUsage.Cluster = len(res.Clusters)
}

{
c := ec2.NewFromConfig(cfg)
res, err := c.DescribeAddresses(ctx, &ec2.DescribeAddressesInput{}, func(o *ec2.Options) {
o.Region = "ap-northeast-2"
})
if err != nil {
log.ErrorWithContext(ctx, err)
return err
}
currentUsage.EIP = len(res.Addresses)
}

for key, val := range quotaMap {
res, err := getServiceQuota(client, key, val)
if err != nil {
return err
}
log.DebugfWithContext(ctx, "%s %s %v", *res.Quota.QuotaName, *res.Quota.QuotaCode, *res.Quota.Value)

quotaValue := int(*res.Quota.Value)

// stack 1개 생성하는데 필요한 quota
// Classic 1
// Network 5
// IGW 1
// EIP 3
// Cluster 1
switch key {
case "L-69A177A2": // NLB
log.InfofWithContext(ctx, "NLB : usage %d, quota %d", currentUsage.NLB, quotaValue)
if quotaValue < currentUsage.NLB+5 {
return httpErrors.NewInternalServerError(fmt.Errorf("Not enough quota (NLB). current[%d], quota[%d]", currentUsage.NLB, quotaValue), "S_NOT_ENOUGH_QUOTA", "")
}
case "L-E9E9831D": // Classic
log.InfofWithContext(ctx, "CLB : usage %d, quota %d", currentUsage.CLB, quotaValue)
if quotaValue < currentUsage.CLB+1 {
return httpErrors.NewInternalServerError(fmt.Errorf("Not enough quota (Classic ELB). current[%d], quota[%d]", currentUsage.CLB, quotaValue), "S_NOT_ENOUGH_QUOTA", "")
}
case "L-A4707A72": // IGW
log.InfofWithContext(ctx, "IGW : usage %d, quota %d", currentUsage.IGW, quotaValue)
if quotaValue < currentUsage.IGW+1 {
return httpErrors.NewInternalServerError(fmt.Errorf("Not enough quota (Internet Gateway). current[%d], quota[%d]", currentUsage.IGW, quotaValue), "S_NOT_ENOUGH_QUOTA", "")
}
case "L-1194D53C": // Cluster
log.InfofWithContext(ctx, "Cluster : usage %d, quota %d", currentUsage.Cluster, quotaValue)
if quotaValue < currentUsage.Cluster+1 {
return httpErrors.NewInternalServerError(fmt.Errorf("Not enough quota (EKS cluster quota). current[%d], quota[%d]", currentUsage.Cluster, quotaValue), "S_NOT_ENOUGH_QUOTA", "")
}
case "L-0263D0A3": // Elastic IP
log.InfofWithContext(ctx, "Elastic IP : usage %d, quota %d", currentUsage.EIP, quotaValue)
if quotaValue < currentUsage.EIP+3 {
return httpErrors.NewInternalServerError(fmt.Errorf("Not enough quota (Elastic IP). current[%d], quota[%d]", currentUsage.EIP, quotaValue), "S_NOT_ENOUGH_QUOTA", "")
}
}

}

//return fmt.Errorf("Always return err")
return nil
}

func (u *CloudAccountUsecase) getClusterCnt(cloudAccountId uuid.UUID) (cnt int) {
cnt = 0

Expand Down
4 changes: 0 additions & 4 deletions internal/usecase/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ func (u *StackUsecase) Create(ctx context.Context, dto domain.Stack) (stackId do
TksUserNode: dto.NodesIO.TksUserNode.Count,
}

log.Info(stackConf)

workflow := ""
if stackTemplate.TemplateType == domain.STACK_TEMPLATE_TYPE_STANDARD && stackTemplate.CloudService == "AWS" {
workflow = "tks-stack-create-aws"
Expand All @@ -123,8 +121,6 @@ func (u *StackUsecase) Create(ctx context.Context, dto domain.Stack) (stackId do
return "", httpErrors.NewInternalServerError(fmt.Errorf("Invalid stackTemplate. %s", stackTemplate.Template), "", "")
}

return

workflowId, err := u.argo.SumbitWorkflowFromWftpl(workflow, argowf.SubmitOptions{
Parameters: []string{
fmt.Sprintf("tks_api_url=%s", viper.GetString("external-address")),
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/stack-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type CreateStackTemplateRequest struct {
Version string `json:"version" validate:"required"`
Platform string `json:"platform" validate:"required"`
Template string `json:"template" validate:"required"`
TemplateType string `json:"template" validate:"oneof=STANDARD MSA"`
TemplateType string `json:"templateType" validate:"oneof=STANDARD MSA"`
}

type CreateStackTemplateResponse struct {
Expand Down

0 comments on commit 3e989b5

Please sign in to comment.