Repository: yonahd/kor
Pull request #156 (2023-11-15 08:49:53)

add ability to discover projected secrets

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/yonahd/kor/pull/156?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yonah+Dissen) Report
Attention: `7 lines` in your changes are missing coverage. Please review.
> Comparison is base [(`4a93e14`)](https://app.codecov.io/gh/yonahd/kor/commit/4a93e141b15605a66d661979e66976c5ed7c17d9?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yonah+Dissen) 48.08% compared to head [(`b602aab`)](https://app.codecov.io/gh/yonahd/kor/pull/156?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yonah+Dissen) 47.89%.

| [Files](https://app.codecov.io/gh/yonahd/kor/pull/156?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yonah+Dissen) | Patch % | Lines |
|---|---|---|
| [pkg/kor/secrets.go](https://app.codecov.io/gh/yonahd/kor/pull/156?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yonah+Dissen#diff-cGtnL2tvci9zZWNyZXRzLmdv) | 28.57% | [4 Missing and 1 partial :warning: ](https://app.codecov.io/gh/yonahd/kor/pull/156?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yonah+Dissen) |
| [pkg/kor/confimgmaps.go](https://app.codecov.io/gh/yonahd/kor/pull/156?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yonah+Dissen#diff-cGtnL2tvci9jb25maW1nbWFwcy5nbw==) | 66.66% | [2 Missing :warning: ](https://app.codecov.io/gh/yonahd/kor/pull/156?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yonah+Dissen) |

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #156      +/-   ##
==========================================
- Coverage   48.08%   47.89%   -0.20%     
==========================================
  Files          20       20              
  Lines        1805     1808       +3     
==========================================
- Hits          868      866       -2     
- Misses        792      796       +4     
- Partials      145      146       +1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/yonahd/kor/pull/156?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yonah+Dissen).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Yonah+Dissen).


Patch:
diff --git a/pkg/kor/confimgmaps.go b/pkg/kor/confimgmaps.go
index d6729b0..218f4fa 100644
--- a/pkg/kor/confimgmaps.go
+++ b/pkg/kor/confimgmaps.go
@@ -17,9 +17,8 @@ var exceptionconfigmaps = []ExceptionResource{
 	{ResourceName: "kube-root-ca.crt", Namespace: "*"},
 }
 
-func retrieveUsedCM(clientset kubernetes.Interface, namespace string) ([]string, []string, []string, []string, []string, []string, error) {
+func retrieveUsedCM(clientset kubernetes.Interface, namespace string) ([]string, []string, []string, []string, []string, error) {
 	var volumesCM []string
-	var volumesProjectedCM []string
 	var envCM []string
 	var envFromCM []string
 	var envFromContainerCM []string
@@ -27,7 +26,7 @@ func retrieveUsedCM(clientset kubernetes.Interface, namespace string) ([]string,
 
 	pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
 	if err != nil {
-		return nil, nil, nil, nil, nil, nil, err
+		return nil, nil, nil, nil, nil, err
 	}
 
 	for _, pod := range pods.Items {
@@ -38,7 +37,7 @@ func retrieveUsedCM(clientset kubernetes.Interface, namespace string) ([]string,
 			if volume.Projected != nil {
 				for _, source := range volume.Projected.Sources {
 					if source.ConfigMap != nil {
-						volumesProjectedCM = append(volumesProjectedCM, source.ConfigMap.Name)
+						volumesCM = append(volumesCM, source.ConfigMap.Name)
 					}
 				}
 			}
@@ -80,7 +79,7 @@ func retrieveUsedCM(clientset kubernetes.Interface, namespace string) ([]string,
 		}
 	}
 
-	return volumesCM, volumesProjectedCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM, nil
+	return volumesCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM, nil
 }
 
 func retrieveConfigMapNames(clientset kubernetes.Interface, namespace string, filterOpts *FilterOptions) ([]string, error) {
@@ -111,13 +110,12 @@ func retrieveConfigMapNames(clientset kubernetes.Interface, namespace string, fi
 }
 
 func processNamespaceCM(clientset kubernetes.Interface, namespace string, filterOpts *FilterOptions) ([]string, error) {
-	volumesCM, volumesProjectedCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM, err := retrieveUsedCM(clientset, namespace)
+	volumesCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM, err := retrieveUsedCM(clientset, namespace)
 	if err != nil {
 		return nil, err
 	}
 
 	volumesCM = RemoveDuplicatesAndSort(volumesCM)
-	volumesProjectedCM = RemoveDuplicatesAndSort(volumesProjectedCM)
 	envCM = RemoveDuplicatesAndSort(envCM)
 	envFromCM = RemoveDuplicatesAndSort(envFromCM)
 	envFromContainerCM = RemoveDuplicatesAndSort(envFromContainerCM)
@@ -129,7 +127,7 @@ func processNamespaceCM(clientset kubernetes.Interface, namespace string, filter
 	}
 
 	var usedConfigMaps []string
-	slicesToAppend := [][]string{volumesCM, volumesProjectedCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM}
+	slicesToAppend := [][]string{volumesCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM}
 
 	for _, slice := range slicesToAppend {
 		usedConfigMaps = append(usedConfigMaps, slice...)
diff --git a/pkg/kor/secrets.go b/pkg/kor/secrets.go
index 864dab1..cef2fb9 100644
--- a/pkg/kor/secrets.go
+++ b/pkg/kor/secrets.go
@@ -37,7 +37,7 @@ func retrieveIngressTLS(clientset kubernetes.Interface, namespace string) ([]str
 
 }
 
-func retrieveUsedSecret(clientset kubernetes.Interface, namespace string, filterOpts *FilterOptions) ([]string, []string, []string, []string, []string, []string, error) {
+func retrieveUsedSecret(clientset kubernetes.Interface, namespace string) ([]string, []string, []string, []string, []string, []string, error) {
 	var envSecrets []string
 	var envSecrets2 []string
 	var volumeSecrets []string
@@ -77,7 +77,15 @@ func retrieveUsedSecret(clientset kubernetes.Interface, namespace string, filter
 			if volume.Secret != nil {
 				volumeSecrets = append(volumeSecrets, volume.Secret.SecretName)
 			}
+			if volume.Projected != nil && volume.Projected.Sources != nil {
+				for _, projectedResource := range volume.Projected.Sources {
+					if projectedResource.Secret != nil {
+						volumeSecrets = append(volumeSecrets, projectedResource.Secret.Name)
+					}
+				}
+			}
 		}
+
 		if pod.Spec.ImagePullSecrets != nil {
 			for _, secret := range pod.Spec.ImagePullSecrets {
 				pullSecrets = append(pullSecrets, secret.Name)
@@ -123,7 +131,7 @@ func retrieveSecretNames(clientset kubernetes.Interface, namespace string, filte
 }
 
 func processNamespaceSecret(clientset kubernetes.Interface, namespace string, filterOpts *FilterOptions) ([]string, error) {
-	envSecrets, envSecrets2, volumeSecrets, initContainerEnvSecrets, pullSecrets, tlsSecrets, err := retrieveUsedSecret(clientset, namespace, filterOpts)
+	envSecrets, envSecrets2, volumeSecrets, initContainerEnvSecrets, pullSecrets, tlsSecrets, err := retrieveUsedSecret(clientset, namespace)
 	if err != nil {
 		return nil, err
 	}


Test patch:
diff --git a/pkg/kor/configmaps_test.go b/pkg/kor/configmaps_test.go
index e0bd245..6930b22 100644
--- a/pkg/kor/configmaps_test.go
+++ b/pkg/kor/configmaps_test.go
@@ -130,7 +130,7 @@ func TestProcessNamespaceCM(t *testing.T) {
 func TestRetrieveUsedCM(t *testing.T) {
 	clientset := createTestConfigmaps(t)
 
-	volumesCM, volumesProjectedCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM, err := retrieveUsedCM(clientset, testNamespace)
+	volumesCM, envCM, envFromCM, envFromContainerCM, envFromInitContainerCM, err := retrieveUsedCM(clientset, testNamespace)
 
 	if err != nil {
 		t.Fatalf("Error retrieving used ConfigMaps: %v", err)
@@ -141,11 +141,6 @@ func TestRetrieveUsedCM(t *testing.T) {
 		t.Errorf("Expected volume configmaps %v, got %v", expectedVolumesCM, volumesCM)
 	}
 
-	var expectedVolumesProjectedCM []string
-	if !equalSlices(volumesProjectedCM, expectedVolumesProjectedCM) {
-		t.Errorf("Expected volume configmaps %v, got %v", expectedVolumesProjectedCM, volumesProjectedCM)
-	}
-
 	expectedEnvCM := []string{"configmap-1"}
 	if !equalSlices(envCM, expectedEnvCM) {
 		t.Errorf("Expected env configmaps %v, got %v", expectedEnvCM, envCM)
diff --git a/pkg/kor/secrets_test.go b/pkg/kor/secrets_test.go
index c4def34..1814ba4 100644
--- a/pkg/kor/secrets_test.go
+++ b/pkg/kor/secrets_test.go
@@ -158,7 +158,7 @@ func TestRetrieveIngressTLS(t *testing.T) {
 func TestRetrieveUsedSecret(t *testing.T) {
 	clientset := createTestSecrets(t)
 
-	envSecrets, envSecrets2, volumeSecrets, initContainerEnvSecrets, pullSecrets, _, err := retrieveUsedSecret(clientset, testNamespace, &FilterOptions{})
+	envSecrets, envSecrets2, volumeSecrets, initContainerEnvSecrets, pullSecrets, _, err := retrieveUsedSecret(clientset, testNamespace)
 	if err != nil {
 		t.Fatalf("Error retrieving used secrets: %v", err)
 	}