Skip to content

Commit

Permalink
Add kubectl moco sub commands
Browse files Browse the repository at this point in the history
Signed-off-by: d-kuro <[email protected]>
  • Loading branch information
d-kuro committed Oct 18, 2023
1 parent 0be37dd commit b6e78e7
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 0 deletions.
96 changes: 96 additions & 0 deletions cmd/kubectl-moco/cmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cmd

import (
"context"
"fmt"

mocov1beta2 "github.com/cybozu-go/moco/api/v1beta2"
"github.com/cybozu-go/moco/pkg/constants"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/types"
)

func init() {
rootCmd.AddCommand(startCmd)
startCmd.AddCommand(startClusteringCmd)
startCmd.AddCommand(startReconciliationCmd)
}

var startCmd = &cobra.Command{
Use: "start",
Short: "Starts the MySQLCluster reconciliation or clustering",
Long: "The start command is used to start the reconciliation or clustering of MySQLCluster",
}

var startClusteringCmd = &cobra.Command{
Use: "clustering CLUSTER_NAME",
Short: "Start the specified MySQLCluster's clustering",
Long: "start clustering is a command to start the clustering of the specified MySQLCluster. It requires the cluster name as the parameter.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return startClustering(cmd.Context(), args[0])
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return mysqlClusterCandidates(cmd.Context(), cmd, args, toComplete)
},
}

func startClustering(ctx context.Context, name string) error {
cluster := &mocov1beta2.MySQLCluster{}
if err := kubeClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, cluster); err != nil {
return err
}

orig := cluster.DeepCopy()

if ann, ok := cluster.Annotations[constants.AnnClusteringStopped]; ok && ann == "true" {
delete(cluster.Annotations, constants.AnnClusteringStopped)
}

if equality.Semantic.DeepEqual(orig, cluster) {
return nil
}

if err := kubeClient.Update(ctx, cluster); err != nil {
return fmt.Errorf("failed to start clustering of MySQLCluster: %w", err)
}

return nil
}

var startReconciliationCmd = &cobra.Command{
Use: "reconciliation CLUSTER_NAME",
Short: "Start the specified MySQLCluster's reconciliation",
Long: "start reconciliation is a command to start the reconciliation process for the specified MySQLCluster. This requires the cluster name as the parameter.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return startReconciliation(cmd.Context(), args[0])
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return mysqlClusterCandidates(cmd.Context(), cmd, args, toComplete)
},
}

func startReconciliation(ctx context.Context, name string) error {
cluster := &mocov1beta2.MySQLCluster{}
if err := kubeClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, cluster); err != nil {
return err
}

orig := cluster.DeepCopy()

if ann, ok := cluster.Annotations[constants.AnnReconciliationStopped]; ok && ann == "true" {
delete(cluster.Annotations, constants.AnnReconciliationStopped)
}

if equality.Semantic.DeepEqual(orig, cluster) {
return nil
}

if err := kubeClient.Update(ctx, cluster); err != nil {
return fmt.Errorf("failed to start reconciliation of MySQLCluster: %w", err)
}

return nil
}
93 changes: 93 additions & 0 deletions cmd/kubectl-moco/cmd/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package cmd

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/api/equality"

mocov1beta2 "github.com/cybozu-go/moco/api/v1beta2"
"github.com/cybozu-go/moco/pkg/constants"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
)

func init() {
rootCmd.AddCommand(stopCmd)
stopCmd.AddCommand(stopClusteringCmd)
stopCmd.AddCommand(stopReconciliationCmd)
}

var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stops the MySQLCluster reconciliation or clustering",
Long: "The stop command is used to halt the reconciliation or clustering of MySQLCluster",
}

var stopClusteringCmd = &cobra.Command{
Use: "clustering CLUSTER_NAME",
Short: "Stop the specified MySQLCluster's clustering",
Long: "stop clustering is a command to stop the clustering of the specified MySQLCluster. It requires the cluster name as the parameter.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return stopClustering(cmd.Context(), args[0])
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return mysqlClusterCandidates(cmd.Context(), cmd, args, toComplete)
},
}

func stopClustering(ctx context.Context, name string) error {
cluster := &mocov1beta2.MySQLCluster{}
if err := kubeClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, cluster); err != nil {
return err
}

orig := cluster.DeepCopy()

cluster.Annotations[constants.AnnClusteringStopped] = "true"

if equality.Semantic.DeepEqual(orig, cluster) {
return nil
}

if err := kubeClient.Update(ctx, cluster); err != nil {
return fmt.Errorf("failed to stop clustering of MySQLCluster: %w", err)
}

return nil
}

var stopReconciliationCmd = &cobra.Command{
Use: "reconciliation CLUSTER_NAME",
Short: "Stop the specified MySQLCluster's reconciliation",
Long: "stop reconciliation is a command to stop the reconciliation process for the specified MySQLCluster. This requires the cluster name as the parameter.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return stopReconciliation(cmd.Context(), args[0])
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return mysqlClusterCandidates(cmd.Context(), cmd, args, toComplete)
},
}

func stopReconciliation(ctx context.Context, name string) error {
cluster := &mocov1beta2.MySQLCluster{}
if err := kubeClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, cluster); err != nil {
return err
}

orig := cluster.DeepCopy()

cluster.Annotations[constants.AnnReconciliationStopped] = "true"

if equality.Semantic.DeepEqual(orig, cluster) {
return nil
}

if err := kubeClient.Update(ctx, cluster); err != nil {
return fmt.Errorf("failed to stop reconciliation of MySQLCluster: %w", err)
}

return nil
}

0 comments on commit b6e78e7

Please sign in to comment.