Skip to content

Commit

Permalink
Merge pull request #537 from openinfradev/byok1
Browse files Browse the repository at this point in the history
trivial. change type to string from []byte for kubeconfig
  • Loading branch information
ktkfree authored Jul 22, 2024
2 parents d08395d + c72fee6 commit 8e0a648
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 71 deletions.
2 changes: 1 addition & 1 deletion internal/repository/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
4 changes: 2 additions & 2 deletions internal/usecase/app-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
67 changes: 11 additions & 56 deletions internal/usecase/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"time"

"github.com/Nerzal/gocloak/v13"
"github.com/openinfradev/tks-api/internal/keycloak"

"github.com/google/uuid"
Expand Down Expand Up @@ -248,6 +247,15 @@ 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")
Expand All @@ -266,6 +274,7 @@ 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=" + dto.Kubeconfig,
"git_account=" + viper.GetString("git-account"),
Expand All @@ -283,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
}

Expand Down Expand Up @@ -443,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"), "", "")
}
Expand Down Expand Up @@ -497,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"), "", "")
Expand Down
24 changes: 12 additions & 12 deletions pkg/domain/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`
Expand Down

0 comments on commit 8e0a648

Please sign in to comment.