/
plugin.go
118 lines (99 loc) · 3.98 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
/*
* Copyright 2020 The Multicluster-Scheduler 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 candidate
import (
"context"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"admiralty.io/multicluster-scheduler/pkg/common"
"admiralty.io/multicluster-scheduler/pkg/generated/clientset/versioned"
)
type Plugin struct {
handle framework.FrameworkHandle
client versioned.Interface
}
var _ framework.PreFilterPlugin = &Plugin{}
var _ framework.ReservePlugin = &Plugin{}
var _ framework.PreBindPlugin = &Plugin{}
// Name is the name of the plugin used in the plugin registry and configurations.
const Name = "candidate"
// Name returns name of the plugin. It is used in logs, etc.
func (pl *Plugin) Name() string {
return Name
}
func (pl *Plugin) PreFilter(ctx context.Context, state *framework.CycleState, p *v1.Pod) *framework.Status {
// reset annotations
patch := []byte(`{"metadata":{"annotations":{
"` + common.AnnotationKeyIsReserved + `":null,
"` + common.AnnotationKeyIsAllowed + `":null}}}`)
if _, err := pl.client.MulticlusterV1alpha1().PodChaperons(p.Namespace).Patch(ctx, p.Name, types.MergePatchType, patch, metav1.PatchOptions{}); err != nil {
return framework.NewStatus(framework.Error, err.Error())
}
return nil
}
func (pl *Plugin) PreFilterExtensions() framework.PreFilterExtensions {
return nil
}
func (pl *Plugin) Reserve(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) *framework.Status {
patch := []byte(`{"metadata":{"annotations":{"` + common.AnnotationKeyIsReserved + `":"true"}}}`)
if _, err := pl.client.MulticlusterV1alpha1().PodChaperons(p.Namespace).Patch(ctx, p.Name, types.MergePatchType, patch, metav1.PatchOptions{}); err != nil {
return framework.NewStatus(framework.Error, err.Error())
}
return nil
}
const waitDuration = 30 * time.Second
func (pl *Plugin) PreBind(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) *framework.Status {
ctx, cancel := context.WithTimeout(ctx, waitDuration)
defer cancel()
// wait until pod is allowed, if ever
if err := wait.PollImmediateUntil(time.Second, func() (bool, error) {
return pl.isAllowed(ctx, p)
}, ctx.Done()); err != nil {
// condition func doesn't throw, i.e., ctx timed out, pod was never allowed
return framework.NewStatus(framework.Error, err.Error())
}
return nil
}
func (pl *Plugin) isAllowed(ctx context.Context, p *v1.Pod) (bool, error) {
pod, err := pl.client.MulticlusterV1alpha1().PodChaperons(p.Namespace).Get(ctx, p.Name, metav1.GetOptions{})
if err != nil {
// TODO handle retriable vs. not retriable, esp. not found (we assume retriable for now)
// TODO log
return false, nil
}
if _, ok := pod.Annotations[common.AnnotationKeyIsAllowed]; !ok {
// pod not allowed (yet?)
klog.V(1).Infof("candidate %s is not allowed", pod.Name)
return false, nil
}
klog.V(1).Infof("candidate %s is allowed", pod.Name)
return true, nil
}
// New initializes a new plugin and returns it.
func New(_ *runtime.Unknown, h framework.FrameworkHandle) (framework.Plugin, error) {
cfg := config.GetConfigOrDie()
client, err := versioned.NewForConfig(cfg)
utilruntime.Must(err)
return &Plugin{handle: h, client: client}, nil
}