@@ -81,7 +81,7 @@ func (c *controller) NewDaemonSet(rawObj runtime.Object) *apps.DaemonSet {
8181 }
8282
8383 yes := true
84- return & apps.DaemonSet {
84+ daemonSet := apps.DaemonSet {
8585 ObjectMeta : metav1.ObjectMeta {
8686 Namespace : devicePlugin .Namespace ,
8787 GenerateName : devicePlugin .Name + "-" ,
@@ -170,6 +170,59 @@ func (c *controller) NewDaemonSet(rawObj runtime.Object) *apps.DaemonSet {
170170 },
171171 },
172172 }
173+ // add the optional InitImage
174+ if devicePlugin .Spec .InitImage != "" {
175+ setInitContainer (& daemonSet .Spec .Template .Spec , devicePlugin .Spec .InitImage )
176+ }
177+ return & daemonSet
178+ }
179+
180+ func setInitContainer (spec * v1.PodSpec , imageName string ) {
181+ yes := true
182+ spec .InitContainers = []v1.Container {
183+ {
184+ Image : imageName ,
185+ ImagePullPolicy : "IfNotPresent" ,
186+ Name : "intel-gpu-initcontainer" ,
187+ SecurityContext : & v1.SecurityContext {
188+ ReadOnlyRootFilesystem : & yes ,
189+ },
190+ VolumeMounts : []v1.VolumeMount {
191+ {
192+ MountPath : "/etc/kubernetes/node-feature-discovery/source.d/" ,
193+ Name : "nfd-source-hooks" ,
194+ },
195+ },
196+ }}
197+ directoryOrCreate := v1 .HostPathDirectoryOrCreate
198+ missing := true
199+ for _ , vol := range spec .Volumes {
200+ if vol .Name == "nfd-source-hooks" {
201+ missing = false
202+ break
203+ }
204+ }
205+ if missing {
206+ spec .Volumes = append (spec .Volumes , v1.Volume {
207+ Name : "nfd-source-hooks" ,
208+ VolumeSource : v1.VolumeSource {
209+ HostPath : & v1.HostPathVolumeSource {
210+ Path : "/etc/kubernetes/node-feature-discovery/source.d/" ,
211+ Type : & directoryOrCreate ,
212+ },
213+ },
214+ })
215+ }
216+ }
217+
218+ func removeVolume (volumes []v1.Volume , name string ) []v1.Volume {
219+ newVolumes := []v1.Volume {}
220+ for _ , volume := range volumes {
221+ if volume .Name != name {
222+ newVolumes = append (newVolumes , volume )
223+ }
224+ }
225+ return newVolumes
173226}
174227
175228func (c * controller ) UpdateDaemonSet (rawObj runtime.Object , ds * apps.DaemonSet ) (updated bool ) {
@@ -180,6 +233,17 @@ func (c *controller) UpdateDaemonSet(rawObj runtime.Object, ds *apps.DaemonSet)
180233 updated = true
181234 }
182235
236+ if dp .Spec .InitImage == "" {
237+ if ds .Spec .Template .Spec .InitContainers != nil {
238+ ds .Spec .Template .Spec .InitContainers = nil
239+ ds .Spec .Template .Spec .Volumes = removeVolume (ds .Spec .Template .Spec .Volumes , "nfd-source-hooks" )
240+ updated = true
241+ }
242+ } else {
243+ setInitContainer (& ds .Spec .Template .Spec , dp .Spec .InitImage )
244+ updated = true
245+ }
246+
183247 if dp .Spec .NodeSelector == nil {
184248 dp .Spec .NodeSelector = map [string ]string {"kubernetes.io/arch" : "amd64" }
185249 } else {
0 commit comments