Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
feat: save kubeconfig file
Browse files Browse the repository at this point in the history
save kubeconfig file into database
improve docker compose to avoid issues with volumes
  • Loading branch information
Javlopez committed Mar 19, 2024
1 parent 6ade562 commit dd222a6
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ test:

push:
docker push koorinc/genesis:$(TAG)

compose:
@USER_ID=$(shell id -u) \
GROUP_ID=$(shell id -g) \
docker compose up
19 changes: 18 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
version: '3.8'
version: '3.9'

services:
change-vol-ownership:
image: alpine
user: "root"
# Specify the group in question
group_add:
- '${GROUP_ID}'
volumes:
# The volume to chown
- clients:/tmp/clients
command: chown -R ${USER_ID}:${GROUP_ID} /tmp/clients

api:
user: "${USER_ID}:${GROUP_ID}"
image: koor-tech/genesis
build:
dockerfile: Dockerfile
Expand All @@ -13,13 +25,16 @@ services:
condition: service_healthy
store:
condition: service_healthy
change-vol-ownership:
condition: service_completed_successfully
volumes:
- ./config.yaml:/home/koor/config.yaml
- ./templates:/koor/templates
- ./charts:/koor/charts
- clients:/koor/clients

worker:
user: "${USER_ID}:${GROUP_ID}"
image: koor-tech/genesis
build:
dockerfile: Dockerfile
Expand All @@ -30,6 +45,8 @@ services:
condition: service_healthy
store:
condition: service_healthy
change-vol-ownership:
condition: service_completed_successfully
volumes:
- ./templates:/koor/templates
- ./charts:/koor/charts
Expand Down
6 changes: 3 additions & 3 deletions gateway/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func RegisterRoutes(r *gin.Engine, clusterHandler *handler.Cluster) *gin.Engine

v1 := r.Group("/api/v1")
{
v1.POST("/cluster", clusterHandler.CreateCluster)
v1.POST("/clusters", clusterHandler.CreateCluster)
v1.GET("/clusters/:id", clusterHandler.GetCluster)
v1.DELETE("/cluster/:id", clusterHandler.DeleteCluster)
v1.PUT("/cluster/:id", clusterHandler.ResumeCluster)
v1.DELETE("/clusters/:id", clusterHandler.DeleteCluster)
v1.PUT("/clusters/:id", clusterHandler.ResumeCluster)
}

return r
Expand Down
23 changes: 21 additions & 2 deletions internal/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,15 @@ func (s *Service) ResumeCluster(ctx context.Context, clusterID uuid.UUID) error
if err != nil {
s.logger.Error("unable to save the state of the cluster ", "err ", err, "clusterID", cluster.ID)
}
k8sCluster := k8s.New(s.getCustomerDir(&cluster.Customer)+"/"+kubeConfigName, s.dirCfg.ChartsDir())
k8sCluster.InstallCharts()

kubeConfigFile := s.getCustomerDir(&cluster.Customer) + "/" + kubeConfigName

k8sCluster := k8s.New(kubeConfigFile, s.dirCfg.ChartsDir())
err = k8sCluster.InstallCharts()
if err != nil {
s.logger.Error("unable to install rook-ceph charts ", "err ", err, "clusterID", cluster.ID)
return err
}
clusterState.Phase = models.ClusterPhaseInstallCephDone
err = s.clusterStateRepository.Update(ctx, clusterState)
if err != nil {
Expand All @@ -317,6 +324,18 @@ func (s *Service) ResumeCluster(ctx context.Context, clusterID uuid.UUID) error

s.logger.Info("Ceph Cluster provisioned")

kubeConfigData, err := utils.ReadFileAsString(kubeConfigFile)
if err != nil {
s.logger.Error("unable to get the content of kubeConfig file", "file", kubeConfigFile, "err", err)
return err
}

cluster.KubeConfig = &kubeConfigData
err = s.clustersRepository.Update(ctx, *cluster)
if err != nil {
s.logger.Error("unable to update the cluster ", "err ", err, "clusterID", cluster.ID)
}

if err := s.notifier.Send(cluster.Customer); err != nil {
s.logger.Error("failed to notify customer", "err", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/models/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ type Cluster struct {
ProviderID uuid.UUID `db:"provider_id"`
Customer Customer `db:"customers"`
Provider Provider `db:"providers"`
KubeConfig *string `db:"kube_config"`
ClusterState ClusterState `db:"cs"`
}
2 changes: 1 addition & 1 deletion pkg/models/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewTerraformConfig(cluster *Cluster, dst string) *TerraformConfig {
return &TerraformConfig{
ClusterName: "koor-client-" + cluster.Customer.Name,
SshPublicKeyFile: dst + "/id_ed25519.pub",
ControlPlaneVmCount: 3,
ControlPlaneVmCount: 1,
InitialMachineDeploymentReplicas: 3,
WorkerType: "cpx41",
ControlPlaneType: "cpx31",
Expand Down
26 changes: 26 additions & 0 deletions pkg/repositories/postgres/cluster/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,37 @@ func (r *ClusterRepository) Save(ctx context.Context, cluster models.Cluster) (*
return &cluster, nil
}

func (r *ClusterRepository) Update(ctx context.Context, params models.Cluster) error {
c, err := r.QueryByID(ctx, params.ID)
if err != nil {
return err
}
if params.KubeConfig != nil {
c.KubeConfig = params.KubeConfig
}
updateStmt, args, _ := sq.StatementBuilder.PlaceholderFormat(sq.Dollar).
Update(`clusters`).
Set(`kube_config`, c.KubeConfig).
Where("id = $2", c.ID).
ToSql()

_, err = r.db.Conn.ExecContext(ctx, updateStmt, args...)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return models.ErrClusterNotFound
}
return err
}

return nil
}

func (r *ClusterRepository) QueryByID(ctx context.Context, clusterID uuid.UUID) (*models.Cluster, error) {
var builder = sq.StatementBuilder.PlaceholderFormat(sq.Dollar).
Select(
`c.id`,
`c.customer_id`,
`c.kube_config`,
`customers.id as "customers.id"`,
`customers.name as "customers.name"`,
`c.provider_id`,
Expand Down
1 change: 1 addition & 0 deletions pkg/repositories/postgres/cluster/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import (
type ClustersInterface interface {
Save(ctx context.Context, cluster models.Cluster) (*models.Cluster, error)
QueryByID(ctx context.Context, clusterID uuid.UUID) (*models.Cluster, error)
Update(ctx context.Context, params models.Cluster) error
}
9 changes: 9 additions & 0 deletions pkg/utils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,12 @@ func SaveInFile(name, content string, permissions int) error {

return nil
}

func ReadFileAsString(name string) (string, error) {
content, err := os.ReadFile(name)
if err != nil {
return "", fmt.Errorf("failed to read file. %w", err)
}

return string(content), nil
}

0 comments on commit dd222a6

Please sign in to comment.