-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
272 lines (238 loc) · 7.62 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"flag"
"fmt"
gamekruiseiov1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"
kruisegameclientset "github.com/openkruise/kruise-game/pkg/client/clientset/versioned"
"github.com/openkruise/kruise-game/pkg/util"
"golang.org/x/net/context"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"strconv"
"strings"
"time"
)
type input struct {
gssName string
namespace string
timeout int
selectIds string
selectOpsState string
selectNetworkDisabled string
selectNotContainerImage string
expOpsState string
expNetworkDisabled string
}
type selectOption struct {
gssNames []string
namespace string
gsNames []string
opsState *gamekruiseiov1alpha1.OpsState
networkDisabled *bool
notContainerImage map[string]string
}
type expectOption struct {
namespace string
opsState *gamekruiseiov1alpha1.OpsState
networkDisabled *bool
}
func main() {
i := input{}
flag.StringVar(&i.gssName, "gss-name", "", "gssName")
flag.StringVar(&i.namespace, "namespace", "", "namespace")
flag.IntVar(&i.timeout, "timeout", 300, "timeout")
flag.StringVar(&i.selectIds, "select-ids", "", "selectIds")
flag.StringVar(&i.selectOpsState, "select-opsState", "", "selectOpsState")
flag.StringVar(&i.selectNetworkDisabled, "select-networkDisabled", "", "selectNetworkDisabled")
flag.StringVar(&i.selectNotContainerImage, "select-not-container-image", "", "selectNotContainerImage")
flag.StringVar(&i.expOpsState, "exp-opsState", "", "expOpsState")
flag.StringVar(&i.expNetworkDisabled, "exp-networkDisabled", "", "expNetworkDisabled")
flag.Parse()
ctx := context.Background()
cfg := config.GetConfigOrDie()
kruisegameClient := kruisegameclientset.NewForConfigOrDie(cfg)
kubeClient := clientset.NewForConfigOrDie(cfg)
so, err := consSelectOption(ctx, kruisegameClient, i)
if err != nil {
panic(err)
}
fmt.Printf("selectOption: %+v\n", so)
eo, err := consExpectOption(i)
if err != nil {
panic(err)
}
fmt.Printf("expectOption: %+v\n", eo)
err = wait.PollUntilContextTimeout(ctx, 5*time.Second, time.Duration(i.timeout)*time.Second, true, func(ctx context.Context) (done bool, err error) {
selectedGameServers, err := SelectGameServers(ctx, kruisegameClient, kubeClient, so)
if err != nil {
return false, err
}
if err = UpdateGameServers(ctx, kruisegameClient, selectedGameServers, eo); err != nil {
return false, err
}
return true, nil
})
if err != nil {
panic(fmt.Errorf("update GameServers failed, err:%v\n", err))
}
fmt.Printf("update GameServers Done\n")
}
func UpdateGameServers(ctx context.Context, kgClient *kruisegameclientset.Clientset, gsList []gamekruiseiov1alpha1.GameServer, eo *expectOption) error {
for _, gs := range gsList {
gsNew := gs.DeepCopy()
// set networkDisabled
if eo.networkDisabled != nil {
gsNew.Spec.NetworkDisabled = *eo.networkDisabled
}
// set opsState
if eo.opsState != nil {
gsNew.Spec.OpsState = *eo.opsState
}
_, err := kgClient.GameV1alpha1().GameServers(gs.Namespace).Update(ctx, gsNew, metav1.UpdateOptions{})
if err != nil {
return err
}
}
return nil
}
func SelectGameServers(ctx context.Context, kgClient kruisegameclientset.Interface, kubeClient clientset.Interface, so *selectOption) ([]gamekruiseiov1alpha1.GameServer, error) {
var selectedGameServers []gamekruiseiov1alpha1.GameServer
var selectNames []string
for _, gssName := range so.gssNames {
labelSelector := labels.SelectorFromSet(map[string]string{
gamekruiseiov1alpha1.GameServerOwnerGssKey: gssName,
}).String()
gsList, err := kgClient.GameV1alpha1().GameServers(so.namespace).List(ctx, metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, err
}
for _, gs := range gsList.Items {
// filter by idList
if len(so.gsNames) > 0 && !util.IsStringInList(gs.Name, so.gsNames) {
continue
}
// filter by opsState
if so.opsState != nil && gs.Spec.OpsState != *so.opsState {
continue
}
// filter by networkDisabled
if so.networkDisabled != nil && *so.networkDisabled != gs.Spec.NetworkDisabled {
continue
}
// filter by containerImage
if so.notContainerImage != nil {
pod, err := kubeClient.CoreV1().Pods(so.namespace).Get(ctx, gs.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
actual := getContainerImage(pod.DeepCopy())
hit := false
for container, image := range so.notContainerImage {
if actual[container] == image {
hit = true
break
}
}
if hit {
continue
}
}
selectedGameServers = append(selectedGameServers, gs)
selectNames = append(selectNames, gs.Name)
}
}
fmt.Printf("select GameServers Names: %v\n", selectNames)
return selectedGameServers, nil
}
func consExpectOption(i input) (*expectOption, error) {
// parse expOpsState
var opsState *gamekruiseiov1alpha1.OpsState
if i.expOpsState != "" {
opsStateTemp := (gamekruiseiov1alpha1.OpsState)(i.expOpsState)
opsState = &opsStateTemp
}
// parse expNetworkDisabled
var networkDisabled *bool
if i.expNetworkDisabled != "" {
networkDisabledTemp := strings.ToLower(i.expNetworkDisabled) == "true"
networkDisabled = &networkDisabledTemp
}
return &expectOption{
namespace: i.namespace,
opsState: opsState,
networkDisabled: networkDisabled,
}, nil
}
func consSelectOption(ctx context.Context, kgClient kruisegameclientset.Interface, i input) (*selectOption, error) {
// parse gssNames
var gssNames []string
if i.gssName == "" {
gssList, err := kgClient.GameV1alpha1().GameServerSets(i.namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, gss := range gssList.Items {
gssNames = append(gssNames, gss.Name)
}
} else {
gssNames = strings.Split(i.gssName, ",")
}
// parse selectIds
var gsNames []string
if i.selectIds != "" {
for _, gssName := range gssNames {
for _, idStr := range strings.Split(i.selectIds, ",") {
idInt, err := strconv.Atoi(idStr)
if err != nil {
return nil, err
}
if idInt < 0 {
return nil, fmt.Errorf("invalid id %s", idStr)
}
gsName := gssName + "-" + idStr
gsNames = append(gsNames, gsName)
}
}
}
// parse selectOpsState
var opsState *gamekruiseiov1alpha1.OpsState
if i.selectOpsState != "" {
opsStateTemp := (gamekruiseiov1alpha1.OpsState)(i.selectOpsState)
opsState = &opsStateTemp
}
// parse selectNetworkDisabled
var networkDisabled *bool
if i.selectNetworkDisabled != "" {
networkDisabledTemp := strings.ToLower(i.selectNetworkDisabled) == "true"
networkDisabled = &networkDisabledTemp
}
// parse selectNotContainerImage
var notContainerImage map[string]string
if i.selectNotContainerImage != "" {
for _, snci := range strings.Split(i.selectNotContainerImage, ",") {
container := strings.Split(snci, "/")[0]
image, _ := strings.CutPrefix(snci, container+"/")
notContainerImage = make(map[string]string)
notContainerImage[container] = image
}
}
return &selectOption{
gssNames: gssNames,
namespace: i.namespace,
gsNames: gsNames,
opsState: opsState,
networkDisabled: networkDisabled,
notContainerImage: notContainerImage,
}, nil
}
func getContainerImage(pod *corev1.Pod) map[string]string {
containerImage := make(map[string]string)
for _, container := range pod.Spec.Containers {
containerImage[container.Name] = container.Image
}
return containerImage
}