forked from openshift/origin
/
simulator.go
471 lines (407 loc) · 15 KB
/
simulator.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
Copyright 2017 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 framework
import (
"fmt"
"io/ioutil"
"os"
"sync"
"time"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/informers"
storageinformers "k8s.io/client-go/informers/storage/v1"
externalclientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/pkg/apis/componentconfig"
//"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/features"
sapps "k8s.io/kubernetes/plugin/cmd/kube-scheduler/app"
"k8s.io/kubernetes/plugin/pkg/scheduler"
schedulerapi "k8s.io/kubernetes/plugin/pkg/scheduler/api"
latestschedulerapi "k8s.io/kubernetes/plugin/pkg/scheduler/api/latest"
"k8s.io/kubernetes/plugin/pkg/scheduler/core"
"k8s.io/kubernetes/plugin/pkg/scheduler/factory"
// register algorithm providers
_ "k8s.io/kubernetes/plugin/pkg/scheduler/algorithmprovider"
ccapi "github.com/kubernetes-incubator/cluster-capacity/pkg/api"
"github.com/kubernetes-incubator/cluster-capacity/pkg/framework/record"
"github.com/kubernetes-incubator/cluster-capacity/pkg/framework/restclient/external"
"github.com/kubernetes-incubator/cluster-capacity/pkg/framework/store"
"github.com/kubernetes-incubator/cluster-capacity/pkg/framework/strategy"
)
const (
podProvisioner = "cc.kubernetes.io/provisioned-by"
)
type ClusterCapacity struct {
// caches modified by emulation strategy
resourceStore store.ResourceStore
// emulation strategy
strategy strategy.Strategy
externalkubeclient *externalclientset.Clientset
informerFactory informers.SharedInformerFactory
// schedulers
schedulers map[string]*scheduler.Scheduler
schedulerConfigs map[string]*scheduler.Config
defaultSchedulerName string
// pod to schedule
simulatedPod *v1.Pod
lastSimulatedPod *v1.Pod
maxSimulated int
simulated int
status Status
report *ClusterCapacityReview
// analysis limitation
informerStopCh chan struct{}
// stop the analysis
stop chan struct{}
stopMux sync.RWMutex
stopped bool
closedMux sync.RWMutex
closed bool
}
// capture all scheduled pods with reason why the analysis could not continue
type Status struct {
Pods []*v1.Pod
StopReason string
}
func (c *ClusterCapacity) Report() *ClusterCapacityReview {
if c.report == nil {
// Preparation before pod sequence scheduling is done
pods := make([]*v1.Pod, 0)
pods = append(pods, c.simulatedPod)
c.report = GetReport(pods, c.status)
c.report.Spec.Replicas = int32(c.maxSimulated)
}
return c.report
}
func (c *ClusterCapacity) SyncWithClient(client externalclientset.Interface) error {
for _, resource := range c.resourceStore.Resources() {
listWatcher := cache.NewListWatchFromClient(client.Core().RESTClient(), resource.String(), metav1.NamespaceAll, fields.ParseSelectorOrDie(""))
options := metav1.ListOptions{ResourceVersion: "0"}
list, err := listWatcher.List(options)
if err != nil {
return fmt.Errorf("Failed to list objects: %v", err)
}
listMetaInterface, err := meta.ListAccessor(list)
if err != nil {
return fmt.Errorf("Unable to understand list result %#v: %v", list, err)
}
resourceVersion := listMetaInterface.GetResourceVersion()
items, err := meta.ExtractList(list)
if err != nil {
return fmt.Errorf("Unable to understand list result %#v (%v)", list, err)
}
found := make([]interface{}, 0, len(items))
for _, item := range items {
found = append(found, item)
}
err = c.resourceStore.Replace(resource, found, resourceVersion)
if err != nil {
return fmt.Errorf("Unable to store %s list result: %v", resource, err)
}
}
return nil
}
func (c *ClusterCapacity) SyncWithStore(resourceStore store.ResourceStore) error {
for _, resource := range resourceStore.Resources() {
err := c.resourceStore.Replace(resource, resourceStore.List(resource), "0")
if err != nil {
return fmt.Errorf("Resource replace error: %v\n", err)
}
}
return nil
}
func (c *ClusterCapacity) Bind(binding *v1.Binding, schedulerName string) error {
// run the pod through strategy
key := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: binding.Name, Namespace: binding.Namespace},
}
pod, exists, err := c.resourceStore.Get(ccapi.Pods, runtime.Object(key))
if err != nil {
return fmt.Errorf("Unable to bind: %v", err)
}
if !exists {
return fmt.Errorf("Unable to bind, pod %v not found", pod)
}
updatedPod := *pod.(*v1.Pod)
updatedPod.Spec.NodeName = binding.Target.Name
updatedPod.Status.Phase = v1.PodRunning
// TODO(jchaloup): rename Add to Update as this actually updates the scheduled pod
if err := c.strategy.Add(&updatedPod); err != nil {
return fmt.Errorf("Unable to recompute new cluster state: %v", err)
}
c.status.Pods = append(c.status.Pods, &updatedPod)
go func() {
<-c.schedulerConfigs[schedulerName].Recorder.(*record.Recorder).Events
}()
if c.maxSimulated > 0 && c.simulated >= c.maxSimulated {
c.status.StopReason = fmt.Sprintf("LimitReached: Maximum number of pods simulated: %v", c.maxSimulated)
c.Close()
c.stop <- struct{}{}
return nil
}
// all good, create another pod
if err := c.nextPod(); err != nil {
return fmt.Errorf("Unable to create next pod to schedule: %v", err)
}
return nil
}
func (c *ClusterCapacity) Close() {
c.closedMux.Lock()
defer c.closedMux.Unlock()
if c.closed {
return
}
for _, name := range c.schedulerConfigs {
close(name.StopEverything)
}
close(c.informerStopCh)
c.closed = true
}
func (c *ClusterCapacity) Update(pod *v1.Pod, podCondition *v1.PodCondition, schedulerName string) error {
stop := podCondition.Type == v1.PodScheduled && podCondition.Status == v1.ConditionFalse && podCondition.Reason == "Unschedulable"
// Only for pending pods provisioned by cluster-capacity
if stop && metav1.HasAnnotation(pod.ObjectMeta, podProvisioner) {
c.status.StopReason = fmt.Sprintf("%v: %v", podCondition.Reason, podCondition.Message)
c.Close()
// The Update function can be run more than once before any corresponding
// scheduler is closed. The behaviour is implementation specific
c.stopMux.Lock()
defer c.stopMux.Unlock()
c.stopped = true
c.stop <- struct{}{}
}
return nil
}
func (c *ClusterCapacity) nextPod() error {
pod := v1.Pod{}
pod = *c.simulatedPod.DeepCopy()
// reset any node designation set
pod.Spec.NodeName = ""
// use simulated pod name with an index to construct the name
pod.ObjectMeta.Name = fmt.Sprintf("%v-%v", c.simulatedPod.Name, c.simulated)
// Add pod provisioner annotation
if pod.ObjectMeta.Annotations == nil {
pod.ObjectMeta.Annotations = map[string]string{}
}
// Stores the scheduler name
pod.ObjectMeta.Annotations[podProvisioner] = c.defaultSchedulerName
c.simulated++
c.lastSimulatedPod = &pod
return c.resourceStore.Add(ccapi.Pods, runtime.Object(&pod))
}
func (c *ClusterCapacity) Run() error {
// Start all informers.
c.informerFactory.Start(c.informerStopCh)
c.informerFactory.WaitForCacheSync(c.informerStopCh)
// TODO(jchaloup): remove all pods that are not scheduled yet
for _, scheduler := range c.schedulers {
scheduler.Run()
}
// wait some time before at least nodes are populated
// TODO(jchaloup); find a better way how to do this or at least decrease it to <100ms
time.Sleep(100 * time.Millisecond)
// create the first simulated pod
err := c.nextPod()
if err != nil {
c.Close()
close(c.stop)
return fmt.Errorf("Unable to create next pod to schedule: %v", err)
}
<-c.stop
close(c.stop)
return nil
}
type localBinderPodConditionUpdater struct {
SchedulerName string
C *ClusterCapacity
}
func (b *localBinderPodConditionUpdater) Bind(binding *v1.Binding) error {
return b.C.Bind(binding, b.SchedulerName)
}
func (b *localBinderPodConditionUpdater) Update(pod *v1.Pod, podCondition *v1.PodCondition) error {
return b.C.Update(pod, podCondition, b.SchedulerName)
}
func (c *ClusterCapacity) createScheduler(s *sapps.SchedulerServer) (*scheduler.Scheduler, error) {
c.informerFactory = s.InformerFactory
s.Recorder = record.NewRecorder(10)
schedulerConfig, err := SchedulerConfigLocal(s)
if err != nil {
return nil, err
}
// Replace the binder with simulator pod counter
lbpcu := &localBinderPodConditionUpdater{
SchedulerName: s.SchedulerName,
C: c,
}
schedulerConfig.Binder = lbpcu
schedulerConfig.PodConditionUpdater = lbpcu
// pending merge of https://github.com/kubernetes/kubernetes/pull/44115
// we wrap how error handling is done to avoid extraneous logging
errorFn := schedulerConfig.Error
wrappedErrorFn := func(pod *v1.Pod, err error) {
if _, ok := err.(*core.FitError); !ok {
errorFn(pod, err)
}
}
schedulerConfig.Error = wrappedErrorFn
// Create the scheduler.
scheduler := scheduler.NewFromConfig(schedulerConfig)
return scheduler, nil
}
// TODO(avesh): enable when support for multiple schedulers is added.
/*func (c *ClusterCapacity) AddScheduler(s *sapps.SchedulerServer) error {
scheduler, err := c.createScheduler(s)
if err != nil {
return err
}
c.schedulers[s.SchedulerName] = scheduler
c.schedulerConfigs[s.SchedulerName] = scheduler.Config()
return nil
}*/
// Create new cluster capacity analysis
// The analysis is completely independent of apiserver so no need
// for kubeconfig nor for apiserver url
func New(schedServer *sapps.SchedulerServer, simulatedPod *v1.Pod, maxPods int) (*ClusterCapacity, error) {
resourceStore := store.NewResourceStore()
restClient := external.NewRESTClient(resourceStore, "core")
cc := &ClusterCapacity{
resourceStore: resourceStore,
strategy: strategy.NewPredictiveStrategy(resourceStore),
externalkubeclient: externalclientset.New(restClient),
simulatedPod: simulatedPod,
simulated: 0,
maxSimulated: maxPods,
}
for _, resource := range resourceStore.Resources() {
// The resource variable would be shared among all [Add|Update|Delete]Func functions
// and resource would be set to the last item in resources list.
// Thus, it needs to be stored to a local variable in each iteration.
rt := resource
resourceStore.RegisterEventHandler(rt, cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
restClient.EmitObjectWatchEvent(rt, watch.Added, obj.(runtime.Object))
},
UpdateFunc: func(oldObj, newObj interface{}) {
restClient.EmitObjectWatchEvent(rt, watch.Modified, newObj.(runtime.Object))
},
DeleteFunc: func(obj interface{}) {
restClient.EmitObjectWatchEvent(rt, watch.Deleted, obj.(runtime.Object))
},
})
}
// Replace InformerFactory
schedServer.InformerFactory = informers.NewSharedInformerFactory(cc.externalkubeclient, 0)
schedServer.Client = cc.externalkubeclient
cc.schedulers = make(map[string]*scheduler.Scheduler)
cc.schedulerConfigs = make(map[string]*scheduler.Config)
scheduler, err := cc.createScheduler(schedServer)
if err != nil {
return nil, err
}
cc.schedulers[schedServer.SchedulerName] = scheduler
cc.schedulerConfigs[schedServer.SchedulerName] = scheduler.Config()
cc.defaultSchedulerName = schedServer.SchedulerName
cc.stop = make(chan struct{})
cc.informerStopCh = make(chan struct{})
return cc, nil
}
// SchedulerConfig creates the scheduler configuration.
func SchedulerConfigLocal(s *sapps.SchedulerServer) (*scheduler.Config, error) {
var storageClassInformer storageinformers.StorageClassInformer
if utilfeature.DefaultFeatureGate.Enabled(features.VolumeScheduling) {
storageClassInformer = s.InformerFactory.Storage().V1().StorageClasses()
}
fakeClient := fake.NewSimpleClientset()
fakeInformerFactory := informers.NewSharedInformerFactory(fakeClient, 0)
// Set up the configurator which can create schedulers from configs.
configurator := factory.NewConfigFactory(
s.SchedulerName,
s.Client,
s.InformerFactory.Core().V1().Nodes(),
s.InformerFactory.Core().V1().Pods(),
s.InformerFactory.Core().V1().PersistentVolumes(),
s.InformerFactory.Core().V1().PersistentVolumeClaims(),
fakeInformerFactory.Core().V1().ReplicationControllers(),
fakeInformerFactory.Extensions().V1beta1().ReplicaSets(),
fakeInformerFactory.Apps().V1beta1().StatefulSets(),
s.InformerFactory.Core().V1().Services(),
fakeInformerFactory.Policy().V1beta1().PodDisruptionBudgets(),
storageClassInformer,
s.HardPodAffinitySymmetricWeight,
utilfeature.DefaultFeatureGate.Enabled(features.EnableEquivalenceClassCache),
)
source := s.AlgorithmSource
var config *scheduler.Config
switch {
case source.Provider != nil:
// Create the config from a named algorithm provider.
sc, err := configurator.CreateFromProvider(*source.Provider)
if err != nil {
return nil, fmt.Errorf("couldn't create scheduler using provider %q: %v", *source.Provider, err)
}
config = sc
case source.Policy != nil:
// Create the config from a user specified policy source.
policy := &schedulerapi.Policy{}
switch {
case source.Policy.File != nil:
// Use a policy serialized in a file.
policyFile := source.Policy.File.Path
_, err := os.Stat(policyFile)
if err != nil {
return nil, fmt.Errorf("missing policy config file %s", policyFile)
}
data, err := ioutil.ReadFile(policyFile)
if err != nil {
return nil, fmt.Errorf("couldn't read policy config: %v", err)
}
err = runtime.DecodeInto(latestschedulerapi.Codec, []byte(data), policy)
if err != nil {
return nil, fmt.Errorf("invalid policy: %v", err)
}
case source.Policy.ConfigMap != nil:
// Use a policy serialized in a config map value.
policyRef := source.Policy.ConfigMap
policyConfigMap, err := s.Client.CoreV1().ConfigMaps(policyRef.Namespace).Get(policyRef.Name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("couldn't get policy config map %s/%s: %v", policyRef.Namespace, policyRef.Name, err)
}
data, found := policyConfigMap.Data[componentconfig.SchedulerPolicyConfigMapKey]
if !found {
return nil, fmt.Errorf("missing policy config map value at key %q", componentconfig.SchedulerPolicyConfigMapKey)
}
err = runtime.DecodeInto(latestschedulerapi.Codec, []byte(data), policy)
if err != nil {
return nil, fmt.Errorf("invalid policy: %v", err)
}
}
sc, err := configurator.CreateFromConfig(*policy)
if err != nil {
return nil, fmt.Errorf("couldn't create scheduler from policy: %v", err)
}
config = sc
default:
return nil, fmt.Errorf("unsupported algorithm source: %v", source)
}
// Additional tweaks to the config produced by the configurator.
config.Recorder = s.Recorder
return config, nil
}