Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check initContainers if they have env vars from a configmap or a secret #388

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/kor/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func retrieveUsedCM(clientset kubernetes.Interface, namespace string) ([]string,
envFromInitContainerCM = append(envFromInitContainerCM, env.ValueFrom.ConfigMapKeyRef.Name)
}
}
for _, envFrom := range initContainer.EnvFrom {
if envFrom.ConfigMapRef != nil {
envFromInitContainerCM = append(envFromInitContainerCM, envFrom.ConfigMapRef.Name)
}
}
}
}

Expand Down
26 changes: 24 additions & 2 deletions pkg/kor/configmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func createTestConfigmaps(t *testing.T) *fake.Clientset {
t.Fatalf("Error creating fake configmap: %v", err)
}

configmap6 := CreateTestConfigmap(testNamespace, "configmap-6", AppLabels)
_, err = clientset.CoreV1().ConfigMaps(testNamespace).Create(context.TODO(), configmap6, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating fake configmap: %v", err)
}

pod1 := CreateTestPod(testNamespace, "pod-1", "", []corev1.Volume{
{
Name: "vol-1",
Expand Down Expand Up @@ -98,6 +104,17 @@ func createTestConfigmaps(t *testing.T) *fake.Clientset {
},
}

pod5 := CreateTestPod(testNamespace, "pod-5", "", nil, AppLabels)
pod5.Spec.InitContainers = []corev1.Container{
{
EnvFrom: []corev1.EnvFromSource{
almilaneze marked this conversation as resolved.
Show resolved Hide resolved
{
ConfigMapRef: &corev1.ConfigMapEnvSource{LocalObjectReference: corev1.LocalObjectReference{Name: configmap6.ObjectMeta.Name}},
},
},
},
}

_, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod1, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating fake pod: %v", err)
Expand All @@ -118,6 +135,11 @@ func createTestConfigmaps(t *testing.T) *fake.Clientset {
t.Fatalf("Error creating fake pod: %v", err)
}

_, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod5, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating fake pod: %v", err)
}

return clientset
}

Expand All @@ -134,6 +156,7 @@ func TestRetrieveConfigMapNames(t *testing.T) {
"configmap-1",
"configmap-2",
"configmap-3",
"configmap-6",
}
if !equalSlices(configMapNames, expectedConfigMapNames) {
t.Errorf("Expected configmap names %v, got %v", expectedConfigMapNames, configMapNames)
Expand Down Expand Up @@ -188,11 +211,10 @@ func TestRetrieveUsedCM(t *testing.T) {
t.Errorf("Expected envFrom configmaps %v, got %v", expectedEnvFromContainerCM, envFromContainerCM)
}

expectedEnvFromInitContainerCM := []string{"configmap-2"}
expectedEnvFromInitContainerCM := []string{"configmap-2", "configmap-6"}
if !equalSlices(envFromInitContainerCM, expectedEnvFromInitContainerCM) {
t.Errorf("Expected initContainer env configmaps %v, got %v", expectedEnvFromInitContainerCM, envFromInitContainerCM)
}

}

func TestGetUnusedConfigmapsStructured(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/kor/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func retrieveUsedSecret(clientset kubernetes.Interface, namespace string) ([]str
initContainerEnvSecrets = append(initContainerEnvSecrets, env.ValueFrom.SecretKeyRef.Name)
}
}
for _, envFrom := range initContainer.EnvFrom {
if envFrom.SecretRef != nil {
initContainerEnvSecrets = append(initContainerEnvSecrets, envFrom.SecretRef.Name)
}
}
}

for _, volume := range pod.Spec.Volumes {
Expand Down
26 changes: 23 additions & 3 deletions pkg/kor/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func createTestSecrets(t *testing.T) *fake.Clientset {
secret3 := CreateTestSecret(testNamespace, "test-secret3", AppLabels)
secret4 := CreateTestSecret(testNamespace, "test-secret4", UsedLabels)
secret5 := CreateTestSecret(testNamespace, "test-secret5", UnusedLabels)
secret6 := CreateTestSecret(testNamespace, "test-secret6", AppLabels)

pod1 := CreateTestPod(testNamespace, "pod-1", "", []corev1.Volume{
{
Expand Down Expand Up @@ -87,6 +88,15 @@ func createTestSecrets(t *testing.T) *fake.Clientset {
{Name: secret2.ObjectMeta.Name},
}

pod7 := CreateTestPod(testNamespace, "pod-7", "", nil, AppLabels)
pod7.Spec.InitContainers = []corev1.Container{
{
EnvFrom: []corev1.EnvFromSource{
{SecretRef: &corev1.SecretEnvSource{LocalObjectReference: corev1.LocalObjectReference{Name: secret6.ObjectMeta.Name}}},
},
},
}

_, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod1, v1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating fake pod: %v", err)
Expand Down Expand Up @@ -117,6 +127,11 @@ func createTestSecrets(t *testing.T) *fake.Clientset {
t.Fatalf("Error creating fake pod: %v", err)
}

_, err = clientset.CoreV1().Pods(testNamespace).Create(context.TODO(), pod7, v1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating fake pod: %v", err)
}

_, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret1, v1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating fake %s: %v", "Secret", err)
Expand All @@ -142,6 +157,11 @@ func createTestSecrets(t *testing.T) *fake.Clientset {
t.Fatalf("Error creating fake %s: %v", "Secret", err)
}

_, err = clientset.CoreV1().Secrets(testNamespace).Create(context.TODO(), secret6, v1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating fake %s: %v", "Secret", err)
}

return clientset
}

Expand Down Expand Up @@ -209,7 +229,7 @@ func TestRetrieveUsedSecret(t *testing.T) {
t.Errorf("Expected envFrom secrets %v, got %v", expectedEnvSecrets2, envSecrets2)
}

expectedInitContainerEnvSecrets := []string{"test-secret1"}
expectedInitContainerEnvSecrets := []string{"test-secret1", "test-secret6"}
if !equalSlices(initContainerEnvSecrets, expectedInitContainerEnvSecrets) {
t.Errorf("Expected initContainer env secrets %v, got %v", expectedInitContainerEnvSecrets, initContainerEnvSecrets)
}
Expand Down Expand Up @@ -265,11 +285,11 @@ func TestProcessNamespaceSecret(t *testing.T) {
}

if len(unusedSecrets) != 2 {
t.Errorf("Expected 2 used Secret objects, got %d", len(unusedSecrets))
t.Errorf("Expected 2 unused Secret objects, got %d", len(unusedSecrets))
}

if !resourceInfoContains(unusedSecrets, "test-secret3") {
t.Error("Expected specific Secret in the list")
t.Error("Expected specific Secret in the list")
}

}
Expand Down
Loading