generated from cybozu-go/neco-template
/
start.go
103 lines (83 loc) · 3.38 KB
/
start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
}