-
Notifications
You must be signed in to change notification settings - Fork 80
/
exclusion_status.go
250 lines (204 loc) · 6.89 KB
/
exclusion_status.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* exclusion_status.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2021 Apple Inc. and the FoundationDB project authors
*
* 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 cmd
import (
"encoding/json"
"fmt"
"github.com/FoundationDB/fdb-kubernetes-operator/pkg/fdbstatus"
"sort"
"strconv"
"time"
fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func newExclusionStatusCmd(streams genericclioptions.IOStreams) *cobra.Command {
o := newFDBOptions(streams)
cmd := &cobra.Command{
Use: "exclusion-status",
Short: "Get the exclusion status for all excluded processes.",
Long: "Get the exclusion status for all excluded processes.",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
ignoreFullyExcluded, err := cmd.Flags().GetBool("ignore-fully-excluded")
if err != nil {
return err
}
interval, err := cmd.Flags().GetDuration("interval")
if err != nil {
return err
}
config, err := o.configFlags.ToRESTConfig()
if err != nil {
return err
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
kubeClient, err := getKubeClient(cmd.Context(), o)
if err != nil {
return err
}
namespace, err := getNamespace(*o.configFlags.Namespace)
if err != nil {
return err
}
cluster, err := loadCluster(kubeClient, namespace, args[0])
if err != nil {
return err
}
pods, err := getPodsForCluster(kubeClient, cluster)
if err != nil {
return err
}
if len(pods.Items) == 0 {
return fmt.Errorf("no running Pods are found for cluster: %s/%s", cluster.Namespace, cluster.Name)
}
// TODO get the pod randomly
err = getExclusionStatus(cmd, config, clientSet, pods.Items[0].Name, namespace, ignoreFullyExcluded, interval)
if err != nil {
return err
}
return nil
},
Example: `
Experimental feature!
This command shows the ongoing exclusions for a cluster and how much data must be moved before the exclusion is done.
# Get the exclusion status for cluster c1
kubectl fdb get exclusion-status c1
# Get the exclusion status for cluster c1 and prints out processes that are fully excluded
kubectl fdb get exclusion-status c1 --ignore-fully-excluded=false
# Get the exclusion status for cluster c1 and updates the data every 5 minutes
kubectl fdb get exclusion-status c1 --interval=5m
`,
}
cmd.SetOut(o.Out)
cmd.SetErr(o.ErrOut)
cmd.SetIn(o.In)
cmd.Flags().Bool("ignore-fully-excluded", true, "defines if processes that are fully excluded should be ignored.")
cmd.Flags().Duration("interval", 1*time.Minute, "defines in which interval new information should be fetched from the cluster.")
o.configFlags.AddFlags(cmd.Flags())
return cmd
}
type exclusionResult struct {
id string
estimate string
storedBytes int
timestamp time.Time
}
func getExclusionStatus(cmd *cobra.Command, restConfig *rest.Config, kubeClient *kubernetes.Clientset, clientPod string, namespace string, ignoreFullyExcluded bool, interval time.Duration) error {
timer := time.NewTicker(interval)
previousRun := map[string]exclusionResult{}
for {
// TODO: Keeping a stream open is probably more efficient.
out, serr, err := executeCmd(restConfig, kubeClient, clientPod, namespace, "fdbcli --exec 'status json'")
if err != nil {
// If an error occurs retry
cmd.PrintErrln(err)
continue
}
if serr.Len() > 0 {
cmd.PrintErrln(serr.String())
}
res, err := fdbstatus.RemoveWarningsInJSON(out.String())
if err != nil {
// If an error occurs retry
cmd.PrintErrln(err)
continue
}
status := &fdbv1beta2.FoundationDBStatus{}
err = json.Unmarshal(res, status)
if err != nil {
// If an error occurs retry
cmd.PrintErrln(err)
continue
}
var ongoingExclusions []exclusionResult
timestamp := time.Now()
for _, process := range status.Cluster.Processes {
if !process.Excluded {
continue
}
// If more than one storage server per Pod is running we have to differentiate those processes. If the
// process ID is not set, fall back to the instance ID.
instance, ok := process.Locality[fdbv1beta2.FDBLocalityProcessIDKey]
if !ok {
instance = process.Locality[fdbv1beta2.FDBLocalityInstanceIDKey]
}
if !ignoreFullyExcluded && len(process.Roles) == 0 {
cmd.Println(instance, "is fully excluded")
continue
}
// TODO: Add progress bars
for _, role := range process.Roles {
roleClass := fdbv1beta2.ProcessClass(role.Role)
if roleClass.IsStateful() {
var estimate string
previousResult, ok := previousRun[instance]
if ok {
estimateDuration := time.Duration(role.StoredBytes/(previousResult.storedBytes-role.StoredBytes)) * timestamp.Sub(previousResult.timestamp)
estimate = estimateDuration.String()
} else {
estimate = "N/A"
}
result := exclusionResult{
id: instance,
storedBytes: role.StoredBytes,
estimate: estimate,
timestamp: timestamp,
}
// TODO: Check if StoredBytes is the correct value
ongoingExclusions = append(ongoingExclusions, result)
previousRun[instance] = result
}
}
}
if len(ongoingExclusions) == 0 {
timer.Stop()
break
}
sort.SliceStable(ongoingExclusions, func(i, j int) bool {
return ongoingExclusions[i].id < ongoingExclusions[j].id
})
for _, exclusion := range ongoingExclusions {
cmd.Printf("%s:\t %s are left - estimate: %s\n", exclusion.id, prettyPrintStoredBytes(exclusion.storedBytes), exclusion.estimate)
}
cmd.Println("There are", len(ongoingExclusions), "processes that are not fully excluded.")
cmd.Println("======================================================================================================")
<-timer.C
}
return nil
}
// prettyPrintStoredBytes will return a string that represents the storedBytes in a human-readable format.
func prettyPrintStoredBytes(storedBytes int) string {
units := []string{"", "Ki", "Mi", "Gi", "Ti", "Pi"}
currentBytes := float64(storedBytes)
for _, unit := range units {
if currentBytes < 1024 {
return fmt.Sprintf("%3.2f%s", currentBytes, unit)
}
currentBytes /= 1024
}
// Fallback will be to printout the bytes.
return strconv.Itoa(storedBytes)
}