diff --git a/pkg/kor/confimgmaps.go b/pkg/kor/confimgmaps.go index 380f8c25..9fbf2b67 100644 --- a/pkg/kor/confimgmaps.go +++ b/pkg/kor/confimgmaps.go @@ -17,17 +17,18 @@ var exceptionconfigmaps = []ExceptionResource{ {ResourceName: "kube-root-ca.crt", Namespace: "*"}, } -func retrieveUsedCM(kubeClient *kubernetes.Clientset, namespace string) ([]string, []string, []string, []string, []string, error) { - volumesCM := []string{} - volumesProjectedCM := []string{} - envCM := []string{} - envFromCM := []string{} - envFromContainerCM := []string{} +func retrieveUsedCM(kubeClient *kubernetes.Clientset, namespace string) ([]string, []string, []string, []string, []string, []string, error) { + var volumesCM []string + var volumesProjectedCM []string + var envCM []string + var envFromCM []string + var envFromContainerCM []string + var envFromInitContainerCM []string // Retrieve pods in the specified namespace pods, err := kubeClient.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { - return nil, nil, nil, nil, nil, err + return nil, nil, nil, nil, nil, nil, err } // Extract volume and environment information from pods @@ -61,6 +62,18 @@ func retrieveUsedCM(kubeClient *kubernetes.Clientset, namespace string) ([]strin } } } + for _, initContainer := range pod.Spec.InitContainers { + for _, volume := range initContainer.VolumeMounts { + if volume.Name != "" && volume.MountPath != "" { + volumesCM = append(volumesCM, volume.Name) + } + } + for _, env := range initContainer.Env { + if env.ValueFrom != nil && env.ValueFrom.ConfigMapKeyRef != nil { + envFromInitContainerCM = append(envFromInitContainerCM, env.ValueFrom.ConfigMapKeyRef.Name) + } + } + } } for _, resource := range exceptionconfigmaps { @@ -69,7 +82,7 @@ func retrieveUsedCM(kubeClient *kubernetes.Clientset, namespace string) ([]strin } } - return volumesCM, volumesProjectedCM, envCM, envFromCM, envFromContainerCM, nil + return volumesCM, volumesProjectedCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM, nil } func retrieveConfigMapNames(kubeClient *kubernetes.Clientset, namespace string) ([]string, error) { @@ -85,7 +98,7 @@ func retrieveConfigMapNames(kubeClient *kubernetes.Clientset, namespace string) } func processNamespaceCM(kubeClient *kubernetes.Clientset, namespace string) ([]string, error) { - volumesCM, volumesProjectedCM, envCM, envFromCM, envFromContainerCM, err := retrieveUsedCM(kubeClient, namespace) + volumesCM, volumesProjectedCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM, err := retrieveUsedCM(kubeClient, namespace) if err != nil { return nil, err } @@ -95,13 +108,19 @@ func processNamespaceCM(kubeClient *kubernetes.Clientset, namespace string) ([]s envCM = RemoveDuplicatesAndSort(envCM) envFromCM = RemoveDuplicatesAndSort(envFromCM) envFromContainerCM = RemoveDuplicatesAndSort(envFromContainerCM) + envFromInitContainerCM = RemoveDuplicatesAndSort(envFromInitContainerCM) configMapNames, err := retrieveConfigMapNames(kubeClient, namespace) if err != nil { return nil, err } - usedConfigMaps := append(append(append(append(volumesCM, volumesProjectedCM...), envCM...), envFromCM...), envFromContainerCM...) + var usedConfigMaps []string + slicesToAppend := [][]string{volumesCM, volumesProjectedCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM} + + for _, slice := range slicesToAppend { + usedConfigMaps = append(usedConfigMaps, slice...) + } diff := CalculateResourceDifference(usedConfigMaps, configMapNames) return diff, nil