Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mzottola committed Feb 2, 2024
1 parent 53b2b99 commit de6fa91
Show file tree
Hide file tree
Showing 8 changed files with 675 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/admin_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"os"

"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
)

var (
adminClusterCmd = &cobra.Command{
Use: "cluster",
Short: "Manage clusters",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)

if len(args) == 0 {
_ = cmd.Help()
os.Exit(0)
}
},
}
// TODO (mzo) handle parameters to be split, e.g --filters ["CurrentStatus"]="FAILED,INVALID_CREDENTIALS"
// TODO (mzo) handle parameter to compare last_deployed_at, e.g "--before-last-deployed-date"
// TODO (mzo) enable processing deployments by "order by" org plans, e.g FREE then TEAM then ...
// TODO (mzo) add parameter to random deploy clusters
// TODO (mzo) handle progression in a file
)

func init() {
adminCmd.AddCommand(adminClusterCmd)
}
65 changes: 65 additions & 0 deletions cmd/admin_cluster_deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"os"

"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/pkg"
"github.com/qovery/qovery-cli/utils"
)

var (
adminClusterDeployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy or upgrade clusters",
Run: func(cmd *cobra.Command, args []string) {
deployClusters()
},
}
refreshDelay int
filters map[string]string
executionMode string
newK8sVersion string
)

func init() {
adminClusterDeployCmd.Flags().BoolVarP(&dryRun, "disable-dry-run", "y", false, "Disable dry run mode")
adminClusterDeployCmd.Flags().IntVarP(&parallelRun, "parallel-run", "n", 5, "Number of clusters to update in parallel - must be set between 1 and 20")
adminClusterDeployCmd.Flags().IntVarP(&refreshDelay, "refresh-delay", "r", 30, "Time in seconds to wait before checking clusters status during deployment - must be between [5-120]")
adminClusterDeployCmd.Flags().StringToStringVarP(&filters, "filters", "f", make(map[string]string), "Value to filter the property selected (property-to-filter must be set as well)")
adminClusterDeployCmd.Flags().StringVarP(&executionMode, "execution-mode", "e", "batch", "Batch execution mode - 'batch' will wait for the N deployments to be finished and ask validation to continue - 'on-the-fly' will deploy continuously as soon as a slot is available")
adminClusterDeployCmd.Flags().StringVarP(&newK8sVersion, "new-k8s-version", "k", "", "K8S version when upgrading clusters")
adminClusterCmd.AddCommand(adminClusterDeployCmd)

}

func deployClusters() {
utils.CheckAdminUrl()

// if no filters is set, enforce to select only RUNNING clusters to avoid mistakes (e.g deploying a stopped cluster)
_, containsKey := filters["ClusterStatus"]
if !containsKey {
filters["CurrentStatus"] = "DEPLOYED"
}

listService, err := pkg.NewAdminClusterListServiceImpl(filters)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
deployService, err := pkg.NewAdminClusterBatchDeployServiceImpl(dryRun, parallelRun, refreshDelay, executionMode, newK8sVersion)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

err = pkg.DeployClustersByBatch(listService, deployService)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
}
42 changes: 42 additions & 0 deletions cmd/admin_cluster_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"os"

"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/pkg"
"github.com/qovery/qovery-cli/utils"
)

var (
adminClusterListCmd = &cobra.Command{
Use: "list",
Short: "List clusters by applying any filter",
Run: func(cmd *cobra.Command, args []string) {
listClusters()
},
}
)

func init() {
adminClusterListCmd.Flags().StringToStringVarP(&filters, "filters", "f", make(map[string]string), "Value to filter the property selected (property-to-filter must be set as well)")
adminClusterCmd.AddCommand(adminClusterListCmd)
}

func listClusters() {
utils.CheckAdminUrl()

listService, err := pkg.NewAdminClusterListServiceImpl(filters)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
err = pkg.ListClusters(listService)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
}
46 changes: 46 additions & 0 deletions pkg/admin_cluster_deploy_by_batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package pkg

import (
"fmt"

"github.com/qovery/qovery-cli/utils"
)

func DeployClustersByBatch(listService AdminClusterListService, deployService AdminClusterBatchDeployService) error {
clusters, err := listService.SelectClusters()
if err != nil {
return err
}

utils.Println(fmt.Sprintf("%d clusters to deploy:", len(clusters)))
err = PrintClustersTable(clusters)
if err != nil {
return err
}

// TODO (mzo) print every input values before asking to deploy

utils.Println(fmt.Sprintf("Do you want to deploy those clusters ?"))

Check failure on line 23 in pkg/admin_cluster_deploy_by_batch.go

View workflow job for this annotation

GitHub Actions / lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
var validated = utils.Validate("deploy")
if !validated {
utils.Println("Exiting: Validation failed")
return nil
}

deployResult, err := deployService.Deploy(clusters)
if err != nil {
return err
}

if len(deployResult.PendingClusters) > 0 {
// TODO (mzo) handle the pending queue
utils.Println(fmt.Sprintf("%d clusters not triggered because in non-terminal state (queue not implemented yet):", len(deployResult.PendingClusters)))
}

if len(deployResult.ProcessedClusters) > 0 {
utils.Println(fmt.Sprintf("%d clusters deployed:", len(clusters)))
PrintClustersTable(deployResult.ProcessedClusters)

Check failure on line 42 in pkg/admin_cluster_deploy_by_batch.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
}

return nil
}
16 changes: 16 additions & 0 deletions pkg/admin_cluster_deploy_by_batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pkg

import (
"testing"
)

func TestAdminClusterDeploy(t *testing.T) {
var filters = make(map[string]string)

filters["OrganizationName"] = "Qovery tests AWS"

listService, _ := NewAdminClusterListServiceImpl(filters)
deployService, _ := NewAdminClusterBatchDeployServiceImpl(false, 1, 30, "batch", "")

DeployClustersByBatch(listService, deployService)

Check failure on line 15 in pkg/admin_cluster_deploy_by_batch_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
}
21 changes: 21 additions & 0 deletions pkg/admin_cluster_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pkg

import (
"fmt"

"github.com/qovery/qovery-cli/utils"
)

func ListClusters(listService AdminClusterListService) error {
clusters, err := listService.SelectClusters()
if err != nil {
return err
}

utils.Println(fmt.Sprintf("Found %d clusters", len(clusters)))
err = PrintClustersTable(clusters)
if err != nil {
return err
}
return nil
}
15 changes: 15 additions & 0 deletions pkg/admin_cluster_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pkg

import (
"testing"
)

func TestAdminClusterList(t *testing.T) {
var filters = make(map[string]string)

filters["OrganizationName"] = "Qovery tests AWS"

listService, _ := NewAdminClusterListServiceImpl(filters)

ListClusters(listService)

Check failure on line 14 in pkg/admin_cluster_list_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
}
Loading

0 comments on commit de6fa91

Please sign in to comment.