Skip to content

Commit

Permalink
feat: add playground command (#101)
Browse files Browse the repository at this point in the history
* feat: add playground command

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* codegen

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* rm wasm

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* mkdocs nav

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* workflows

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Co-authored-by: Jim Bugwadia <jim@nirmata.com>
  • Loading branch information
eddycharly and JimBugwadia authored Oct 19, 2023
1 parent 8165d6b commit 08cdc4d
Show file tree
Hide file tree
Showing 48 changed files with 1,525 additions and 221 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/check-actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ jobs:
required:
runs-on: ubuntu-latest
steps:
- name: Free disk space
uses: jlumbroso/free-disk-space@f68fdb76e2ea636224182cfb7377ff9a1708f9b8 # v1.3.0
with:
tool-cache: true
android: true
dotnet: true
haskell: true
large-packages: false
docker-images: true
swap-storage: false
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Ensure SHA pinned actions
Expand Down
10 changes: 0 additions & 10 deletions .github/workflows/codegen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ jobs:
required:
runs-on: ubuntu-latest
steps:
- name: Free disk space
uses: jlumbroso/free-disk-space@f68fdb76e2ea636224182cfb7377ff9a1708f9b8 # v1.3.0
with:
tool-cache: true
android: true
dotnet: true
haskell: true
large-packages: false
docker-images: true
swap-storage: false
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
Expand Down
10 changes: 0 additions & 10 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ jobs:
required:
runs-on: ubuntu-latest
steps:
- name: Free disk space
uses: jlumbroso/free-disk-space@f68fdb76e2ea636224182cfb7377ff9a1708f9b8 # v1.3.0
with:
tool-cache: true
android: true
dotnet: true
haskell: true
large-packages: false
docker-images: true
swap-storage: false
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
Expand Down
10 changes: 0 additions & 10 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ jobs:
required:
runs-on: ubuntu-latest
steps:
- name: Free disk space
uses: jlumbroso/free-disk-space@f68fdb76e2ea636224182cfb7377ff9a1708f9b8 # v1.3.0
with:
tool-cache: true
android: true
dotnet: true
haskell: true
large-packages: false
docker-images: true
swap-storage: false
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/kyverno-json
website/site
website/playground/assets/main.wasm
pkg/server/ui/dist/assets/main.wasm
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ vet: ## Run go vet
@echo Go vet... >&2
@go vet ./...

$(CLI_BIN): fmt vet build-wasm codegen-crds codegen-deepcopy codegen-register codegen-client
$(CLI_BIN): fmt vet build-wasm codegen-crds codegen-deepcopy codegen-register codegen-client codegen-playground
@echo Build cli binary... >&2
@CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) go build -o ./$(CLI_BIN) -ldflags=$(LD_FLAGS) ./$(CLI_DIR)

Expand Down Expand Up @@ -270,6 +270,11 @@ codegen-schemas-json: codegen-schemas-openapi ## Generate json schemas
.PHONY: codegen-schemas
codegen-schemas: codegen-schemas-openapi codegen-schemas-json ## Generate openapi and json schemas

.PHONY: codegen-playground
codegen-playground: build-wasm ## Generate playground
@echo Generate playground... >&2
@cp -r ./website/playground/* ./pkg/server/ui/dist

.PHONY: codegen-helm-crds
codegen-helm-crds: codegen-crds ## Generate helm CRDs
@echo Generate helm crds... >&2
Expand All @@ -292,7 +297,7 @@ codegen-helm-docs: ## Generate helm docs
@docker run -v ${PWD}/charts:/work -w /work jnorwood/helm-docs:v1.11.0 -s file

.PHONY: codegen
codegen: codegen-crds codegen-deepcopy codegen-register codegen-client codegen-docs codegen-mkdocs codegen-schemas codegen-helm-docs ## Rebuild all generated code and docs
codegen: codegen-crds codegen-deepcopy codegen-register codegen-client codegen-docs codegen-mkdocs codegen-schemas codegen-playground codegen-helm-crds codegen-helm-docs ## Rebuild all generated code and docs

.PHONY: verify-codegen
verify-codegen: codegen ## Verify all generated code and docs are up to date
Expand Down
35 changes: 7 additions & 28 deletions cmd/wasm/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright 2023 Undistro Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build js && wasm

package main
Expand All @@ -20,35 +6,28 @@ import (
"context"
"os/signal"
"syscall"
"time"

server "github.com/kyverno/kyverno-json/pkg/server/wasm"
"github.com/gin-gonic/gin"
"github.com/kyverno/kyverno-json/pkg/server"
"github.com/kyverno/kyverno-json/pkg/server/playground"
)

func main() {
// initialise gin framework
// gin.SetMode(c.ginFlags.mode)
// tonic.SetBindHook(tonic.DefaultBindingHookMaxBodyBytes(int64(c.ginFlags.maxBodySize)))
gin.SetMode(gin.DebugMode)
// create server
server, err := server.New(true, true)
router, err := server.New(true, true)
if err != nil {
panic(err)
}
// register playground routes
if err := server.AddPlaygroundRoutes(); err != nil {
if err := playground.AddRoutes(router.Group(server.PlaygroundPrefix)); err != nil {
panic(err)
}
// run server
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
shutdown := server.Run(ctx)
server.RunWasm(ctx, router)
<-ctx.Done()
stop()
if shutdown != nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := shutdown(ctx); err != nil {
panic(err)
}
}
}
1 change: 1 addition & 0 deletions docs/user/commands/kyverno-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ kyverno-json [flags]
* [kyverno-json completion](kyverno-json_completion.md) - Generate the autocompletion script for the specified shell
* [kyverno-json docs](kyverno-json_docs.md) - Generates reference documentation.
* [kyverno-json jp](kyverno-json_jp.md) - Provides a command-line interface to JMESPath, enhanced with custom functions.
* [kyverno-json playground](kyverno-json_playground.md) - playground
* [kyverno-json scan](kyverno-json_scan.md) - scan
* [kyverno-json serve](kyverno-json_serve.md) - serve
* [kyverno-json version](kyverno-json_version.md) - Prints the version informations.
Expand Down
28 changes: 28 additions & 0 deletions docs/user/commands/kyverno-json_playground.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## kyverno-json playground

playground

### Synopsis

Serve playground

```
kyverno-json playground [flags]
```

### Options

```
--gin-cors enable gin cors (default true)
--gin-log enable gin logger (default true)
--gin-max-body-size int gin max body size (default 2097152)
--gin-mode string gin run mode (default "release")
-h, --help help for playground
--server-host string server host (default "0.0.0.0")
--server-port int server port (default 8080)
```

### SEE ALSO

* [kyverno-json](kyverno-json.md) - kyverno-json is a CLI tool to apply policies to json resources.

27 changes: 27 additions & 0 deletions pkg/commands/playground/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package playground

import (
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
)

func Command(parents ...string) *cobra.Command {
var command options
cmd := &cobra.Command{
Use: "playground",
Short: "playground",
Long: "Serve playground",
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: command.Run,
}
// server flags
cmd.Flags().StringVar(&command.serverFlags.host, "server-host", "0.0.0.0", "server host")
cmd.Flags().IntVar(&command.serverFlags.port, "server-port", 8080, "server port")
// gin flags
cmd.Flags().StringVar(&command.ginFlags.mode, "gin-mode", gin.ReleaseMode, "gin run mode")
cmd.Flags().BoolVar(&command.ginFlags.log, "gin-log", true, "enable gin logger")
cmd.Flags().BoolVar(&command.ginFlags.cors, "gin-cors", true, "enable gin cors")
cmd.Flags().IntVar(&command.ginFlags.maxBodySize, "gin-max-body-size", 2*1024*1024, "gin max body size")
return cmd
}
60 changes: 60 additions & 0 deletions pkg/commands/playground/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package playground

import (
"context"
"os/signal"
"syscall"
"time"

"github.com/gin-gonic/gin"
"github.com/kyverno/kyverno-json/pkg/server"
"github.com/kyverno/kyverno-json/pkg/server/ui"
"github.com/loopfz/gadgeto/tonic"
"github.com/spf13/cobra"
)

type options struct {
serverFlags serverFlags
ginFlags ginFlags
}

type serverFlags struct {
host string
port int
}

type ginFlags struct {
mode string
log bool
cors bool
maxBodySize int
}

func (c *options) Run(_ *cobra.Command, _ []string) error {
// initialise gin framework
gin.SetMode(c.ginFlags.mode)
tonic.SetBindHook(tonic.DefaultBindingHookMaxBodyBytes(int64(c.ginFlags.maxBodySize)))
// create router
router, err := server.New(c.ginFlags.log, c.ginFlags.cors)
if err != nil {
return err
}
// register api routes
if err := ui.AddRoutes(router); err != nil {
return err
}
// run server
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
shutdown := server.Run(ctx, router, c.serverFlags.host, c.serverFlags.port)
<-ctx.Done()
stop()
if shutdown != nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := shutdown(ctx); err != nil {
return err
}
}
return nil
}
2 changes: 2 additions & 0 deletions pkg/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/kyverno/kyverno-json/pkg/command"
"github.com/kyverno/kyverno-json/pkg/commands/docs"
"github.com/kyverno/kyverno-json/pkg/commands/jp"
"github.com/kyverno/kyverno-json/pkg/commands/playground"
"github.com/kyverno/kyverno-json/pkg/commands/scan"
"github.com/kyverno/kyverno-json/pkg/commands/serve"
"github.com/kyverno/kyverno-json/pkg/commands/version"
Expand All @@ -28,6 +29,7 @@ func RootCommand() *cobra.Command {
cmd.AddCommand(
docs.Command("kyverno-json"),
jp.Command("kyverno-json"),
playground.Command(),
scan.Command(),
serve.Command("kyverno-json"),
version.Command("kyverno-json"),
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestRootCommand(t *testing.T) {
cmd := RootCommand()
assert.NotNil(t, cmd)
assert.Len(t, cmd.Commands(), 5)
assert.Len(t, cmd.Commands(), 6)
err := cmd.Execute()
assert.NoError(t, err)
}
Expand Down
16 changes: 7 additions & 9 deletions pkg/commands/serve/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/kyverno/kyverno-json/pkg/client/clientset/versioned"
"github.com/kyverno/kyverno-json/pkg/server"
"github.com/kyverno/kyverno-json/pkg/server/api"
"github.com/kyverno/kyverno-json/pkg/server/scan"
restutils "github.com/kyverno/kyverno-json/pkg/utils/rest"
"github.com/loopfz/gadgeto/tonic"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -42,8 +42,8 @@ func (c *options) Run(_ *cobra.Command, _ []string) error {
// initialise gin framework
gin.SetMode(c.ginFlags.mode)
tonic.SetBindHook(tonic.DefaultBindingHookMaxBodyBytes(int64(c.ginFlags.maxBodySize)))
// create server
server, err := server.New(c.ginFlags.log, c.ginFlags.cors)
// create router
router, err := server.New(c.ginFlags.log, c.ginFlags.cors)
if err != nil {
return err
}
Expand All @@ -55,19 +55,17 @@ func (c *options) Run(_ *cobra.Command, _ []string) error {
if err != nil {
return err
}
config := api.Configuration{
PolicyProvider: &provider{
client: client,
},
provider := &provider{
client: client,
}
// register api routes
if err := server.AddApiRoutes(config); err != nil {
if err := scan.AddRoutes(router.Group(server.ApiPrefix), provider); err != nil {
return err
}
// run server
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
shutdown := server.Run(ctx, c.serverFlags.host, c.serverFlags.port)
shutdown := server.Run(ctx, router, c.serverFlags.host, c.serverFlags.port)
<-ctx.Done()
stop()
if shutdown != nil {
Expand Down
9 changes: 0 additions & 9 deletions pkg/server/api/config.go

This file was deleted.

13 changes: 0 additions & 13 deletions pkg/server/api/routes.go

This file was deleted.

Loading

0 comments on commit 08cdc4d

Please sign in to comment.