44 "bytes"
55 _ "embed" // nolint
66 "encoding/json"
7+ "fmt"
8+ "strings"
79 "text/template"
810
911 corev1 "k8s.io/api/core/v1"
@@ -16,6 +18,17 @@ import (
1618 cisoperatorapiv1 "github.com/rancher/cis-operator/pkg/apis/cis.cattle.io/v1"
1719)
1820
21+ var requiredPaths = map [string ]string {
22+ "var-rancher" : "/var/lib/rancher" ,
23+ "etc-rancher" : "/etc/rancher" ,
24+ "etc-cni" : "/etc/cni/net.d" ,
25+ "var-cni" : "/var/lib/cni" ,
26+ "var-log" : "/var/log" ,
27+ "run-log" : "/run/log" ,
28+ "etc-kubelet" : "/etc/kubernetes/kubelet" ,
29+ "var-kubelet" : "/var/lib/kubelet" ,
30+ }
31+
1932//go:embed templates/pluginConfig.template
2033var pluginConfigTemplate string
2134
@@ -31,7 +44,8 @@ const (
3144 ConfigFileName = "config.json"
3245)
3346
34- func NewConfigMaps (clusterscan * cisoperatorapiv1.ClusterScan , clusterscanprofile * cisoperatorapiv1.ClusterScanProfile , clusterscanbenchmark * cisoperatorapiv1.ClusterScanBenchmark , _ string , imageConfig * cisoperatorapiv1.ScanImageConfig , configmapsClient wcorev1.ConfigMapController ) (cmMap map [string ]* corev1.ConfigMap , err error ) {
47+ func NewConfigMaps (clusterscan * cisoperatorapiv1.ClusterScan , clusterscanprofile * cisoperatorapiv1.ClusterScanProfile , clusterscanbenchmark * cisoperatorapiv1.ClusterScanBenchmark , _ string , imageConfig * cisoperatorapiv1.ScanImageConfig ,
48+ configmapsClient wcorev1.ConfigMapController , customScanHostPaths []string ) (cmMap map [string ]* corev1.ConfigMap , err error ) {
3549 cmMap = make (map [string ]* corev1.ConfigMap )
3650
3751 configdata := map [string ]interface {}{
@@ -62,6 +76,8 @@ func NewConfigMaps(clusterscan *cisoperatorapiv1.ClusterScan, clusterscanprofile
6276 customBenchmarkConfigMapName = customcm .Name
6377 }
6478
79+ hostPathVolumes , hostPathVolumeMounts := pluginConfigHostPathVolumesData (customScanHostPaths )
80+
6581 plugindata := map [string ]interface {}{
6682 "namespace" : cisoperatorapiv1 .ClusterScanNS ,
6783 "name" : name .SafeConcatName (cisoperatorapiv1 .ClusterScanPluginsConfigMap , clusterscan .Name ),
@@ -74,6 +90,8 @@ func NewConfigMaps(clusterscan *cisoperatorapiv1.ClusterScan, clusterscanprofile
7490 "configDir" : cisoperatorapiv1 .CustomBenchmarkBaseDir ,
7591 "customBenchmarkConfigMapName" : customBenchmarkConfigMapName ,
7692 "customBenchmarkConfigMapData" : customBenchmarkConfigMapData ,
93+ "hostPathVolumes" : hostPathVolumes ,
94+ "hostPathVolumeMounts" : hostPathVolumeMounts ,
7795 }
7896 plugincm , err := generateConfigMap (clusterscan , "pluginConfig.template" , pluginConfigTemplate , plugindata )
7997 if err != nil {
@@ -176,3 +194,40 @@ func getCustomBenchmarkConfigMap(benchmark *cisoperatorapiv1.ClusterScanBenchmar
176194 }
177195 return configmapsClient .Create (& configmapCopy )
178196}
197+
198+ func pluginConfigHostPathVolumesData (customScanHostPaths []string ) ([]* corev1.Volume , []* corev1.VolumeMount ) {
199+ volumes := make ([]* corev1.Volume , 0 , len (requiredPaths )+ len (customScanHostPaths ))
200+ volumeMounts := make ([]* corev1.VolumeMount , 0 , len (requiredPaths )+ len (customScanHostPaths ))
201+ hostPaths := make (map [string ]bool , len (requiredPaths ))
202+
203+ // Add required volumes
204+ for name , path := range requiredPaths {
205+ path = strings .TrimSuffix (path , "/" )
206+ volumes = append (volumes , & corev1.Volume {
207+ Name : name ,
208+ VolumeSource : corev1.VolumeSource {
209+ HostPath : & corev1.HostPathVolumeSource {Path : path },
210+ },
211+ })
212+ volumeMounts = append (volumeMounts , & corev1.VolumeMount {Name : name , MountPath : path , ReadOnly : true })
213+ hostPaths [path ] = true
214+ }
215+
216+ // Add custom volumes if they are not already included
217+ for idx , path := range customScanHostPaths {
218+ if ! hostPaths [path ] {
219+ path = strings .TrimSuffix (path , "/" )
220+ name := fmt .Sprintf ("custom-volume-%d" , idx )
221+ volumes = append (volumes , & corev1.Volume {
222+ Name : name ,
223+ VolumeSource : corev1.VolumeSource {
224+ HostPath : & corev1.HostPathVolumeSource {Path : path },
225+ },
226+ })
227+ volumeMounts = append (volumeMounts , & corev1.VolumeMount {Name : name , MountPath : path , ReadOnly : true })
228+ hostPaths [path ] = true
229+ }
230+ }
231+
232+ return volumes , volumeMounts
233+ }
0 commit comments