Skip to content

Commit

Permalink
Merge pull request #138 from openinfradev/release
Browse files Browse the repository at this point in the history
  • Loading branch information
bluejayA authored Aug 14, 2023
2 parents b37124a + 993c62d commit 335f17c
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 30 deletions.
42 changes: 25 additions & 17 deletions internal/delivery/http/app-serve-app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"fmt"
"math/rand"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -89,7 +90,7 @@ func NewAppServeAppHandler(h usecase.IAppServeAppUsecase) *AppServeAppHandler {
func (h *AppServeAppHandler) CreateAppServeApp(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
organizationId, ok := vars["organizationId"]
fmt.Printf("organizationId = [%v]\n", organizationId)
log.Debugf("organizationId = [%v]\n", organizationId)
if !ok {
ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid organizationId"), "C_INVALID_ORGANIZATION_ID", ""))
return
Expand All @@ -110,6 +111,8 @@ func (h *AppServeAppHandler) CreateAppServeApp(w http.ResponseWriter, r *http.Re
return
}

log.Infof("Processing CREATE request for app '%s'...", app.Name)

now := time.Now()
app.OrganizationId = organizationId
app.EndpointUrl = "N/A"
Expand Down Expand Up @@ -147,6 +150,25 @@ func (h *AppServeAppHandler) CreateAppServeApp(w http.ResponseWriter, r *http.Re
return
}

// Check if the namespace is already used in the target cluster
ns := ""
nsExist := true
for nsExist {
// Generate unique namespace based on name and random number
src := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(src)
ns = fmt.Sprintf("%s-%s", app.Name, strconv.Itoa(r1.Intn(10000)))

nsExist, err = h.usecase.IsAppServeAppNamespaceExist(app.TargetClusterId, ns)
if err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

log.Infof("Using namespace: %s", ns)
app.Namespace = ns

// Validate port param for springboot app
if app.AppType == "springboot" {
if task.Port == "" {
Expand Down Expand Up @@ -428,22 +450,8 @@ func makeStage(app *domain.AppServeApp, pl string) domain.StageResponse {

var actions []domain.ActionResponse
if stage.Status == "DEPLOY_SUCCESS" {
if strategy == "rolling-update" {
action := domain.ActionResponse{
Name: "ENDPOINT",
Uri: app.EndpointUrl,
Type: "LINK",
}
actions = append(actions, action)
} else if strategy == "blue-green" {
if taskStatus == "PROMOTE_SUCCESS" || taskStatus == "ABORT_SUCCESS" {
action := domain.ActionResponse{
Name: "ENDPOINT",
Uri: app.EndpointUrl,
Type: "LINK",
}
actions = append(actions, action)
} else if taskStatus == "PROMOTE_WAIT" {
if strategy == "blue-green" {
if taskStatus == "PROMOTE_WAIT" {
action := domain.ActionResponse{
Name: "OLD_EP",
Uri: app.EndpointUrl,
Expand Down
4 changes: 2 additions & 2 deletions internal/pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewPagination(urlParams *url.Values) (*Pagination, error) {
}
case SORT_ORDER:
if value[0] == "" {
pg.SortOrder = "ASC"
pg.SortOrder = "DESC"
} else {
pg.SortOrder = value[0]
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func NewPagination(urlParams *url.Values) (*Pagination, error) {
func NewDefaultPagination() *Pagination {
return &Pagination{
SortColumn: "created_at",
SortOrder: "ASC",
SortOrder: "DESC",
Page: 1,
Limit: MAX_LIMIT,
}
Expand Down
4 changes: 2 additions & 2 deletions internal/repository/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (r *ClusterRepository) FetchByOrganizationId(organizationId string, pg *pag
if pg == nil {
pg = pagination.NewDefaultPagination()
}
pg.SortColumn = "updated_at"
pg.SortColumn = "created_at"
pg.SortOrder = "DESC"
filterFunc := CombinedGormFilter("clusters", pg.GetFilters(), pg.CombinedFilter)
db := filterFunc(r.db.Model(&Cluster{}).Preload(clause.Associations).
Expand All @@ -138,7 +138,7 @@ func (r *ClusterRepository) FetchByCloudAccountId(cloudAccountId uuid.UUID, pg *
if pg == nil {
pg = pagination.NewDefaultPagination()
}
pg.SortColumn = "updated_at"
pg.SortColumn = "created_at"
pg.SortOrder = "DESC"
filterFunc := CombinedGormFilter("clusters", pg.GetFilters(), pg.CombinedFilter)
db := filterFunc(r.db.Model(&Cluster{}).Preload("CloudAccount").
Expand Down
18 changes: 15 additions & 3 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package repository

import (
"fmt"
"strings"

"gorm.io/gorm"

"github.com/openinfradev/tks-api/internal/pagination"
Expand All @@ -28,13 +30,18 @@ func CombinedGormFilter(table string, filters []pagination.Filter, combinedFilte
if len(filter.Values) > 1 {
inQuery := fmt.Sprintf("LOWER(%s.%s::text) in (", table, filter.Column)
for _, val := range filter.Values {
inQuery = inQuery + fmt.Sprintf("LOWER('%s'),", val)
inQuery = inQuery + fmt.Sprintf("LOWER($$%s$$),", val)
}
inQuery = inQuery[:len(inQuery)-1] + ")"
db = db.Where(inQuery)
} else {
if len(filter.Values[0]) > 0 {
db = db.Where(fmt.Sprintf("LOWER(%s.%s::text) like LOWER('%%%s%%')", table, filter.Column, filter.Values[0]))
if strings.Contains(filter.Values[0], "%") {
filterVal := strings.Replace(filter.Values[0], "%", "`%", -1)
db = db.Where(fmt.Sprintf("LOWER(%s.%s::text) like LOWER($$%%%s%%$$) escape '`'", table, filter.Column, filterVal))
} else {
db = db.Where(fmt.Sprintf("LOWER(%s.%s::text) like LOWER($$%%%s%%$$)", table, filter.Column, filter.Values[0]))
}
}
}
}
Expand All @@ -44,7 +51,12 @@ func CombinedGormFilter(table string, filters []pagination.Filter, combinedFilte
if len(combinedFilter.Columns) > 0 {
orQuery := ""
for _, column := range combinedFilter.Columns {
orQuery = orQuery + fmt.Sprintf("LOWER(%s.%s::text) like LOWER('%%%s%%') OR ", table, column, combinedFilter.Value)
if strings.Contains(combinedFilter.Value, "%") {
filterVal := strings.Replace(combinedFilter.Value, "%", "`%", -1)
orQuery = orQuery + fmt.Sprintf("LOWER(%s.%s::text) like LOWER($$%%%s%%$$) escape '`' OR ", table, column, filterVal)
} else {
orQuery = orQuery + fmt.Sprintf("LOWER(%s.%s::text) like LOWER($$%%%s%%$$) OR ", table, column, combinedFilter.Value)
}
}
orQuery = orQuery[:len(orQuery)-3]
db = db.Where(orQuery)
Expand Down
26 changes: 26 additions & 0 deletions internal/usecase/app-serve-app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package usecase

import (
"context"
"encoding/json"
"fmt"

Expand All @@ -10,7 +11,9 @@ import (

"github.com/pkg/errors"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/openinfradev/tks-api/internal/kubernetes"
"github.com/openinfradev/tks-api/internal/pagination"
"github.com/openinfradev/tks-api/internal/repository"
argowf "github.com/openinfradev/tks-api/pkg/argo-client"
Expand All @@ -27,6 +30,7 @@ type IAppServeAppUsecase interface {
GetNumOfAppsOnStack(organizationId string, clusterId string) (int64, error)
IsAppServeAppExist(appId string) (bool, error)
IsAppServeAppNameExist(orgId string, appName string) (bool, error)
IsAppServeAppNamespaceExist(clusterId string, namespace string) (bool, error)
UpdateAppServeAppStatus(appId string, taskId string, status string, output string) (ret string, err error)
DeleteAppServeApp(appId string) (res string, err error)
UpdateAppServeApp(app *domain.AppServeApp, appTask *domain.AppServeAppTask) (ret string, err error)
Expand Down Expand Up @@ -231,6 +235,28 @@ func (u *AppServeAppUsecase) IsAppServeAppNameExist(orgId string, appName string
return false, nil
}

func (u *AppServeAppUsecase) IsAppServeAppNamespaceExist(clusterId string, new_ns string) (bool, error) {
clientset, err := kubernetes.GetClientFromClusterId(clusterId)
if err != nil {
log.Error(err)
return false, err
}

namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Error(err)
return false, err
}
for _, ns := range namespaces.Items {
if new_ns == ns.ObjectMeta.Name {
log.Debugf("Namespace %s already exists.", new_ns)
return true, nil
}
}
log.Debugf("Namespace %s is available", new_ns)
return false, nil
}

func (u *AppServeAppUsecase) UpdateAppServeAppStatus(
appId string,
taskId string,
Expand Down
3 changes: 0 additions & 3 deletions pkg/domain/app-serve-app.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ type CreateAppServeAppRequest struct {
}

func (c *CreateAppServeAppRequest) SetDefaultValue() {
if c.Namespace == "" {
c.Namespace = c.Name
}
if c.Type == "" {
c.Type = "all"
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (m StackStatus) FromString(s string) StackStatus {
return StackStatus_PENDING
}

const MAX_STEP_CLUSTER_CREATE = 16
const MAX_STEP_CLUSTER_CREATE = 15
const MAX_STEP_CLUSTER_REMOVE = 11
const MAX_STEP_LMA_CREATE_PRIMARY = 42
const MAX_STEP_LMA_CREATE_MEMBER = 27
Expand Down
4 changes: 2 additions & 2 deletions scripts/init_postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ values ( 'fe1d97e0-7428-4be6-9c69-310a88b4ff46', 'master', 'AWS Standard (arm)',
insert into stack_templates ( id, organization_id, name, description, version, cloud_service, platform, template, kube_version, kube_type, created_at, updated_at, services )
values ( '3696cb38-4da0-4235-97eb-b6eb15962bd1', 'master', 'AWS Standard (arm)', 'included LMA, SERVICE_MESH', 'v2', 'AWS', 'arm', 'aws-arm-msa-reference', 'v1.25', 'EKS', now(), now(), '[{"name": "Logging,Monitoring,Alerting", "type": "LMA", "applications": [{"name": "prometheus-stack", "version": "v.44.3.1", "description": "통계데이터 제공을 위한 backend 플랫폼"}, {"name": "elastic-system", "version": "v1.8.0", "description": "로그 데이터 적재를 위한 Storage"}, {"name": "alertmanager", "version": "v0.23.0", "description": "Alert 관리를 위한 backend 서비스"}, {"name": "grafana", "version": "v6.50.7", "description": "모니터링 통합 포탈"}]}, {"name": "MSA", "type": "SERVICE_MESH", "applications": [{"name": "istio", "version": "v1.13.1", "description": "MSA 플랫폼"}, {"name": "jagger", "version": "v2.27.1", "description": "분산 서비스간 트랜잭션 추적을 위한 로깅 플랫폼"}, {"name": "kiali", "version": "v1.45.1", "description": "MSA 통합 모니터링포탈"}]}]' );
insert into stack_templates ( id, organization_id, name, description, version, cloud_service, platform, template, kube_version, kube_type, created_at, updated_at, services )
values ( 'c8a4658d-d5a6-4191-8a91-e26f6aee007f', 'master', 'EKS Standard (x86)', 'included LMA', 'v1', 'AWS', 'x86', 'eks-reference', 'v1.25', 'AWS', now(), now(), '[{"name":"Logging,Monitoring,Alerting","type":"LMA","applications":[{"name":"thanos","version":"0.17.2","description":"다중클러스터의 모니터링 데이터 통합 질의처리"},{"name":"prometheus-stack","version":"v0.62.0","description":"모니터링 데이터 수집/저장 및 질의처리"},{"name":"alertmanager","version":"v0.23.0","description":"알람 처리를 위한 노티피케이션 서비스"},{"name":"loki","version":"2.6.1","description":"로그데이터 저장 및 질의처리"},{"name":"grafana","version":"9.3.6","description":"모니터링/로그 통합대시보드"}]}]' );
values ( 'c8a4658d-d5a6-4191-8a91-e26f6aee007f', 'master', 'EKS Standard (x86)', 'included LMA', 'v1', 'AWS', 'x86', 'eks-reference', 'v1.25', 'AWS', now(), now(), '[{"name":"Logging,Monitoring,Alerting","type":"LMA","applications":[{"name":"thanos","version":"0.30.2","description":"다중클러스터의 모니터링 데이터 통합 질의처리"},{"name":"prometheus-stack","version":"v0.66.0","description":"모니터링 데이터 수집/저장 및 질의처리"},{"name":"alertmanager","version":"v0.25.0","description":"알람 처리를 위한 노티피케이션 서비스"},{"name":"loki","version":"2.6.1","description":"로그데이터 저장 및 질의처리"},{"name":"grafana","version":"8.3.3","description":"모니터링/로그 통합대시보드"}]}]' );
insert into stack_templates ( id, organization_id, name, description, version, cloud_service, platform, template, kube_version, kube_type, created_at, updated_at, services )
values ( '39f18a09-5b94-4772-bdba-e4c32ee002f7', 'master', 'EKS MSA Standard (x86)', 'included LMA, SERVICE MESH', 'v1', 'AWS', 'x86', 'eks-msa-reference', 'v1.25', 'AWS', now(), now(), '[{"name":"Logging,Monitoring,Alerting","type":"LMA","applications":[{"name":"thanos","version":"0.17.2","description":"다중클러스터의 모니터링 데이터 통합 질의처리"},{"name":"prometheus-stack","version":"v0.62.0","description":"모니터링 데이터 수집/저장 및 질의처리"},{"name":"alertmanager","version":"v0.23.0","description":"알람 처리를 위한 노티피케이션 서비스"},{"name":"loki","version":"2.6.1","description":"로그데이터 저장 및 질의처리"},{"name":"grafana","version":"9.3.6","description":"모니터링/로그 통합대시보드"}]},{"name":"MSA","type":"SERVICE_MESH","applications":[{"name":"istio","version":"v1.17.2","description":"MSA 플랫폼"},{"name":"jagger","version":"1.35.0","description":"분산 서비스간 트랜잭션 추적을 위한 플랫폼"},{"name":"kiali","version":"v1.63.0","description":"MSA 구조 및 성능을 볼 수 있는 Dashboard"},{"name":"k8ssandra","version":"1.6.0","description":"분산 서비스간 호출 로그를 저장하는 스토리지"}]}]' );
values ( '39f18a09-5b94-4772-bdba-e4c32ee002f7', 'master', 'EKS MSA Standard (x86)', 'included LMA, SERVICE MESH', 'v1', 'AWS', 'x86', 'eks-msa-reference', 'v1.25', 'AWS', now(), now(), '[{"name":"Logging,Monitoring,Alerting","type":"LMA","applications":[{"name":"thanos","version":"0.30.2","description":"다중클러스터의 모니터링 데이터 통합 질의처리"},{"name":"prometheus-stack","version":"v0.66.0","description":"모니터링 데이터 수집/저장 및 질의처리"},{"name":"alertmanager","version":"v0.25.0","description":"알람 처리를 위한 노티피케이션 서비스"},{"name":"loki","version":"2.6.1","description":"로그데이터 저장 및 질의처리"},{"name":"grafana","version":"8.3.3","description":"모니터링/로그 통합대시보드"}]},{"name":"MSA","type":"SERVICE_MESH","applications":[{"name":"istio","version":"v1.17.2","description":"MSA 플랫폼"},{"name":"jagger","version":"1.35.0","description":"분산 서비스간 트랜잭션 추적을 위한 플랫폼"},{"name":"kiali","version":"v1.63.0","description":"MSA 구조 및 성능을 볼 수 있는 Dashboard"},{"name":"k8ssandra","version":"1.6.0","description":"분산 서비스간 호출 로그를 저장하는 스토리지"}]}]' );

0 comments on commit 335f17c

Please sign in to comment.