Skip to content

Commit

Permalink
feat: wip admission controller
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerwill90 committed Oct 15, 2024
1 parent c137187 commit 007d20f
Showing 1 changed file with 49 additions and 12 deletions.
61 changes: 49 additions & 12 deletions admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"cmp"
"context"
"iter"
"log"
"slices"
)

Expand All @@ -14,6 +15,7 @@ type hook struct {
type ValidationTree interface {
Has(method, path string) bool
Route(method, path string) *Route
Iter() Iter
}

type MutationTree interface {
Expand All @@ -34,13 +36,27 @@ type ValidationRoute interface {
type MutationRoute interface {
ValidationRoute
SetPath(path string)
PatchAnnotation(fn func(annotations iter.Seq[Annotation]) iter.Seq[Annotation])
PatchAnnotations(fn func(annotations iter.Seq[Annotation]) iter.Seq[Annotation])
// TODO Patch middleware
SetRedirectTrailingSlash(enable bool)
SetIgnoreTrailingSlash(enable bool)
SetClientIPStrategy(strategy ClientIPStrategy)
}

type AdmissionController interface {
Handles(operation Operation) bool
Admit(ctx context.Context, tree MutationTree, route MutationRoute) (err error)
Validate(ctx context.Context, tree ValidationTree, route ValidationRoute) (err error)
}

type Operation uint8

const (
Insert Operation = iota
Update
Delete
)

type mutationRoute struct {
*Route
}
Expand All @@ -49,7 +65,7 @@ func (r mutationRoute) SetPath(path string) {
r.path = path
}

func (r mutationRoute) PatchAnnotation(fn func(annotations iter.Seq[Annotation]) iter.Seq[Annotation]) {
func (r mutationRoute) PatchAnnotations(fn func(annotations iter.Seq[Annotation]) iter.Seq[Annotation]) {
r.annots = slices.Collect(fn(slices.Values(r.annots)))
}

Expand All @@ -65,16 +81,37 @@ func (r mutationRoute) SetClientIPStrategy(strategy ClientIPStrategy) {
r.ipStrategy = cmp.Or(strategy, ClientIPStrategy(noClientIPStrategy{}))
}

type AdmissionController interface {
Handles(operation Operation) bool
Admit(ctx context.Context, tree MutationTree, route MutationRoute) (err error)
Validate(ctx context.Context, tree ValidationTree, route ValidationRoute) (err error)
type validationRoute struct {

Check failure on line 84 in admission.go

View workflow job for this annotation

GitHub Actions / Lint Fox (>=1.21)

type `validationRoute` is unused (unused)
*Route
}

type Operation uint8
func newController() *controller {

Check failure on line 88 in admission.go

View workflow job for this annotation

GitHub Actions / Lint Fox (>=1.21)

func `newController` is unused (unused)
c := &controller{
c: make(chan ValidationRoute),
}

const (
Insert Operation = iota
Update
Delete
)
go func(c <-chan ValidationRoute) {
for rte := range c {
log.Println(rte.Path())
}
}(c.c)

return c
}

type controller struct {

Check failure on line 102 in admission.go

View workflow job for this annotation

GitHub Actions / Lint Fox (>=1.21)

type `controller` is unused (unused)
c chan ValidationRoute
}

func (c *controller) Handles(operation Operation) bool {

Check failure on line 106 in admission.go

View workflow job for this annotation

GitHub Actions / Lint Fox (>=1.21)

func `(*controller).Handles` is unused (unused)
return operation == Insert || operation == Update
}

func (c *controller) Admit(ctx context.Context, tree MutationTree, route MutationRoute) (err error) {

Check failure on line 110 in admission.go

View workflow job for this annotation

GitHub Actions / Lint Fox (>=1.21)

func `(*controller).Admit` is unused (unused)
return nil
}

func (c *controller) Validate(ctx context.Context, tree ValidationTree, route ValidationRoute) (err error) {

Check failure on line 114 in admission.go

View workflow job for this annotation

GitHub Actions / Lint Fox (>=1.21)

func `(*controller).Validate` is unused (unused)
route.Annotations()
return nil
}

0 comments on commit 007d20f

Please sign in to comment.