-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
f4c1c19
commit fda7c15
Showing
12 changed files
with
246 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package constant | ||
|
||
import ( | ||
"github.com/eddycharly/tf-kyverno/pkg/engine" | ||
) | ||
|
||
type constant[TREQUEST any, TRESPONSE any] struct { | ||
responses []TRESPONSE | ||
} | ||
|
||
func (b *constant[TREQUEST, TRESPONSE]) Run(_ TREQUEST) []TRESPONSE { | ||
return b.responses | ||
} | ||
|
||
func New[TREQUEST any, TRESPONSE any](responses ...TRESPONSE) engine.Engine[TREQUEST, TRESPONSE] { | ||
return &constant[TREQUEST, TRESPONSE]{ | ||
responses: responses, | ||
} | ||
} |
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,19 @@ | ||
package function | ||
|
||
import ( | ||
"github.com/eddycharly/tf-kyverno/pkg/engine" | ||
) | ||
|
||
type function[TREQUEST any, TRESPONSE any] struct { | ||
function func(TREQUEST) TRESPONSE | ||
} | ||
|
||
func (b *function[TREQUEST, TRESPONSE]) Run(request TREQUEST) []TRESPONSE { | ||
return []TRESPONSE{b.function(request)} | ||
} | ||
|
||
func New[TREQUEST any, TRESPONSE any](f func(TREQUEST) TRESPONSE) engine.Engine[TREQUEST, TRESPONSE] { | ||
return &function[TREQUEST, TRESPONSE]{ | ||
function: f, | ||
} | ||
} |
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,25 @@ | ||
package loop | ||
|
||
import ( | ||
"github.com/eddycharly/tf-kyverno/pkg/engine" | ||
) | ||
|
||
type loop[TPARENT any, TCHILD any, TRESPONSE any] struct { | ||
inner engine.Engine[TCHILD, TRESPONSE] | ||
looper func(TPARENT) []TCHILD | ||
} | ||
|
||
func (b *loop[TPARENT, TCHILD, TRESPONSE]) Run(parent TPARENT) []TRESPONSE { | ||
var responses []TRESPONSE | ||
for _, child := range b.looper(parent) { | ||
responses = append(responses, b.inner.Run(child)...) | ||
} | ||
return responses | ||
} | ||
|
||
func New[TPARENT any, TCHILD any, TRESPONSE any](inner engine.Engine[TCHILD, TRESPONSE], looper func(TPARENT) []TCHILD) engine.Engine[TPARENT, TRESPONSE] { | ||
return &loop[TPARENT, TCHILD, TRESPONSE]{ | ||
inner: inner, | ||
looper: looper, | ||
} | ||
} |
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,15 @@ | ||
package null | ||
|
||
import ( | ||
"github.com/eddycharly/tf-kyverno/pkg/engine" | ||
) | ||
|
||
type null[TREQUEST any, TRESPONSE any] struct{} | ||
|
||
func (b *null[TREQUEST, TRESPONSE]) Run(_ TREQUEST) []TRESPONSE { | ||
return nil | ||
} | ||
|
||
func New[TREQUEST any, TRESPONSE any]() engine.Engine[TREQUEST, TRESPONSE] { | ||
return &null[TREQUEST, TRESPONSE]{} | ||
} |
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,24 @@ | ||
package predicate | ||
|
||
import ( | ||
"github.com/eddycharly/tf-kyverno/pkg/engine" | ||
) | ||
|
||
type predicate[TREQUEST any, TRESPONSE any] struct { | ||
inner engine.Engine[TREQUEST, TRESPONSE] | ||
predicate func(TREQUEST) bool | ||
} | ||
|
||
func (b *predicate[TREQUEST, TRESPONSE]) Run(request TREQUEST) []TRESPONSE { | ||
if !b.predicate(request) { | ||
return nil | ||
} | ||
return b.inner.Run(request) | ||
} | ||
|
||
func New[TREQUEST any, TRESPONSE any](inner engine.Engine[TREQUEST, TRESPONSE], condition func(TREQUEST) bool) engine.Engine[TREQUEST, TRESPONSE] { | ||
return &predicate[TREQUEST, TRESPONSE]{ | ||
inner: inner, | ||
predicate: condition, | ||
} | ||
} |
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,28 @@ | ||
package builder | ||
|
||
import ( | ||
"github.com/eddycharly/tf-kyverno/pkg/engine" | ||
"github.com/eddycharly/tf-kyverno/pkg/engine/blocks/constant" | ||
"github.com/eddycharly/tf-kyverno/pkg/engine/blocks/function" | ||
"github.com/eddycharly/tf-kyverno/pkg/engine/blocks/predicate" | ||
) | ||
|
||
type Engine[TREQUEST any, TRESPONSE any] struct { | ||
engine.Engine[TREQUEST, TRESPONSE] | ||
} | ||
|
||
func new[TREQUEST any, TRESPONSE any](engine engine.Engine[TREQUEST, TRESPONSE]) Engine[TREQUEST, TRESPONSE] { | ||
return Engine[TREQUEST, TRESPONSE]{engine} | ||
} | ||
|
||
func Constant[TREQUEST any, TRESPONSE any](responses ...TRESPONSE) Engine[TREQUEST, TRESPONSE] { | ||
return new(constant.New[TREQUEST](responses...)) | ||
} | ||
|
||
func (inner Engine[TREQUEST, TRESPONSE]) Predicate(condition func(TREQUEST) bool) Engine[TREQUEST, TRESPONSE] { | ||
return new(predicate.New(inner, condition)) | ||
} | ||
|
||
func Function[TREQUEST any, TRESPONSE any](f func(TREQUEST) TRESPONSE) Engine[TREQUEST, TRESPONSE] { | ||
return new(function.New(f)) | ||
} |
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,5 @@ | ||
package engine | ||
|
||
type Engine[TREQUEST any, TRESPONSE any] interface { | ||
Run(TREQUEST) []TRESPONSE | ||
} |
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,63 @@ | ||
package tfengine | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/eddycharly/tf-kyverno/pkg/apis/v1alpha1" | ||
"github.com/eddycharly/tf-kyverno/pkg/engine" | ||
"github.com/eddycharly/tf-kyverno/pkg/engine/blocks/loop" | ||
"github.com/eddycharly/tf-kyverno/pkg/engine/builder" | ||
"github.com/eddycharly/tf-kyverno/pkg/match" | ||
"github.com/eddycharly/tf-kyverno/pkg/plan" | ||
) | ||
|
||
type TfEngineRequest struct { | ||
Plan *plan.Plan | ||
Policies []*v1alpha1.Policy | ||
} | ||
|
||
type TfEngineResponse struct { | ||
Policy *v1alpha1.Policy | ||
Rule *v1alpha1.Rule | ||
Resource interface{} | ||
Error error | ||
} | ||
|
||
func New() engine.Engine[TfEngineRequest, TfEngineResponse] { | ||
type request struct { | ||
policy *v1alpha1.Policy | ||
rule *v1alpha1.Rule | ||
resource interface{} | ||
} | ||
looper := func(r TfEngineRequest) []request { | ||
var requests []request | ||
for _, resource := range r.Plan.Resources { | ||
for _, policy := range r.Policies { | ||
for _, rule := range policy.Spec.Rules { | ||
requests = append(requests, request{ | ||
policy: policy, | ||
rule: &rule, | ||
resource: resource, | ||
}) | ||
} | ||
} | ||
} | ||
return requests | ||
} | ||
inner := builder. | ||
Function(func(r request) TfEngineResponse { | ||
response := TfEngineResponse{ | ||
Policy: r.policy, | ||
Rule: r.rule, | ||
Resource: r.resource, | ||
} | ||
if !match.Match(r.rule.Validation.Pattern, r.resource) { | ||
response.Error = errors.New(r.rule.Validation.Message) | ||
} | ||
return response | ||
}). | ||
Predicate(func(r request) bool { return !match.MatchResources(r.rule.ExcludeResources, r.resource) }). | ||
Predicate(func(r request) bool { return match.MatchResources(r.rule.MatchResources, r.resource) }) | ||
// TODO: we can't use the builder package for loops :( | ||
return loop.New(inner, looper) | ||
} |
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