Skip to content

Commit

Permalink
Merge c979b32 into 1ae8787
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonrichardsmith committed Aug 29, 2018
2 parents 1ae8787 + c979b32 commit 36bc7f6
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 53 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Limits will insure all pods have limits for cpu and memory set and are within th

```yaml
limits:
type: Pod
enabled: true
ignoredNamespaces:
- "test2"
Expand All @@ -45,7 +44,6 @@ Healthz just insures liveliness and readiness probes are set.

```yaml
healthz:
type: Pod
enabled: true
ignoredNamespaces:
- "test1"
Expand All @@ -59,7 +57,6 @@ Images insures no containers launch with 'latest' or with no tag set.

```yaml
images:
type: Pod
enabled: true
ignoredNamespaces:
- "test1"
Expand Down
4 changes: 4 additions & 0 deletions healthz/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const (

type HealthzSentry struct{}

func (hs HealthzSentry) Type() string {
return "Pod"
}

func (hs HealthzSentry) Admit(receivedAdmissionReview v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
log.Info("Checking health checks are present")
raw := receivedAdmissionReview.Request.Object.Raw
Expand Down
6 changes: 6 additions & 0 deletions healthz/healthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func init() {
}
}

func TestType(t *testing.T) {
is := HealthzSentry{}
if is.Type() != "Pod" {
t.Fatal("Failed type test")
}
}
func TestAdmit(t *testing.T) {
c := Config{}
hs, err := c.LoadSentry()
Expand Down
4 changes: 4 additions & 0 deletions images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const (

type ImagesSentry struct{}

func (is ImagesSentry) Type() string {
return "Pod"
}

func (is ImagesSentry) Admit(receivedAdmissionReview v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
log.Info("Checking image tags are present")
raw := receivedAdmissionReview.Request.Object.Raw
Expand Down
7 changes: 7 additions & 0 deletions images/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func init() {
}
}

func TestType(t *testing.T) {
is := ImagesSentry{}
if is.Type() != "Pod" {
t.Fatal("Failed type test")
}
}

func TestAdmit(t *testing.T) {
c := Config{}
is, err := c.LoadSentry()
Expand Down
4 changes: 4 additions & 0 deletions limits/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type LimitSentry struct {
CPUMax resource.Quantity
}

func (ls LimitSentry) Type() string {
return "Pod"
}

func (ls LimitSentry) BetweenCPU(q resource.Quantity) bool {
if ls.CPUMax.Cmp(q) >= 0 && ls.CPUMin.Cmp(q) <= 0 {
return true
Expand Down
7 changes: 7 additions & 0 deletions limits/limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func init() {
}
}

func TestType(t *testing.T) {
is := LimitSentry{}
if is.Type() != "Pod" {
t.Fatal("Failed type test")
}
}

func TestBetweenCPU(t *testing.T) {
ls := LimitSentry{
CPUMax: highqty,
Expand Down
3 changes: 0 additions & 3 deletions manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ data:
config.yaml: |
---
limits:
type: Pod
enabled: true
ignoredNamespaces:
- "kube-system"
Expand All @@ -93,14 +92,12 @@ data:
min: 1G
max: 2G
healthz:
type: Pod
enabled: true
ignoredNamespaces:
- "kube-system"
- "test1"
- "test3"
images:
type: Pod
enabled: true
ignoredNamespaces:
- "kube-system"
Expand Down
28 changes: 11 additions & 17 deletions mux/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestLoadFromFile(t *testing.T) {
Max: "1G",
},
Config: sentry.Config{
Type: "Pod",
Enabled: true,
IgnoredNamespaces: []string{
"test2",
Expand All @@ -34,7 +33,6 @@ func TestLoadFromFile(t *testing.T) {
},
Healthz: healthz.Config{
Config: sentry.Config{
Type: "Pod",
Enabled: true,
IgnoredNamespaces: []string{
"test1",
Expand All @@ -44,7 +42,6 @@ func TestLoadFromFile(t *testing.T) {
},
Images: images.Config{
Config: sentry.Config{
Type: "Pod",
Enabled: true,
IgnoredNamespaces: []string{
"test1",
Expand Down Expand Up @@ -75,7 +72,6 @@ func TestLoadSentry(t *testing.T) {
Max: "1G",
},
Config: sentry.Config{
Type: "Pod",
Enabled: true,
IgnoredNamespaces: []string{
"test1",
Expand All @@ -89,19 +85,17 @@ func TestLoadSentry(t *testing.T) {
t.Fatal(err)
}
match := SentryMux{
Sentries: map[string]map[string]sentryModule{
"Pod": map[string]sentryModule{
"limits": sentryModule{
Sentry: limits.LimitSentry{
MemoryMin: qty,
MemoryMax: qty,
CPUMin: qty,
CPUMax: qty,
},
ignored: []string{
"test1",
"test2",
},
Sentries: []sentryModule{
sentryModule{
Sentry: limits.LimitSentry{
MemoryMin: qty,
MemoryMax: qty,
CPUMin: qty,
CPUMax: qty,
},
ignored: []string{
"test1",
"test2",
},
},
},
Expand Down
29 changes: 11 additions & 18 deletions mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ type sentryModule struct {
}

type SentryMux struct {
Sentries map[string]map[string]sentryModule
Sentries []sentryModule
}

func NewFromConfig(c Config) (SentryMux, error) {
sm := SentryMux{
Sentries: make(map[string]map[string]sentryModule),
Sentries: make([]sentryModule, 0),
}
if c.Limits.Enabled {
log.Info("Limits enabled loading")
Expand All @@ -30,7 +30,7 @@ func NewFromConfig(c Config) (SentryMux, error) {
c.Limits.IgnoredNamespaces,
}
log.Info("Ignoring Namespaces ", mod.ignored)
sm.Sentries[c.Limits.Type] = map[string]sentryModule{"limits": mod}
sm.Sentries = append(sm.Sentries, mod)
}
if c.Healthz.Enabled {
log.Info("Healthz enabled loading")
Expand All @@ -43,11 +43,7 @@ func NewFromConfig(c Config) (SentryMux, error) {
c.Healthz.IgnoredNamespaces,
}
log.Info("Ignoring Namespaces ", mod.ignored)
if v, ok := sm.Sentries[c.Healthz.Type]; ok {
v["healthz"] = mod
} else {
sm.Sentries[c.Healthz.Type] = map[string]sentryModule{"healthz": mod}
}
sm.Sentries = append(sm.Sentries, mod)
}
if c.Images.Enabled {
log.Info("Images enabled loading")
Expand All @@ -60,11 +56,7 @@ func NewFromConfig(c Config) (SentryMux, error) {
c.Images.IgnoredNamespaces,
}
log.Info("Ignoring Namespaces ", mod.ignored)
if v, ok := sm.Sentries[c.Images.Type]; ok {
v["images"] = mod
} else {
sm.Sentries[c.Images.Type] = map[string]sentryModule{"images": mod}
}
sm.Sentries = append(sm.Sentries, mod)
}
return sm, nil
}
Expand All @@ -81,12 +73,14 @@ func (sm sentryModule) Ignore(namespace string) bool {
log.Infof("Namespace %v not ignored", namespace)
return false
}

func (sm SentryMux) Type() string {
return "*"
}
func (sm SentryMux) Admit(receivedAdmissionReview v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
log.Infof("Received request of kind %v", receivedAdmissionReview.Request.Kind.Kind)
if sms, ok := sm.Sentries[receivedAdmissionReview.Request.Kind.Kind]; ok {
log.Infof("Found sentries for kind %v, itterating over %v sentries.", receivedAdmissionReview.Request.Kind.Kind, len(sms))
for k, sm := range sms {
log.Infof("Itterating over %v sentries.", receivedAdmissionReview.Request.Kind.Kind, len(sm.Sentries))
for k, sm := range sm.Sentries {
if receivedAdmissionReview.Request.Kind.Kind == sm.Type() {
if !sm.Ignore(receivedAdmissionReview.Request.Namespace) {
log.Infof("Running admit for %v", k)
ar := sm.Admit(receivedAdmissionReview)
Expand All @@ -96,7 +90,6 @@ func (sm SentryMux) Admit(receivedAdmissionReview v1beta1.AdmissionReview) *v1be
}
log.Infof("Allowed by %v", k)
}

}

}
Expand Down
32 changes: 21 additions & 11 deletions mux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func TestIgnore(t *testing.T) {
}
}

func TestType(t *testing.T) {
is := SentryMux{}
if is.Type() != "*" {
t.Fatal("Failed type test")
}
}
func TestNewFromConfig(t *testing.T) {
c := New()
m, err := NewFromConfig(*c)
Expand All @@ -46,7 +52,7 @@ func TestNewFromConfig(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(m.Sentries[""]) != 3 {
if len(m.Sentries) != 3 {
t.Fatal("Extected 3 entries enabled")
}
}
Expand All @@ -55,6 +61,10 @@ type FakeSentry struct {
admit bool
}

func (fs FakeSentry) Type() string {
return "Pod"

}
func (fs FakeSentry) Admit(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {

reviewResponse := v1beta1.AdmissionResponse{}
Expand All @@ -64,14 +74,12 @@ func (fs FakeSentry) Admit(ar v1beta1.AdmissionReview) *v1beta1.AdmissionRespons

func TestAdmit(t *testing.T) {
mux := SentryMux{
Sentries: map[string]map[string]sentryModule{
"Pod": {
"fake": sentryModule{
Sentry: FakeSentry{true},
ignored: []string{
"test1",
"test2",
},
Sentries: []sentryModule{
sentryModule{
Sentry: FakeSentry{true},
ignored: []string{
"test1",
"test2",
},
},
},
Expand All @@ -98,8 +106,10 @@ func TestAdmit(t *testing.T) {
if resp.Allowed != true {
t.Fatal("Return false expected true")
}
mux.Sentries["Pod"]["fake"] = sentryModule{
Sentry: FakeSentry{false},
mux.Sentries = []sentryModule{
sentryModule{
Sentry: FakeSentry{false},
},
}
resp = mux.Admit(ar)
if resp.Allowed != false {
Expand Down
2 changes: 1 addition & 1 deletion sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ var (
)

type Config struct {
Type string `yaml:"type"`
Enabled bool `yaml:"enabled"`
IgnoredNamespaces []string `yaml:"ignoredNamespaces"`
}

type Sentry interface {
Admit(v1beta1.AdmissionReview) *v1beta1.AdmissionResponse
Type() string
}

type Loader interface {
Expand Down
3 changes: 3 additions & 0 deletions sentry/sentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (

type FakeSentry struct{}

func (fs FakeSentry) Type() string {
return "Pod"
}
func (fs FakeSentry) Admit(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {

reviewResponse := v1beta1.AdmissionResponse{}
Expand Down

0 comments on commit 36bc7f6

Please sign in to comment.