/
main.go
104 lines (86 loc) · 2.81 KB
/
main.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
104
/*
Copyright 2019 Pressinfra SRL
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"log"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"github.com/presslabs/mysql-operator/pkg/apis"
"github.com/presslabs/mysql-operator/pkg/orc-helper"
)
var (
c client.Client
)
func main() {
cmd := &cobra.Command{
Use: "orchestrator-helper",
Short: "Helper for orchestrator.",
Long: `This command is a helper for updating MySQL cluster resources. Record events.`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatal("you run orchestrator helper, see help section")
},
}
// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
log.Fatal("unable to get configuration: ", err)
}
// Setup Scheme for all resources
s := scheme.Scheme
if err = apis.AddToScheme(s); err != nil {
log.Fatal("unable to register types to scheme: ", err)
}
// initialize k8s client
c, err = client.New(cfg, client.Options{Scheme: s})
if err != nil {
log.Fatal("unable to get the k8s client: ", err)
}
fipCmd := &cobra.Command{
Use: "failover-in-progress",
Short: "Set failover in progress condition for given cluster",
Run: func(cmd *cobra.Command, args []string) {
// check command line args
if len(args) != 2 {
log.Fatal("see usage: <cluster.name> <message>")
}
err = orchelper.UpdateClusterFailoverCond(c, args[0], "OrcFailoverInProgress", args[1], true)
if err != nil {
log.Fatal("error in updating cluster: ", err)
}
},
}
cmd.AddCommand(fipCmd)
evWarningType := false
evCmd := &cobra.Command{
Use: "event",
Short: "Set event on a given cluster",
Run: func(cmd *cobra.Command, args []string) {
// check command line args
if len(args) != 3 {
log.Fatal("see usage: <cluster.name> <event-name> <message> [-warning]")
}
err = orchelper.UpdateEventForCluster(c, s, args[0], args[1], args[2], evWarningType)
if err != nil {
log.Fatal("error in updating cluster: ", err)
}
},
}
evCmd.Flags().BoolVarP(&evWarningType, "warning", "w", false, "if it's a warning event in k8s")
cmd.AddCommand(evCmd)
if err := cmd.Execute(); err != nil {
log.Fatal("failed to execute command: ", err)
}
}