@@ -22,7 +22,6 @@ import (
2222 "encoding/json"
2323 "fmt"
2424 "os"
25- "sort"
2625 "strings"
2726
2827 errs "github.com/pkg/errors"
@@ -51,7 +50,7 @@ import (
5150)
5251
5352var (
54- webhooks = map [string ]( string ) {
53+ webhooks = map [string ]string {
5554 constants .InjectorWebHookName : constants .InjectorWebHookPath ,
5655 constants .OperatorWebHookName : constants .OperatorWebHookPath ,
5756 }
@@ -162,29 +161,33 @@ func formatJSON(str string) (string, error) {
162161 return prettyJSON .String (), nil
163162}
164163
164+ // GetDefaultNodeSelector return a nodeSelector with worker and linux os
165165func GetDefaultNodeSelector () map [string ]string {
166- return map [string ]string {"node-role.kubernetes.io/worker" : "" ,
167- "kubernetes.io/os" : "linux" }
166+ return map [string ]string {
167+ "node-role.kubernetes.io/worker" : "" ,
168+ "kubernetes.io/os" : "linux" ,
169+ }
168170}
169171
170- // hasNoValidPolicy returns true if no SriovNetworkNodePolicy
171- // or only the (deprecated) "default" policy is present
172- func hasNoValidPolicy (pl []sriovnetworkv1.SriovNetworkNodePolicy ) bool {
173- switch len (pl ) {
174- case 0 :
175- return true
176- case 1 :
177- return pl [0 ].Name == constants .DefaultPolicyName
178- default :
179- return false
172+ // GetDefaultNodeSelectorForDevicePlugin return a nodeSelector with worker linux os
173+ // and the enabled sriov device plugin
174+ func GetNodeSelectorForDevicePlugin (dc * sriovnetworkv1.SriovOperatorConfig ) map [string ]string {
175+ if len (dc .Spec .ConfigDaemonNodeSelector ) == 0 {
176+ return map [string ]string {
177+ "kubernetes.io/os" : "linux" ,
178+ constants .SriovDevicePluginLabel : constants .SriovDevicePluginLabelEnabled ,
179+ }
180180 }
181+
182+ tmp := dc .Spec .DeepCopy ()
183+ tmp .ConfigDaemonNodeSelector [constants .SriovDevicePluginLabel ] = constants .SriovDevicePluginLabelEnabled
184+ return tmp .ConfigDaemonNodeSelector
181185}
182186
183187func syncPluginDaemonObjs (ctx context.Context ,
184188 client k8sclient.Client ,
185189 scheme * runtime.Scheme ,
186- dc * sriovnetworkv1.SriovOperatorConfig ,
187- pl * sriovnetworkv1.SriovNetworkNodePolicyList ) error {
190+ dc * sriovnetworkv1.SriovOperatorConfig ) error {
188191 logger := log .Log .WithName ("syncPluginDaemonObjs" )
189192 logger .V (1 ).Info ("Start to sync sriov daemons objects" )
190193
@@ -195,42 +198,17 @@ func syncPluginDaemonObjs(ctx context.Context,
195198 data .Data ["ReleaseVersion" ] = os .Getenv ("RELEASEVERSION" )
196199 data .Data ["ResourcePrefix" ] = vars .ResourcePrefix
197200 data .Data ["ImagePullSecrets" ] = GetImagePullSecrets ()
198- data .Data ["NodeSelectorField" ] = GetDefaultNodeSelector ( )
201+ data .Data ["NodeSelectorField" ] = GetNodeSelectorForDevicePlugin ( dc )
199202 data .Data ["UseCDI" ] = dc .Spec .UseCDI
200203 objs , err := renderDsForCR (constants .PluginPath , & data )
201204 if err != nil {
202205 logger .Error (err , "Fail to render SR-IoV manifests" )
203206 return err
204207 }
205208
206- if hasNoValidPolicy (pl .Items ) {
207- for _ , obj := range objs {
208- err := deleteK8sResource (ctx , client , obj )
209- if err != nil {
210- return err
211- }
212- }
213- return nil
214- }
215-
216209 // Sync DaemonSets
217210 for _ , obj := range objs {
218- if obj .GetKind () == constants .DaemonSet && len (dc .Spec .ConfigDaemonNodeSelector ) > 0 {
219- scheme := kscheme .Scheme
220- ds := & appsv1.DaemonSet {}
221- err = scheme .Convert (obj , ds , nil )
222- if err != nil {
223- logger .Error (err , "Fail to convert to DaemonSet" )
224- return err
225- }
226- ds .Spec .Template .Spec .NodeSelector = dc .Spec .ConfigDaemonNodeSelector
227- err = scheme .Convert (ds , obj , nil )
228- if err != nil {
229- logger .Error (err , "Fail to convert to Unstructured" )
230- return err
231- }
232- }
233- err = syncDsObject (ctx , client , scheme , dc , pl , obj )
211+ err = syncDsObject (ctx , client , scheme , dc , obj )
234212 if err != nil {
235213 logger .Error (err , "Couldn't sync SR-IoV daemons objects" )
236214 return err
@@ -240,14 +218,7 @@ func syncPluginDaemonObjs(ctx context.Context,
240218 return nil
241219}
242220
243- func deleteK8sResource (ctx context.Context , client k8sclient.Client , in * uns.Unstructured ) error {
244- if err := apply .DeleteObject (ctx , client , in ); err != nil {
245- return fmt .Errorf ("failed to delete object %v with err: %v" , in , err )
246- }
247- return nil
248- }
249-
250- func syncDsObject (ctx context.Context , client k8sclient.Client , scheme * runtime.Scheme , dc * sriovnetworkv1.SriovOperatorConfig , pl * sriovnetworkv1.SriovNetworkNodePolicyList , obj * uns.Unstructured ) error {
221+ func syncDsObject (ctx context.Context , client k8sclient.Client , scheme * runtime.Scheme , dc * sriovnetworkv1.SriovOperatorConfig , obj * uns.Unstructured ) error {
251222 logger := log .Log .WithName ("syncDsObject" )
252223 kind := obj .GetKind ()
253224 logger .V (1 ).Info ("Start to sync Objects" , "Kind" , kind )
@@ -267,7 +238,7 @@ func syncDsObject(ctx context.Context, client k8sclient.Client, scheme *runtime.
267238 logger .Error (err , "Fail to convert to DaemonSet" )
268239 return err
269240 }
270- err = syncDaemonSet (ctx , client , scheme , dc , pl , ds )
241+ err = syncDaemonSet (ctx , client , scheme , dc , ds )
271242 if err != nil {
272243 logger .Error (err , "Fail to sync DaemonSet" , "Namespace" , ds .Namespace , "Name" , ds .Name )
273244 return err
@@ -276,54 +247,6 @@ func syncDsObject(ctx context.Context, client k8sclient.Client, scheme *runtime.
276247 return nil
277248}
278249
279- func setDsNodeAffinity (pl * sriovnetworkv1.SriovNetworkNodePolicyList , ds * appsv1.DaemonSet ) error {
280- terms := nodeSelectorTermsForPolicyList (pl .Items )
281- if len (terms ) > 0 {
282- ds .Spec .Template .Spec .Affinity = & corev1.Affinity {
283- NodeAffinity : & corev1.NodeAffinity {
284- RequiredDuringSchedulingIgnoredDuringExecution : & corev1.NodeSelector {
285- NodeSelectorTerms : terms ,
286- },
287- },
288- }
289- }
290- return nil
291- }
292-
293- func nodeSelectorTermsForPolicyList (policies []sriovnetworkv1.SriovNetworkNodePolicy ) []corev1.NodeSelectorTerm {
294- terms := []corev1.NodeSelectorTerm {}
295- for _ , p := range policies {
296- // Note(adrianc): default policy is deprecated and ignored.
297- if p .Name == constants .DefaultPolicyName {
298- continue
299- }
300-
301- if len (p .Spec .NodeSelector ) == 0 {
302- continue
303- }
304- expressions := []corev1.NodeSelectorRequirement {}
305- for k , v := range p .Spec .NodeSelector {
306- exp := corev1.NodeSelectorRequirement {
307- Operator : corev1 .NodeSelectorOpIn ,
308- Key : k ,
309- Values : []string {v },
310- }
311- expressions = append (expressions , exp )
312- }
313- // sorting is needed to keep the daemon spec stable.
314- // the items are popped in a random order from the map
315- sort .Slice (expressions , func (i , j int ) bool {
316- return expressions [i ].Key < expressions [j ].Key
317- })
318- nodeSelector := corev1.NodeSelectorTerm {
319- MatchExpressions : expressions ,
320- }
321- terms = append (terms , nodeSelector )
322- }
323-
324- return terms
325- }
326-
327250// renderDsForCR returns a busybox pod with the same name/namespace as the cr
328251func renderDsForCR (path string , data * render.RenderData ) ([]* uns.Unstructured , error ) {
329252 logger := log .Log .WithName ("renderDsForCR" )
@@ -336,16 +259,11 @@ func renderDsForCR(path string, data *render.RenderData) ([]*uns.Unstructured, e
336259 return objs , nil
337260}
338261
339- func syncDaemonSet (ctx context.Context , client k8sclient.Client , scheme * runtime.Scheme , dc * sriovnetworkv1.SriovOperatorConfig , pl * sriovnetworkv1. SriovNetworkNodePolicyList , in * appsv1.DaemonSet ) error {
262+ func syncDaemonSet (ctx context.Context , client k8sclient.Client , scheme * runtime.Scheme , dc * sriovnetworkv1.SriovOperatorConfig , in * appsv1.DaemonSet ) error {
340263 logger := log .Log .WithName ("syncDaemonSet" )
341264 logger .V (1 ).Info ("Start to sync DaemonSet" , "Namespace" , in .Namespace , "Name" , in .Name )
342265 var err error
343266
344- if pl != nil {
345- if err = setDsNodeAffinity (pl , in ); err != nil {
346- return err
347- }
348- }
349267 if err = controllerutil .SetControllerReference (dc , in , scheme ); err != nil {
350268 return err
351269 }
0 commit comments