generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop reconciliation and clustering (#578)
* Stop reconciliation and clustering Signed-off-by: d-kuro <[email protected]>
- Loading branch information
Showing
19 changed files
with
954 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
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) { | ||
fmt.Fprintf(os.Stdout, "The clustering is already running.") | ||
return nil | ||
} | ||
|
||
if err := kubeClient.Update(ctx, cluster); err != nil { | ||
return fmt.Errorf("failed to start clustering of MySQLCluster: %w", err) | ||
} | ||
|
||
fmt.Fprintf(os.Stdout, "started clustering of MySQLCluster %q\n", fmt.Sprintf("%s/%s", namespace, name)) | ||
|
||
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) { | ||
fmt.Fprintf(os.Stdout, "The reconciliation is already running.") | ||
return nil | ||
} | ||
|
||
if err := kubeClient.Update(ctx, cluster); err != nil { | ||
return fmt.Errorf("failed to start reconciliation of MySQLCluster: %w", err) | ||
} | ||
|
||
fmt.Fprintf(os.Stdout, "started reconciliation of MySQLCluster %q\n", fmt.Sprintf("%s/%s", namespace, name)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
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(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() | ||
|
||
if cluster.Annotations == nil { | ||
cluster.Annotations = make(map[string]string) | ||
} | ||
cluster.Annotations[constants.AnnClusteringStopped] = "true" | ||
|
||
if equality.Semantic.DeepEqual(orig, cluster) { | ||
fmt.Fprintf(os.Stdout, "The clustering is already stopped.") | ||
return nil | ||
} | ||
|
||
if err := kubeClient.Update(ctx, cluster); err != nil { | ||
return fmt.Errorf("failed to stop clustering of MySQLCluster: %w", err) | ||
} | ||
|
||
fmt.Fprintf(os.Stdout, "stopped clustering of MySQLCluster %q\n", fmt.Sprintf("%s/%s", namespace, name)) | ||
|
||
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() | ||
|
||
if cluster.Annotations == nil { | ||
cluster.Annotations = make(map[string]string) | ||
} | ||
cluster.Annotations[constants.AnnReconciliationStopped] = "true" | ||
|
||
if equality.Semantic.DeepEqual(orig, cluster) { | ||
fmt.Fprintf(os.Stdout, "The reconciliation is already stopped.") | ||
return nil | ||
} | ||
|
||
if err := kubeClient.Update(ctx, cluster); err != nil { | ||
return fmt.Errorf("failed to stop reconciliation of MySQLCluster: %w", err) | ||
} | ||
|
||
fmt.Fprintf(os.Stdout, "stopped reconciliation of MySQLCluster %q\n", fmt.Sprintf("%s/%s", namespace, name)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.