Skip to content

Commit

Permalink
feat: Add cluster admin command
Browse files Browse the repository at this point in the history
  • Loading branch information
mzottola committed Feb 5, 2024
1 parent 6ae75e4 commit 4c53e82
Show file tree
Hide file tree
Showing 6 changed files with 675 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cmd/admin_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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) add parameter to random deploy clusters
// TODO (mzo) handle pending clusters queue, when clusters couldn't be deployed because not in a final state
// TODO (mzo) handle progression in a file to let resume from the last deployment launched in case of interruption
)

func init() {
adminCmd.AddCommand(adminClusterCmd)
}
66 changes: 66 additions & 0 deletions cmd/admin_cluster_deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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
parallelRuns int
)

func init() {
adminClusterDeployCmd.Flags().BoolVarP(&dryRun, "disable-dry-run", "y", false, "Disable dry run mode")
adminClusterDeployCmd.Flags().IntVarP(&parallelRuns, "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, parallelRuns, 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
}
}
52 changes: 52 additions & 0 deletions pkg/admin_cluster_deploy_by_batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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
}

deployService.PrintParameters()

utils.Println("Do you want to continue deploy process ?")
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 {
utils.Println(fmt.Sprintf("%d clusters not triggered because in non-terminal state (queue not implemented yet):", len(deployResult.PendingClusters)))
err := PrintClustersTable(deployResult.PendingClusters)
if err != nil {
return err
}
}

if len(deployResult.ProcessedClusters) > 0 {
utils.Println(fmt.Sprintf("%d clusters deployed:", len(clusters)))
err := PrintClustersTable(deployResult.ProcessedClusters)
if err != nil {
return err
}
}

return nil
}
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
}
Loading

0 comments on commit 4c53e82

Please sign in to comment.