-
Notifications
You must be signed in to change notification settings - Fork 9
/
binding_types_helper.go
167 lines (140 loc) · 4.36 KB
/
binding_types_helper.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
/*
Copyright 2022 The Karmada 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 v1alpha2
// TaskOptions represents options for GracefulEvictionTasks.
type TaskOptions struct {
producer string
reason string
message string
gracePeriodSeconds *int32
suppressDeletion *bool
}
// Option configures a TaskOptions
type Option func(*TaskOptions)
// NewTaskOptions builds a TaskOptions
func NewTaskOptions(opts ...Option) *TaskOptions {
options := TaskOptions{}
for _, opt := range opts {
opt(&options)
}
return &options
}
// WithProducer sets the producer for TaskOptions
func WithProducer(producer string) Option {
return func(o *TaskOptions) {
o.producer = producer
}
}
// WithReason sets the reason for TaskOptions
func WithReason(reason string) Option {
return func(o *TaskOptions) {
o.reason = reason
}
}
// WithMessage sets the message for TaskOptions
func WithMessage(message string) Option {
return func(o *TaskOptions) {
o.message = message
}
}
// WithGracePeriodSeconds sets the gracePeriodSeconds for TaskOptions
func WithGracePeriodSeconds(gracePeriodSeconds *int32) Option {
return func(o *TaskOptions) {
o.gracePeriodSeconds = gracePeriodSeconds
}
}
// WithSuppressDeletion sets the suppressDeletion for TaskOptions
func WithSuppressDeletion(suppressDeletion *bool) Option {
return func(o *TaskOptions) {
o.suppressDeletion = suppressDeletion
}
}
// TargetContains checks if specific cluster present on the target list.
func (s *ResourceBindingSpec) TargetContains(name string) bool {
for i := range s.Clusters {
if s.Clusters[i].Name == name {
return true
}
}
return false
}
// ClusterInGracefulEvictionTasks checks if the target cluster is in the GracefulEvictionTasks which means it is in the process of eviction.
func (s *ResourceBindingSpec) ClusterInGracefulEvictionTasks(name string) bool {
for _, task := range s.GracefulEvictionTasks {
if task.FromCluster == name {
return true
}
}
return false
}
// AssignedReplicasForCluster returns assigned replicas for specific cluster.
func (s *ResourceBindingSpec) AssignedReplicasForCluster(targetCluster string) int32 {
for i := range s.Clusters {
if s.Clusters[i].Name == targetCluster {
return s.Clusters[i].Replicas
}
}
return 0
}
// RemoveCluster removes specific cluster from the target list.
// This function no-opts if cluster not exist.
func (s *ResourceBindingSpec) RemoveCluster(name string) {
var i int
for i = 0; i < len(s.Clusters); i++ {
if s.Clusters[i].Name == name {
break
}
}
// not found, do nothing
if i >= len(s.Clusters) {
return
}
s.Clusters = append(s.Clusters[:i], s.Clusters[i+1:]...)
}
// GracefulEvictCluster removes specific cluster from the target list in a graceful way by
// building a graceful eviction task.
// This function no-opts if the cluster does not exist.
func (s *ResourceBindingSpec) GracefulEvictCluster(name string, options *TaskOptions) {
// find the cluster index
var i int
for i = 0; i < len(s.Clusters); i++ {
if s.Clusters[i].Name == name {
break
}
}
// not found, do nothing
if i >= len(s.Clusters) {
return
}
evictCluster := s.Clusters[i]
// remove the target cluster from scheduling result
s.Clusters = append(s.Clusters[:i], s.Clusters[i+1:]...)
// skip if the target cluster already in the task list
if s.ClusterInGracefulEvictionTasks(evictCluster.Name) {
return
}
// build eviction task
evictingCluster := evictCluster.DeepCopy()
evictionTask := GracefulEvictionTask{
FromCluster: evictingCluster.Name,
Reason: options.reason,
Message: options.message,
Producer: options.producer,
GracePeriodSeconds: options.gracePeriodSeconds,
SuppressDeletion: options.suppressDeletion,
}
if evictingCluster.Replicas > 0 {
evictionTask.Replicas = &evictingCluster.Replicas
}
s.GracefulEvictionTasks = append(s.GracefulEvictionTasks, evictionTask)
}