-
Notifications
You must be signed in to change notification settings - Fork 828
/
cluster_affinity.go
94 lines (77 loc) · 3.11 KB
/
cluster_affinity.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
/*
Copyright 2021 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 clusteraffinity
import (
"context"
clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/scheduler/framework"
"github.com/karmada-io/karmada/pkg/util"
)
const (
// Name is the name of the plugin used in the plugin registry and configurations.
Name = "ClusterAffinity"
)
// ClusterAffinity is a plugin that checks if a resource selector matches the cluster label.
type ClusterAffinity struct{}
var _ framework.FilterPlugin = &ClusterAffinity{}
var _ framework.ScorePlugin = &ClusterAffinity{}
// New instantiates the clusteraffinity plugin.
func New() (framework.Plugin, error) {
return &ClusterAffinity{}, nil
}
// Name returns the plugin name.
func (p *ClusterAffinity) Name() string {
return Name
}
// Filter checks if the cluster matched the placement cluster affinity constraint.
func (p *ClusterAffinity) Filter(
_ context.Context,
bindingSpec *workv1alpha2.ResourceBindingSpec,
bindingStatus *workv1alpha2.ResourceBindingStatus,
cluster *clusterv1alpha1.Cluster,
) *framework.Result {
var affinity *policyv1alpha1.ClusterAffinity
if bindingSpec.Placement.ClusterAffinity != nil {
affinity = bindingSpec.Placement.ClusterAffinity
} else {
for index, term := range bindingSpec.Placement.ClusterAffinities {
if term.AffinityName == bindingStatus.SchedulerObservedAffinityName {
affinity = &bindingSpec.Placement.ClusterAffinities[index].ClusterAffinity
break
}
}
}
if affinity != nil {
if util.ClusterMatches(cluster, *affinity) {
return framework.NewResult(framework.Success)
}
return framework.NewResult(framework.Unschedulable, "cluster(s) did not match the placement cluster affinity constraint")
}
// If no clusters specified and it is not excluded, mark it matched
return framework.NewResult(framework.Success)
}
// Score calculates the score on the candidate cluster.
func (p *ClusterAffinity) Score(_ context.Context,
_ *workv1alpha2.ResourceBindingSpec, _ *clusterv1alpha1.Cluster) (int64, *framework.Result) {
return framework.MinClusterScore, framework.NewResult(framework.Success)
}
// ScoreExtensions of the Score plugin.
func (p *ClusterAffinity) ScoreExtensions() framework.ScoreExtensions {
return p
}
// NormalizeScore normalizes the score for each candidate cluster.
func (p *ClusterAffinity) NormalizeScore(_ context.Context, _ framework.ClusterScoreList) *framework.Result {
return framework.NewResult(framework.Success)
}