/
unquiesce.go
180 lines (148 loc) · 4.39 KB
/
unquiesce.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Copyright 2021.
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 unquiesce
import (
"context"
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/jibudata/amberapp/api/v1alpha1"
"github.com/jibudata/amberapp/pkg/client"
"github.com/jibudata/amberapp/pkg/cmd"
"github.com/jibudata/amberapp/pkg/util"
)
const (
DefaultPollInterval = 1 * time.Second
DefaultPollTimeout = 20 * time.Second
)
type UnquiesceOptions struct {
Name string
//Database string
}
func NewCommand(client *client.Client) *cobra.Command {
option := &UnquiesceOptions{}
c := &cobra.Command{
Use: "unquiesce",
Short: "Unquiesce a Database",
Long: "Unquiesce a Database which has been quiesced",
Run: func(c *cobra.Command, args []string) {
cmd.CheckError(option.Validate(c, client))
cmd.CheckError(option.Run(client))
},
}
option.BindFlags(c.Flags(), c)
return c
}
func (u *UnquiesceOptions) BindFlags(flags *pflag.FlagSet, c *cobra.Command) {
flags.StringVarP(&u.Name, "name", "n", "", "database configration name")
_ = c.MarkFlagRequired("name")
//flags.StringVarP(&c.Database, "database", "d", "", "name of the database instance")
}
func (u *UnquiesceOptions) Validate(command *cobra.Command, kubeclient *client.Client) error {
// Check WATCH_NAMESPACE, and if namespace exits, apphook operator is running
namespace, err := util.GetOperatorNamespace()
if err != nil {
return err
}
ns := &corev1.Namespace{}
err = kubeclient.Get(
context.TODO(),
types.NamespacedName{
Name: namespace,
},
ns)
if err != nil {
return err
}
return nil
}
func (u *UnquiesceOptions) updateHookCR(kubeclient *client.Client, namespace string) error {
crName := u.Name + "-hook"
foundHook := &v1alpha1.AppHook{}
err := kubeclient.Get(
context.TODO(),
types.NamespacedName{
Namespace: namespace,
Name: crName,
},
foundHook)
if err != nil {
return err
}
switch foundHook.Status.Phase {
case v1alpha1.HookReady:
return fmt.Errorf("hook CR %s not quiesced yet", foundHook.Name)
case v1alpha1.HookNotReady:
return fmt.Errorf("hook CR %s not ready yet", foundHook.Name)
case v1alpha1.HookUNQUIESCED:
return fmt.Errorf("hook CR %s already unquiesced", foundHook.Name)
case v1alpha1.HookQUIESCEINPROGRESS:
return fmt.Errorf("hook CR %s quiesce still in progress, please wait", foundHook.Name)
case v1alpha1.HookUNQUIESCEINPROGRESS:
return fmt.Errorf("hook CR %s unquiesce already in progress", foundHook.Name)
case v1alpha1.HookQUIESCED:
}
foundHook.Spec.OperationType = v1alpha1.UNQUIESCE
return kubeclient.Update(context.TODO(), foundHook)
}
func (u *UnquiesceOptions) waitUntilUnquiesced(kubeclient *client.Client, namespace string) (error, bool) {
crName := u.Name + "-hook"
done := false
err := wait.PollImmediate(DefaultPollInterval, DefaultPollTimeout, func() (bool, error) {
foundHook := &v1alpha1.AppHook{}
err := kubeclient.Get(
context.TODO(),
types.NamespacedName{
Namespace: namespace,
Name: crName,
},
foundHook)
if err != nil {
return false, err
}
if foundHook.Status.Phase == v1alpha1.HookUNQUIESCED {
done = true
return true, nil
}
return false, nil
})
return err, done
}
func (u *UnquiesceOptions) Run(kubeclient *client.Client) error {
crName := u.Name + "-hook"
namespace, err := util.GetOperatorNamespace()
if err != nil {
return err
}
err = u.updateHookCR(kubeclient, namespace)
if err == nil {
fmt.Printf("Update hook success: %s, namespace: %s\n", crName, namespace)
} else {
return err
}
startTime := time.Now()
err, done := u.waitUntilUnquiesced(kubeclient, namespace)
doneTime := time.Now()
duration := doneTime.Sub(startTime)
if err != nil {
return err
}
if done {
fmt.Printf("Database is successfully unquiesced: %s, namespace: %s, duration: %s\n", crName, namespace, duration)
}
return err
}