Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Nov 1, 2023
1 parent f91f568 commit 6c0ebaa
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 57 deletions.
51 changes: 26 additions & 25 deletions test/api/go/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,32 @@ import (
"encoding/json"
"log"

"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine"
"gopkg.in/yaml.v2"
"github.com/kyverno/kyverno-json/pkg/policy"
)

func main() {

// load policies
policyYAML := `
apiVersion: json.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
name: authz
spec:
rules:
- name: delete-checks
match:
all:
(input.method): "DELETE"
assert:
all:
- check:
role: "admin"
`
const policyYAML = `
apiVersion: json.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
name: authz
spec:
rules:
- name: delete-checks
match:
all:
(input.method): "DELETE"
assert:
all:
- check:
role: "admin"
`

var policy v1alpha1.ValidatingPolicy
yaml.Unmarshal([]byte(policyYAML), &policy)
func main() {
policies, err := policy.Parse([]byte(policyYAML))
if err != nil {
panic(err)
}

// load payloads
requestJSON := `{
Expand All @@ -44,12 +43,14 @@ func main() {
}`

var payload interface{}
json.Unmarshal([]byte(requestJSON), &payload)
if err := json.Unmarshal([]byte(requestJSON), &payload); err != nil {
panic(err)
}

// create a JsonEngineRequest
request := jsonengine.JsonEngineRequest{
Resources: []interface{}{payload},
Policies: []*v1alpha1.ValidatingPolicy{&policy},
Policies: policies,
}

// create a J
Expand Down
61 changes: 29 additions & 32 deletions website/docs/go-library/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The Go API can be added to a program's dependencies as follows:

```sh
go get github.com/kyverno/kyverno-json/pkg/jsonengine
go get github.com/kyverno/kyverno-json/pkg/policy

```

Here is a sample program that shows the overall flow for programatically using the Kyverno JSON Engine:
Expand All @@ -18,33 +20,32 @@ import (
"encoding/json"
"log"

"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1"
jsonengine "github.com/kyverno/kyverno-json/pkg/json-engine"
"gopkg.in/yaml.v2"
"github.com/kyverno/kyverno-json/pkg/policy"
)

func main() {
const policyYAML = `
apiVersion: json.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
name: authz
spec:
rules:
- name: delete-checks
match:
all:
(input.method): "DELETE"
assert:
all:
- check:
role: "admin"
`

// load policies
policyYAML := `
apiVersion: json.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
name: authz
spec:
rules:
- name: delete-checks
match:
all:
(input.method): "DELETE"
assert:
all:
- check:
role: "admin"
`

var policy v1alpha1.ValidatingPolicy
yaml.Unmarshal([]byte(policyYAML), &policy)
func main() {
policies, err := policy.Parse([]byte(policyYAML))
if err != nil {
panic(err)
}

// load payloads
requestJSON := `{
Expand All @@ -57,21 +58,21 @@ func main() {
}`

var payload interface{}
json.Unmarshal([]byte(requestJSON), &payload)
if err := json.Unmarshal([]byte(requestJSON), &payload); err != nil {
panic(err)
}

// create a JsonEngineRequest
request := jsonengine.JsonEngineRequest{
Resources: []interface{}{payload},
Policies: []*v1alpha1.ValidatingPolicy{&policy},
Policies: policies,
}

// create a JSON Engine
// create a J
engine := jsonengine.New()

// execute the request
responses := engine.Run(context.Background(), request)

// process the response
logger := log.Default()
for _, resp := range responses {
if resp.Error != nil {
Expand All @@ -86,7 +87,3 @@ func main() {
}
}
```




0 comments on commit 6c0ebaa

Please sign in to comment.