forked from kubernetes-retired/kubefed
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.go
241 lines (202 loc) · 6.61 KB
/
plugin.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
/*
Copyright 2018 The Kubernetes 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 schedulingtypes
import (
"fmt"
"reflect"
"sort"
"strings"
"github.com/golang/glog"
"github.com/kubernetes-sigs/federation-v2/pkg/apis/core/typeconfig"
genericclient "github.com/kubernetes-sigs/federation-v2/pkg/client/generic"
"github.com/kubernetes-sigs/federation-v2/pkg/controller/util"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
)
const (
replicasPath = "spec.replicas"
)
type Plugin struct {
targetInformer util.FederatedInformer
federatedStore cache.Store
federatedController cache.Controller
federatedTypeClient util.ResourceClient
typeConfig typeconfig.Interface
stopChannel chan struct{}
}
func NewPlugin(controllerConfig *util.ControllerConfig, eventHandlers SchedulerEventHandlers, typeConfig typeconfig.Interface) (*Plugin, error) {
targetAPIResource := typeConfig.GetTarget()
userAgent := fmt.Sprintf("%s-replica-scheduler", strings.ToLower(targetAPIResource.Kind))
client := genericclient.NewForConfigOrDieWithUserAgent(controllerConfig.KubeConfig, userAgent)
targetInformer, err := util.NewFederatedInformer(
controllerConfig,
client,
&targetAPIResource,
eventHandlers.ClusterEventHandler,
eventHandlers.ClusterLifecycleHandlers,
)
if err != nil {
return nil, err
}
p := &Plugin{
targetInformer: targetInformer,
typeConfig: typeConfig,
stopChannel: make(chan struct{}),
}
targetNamespace := controllerConfig.TargetNamespace
federationEventHandler := eventHandlers.FederationEventHandler
federatedTypeAPIResource := typeConfig.GetFederatedType()
p.federatedTypeClient, err = util.NewResourceClient(controllerConfig.KubeConfig, &federatedTypeAPIResource)
if err != nil {
return nil, err
}
p.federatedStore, p.federatedController = util.NewResourceInformer(p.federatedTypeClient, targetNamespace, federationEventHandler)
return p, nil
}
func (p *Plugin) Start() {
p.targetInformer.Start()
go p.federatedController.Run(p.stopChannel)
}
func (p *Plugin) Stop() {
p.targetInformer.Stop()
close(p.stopChannel)
}
func (p *Plugin) HasSynced() bool {
if !p.targetInformer.ClustersSynced() {
glog.V(2).Infof("Cluster list not synced")
return false
}
if !p.federatedController.HasSynced() {
return false
}
clusters, err := p.targetInformer.GetReadyClusters()
if err != nil {
runtime.HandleError(errors.Wrap(err, "Failed to get ready clusters"))
return false
}
if !p.targetInformer.GetTargetStore().ClustersSynced(clusters) {
return false
}
return true
}
func (p *Plugin) FederatedTypeExists(key string) bool {
_, exist, err := p.federatedStore.GetByKey(key)
if err != nil {
glog.Errorf("Failed to query store while reconciling RSP controller for key %q: %v", key, err)
wrappedErr := errors.Wrapf(err, "Failed to query store while reconciling RSP controller for key %q", key)
runtime.HandleError(wrappedErr)
return false
}
return exist
}
func (p *Plugin) Reconcile(qualifiedName util.QualifiedName, result map[string]int64) error {
fedObject, err := p.federatedTypeClient.Resources(qualifiedName.Namespace).Get(qualifiedName.Name, metav1.GetOptions{})
if err != nil && apierrors.IsNotFound(err) {
// Federated resource has been deleted - no further action required
return nil
}
if err != nil {
return err
}
isDirty := false
newClusterNames := []string{}
for name := range result {
newClusterNames = append(newClusterNames, name)
}
clusterNames, err := util.GetClusterNames(fedObject)
if err != nil {
return err
}
if PlacementUpdateNeeded(clusterNames, newClusterNames) {
if err := util.SetClusterNames(fedObject, newClusterNames); err != nil {
return err
}
isDirty = true
}
overridesMap, err := util.GetOverrides(fedObject)
if err != nil {
return errors.Wrapf(err, "Error reading cluster overrides for %s %q", p.typeConfig.GetFederatedType().Kind, qualifiedName)
}
if OverrideUpdateNeeded(overridesMap, result) {
err := setOverrides(fedObject, overridesMap, result)
if err != nil {
return err
}
isDirty = true
}
if isDirty {
_, err := p.federatedTypeClient.Resources(qualifiedName.Namespace).Update(fedObject, metav1.UpdateOptions{})
if err != nil {
return err
}
}
return nil
}
// These assume that there would be no duplicate clusternames
func PlacementUpdateNeeded(names, newNames []string) bool {
sort.Strings(names)
sort.Strings(newNames)
return !reflect.DeepEqual(names, newNames)
}
func setOverrides(obj *unstructured.Unstructured, overridesMap util.OverridesMap, replicasMap map[string]int64) error {
if overridesMap == nil {
overridesMap = make(util.OverridesMap)
}
updateOverridesMap(overridesMap, replicasMap)
return util.SetOverrides(obj, overridesMap)
}
func updateOverridesMap(overridesMap util.OverridesMap, replicasMap map[string]int64) {
// Remove replicas override for clusters that are not scheduled
for clusterName, clusterOverridesMap := range overridesMap {
if _, ok := replicasMap[clusterName]; !ok {
delete(clusterOverridesMap, replicasPath)
}
}
// Add/update replicas override for clusters that are scheduled
for clusterName, replicas := range replicasMap {
clusterOverridesMap, ok := overridesMap[clusterName]
if !ok {
clusterOverridesMap = make(util.ClusterOverridesMap)
overridesMap[clusterName] = clusterOverridesMap
}
clusterOverridesMap[replicasPath] = replicas
}
}
func OverrideUpdateNeeded(overridesMap util.OverridesMap, result map[string]int64) bool {
resultLen := len(result)
checkLen := 0
for clusterName, clusterOverridesMap := range overridesMap {
for path, rawValue := range clusterOverridesMap {
if path != replicasPath {
continue
}
// The type of the value will be float64 due to how json
// marshalling works for interfaces.
floatValue, ok := rawValue.(float64)
if !ok {
return true
}
value := int64(floatValue)
replicas, ok := result[clusterName]
if !ok || value != int64(replicas) {
return true
}
checkLen += 1
}
}
return checkLen != resultLen
}