-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from Antvirf/feat/pod-status-phase
feat: reaping by `POD_STATUS_PHASES` - `Pending`, `Running`, `Succeeded`, `Failed`, `Unknown`
- Loading branch information
Showing
5 changed files
with
131 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package rules | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const envPodStatusPhase = "POD_STATUS_PHASES" | ||
|
||
var _ Rule = (*podStatusPhase)(nil) | ||
|
||
type podStatusPhase struct { | ||
reapStatusPhases []string | ||
} | ||
|
||
func (rule *podStatusPhase) load() (bool, string, error) { | ||
value, active := os.LookupEnv(envPodStatusPhase) | ||
if !active { | ||
return false, "", nil | ||
} | ||
rule.reapStatusPhases = strings.Split(value, ",") | ||
return true, fmt.Sprintf("pod status phase in [%s]", value), nil | ||
} | ||
|
||
func (rule *podStatusPhase) ShouldReap(pod v1.Pod) (bool, string) { | ||
status := string(pod.Status.Phase) | ||
for _, reapStatusPhase := range rule.reapStatusPhases { | ||
if status == reapStatusPhase { | ||
return true, fmt.Sprintf("has pod status phase %s", reapStatusPhase) | ||
} | ||
} | ||
return false, "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package rules | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func testPodFromPhase(phase v1.PodPhase) v1.Pod { | ||
return v1.Pod{ | ||
Status: v1.PodStatus{ | ||
Phase: phase, | ||
}, | ||
} | ||
} | ||
|
||
func TestPodPhaseLoad(t *testing.T) { | ||
t.Run("load", func(t *testing.T) { | ||
os.Clearenv() | ||
os.Setenv(envPodStatusPhase, "test-phase") | ||
loaded, message, err := (&podStatusPhase{}).load() | ||
assert.NoError(t, err) | ||
assert.Equal(t, "pod status phase in [test-phase]", message) | ||
assert.True(t, loaded) | ||
}) | ||
t.Run("load multiple-statuses", func(t *testing.T) { | ||
os.Clearenv() | ||
os.Setenv(envPodStatusPhase, "test-phase,another-phase") | ||
podStatusPhase := podStatusPhase{} | ||
loaded, message, err := podStatusPhase.load() | ||
assert.NoError(t, err) | ||
assert.Equal(t, "pod status phase in [test-phase,another-phase]", message) | ||
assert.True(t, loaded) | ||
assert.Equal(t, 2, len(podStatusPhase.reapStatusPhases)) | ||
assert.Equal(t, "test-phase", podStatusPhase.reapStatusPhases[0]) | ||
assert.Equal(t, "another-phase", podStatusPhase.reapStatusPhases[1]) | ||
}) | ||
t.Run("no load", func(t *testing.T) { | ||
os.Clearenv() | ||
loaded, message, err := (&podStatusPhase{}).load() | ||
assert.NoError(t, err) | ||
assert.Equal(t, "", message) | ||
assert.False(t, loaded) | ||
}) | ||
} | ||
|
||
func TestPodStatusPhaseShouldReap(t *testing.T) { | ||
t.Run("reap", func(t *testing.T) { | ||
os.Clearenv() | ||
os.Setenv(envPodStatusPhase, "test-phase,another-phase") | ||
podStatusPhase := podStatusPhase{} | ||
podStatusPhase.load() | ||
pod := testPodFromPhase("another-phase") | ||
shouldReap, reason := podStatusPhase.ShouldReap(pod) | ||
assert.True(t, shouldReap) | ||
assert.Regexp(t, ".*another-phase.*", reason) | ||
}) | ||
t.Run("no reap", func(t *testing.T) { | ||
os.Clearenv() | ||
os.Setenv(envPodStatusPhase, "test-phase,another-phase") | ||
podStatusPhase := podStatusPhase{} | ||
podStatusPhase.load() | ||
pod := testPodFromPhase("not-present") | ||
shouldReap, _ := podStatusPhase.ShouldReap(pod) | ||
assert.False(t, shouldReap) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters