Skip to content

Commit

Permalink
find configmaps in init containers
Browse files Browse the repository at this point in the history
  • Loading branch information
yonahd authored Sep 9, 2023
1 parent e54e37f commit 65d3545
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions pkg/kor/confimgmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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
}
Expand All @@ -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

Expand Down

0 comments on commit 65d3545

Please sign in to comment.