Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trivial. change type to string from []byte for kubeconfig #537

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/model/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
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
74 changes: 16 additions & 58 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,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,
Expand All @@ -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"),
Expand All @@ -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
}

Expand Down Expand Up @@ -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"), "", "")
}
Expand Down Expand Up @@ -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"), "", "")
Expand Down
26 changes: 13 additions & 13 deletions pkg/domain/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Loading