@@ -55,11 +55,14 @@ func AddHostnamesToAffinity(hostSelector string, hostNames []string, targetAffin
55
55
Values : hostNames ,
56
56
}
57
57
58
+ const affinityWeight = 50
59
+
58
60
sourceAffinity := & apiv1.Affinity {
59
61
NodeAffinity : & apiv1.NodeAffinity {
60
- RequiredDuringSchedulingIgnoredDuringExecution : & apiv1.NodeSelector {
61
- NodeSelectorTerms : []apiv1.NodeSelectorTerm {
62
- {
62
+ PreferredDuringSchedulingIgnoredDuringExecution : []apiv1.PreferredSchedulingTerm {
63
+ {
64
+ Weight : affinityWeight ,
65
+ Preference : apiv1.NodeSelectorTerm {
63
66
MatchExpressions : []apiv1.NodeSelectorRequirement {
64
67
nodeSelectorRequirement ,
65
68
},
@@ -79,43 +82,38 @@ func AddHostnamesToAffinity(hostSelector string, hostNames []string, targetAffin
79
82
return targetAffinity
80
83
}
81
84
82
- targetExecution := targetAffinity .NodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution
83
- sourceExecution := sourceAffinity .NodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution
85
+ targetExecution := targetAffinity .NodeAffinity .PreferredDuringSchedulingIgnoredDuringExecution
84
86
85
87
if targetExecution == nil {
86
- targetAffinity .NodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution =
87
- sourceAffinity .NodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution
88
+ targetAffinity .NodeAffinity .PreferredDuringSchedulingIgnoredDuringExecution =
89
+ sourceAffinity .NodeAffinity .PreferredDuringSchedulingIgnoredDuringExecution
88
90
return targetAffinity
89
91
}
90
92
91
- if len (targetExecution . NodeSelectorTerms ) == 0 {
92
- targetAffinity .NodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution . NodeSelectorTerms =
93
- sourceAffinity .NodeAffinity .RequiredDuringSchedulingIgnoredDuringExecution . NodeSelectorTerms
93
+ if len (targetExecution ) == 0 {
94
+ targetAffinity .NodeAffinity .PreferredDuringSchedulingIgnoredDuringExecution =
95
+ sourceAffinity .NodeAffinity .PreferredDuringSchedulingIgnoredDuringExecution
94
96
return targetAffinity
95
97
}
96
98
97
99
// find if specific NodeSelectorTerm exists and append
98
- for i := range targetExecution .NodeSelectorTerms {
99
- if len (targetExecution .NodeSelectorTerms [i ].MatchExpressions ) == 0 {
100
- targetExecution .NodeSelectorTerms [i ].MatchExpressions =
101
- append (targetExecution .NodeSelectorTerms [i ].MatchExpressions , sourceExecution .NodeSelectorTerms [0 ].MatchExpressions [0 ])
102
- return targetAffinity
103
- }
104
-
105
- for j := range targetExecution .NodeSelectorTerms [i ].MatchExpressions {
106
- if targetExecution .NodeSelectorTerms [i ].MatchExpressions [j ].Key == hostSelector &&
107
- targetExecution .NodeSelectorTerms [i ].MatchExpressions [j ].Operator == apiv1 .NodeSelectorOpNotIn {
108
- targetExecution .NodeSelectorTerms [i ].MatchExpressions [j ].Values =
109
- append (targetExecution .NodeSelectorTerms [i ].MatchExpressions [j ].Values , hostNames ... )
110
- targetExecution .NodeSelectorTerms [i ].MatchExpressions [j ].Values =
111
- RemoveDuplicates (targetExecution .NodeSelectorTerms [i ].MatchExpressions [j ].Values )
112
- return targetAffinity
100
+ for i := range targetExecution {
101
+ if targetExecution [i ].Weight == affinityWeight {
102
+ for j := range targetExecution [i ].Preference .MatchExpressions {
103
+ if targetExecution [i ].Preference .MatchExpressions [j ].Key == hostSelector &&
104
+ targetExecution [i ].Preference .MatchExpressions [j ].Operator == apiv1 .NodeSelectorOpNotIn {
105
+ targetExecution [i ].Preference .MatchExpressions [j ].Values =
106
+ append (targetExecution [i ].Preference .MatchExpressions [j ].Values , hostNames ... )
107
+ targetExecution [i ].Preference .MatchExpressions [j ].Values =
108
+ RemoveDuplicates (targetExecution [i ].Preference .MatchExpressions [j ].Values )
109
+ return targetAffinity
110
+ }
113
111
}
114
112
}
115
113
}
116
114
117
- targetExecution . NodeSelectorTerms [ 0 ]. MatchExpressions =
118
- append (targetExecution . NodeSelectorTerms [ 0 ]. MatchExpressions , nodeSelectorRequirement )
115
+ targetAffinity . NodeAffinity . PreferredDuringSchedulingIgnoredDuringExecution =
116
+ append (targetAffinity . NodeAffinity . PreferredDuringSchedulingIgnoredDuringExecution , sourceAffinity . NodeAffinity . PreferredDuringSchedulingIgnoredDuringExecution [ 0 ] )
119
117
120
118
return targetAffinity
121
119
}
0 commit comments