Skip to content

Commit adb76bf

Browse files
authored
Only inject Pods that are Pending. (#501)
1 parent e813ad9 commit adb76bf

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

agent-inject/agent/agent.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,11 @@ func ShouldInject(pod *corev1.Pod) (bool, error) {
540540
return false, nil
541541
}
542542

543+
// If injection didn't happen on pod creation, then it's too late now.
544+
if pod.Status.Phase != "" && pod.Status.Phase != corev1.PodPending {
545+
return false, nil
546+
}
547+
543548
// This shouldn't happen so bail.
544549
raw, ok = pod.Annotations[AnnotationAgentStatus]
545550
if !ok {

agent-inject/agent/agent_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,23 @@ func testPodIRSA(annotations map[string]string) *corev1.Pod {
9191
func TestShouldInject(t *testing.T) {
9292
tests := []struct {
9393
annotations map[string]string
94+
phase corev1.PodPhase
9495
inject bool
9596
}{
96-
{map[string]string{AnnotationAgentInject: "true", AnnotationAgentStatus: ""}, true},
97-
{map[string]string{AnnotationAgentInject: "false", AnnotationAgentStatus: ""}, false},
98-
{map[string]string{AnnotationAgentInject: "true", AnnotationAgentStatus: "injected"}, false},
99-
{map[string]string{AnnotationAgentInject: "false", AnnotationAgentStatus: "injected"}, false},
100-
{map[string]string{AnnotationAgentInject: "true", AnnotationAgentStatus: "update"}, true},
97+
{map[string]string{AnnotationAgentInject: "true", AnnotationAgentStatus: ""}, corev1.PodPending, true},
98+
{map[string]string{AnnotationAgentInject: "false", AnnotationAgentStatus: ""}, corev1.PodPending, false},
99+
{map[string]string{AnnotationAgentInject: "true", AnnotationAgentStatus: "injected"}, corev1.PodPending, false},
100+
{map[string]string{AnnotationAgentInject: "false", AnnotationAgentStatus: "injected"}, corev1.PodPending, false},
101+
{map[string]string{AnnotationAgentInject: "true", AnnotationAgentStatus: "update"}, corev1.PodPending, true},
102+
{map[string]string{AnnotationAgentInject: "true", AnnotationAgentStatus: ""}, corev1.PodRunning, false},
103+
{map[string]string{AnnotationAgentInject: "true", AnnotationAgentStatus: ""}, corev1.PodSucceeded, false},
104+
{map[string]string{AnnotationAgentInject: "true", AnnotationAgentStatus: ""}, corev1.PodFailed, false},
105+
{map[string]string{AnnotationAgentInject: "true", AnnotationAgentStatus: "update"}, corev1.PodRunning, false},
101106
}
102107

103108
for _, tt := range tests {
104109
pod := testPod(tt.annotations)
110+
pod.Status.Phase = tt.phase
105111
inject, err := ShouldInject(pod)
106112
if err != nil {
107113
t.Errorf("got error, shouldn't have: %s", err)

0 commit comments

Comments
 (0)