diff --git a/internal/model/cluster.go b/internal/model/cluster.go index 416b640a..73a4e3fa 100644 --- a/internal/model/cluster.go +++ b/internal/model/cluster.go @@ -55,7 +55,7 @@ type Cluster struct { TksUserNode int TksUserNodeMax int TksUserNodeType string - Kubeconfig []byte `gorm:"-:all"` + Kubeconfig string `gorm:"-:all"` PolicyIds []string `gorm:"-:all"` CreatorId *uuid.UUID `gorm:"type:uuid"` Creator User `gorm:"foreignKey:CreatorId"` diff --git a/internal/repository/cluster.go b/internal/repository/cluster.go index f6023559..a2b450cc 100644 --- a/internal/repository/cluster.go +++ b/internal/repository/cluster.go @@ -116,7 +116,7 @@ func (r *ClusterRepository) GetByName(ctx context.Context, organizationId string func (r *ClusterRepository) Create(ctx context.Context, dto model.Cluster) (clusterId domain.ClusterId, err error) { var cloudAccountId *uuid.UUID cloudAccountId = dto.CloudAccountId - if dto.CloudService == domain.CloudService_BYOH || dto.CloudService == domain.CloudService_BYOK || *dto.CloudAccountId == uuid.Nil { + if dto.CloudService != domain.CloudService_AWS || *dto.CloudAccountId == uuid.Nil { cloudAccountId = nil } if dto.ID == "" { diff --git a/internal/usecase/app-group.go b/internal/usecase/app-group.go index 37896b64..60ce2213 100644 --- a/internal/usecase/app-group.go +++ b/internal/usecase/app-group.go @@ -81,7 +81,7 @@ func (u *AppGroupUsecase) Create(ctx context.Context, dto model.AppGroup) (id do // check cloudAccount tksCloudAccountId := "" tksObjectStore := "minio" - if cluster.CloudService != domain.CloudService_BYOH { + if cluster.CloudService == domain.CloudService_AWS { tksObjectStore = "s3" cloudAccounts, err := u.cloudAccountRepo.Fetch(ctx, cluster.OrganizationId, nil) if err != nil { @@ -181,7 +181,7 @@ func (u *AppGroupUsecase) Delete(ctx context.Context, id domain.AppGroupId) (err // check cloudAccount tksCloudAccountId := "" tksObjectStore := "minio" - if cluster.CloudService != domain.CloudService_BYOH { + if cluster.CloudService == domain.CloudService_AWS { tksObjectStore = "s3" cloudAccounts, err := u.cloudAccountRepo.Fetch(ctx, cluster.OrganizationId, nil) if err != nil { diff --git a/internal/usecase/cluster.go b/internal/usecase/cluster.go index dcdd7e9e..6fa35281 100644 --- a/internal/usecase/cluster.go +++ b/internal/usecase/cluster.go @@ -8,7 +8,6 @@ import ( "strings" "time" - "github.com/Nerzal/gocloak/v13" "github.com/openinfradev/tks-api/internal/keycloak" "github.com/google/uuid" @@ -248,12 +247,24 @@ func (u *ClusterUsecase) Import(ctx context.Context, dto model.Cluster) (cluster dto.ID = "tks-admin" dto.Name = "tks-admin" } + + // [TODO] check nodes + dto.TksCpNode = 1 + dto.TksCpNodeMax = 1 + dto.TksInfraNode = 1 + dto.TksInfraNodeMax = 1 + dto.TksUserNode = 1 + dto.TksUserNodeMax = 1 + clusterId, err = u.repo.Create(ctx, dto) if err != nil { return "", errors.Wrap(err, "Failed to create cluster") } - kubeconfigBase64 := base64.StdEncoding.EncodeToString([]byte(dto.Kubeconfig)) + _, err = base64.StdEncoding.DecodeString(dto.Kubeconfig) + if err != nil { + return "", httpErrors.NewBadRequestError(fmt.Errorf("Invalid kubeconfig string"), "", "") + } workflowId, err := u.argo.SumbitWorkflowFromWftpl( ctx, @@ -263,8 +274,9 @@ func (u *ClusterUsecase) Import(ctx context.Context, dto model.Cluster) (cluster fmt.Sprintf("tks_api_url=%s", viper.GetString("external-address")), "contract_id=" + dto.OrganizationId, "cluster_id=" + clusterId.String(), + "site_name=" + clusterId.String(), "template_name=" + stackTemplate.Template, - "kubeconfig=" + kubeconfigBase64, + "kubeconfig=" + dto.Kubeconfig, "git_account=" + viper.GetString("git-account"), "keycloak_url=" + strings.TrimSuffix(viper.GetString("keycloak-address"), "/auth"), "base_repo_branch=" + viper.GetString("revision"), @@ -280,56 +292,6 @@ func (u *ClusterUsecase) Import(ctx context.Context, dto model.Cluster) (cluster return "", errors.Wrap(err, "Failed to initialize status") } - // keycloak setting - log.Debugf(ctx, "Create keycloak client for %s", dto.ID) - // Create keycloak client - clientUUID, err := u.kc.CreateClient(ctx, dto.OrganizationId, dto.ID.String()+"-k8s-api", "", nil) - if err != nil { - log.Errorf(ctx, "Failed to create keycloak client for %s", dto.ID) - return "", err - } - // Create keycloak client protocol mapper - _, err = u.kc.CreateClientProtocolMapper(ctx, dto.OrganizationId, clientUUID, gocloak.ProtocolMapperRepresentation{ - Name: gocloak.StringP("k8s-role-mapper"), - Protocol: gocloak.StringP("openid-connect"), - ProtocolMapper: gocloak.StringP("oidc-usermodel-client-role-mapper"), - ConsentRequired: gocloak.BoolP(false), - Config: &map[string]string{ - "usermodel.clientRoleMapping.clientId": dto.ID.String() + "-k8s-api", - "claim.name": "groups", - "access.token.claim": "false", - "id.token.claim": "true", - "userinfo.token.claim": "true", - "multivalued": "true", - "jsonType.label": "String", - }, - }) - if err != nil { - log.Errorf(ctx, "Failed to create keycloak client protocol mapper for %s", dto.ID) - return "", err - } - // Create keycloak client role - err = u.kc.CreateClientRole(ctx, dto.OrganizationId, clientUUID, "cluster-admin-create") - if err != nil { - log.Errorf(ctx, "Failed to create keycloak client role named %s for %s", "cluster-admin-create", dto.ID) - return "", err - } - err = u.kc.CreateClientRole(ctx, dto.OrganizationId, clientUUID, "cluster-admin-read") - if err != nil { - log.Errorf(ctx, "Failed to create keycloak client role named %s for %s", "cluster-admin-read", dto.ID) - return "", err - } - err = u.kc.CreateClientRole(ctx, dto.OrganizationId, clientUUID, "cluster-admin-update") - if err != nil { - log.Errorf(ctx, "Failed to create keycloak client role named %s for %s", "cluster-admin-update", dto.ID) - return "", err - } - err = u.kc.CreateClientRole(ctx, dto.OrganizationId, clientUUID, "cluster-admin-delete") - if err != nil { - log.Errorf(ctx, "Failed to create keycloak client role named %s for %s", "cluster-admin-delete", dto.ID) - return "", err - } - return clusterId, nil } @@ -440,10 +402,6 @@ func (u *ClusterUsecase) Resume(ctx context.Context, clusterId domain.ClusterId) return httpErrors.NewBadRequestError(fmt.Errorf("Invalid stackId"), "S_INVALID_STACK_ID", "") } - if cluster.CloudService != domain.CloudService_BYOH { - return httpErrors.NewBadRequestError(fmt.Errorf("Invalid cloud service"), "S_INVALID_CLOUD_SERVICE", "") - } - if cluster.WorkflowId == "" { return httpErrors.NewInternalServerError(fmt.Errorf("Invalid workflow id"), "", "") } @@ -494,7 +452,7 @@ func (u *ClusterUsecase) Delete(ctx context.Context, clusterId domain.ClusterId) // FOR TEST. ADD MAGIC KEYWORD // check cloudAccount tksCloudAccountId := "NULL" - if cluster.CloudService != domain.CloudService_BYOH { + if cluster.CloudService == domain.CloudService_AWS { cloudAccount, err := u.cloudAccountRepo.Get(ctx, cluster.CloudAccount.ID) if err != nil { return httpErrors.NewInternalServerError(fmt.Errorf("Failed to get cloudAccount"), "", "") diff --git a/pkg/domain/cluster.go b/pkg/domain/cluster.go index 4c1caa2f..a57ea73e 100644 --- a/pkg/domain/cluster.go +++ b/pkg/domain/cluster.go @@ -176,7 +176,7 @@ type ImportClusterRequest struct { Name string `json:"name" validate:"required,name"` Description string `json:"description"` ClusterType string `json:"clusterType"` - Kubeconfig []byte `json:"kubeconfig"` + Kubeconfig string `json:"kubeconfig" validate:"required"` CloudService string `json:"cloudService"` PolicyIds []string `json:"policyIds,omitempty"` Domains []ClusterDomain `json:"domains,omitempty"` @@ -192,14 +192,14 @@ type ImportClusterResponse struct { type ClusterConfResponse struct { TksCpNode int `json:"tksCpNode"` - TksCpNodeMax int `json:"tksCpNodeMax,omitempty"` - TksCpNodeType string `json:"tksCpNodeType,omitempty"` + TksCpNodeMax int `json:"tksCpNodeMax"` + TksCpNodeType string `json:"tksCpNodeType"` TksInfraNode int `json:"tksInfraNode"` - TksInfraNodeMax int `json:"tksInfraNodeMax,omitempty"` - TksInfraNodeType string `json:"tksInfraNodeType,omitempty"` + TksInfraNodeMax int `json:"tksInfraNodeMax"` + TksInfraNodeType string `json:"tksInfraNodeType"` TksUserNode int `json:"tksUserNode"` - TksUserNodeMax int `json:"tksUserNodeMax,omitempty"` - TksUserNodeType string `json:"tksUserNodeType,omitempty"` + TksUserNodeMax int `json:"tksUserNodeMax"` + TksUserNodeType string `json:"tksUserNodeType"` } type ClusterResponse struct { @@ -236,14 +236,14 @@ type ClusterSiteValuesResponse struct { SshKeyName string `json:"sshKeyName"` ClusterRegion string `json:"clusterRegion"` TksCpNode int `json:"tksCpNode"` - TksCpNodeMax int `json:"tksCpNodeMax,omitempty"` - TksCpNodeType string `json:"tksCpNodeType,omitempty"` + TksCpNodeMax int `json:"tksCpNodeMax"` + TksCpNodeType string `json:"tksCpNodeType"` TksInfraNode int `json:"tksInfraNode"` - TksInfraNodeMax int `json:"tksInfraNodeMax,omitempty"` - TksInfraNodeType string `json:"tksInfraNodeType,omitempty"` + TksInfraNodeMax int `json:"tksInfraNodeMax"` + TksInfraNodeType string `json:"tksInfraNodeType"` TksUserNode int `json:"tksUserNode"` - TksUserNodeMax int `json:"tksUserNodeMax,omitempty"` - TksUserNodeType string `json:"tksUserNodeType,omitempty"` + TksUserNodeMax int `json:"tksUserNodeMax"` + TksUserNodeType string `json:"tksUserNodeType"` ByoClusterEndpointHost string `json:"byoClusterEndpointHost"` ByoClusterEndpointPort int `json:"byoClusterEndpointPort"` Domains []ClusterDomain `json:"domains"`